-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathstrategy_jwt_session.go
102 lines (81 loc) · 2.03 KB
/
strategy_jwt_session.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
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oauth2
import (
"time"
"github.com/mohae/deepcopy"
"github.com/ory/fosite"
"github.com/ory/fosite/token/jwt"
)
type JWTSessionContainer interface {
// GetJWTClaims returns the claims.
GetJWTClaims() jwt.JWTClaimsContainer
// GetJWTHeader returns the header.
GetJWTHeader() *jwt.Headers
fosite.Session
}
// JWTSession Container for the JWT session.
type JWTSession struct {
JWTClaims *jwt.JWTClaims
JWTHeader *jwt.Headers
ExpiresAt map[fosite.TokenType]time.Time
Username string
Subject string
}
func (j *JWTSession) GetJWTClaims() jwt.JWTClaimsContainer {
if j.JWTClaims == nil {
j.JWTClaims = &jwt.JWTClaims{}
}
return j.JWTClaims
}
func (j *JWTSession) GetJWTHeader() *jwt.Headers {
if j.JWTHeader == nil {
j.JWTHeader = &jwt.Headers{}
}
return j.JWTHeader
}
func (j *JWTSession) SetExpiresAt(key fosite.TokenType, exp time.Time) {
if j.ExpiresAt == nil {
j.ExpiresAt = make(map[fosite.TokenType]time.Time)
}
j.ExpiresAt[key] = exp
}
func (j *JWTSession) GetExpiresAt(key fosite.TokenType) time.Time {
if j.ExpiresAt == nil {
j.ExpiresAt = make(map[fosite.TokenType]time.Time)
}
if _, ok := j.ExpiresAt[key]; !ok {
return time.Time{}
}
return j.ExpiresAt[key]
}
func (j *JWTSession) GetUsername() string {
if j == nil {
return ""
}
return j.Username
}
func (j *JWTSession) SetSubject(subject string) {
j.Subject = subject
}
func (j *JWTSession) GetSubject() string {
if j == nil {
return ""
}
return j.Subject
}
func (j *JWTSession) Clone() fosite.Session {
if j == nil {
return nil
}
return deepcopy.Copy(j).(fosite.Session)
}
// GetExtraClaims implements ExtraClaimsSession for JWTSession.
// The returned value is a copy of JWTSession claims.
func (s *JWTSession) GetExtraClaims() map[string]interface{} {
if s == nil {
return nil
}
// We make a clone so that WithScopeField does not change the original value.
return s.Clone().(*JWTSession).GetJWTClaims().WithScopeField(jwt.JWTScopeFieldString).ToMapClaims()
}