-
Notifications
You must be signed in to change notification settings - Fork 3
/
token.go
52 lines (44 loc) · 1.4 KB
/
token.go
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package v2
import (
"errors"
jwt "github.com/golang-jwt/jwt/v4"
)
var (
ErrInvalidToken = errors.New("invalid access or refresh token")
ErrUnauthorized = errors.New("unauthorized")
)
// Claims represents the JWT claims
type Claims struct {
jwt.StandardClaims
// Custom claims
Groups []string `json:"groups"`
Provider AuthProviderClaims `json:"provider"`
APIKey bool `json:"api_key"`
SessionID string `json:"session_id"`
}
// AuthProviderClaims contains information from the authentication provider
type AuthProviderClaims struct {
// ProviderID used to login the user
ProviderID string `json:"provider_id"`
// ProviderType represents the type of provider used
ProviderType string `json:"provider_type"`
// UserID assigned to the user by the provider
UserID string `json:"user_id"`
}
// FixtureClaims returns a testing fixture for a JWT claims
func FixtureClaims(username string, groups []string) *Claims {
return &Claims{
StandardClaims: jwt.StandardClaims{Subject: username},
Groups: groups,
SessionID: "abcdefghijklmnopqrstuvwxyz",
Provider: AuthProviderClaims{
ProviderID: "basic/default",
ProviderType: "basic",
UserID: username,
},
}
}
// StandardClaims returns an initialized jwt.StandardClaims with the subject
func StandardClaims(subject string) jwt.StandardClaims {
return jwt.StandardClaims{Subject: subject}
}