Beispiel #1
0
func (c *AccessTokenGenJWT) GenerateAccessToken(data *osin.AccessData, generaterefresh bool) (accesstoken string, refreshtoken string, err error) {
	// generate JWT access token
	token := jwt.New(jwt.GetSigningMethod("RS256"))
	token.Claims["cid"] = data.Client.GetId()
	token.Claims["exp"] = data.ExpireAt().Unix()

	accesstoken, err = token.SignedString(c.PrivateKey)
	if err != nil {
		return "", "", err
	}

	if generaterefresh {

		// generate JWT access token
		token = jwt.New(jwt.GetSigningMethod("RS256"))
		token.Claims["cid"] = data.Client.GetId()
		token.Claims["at"] = accesstoken
		token.Claims["exp"] = data.ExpireAt().Unix()

		refreshtoken, err = token.SignedString(c.PrivateKey)
		if err != nil {
			return "", "", err
		}
	}
	return
}
func (c *TokenGenJWT) GenerateAccessToken(data *o2aserver.AccessTokenData) error {
	// generate JWT access token
	token := jwt.New(jwt.GetSigningMethod("RS256"))
	token.Claims["userid"] = data.UserId
	token.Claims["exp"] = data.CreatedAt.Add(time.Second * time.Duration(data.ExpiresIn)).Unix()

	tokenString, err := token.SignedString(c.PrivateKey)
	if err != nil {
		return err
	}

	data.AccessToken = tokenString

	// generate random refresh token
	data.RefreshToken = uuid.New()
	data.RefreshToken = base64.StdEncoding.EncodeToString([]byte(data.RefreshToken))

	return nil
}