Mercurial > gemma
view auth/token.go @ 121:9a5bbe9c593c
Split README into client and toplevel.
author | Bernhard Reiter <bernhard@intevation.de> |
---|---|
date | Wed, 27 Jun 2018 10:50:09 +0200 |
parents | 29e56c342c9f |
children | bb9120d28950 |
line wrap: on
line source
package auth import ( "time" "gemma.intevation.de/gemma/config" jwt "github.com/dgrijalva/jwt-go" ) type Claims struct { jwt.StandardClaims User string `json:"user"` Roles []string `json:"roles"` } const maxTokenValid = time.Hour * 3 func NewToken(user string, roles []string) (string, error) { // Create the Claims claims := &Claims{ StandardClaims: jwt.StandardClaims{ ExpiresAt: jwt.TimeFunc().Add(maxTokenValid).Unix(), }, User: user, Roles: roles, } token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) return token.SignedString(config.Config.JWTSignKey) } func TokenToClaims(token string) (*Claims, error) { claims := &Claims{} _, err := jwt.ParseWithClaims(token, claims, func(*jwt.Token) (interface{}, error) { return config.Config.JWTSignKey, nil }) return claims, err }