|
| 1 | +package externalsvc |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "time" |
| 7 | + |
| 8 | + jwt "github.com/dgrijalva/jwt-go" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + ExpirationKey = "exp" |
| 13 | + UserIDKey = "uid" |
| 14 | + IssuerKey = "iss" |
| 15 | + JWTIDKey = "jti" |
| 16 | + AudienceKey = "aud" |
| 17 | + SessionDuration = 8760 * time.Hour // 1 year |
| 18 | +) |
| 19 | + |
| 20 | +func parser(token *jwt.Token) (interface{}, error) { |
| 21 | + if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { |
| 22 | + return nil, errors.New("Unexpected signing method") |
| 23 | + } |
| 24 | + return getJWTSecret(), nil |
| 25 | +} |
| 26 | + |
| 27 | +func GenerateSessionJWT(userID uint64) (string, error) { |
| 28 | + expiration := time.Now().Add(SessionDuration).Unix() |
| 29 | + claims := jwt.MapClaims{ |
| 30 | + IssuerKey: os.Getenv("FRONTEND_ADDRESS"), |
| 31 | + ExpirationKey: expiration, |
| 32 | + UserIDKey: userID, |
| 33 | + } |
| 34 | + return generateJWT(claims, getJWTSecret()) |
| 35 | +} |
| 36 | + |
| 37 | +func GenerateAPIKeyJWT(userID uint64, jwtID string) (string, error) { |
| 38 | + claims := jwt.MapClaims{ |
| 39 | + IssuerKey: os.Getenv("FRONTEND_ADDRESS"), |
| 40 | + JWTIDKey: jwtID, |
| 41 | + UserIDKey: userID, |
| 42 | + } |
| 43 | + return generateJWT(claims, getJWTSecret()) |
| 44 | +} |
| 45 | + |
| 46 | +func GenerateWebhookJWT(url string, secret []byte) (string, error) { |
| 47 | + claims := jwt.MapClaims{ |
| 48 | + IssuerKey: os.Getenv("FRONTEND_ADDRESS"), |
| 49 | + AudienceKey: url, |
| 50 | + } |
| 51 | + return generateJWT(claims, secret) |
| 52 | +} |
| 53 | + |
| 54 | +func generateJWT(claims jwt.MapClaims, secret []byte) (string, error) { |
| 55 | + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) |
| 56 | + return token.SignedString(secret) |
| 57 | +} |
| 58 | + |
| 59 | +func ParseAPIKeyJWT(tokenString string) (uint64, string, error) { |
| 60 | + claims, err := parseJWT(tokenString) |
| 61 | + if err != nil { |
| 62 | + return 0, "", err |
| 63 | + } |
| 64 | + |
| 65 | + issuer := claims[IssuerKey].(string) |
| 66 | + if issuer != os.Getenv("FRONTEND_ADDRESS") { |
| 67 | + return 0, "", errors.New("Incorrect issuer") |
| 68 | + } |
| 69 | + |
| 70 | + jwtID := claims[JWTIDKey].(string) |
| 71 | + userID := uint64(claims[UserIDKey].(float64)) |
| 72 | + return userID, jwtID, nil |
| 73 | +} |
| 74 | + |
| 75 | +func ParseSessionJWT(tokenString string) (uint64, error) { |
| 76 | + claims, err := parseJWT(tokenString) |
| 77 | + if err != nil { |
| 78 | + return 0, err |
| 79 | + } |
| 80 | + |
| 81 | + issuer := claims[IssuerKey].(string) |
| 82 | + if issuer != os.Getenv("FRONTEND_ADDRESS") { |
| 83 | + return 0, errors.New("Incorrect issuer") |
| 84 | + } |
| 85 | + |
| 86 | + expiration := int64(claims[ExpirationKey].(float64)) |
| 87 | + if expiration < time.Now().Unix() { |
| 88 | + return 0, errors.New("Authorization token expired") |
| 89 | + } |
| 90 | + |
| 91 | + userID := uint64(claims[UserIDKey].(float64)) |
| 92 | + return userID, nil |
| 93 | +} |
| 94 | + |
| 95 | +func parseJWT(tokenString string) (jwt.MapClaims, error) { |
| 96 | + token, err := jwt.Parse(tokenString, parser) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { |
| 102 | + return claims, nil |
| 103 | + } |
| 104 | + return nil, errors.New("Unable to parse JWT") |
| 105 | +} |
| 106 | + |
| 107 | +func getJWTSecret() []byte { |
| 108 | + return []byte(os.Getenv("JWT_SECRET")) |
| 109 | +} |
0 commit comments