-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
32 lines (26 loc) · 764 Bytes
/
auth.go
File metadata and controls
32 lines (26 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main
import (
"time"
jwtmiddleware "github.com/auth0/go-jwt-middleware"
jwt "github.com/dgrijalva/jwt-go"
)
// GetJWT returns a new JWT token for an authenticated user
func GetJWT(userId int) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"admin": false,
"uid": userId,
"exp": time.Now().Add(time.Hour * 24).Unix(),
})
tokenString, err := token.SignedString(ssr_jwt_key)
if err != nil {
return "", err
}
return tokenString, nil
}
// jwtMiddleware checks for a valid JWT on protected routes
var jwtMiddleware = jwtmiddleware.New(jwtmiddleware.Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return ssr_jwt_key, nil
},
SigningMethod: jwt.SigningMethodHS256,
})