-
Notifications
You must be signed in to change notification settings - Fork 27
/
resolver.go
229 lines (195 loc) · 6.35 KB
/
resolver.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package oauth
import (
"errors"
"net/http"
"strings"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/session"
"github.com/authgear/authgear-server/pkg/lib/session/access"
"github.com/authgear/authgear-server/pkg/lib/session/idpsession"
"github.com/authgear/authgear-server/pkg/util/clock"
"github.com/authgear/authgear-server/pkg/util/httputil"
)
type ResolverSessionProvider interface {
AccessWithID(id string, accessEvent access.Event) (*idpsession.IDPSession, error)
}
type AccessTokenDecoder interface {
DecodeAccessToken(encodedToken string) (tok string, isHash bool, err error)
}
type ResolverCookieManager interface {
GetCookie(r *http.Request, def *httputil.CookieDef) (*http.Cookie, error)
}
type Resolver struct {
RemoteIP httputil.RemoteIP
UserAgentString httputil.UserAgentString
OAuthConfig *config.OAuthConfig
Authorizations AuthorizationStore
AccessGrants AccessGrantStore
OfflineGrants OfflineGrantStore
AppSessions AppSessionStore
AccessTokenDecoder AccessTokenDecoder
Sessions ResolverSessionProvider
Cookies ResolverCookieManager
Clock clock.Clock
OfflineGrantService OfflineGrantService
}
func (re *Resolver) Resolve(rw http.ResponseWriter, r *http.Request) (session.Session, error) {
// The resolve function has the following outcomes:
// - (nil, nil) which means no session was found and no error.
// - (nil, err) in which err is ErrInvalidSession, which means the session was found but was invalid.
// in which err is something else, which means some unexpected error occurred.
// - (s, nil) which means a session was resolved successfully.
//
// Here we want to try the next resolve function iff the outcome is (nil, nil).
funcs := []func(*http.Request) (session.Session, error){
re.resolveHeader,
re.resolveCookie,
}
for _, f := range funcs {
s, err := f(r)
if err != nil {
return nil, err
}
if s != nil {
return s, nil
}
}
return nil, nil
}
func (re *Resolver) resolveHeader(r *http.Request) (session.Session, error) {
token := parseAuthorizationHeader(r)
if token == "" {
// No bearer token in Authorization header. Simply proceed.
return nil, nil
}
tok, isHash, err := re.AccessTokenDecoder.DecodeAccessToken(token)
if err != nil {
return nil, session.ErrInvalidSession
}
var tokenHash string
if isHash {
tokenHash = tok
} else {
tokenHash = HashToken(token)
}
grant, err := re.AccessGrants.GetAccessGrant(tokenHash)
if errors.Is(err, ErrGrantNotFound) {
return nil, session.ErrInvalidSession
} else if err != nil {
return nil, err
}
_, err = re.Authorizations.GetByID(grant.AuthorizationID)
if errors.Is(err, ErrAuthorizationNotFound) {
// Authorization does not exists (e.g. revoked)
return nil, session.ErrInvalidSession
} else if err != nil {
return nil, err
}
var authSession session.Session
event := access.NewEvent(re.Clock.NowUTC(), re.RemoteIP, re.UserAgentString)
switch grant.SessionKind {
case GrantSessionKindSession:
s, err := re.Sessions.AccessWithID(grant.SessionID, event)
if errors.Is(err, idpsession.ErrSessionNotFound) {
return nil, session.ErrInvalidSession
} else if err != nil {
return nil, err
}
authSession = s
case GrantSessionKindOffline:
g, err := re.OfflineGrants.GetOfflineGrant(grant.SessionID)
if errors.Is(err, ErrGrantNotFound) {
return nil, session.ErrInvalidSession
} else if err != nil {
return nil, err
}
g, err = re.accessOfflineGrant(g, event)
if err != nil {
return nil, err
}
authSession = g
default:
panic("oauth: resolving unknown grant session kind")
}
return authSession, nil
}
func (re *Resolver) resolveCookie(r *http.Request) (session.Session, error) {
cookie, err := re.Cookies.GetCookie(r, session.AppSessionTokenCookieDef)
if err != nil {
// No session cookie. Simply proceed.
return nil, nil
}
aSession, err := re.AppSessions.GetAppSession(HashToken(cookie.Value))
if errors.Is(err, ErrGrantNotFound) {
return nil, session.ErrInvalidSession
} else if err != nil {
return nil, err
}
offlineGrant, err := re.OfflineGrants.GetOfflineGrant(aSession.OfflineGrantID)
if errors.Is(err, ErrGrantNotFound) {
return nil, session.ErrInvalidSession
} else if err != nil {
return nil, err
}
authz, err := re.Authorizations.GetByID(offlineGrant.AuthorizationID)
if errors.Is(err, ErrAuthorizationNotFound) {
// Authorization does not exists (e.g. revoked)
return nil, session.ErrInvalidSession
} else if err != nil {
return nil, err
} else if !authz.IsAuthorized([]string{FullAccessScope}) {
// App sessions must have full user access to be valid
return nil, session.ErrInvalidSession
}
event := access.NewEvent(re.Clock.NowUTC(), re.RemoteIP, re.UserAgentString)
offlineGrant, err = re.accessOfflineGrant(offlineGrant, event)
if err != nil {
return nil, err
}
return offlineGrant, nil
}
func (re *Resolver) accessOfflineGrant(offlineGrant *OfflineGrant, accessEvent access.Event) (*OfflineGrant, error) {
isValid, _, err := re.OfflineGrantService.IsValid(offlineGrant)
if err != nil {
return nil, err
}
if !isValid {
return nil, session.ErrInvalidSession
}
// When accessing the offline grant, also access its idp session
// Access the idp session first, since the idp session expiry will be updated
// sso enabled offline grant expiry depends on its idp session
if offlineGrant.SSOEnabled {
if offlineGrant.IDPSessionID == "" {
return nil, session.ErrInvalidSession
}
_, err := re.Sessions.AccessWithID(offlineGrant.IDPSessionID, accessEvent)
if errors.Is(err, idpsession.ErrSessionNotFound) {
return nil, session.ErrInvalidSession
} else if err != nil {
return nil, err
}
}
expiry, err := re.OfflineGrantService.ComputeOfflineGrantExpiry(offlineGrant)
if errors.Is(err, ErrGrantNotFound) {
return nil, session.ErrInvalidSession
} else if err != nil {
return nil, err
}
offlineGrant, err = re.OfflineGrants.AccessWithID(offlineGrant.ID, accessEvent, expiry)
if err != nil {
return nil, err
}
return offlineGrant, nil
}
func parseAuthorizationHeader(r *http.Request) (token string) {
authorization := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(authorization) != 2 {
return
}
scheme := authorization[0]
if strings.ToLower(scheme) != "bearer" {
return
}
return authorization[1]
}