Skip to content

Instantly share code, notes, and snippets.

@monkov
Last active February 19, 2019 18:38
Show Gist options
  • Save monkov/6a864aa5493cc1247022f28beea90193 to your computer and use it in GitHub Desktop.
Save monkov/6a864aa5493cc1247022f28beea90193 to your computer and use it in GitHub Desktop.
func CreateToken(email, ua, password string) (string, error) {
//Creating empty slice
r := make([]byte, 24)
//Push random data to slice
rand.Read(r)
//Generate secret code for jwt
secret := sha3.New512().Sum(append([]byte(email + ua + password), r...))
//Create custom claims with user data
claims := JwtClaims{
Email: email,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Hour).Unix(),
},
}
//Generate jwt token
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
//Sign jwt token
jwt, err := token.SignedString(secret)
if err != nil {return "", err}
//TODO: Adding hash to database to current device.
return jwt, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment