-
Notifications
You must be signed in to change notification settings - Fork 1
/
claims_jarm.go
107 lines (90 loc) · 2.19 KB
/
claims_jarm.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package jwt
import (
"time"
"github.com/google/uuid"
"authelia.com/provider/oauth2/internal/consts"
)
// JARMClaims represent a token's claims.
type JARMClaims struct {
Issuer string
Audience []string
JTI string
IssuedAt time.Time
ExpiresAt time.Time
Extra map[string]any
}
// ToMap will transform the headers to a map structure
func (c *JARMClaims) ToMap() map[string]any {
var ret = Copy(c.Extra)
if c.Issuer != "" {
ret[consts.ClaimIssuer] = c.Issuer
} else {
delete(ret, consts.ClaimIssuer)
}
if c.JTI != "" {
ret[consts.ClaimJWTID] = c.JTI
} else {
ret[consts.ClaimJWTID] = uuid.New().String()
}
if len(c.Audience) > 0 {
ret[consts.ClaimAudience] = c.Audience
} else {
ret[consts.ClaimAudience] = []string{}
}
if !c.IssuedAt.IsZero() {
ret[consts.ClaimIssuedAt] = c.IssuedAt.Unix()
} else {
delete(ret, consts.ClaimIssuedAt)
}
if !c.ExpiresAt.IsZero() {
ret[consts.ClaimExpirationTime] = c.ExpiresAt.Unix()
} else {
delete(ret, consts.ClaimExpirationTime)
}
return ret
}
// FromMap will set the claims based on a mapping
func (c *JARMClaims) FromMap(m map[string]any) {
c.Extra = make(map[string]any)
for k, v := range m {
switch k {
case consts.ClaimIssuer:
if s, ok := v.(string); ok {
c.Issuer = s
}
case consts.ClaimJWTID:
if s, ok := v.(string); ok {
c.JTI = s
}
case consts.ClaimAudience:
if aud, ok := StringSliceFromMap(v); ok {
c.Audience = aud
}
case consts.ClaimIssuedAt:
c.IssuedAt = toTime(v, c.IssuedAt)
case consts.ClaimExpirationTime:
c.ExpiresAt = toTime(v, c.ExpiresAt)
default:
c.Extra[k] = v
}
}
}
// Add will add a key-value pair to the extra field
func (c *JARMClaims) Add(key string, value any) {
if c.Extra == nil {
c.Extra = make(map[string]any)
}
c.Extra[key] = value
}
// Get will get a value from the extra field based on a given key
func (c JARMClaims) Get(key string) any {
return c.ToMap()[key]
}
// ToMapClaims will return a jwt-go MapClaims representation
func (c JARMClaims) ToMapClaims() MapClaims {
return c.ToMap()
}
// FromMapClaims will populate claims from a jwt-go MapClaims representation
func (c *JARMClaims) FromMapClaims(mc MapClaims) {
c.FromMap(mc)
}