authorizer/server/utils/verificationToken.go

48 lines
1.0 KiB
Go
Raw Normal View History

2021-07-12 18:22:16 +00:00
package utils
import (
"time"
2021-07-23 16:27:44 +00:00
"github.com/authorizerdev/authorizer/server/constants"
2021-07-12 18:22:16 +00:00
"github.com/golang-jwt/jwt"
)
type UserInfo struct {
Email string `json:"email"`
Host string `json:"host"`
RedirectURL string `json:"redirect_url"`
2021-07-12 18:22:16 +00:00
}
type CustomClaim struct {
2021-07-12 18:22:16 +00:00
*jwt.StandardClaims
TokenType string `json:"token_type"`
UserInfo
}
func CreateVerificationToken(email string, tokenType string) (string, error) {
2021-07-12 18:22:16 +00:00
t := jwt.New(jwt.GetSigningMethod(constants.JWT_TYPE))
t.Claims = &CustomClaim{
2021-07-12 18:22:16 +00:00
&jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
},
tokenType,
UserInfo{Email: email, Host: constants.AUTHORIZER_URL, RedirectURL: constants.APP_URL},
2021-07-12 18:22:16 +00:00
}
return t.SignedString([]byte(constants.JWT_SECRET))
}
func VerifyVerificationToken(token string) (*CustomClaim, error) {
claims := &CustomClaim{}
_, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(constants.JWT_SECRET), nil
})
if err != nil {
return claims, err
2021-07-12 18:22:16 +00:00
}
return claims, nil
2021-07-12 18:22:16 +00:00
}