Giter Site home page Giter Site logo

go-jwt-implementation's Introduction

Go JWT Implementation

Json Web Token stands is used to check if a user is authorized or not via creating a JWT token and sending it as a Cookie. This Cookie will be sent/recieved by all the requests now the server make.

How it works in Go

  1. After checking the password, if the user is authorized or not.
	// verify the user by checking password
	expectedPassword, ok := users[credentials.Username]
	if !ok || expectedPassword != credentials.Password {
		w.WriteHeader(http.StatusUnauthorized)
		return
	}
  1. We will create a JWT token which takes Signing Method and Claims struct as input.
	expirationTime := time.Now().Add(5 * time.Minute)

  // claims struct contains the expirationTime and other information about the token
	claims := &Claims{
		Username: credentials.Username,
		RegisteredClaims: jwt.RegisteredClaims{
			Issuer:    credentials.Username,
			ExpiresAt: jwt.NewNumericDate(expirationTime),
			IssuedAt:  jwt.NewNumericDate(time.Now()),
		},
	}

	// this gives unsigned jwt token with claims and algo name
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  1. After the token is created. It is yet to be signed to be sent to the client as a Cookie. So then we sign it with the key.

NOTE: The key is the part that makes the jwt token secure.

	// this signs the jwt token returning complete and signed jwt token
	tokenString, err := token.SignedString(jwtKey)
	if err != nil {
		// If there is an error in creating the JWT return an internal server error
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
  1. Then we send the token as a cookie to the client and it will be sent/recieved with each request.
	// Finally, we set the client cookie for "token" as the JWT we just generated
	// we also set an expiry time which is the same as the token itself
	http.SetCookie(w, &http.Cookie{
		Name:    "token",
		Value:   tokenString,
		Expires: expirationTime,
	})

Extras

For more clarity, you can learn how JWT works in Go

. https://jwt.io/introduction/

you can check the code for more clarity

go-jwt-implementation's People

Contributors

vandit1604 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.