func NewUser(userId, email, pass string) (User, error) { salt := crypto.GenerateRandomKey(128) hpass, err := crypto.HashPassword(pass, salt) if err != nil { return User{}, err } return User{ Id: userId, Email: email, Password: string(hpass), Salt: string(salt), }, nil }
// Generates a JSON Web Token given an userId (typically an id or an email), and the JWT options // to set SigningMethod and the keys you can check // http://github.com/dgrijalva/jwt-go // // In case you use an symmetric-key algorithm set PublicKey and PrivateKey equal to the SecretKey , func GenerateJWTToken(userId string, op Options) (string, error) { t := jwt.New(jwt.GetSigningMethod(op.SigningMethod)) now := time.Now() // set claims t.Claims["iat"] = now.Unix() t.Claims["exp"] = now.Add(op.Expiration).Unix() t.Claims["sub"] = userId t.Claims["jti"] = crypto.GenerateRandomKey(32) tokenString, err := t.SignedString([]byte(op.PrivateKey)) if err != nil { logError("ERROR: GenerateJWTToken: %v\n", err) } return tokenString, err }