authorizer/server/utils/verificationToken.go
Lakhan Samani f88363e6dc
feat: login wall (#42)
* feat: add login-wall app

* fix: rename vars

* fix: rename vars

* update docker file

* add validations for app state

* add host check for app

* fix: docker file
2021-08-04 12:18:57 +05:30

48 lines
1002 B
Go

package utils
import (
"time"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/golang-jwt/jwt"
)
type UserInfo struct {
Email string `json:"email"`
Host string `json:"host"`
}
type CustomClaim struct {
*jwt.StandardClaims
TokenType string `json:"token_type"`
UserInfo
}
// TODO convert tokenType to enum
func CreateVerificationToken(email string, tokenType string, host string) (string, error) {
t := jwt.New(jwt.GetSigningMethod(constants.JWT_TYPE))
t.Claims = &CustomClaim{
&jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Minute * 30).Unix(),
},
tokenType,
UserInfo{Email: email, Host: host},
}
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
}
return claims, nil
}