-
Notifications
You must be signed in to change notification settings - Fork 27
/
session_middleware.go
133 lines (111 loc) · 3.75 KB
/
session_middleware.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
package webapp
import (
"errors"
"net/http"
"github.com/authgear/authgear-server/pkg/lib/oauth/oauthsession"
"github.com/authgear/authgear-server/pkg/lib/oauth/oidc"
"github.com/authgear/authgear-server/pkg/lib/oauth/protocol"
"github.com/authgear/authgear-server/pkg/util/httputil"
)
type SessionMiddlewareOAuthSessionService interface {
Get(entryID string) (*oauthsession.Entry, error)
}
type SessionMiddlewareSessionService interface {
CreateSession(session *Session, redirectURI string) (*Result, error)
}
type SessionMiddlewareStore interface {
Get(id string) (*Session, error)
}
type SessionMiddlewareUIInfoResolver interface {
GetOAuthSessionID(req *http.Request, urlQuery string) (string, bool)
RemoveOAuthSessionID(w http.ResponseWriter, r *http.Request)
ResolveForUI(r protocol.AuthorizationRequest) (*oidc.UIInfo, error)
}
type SessionMiddleware struct {
Sessions SessionMiddlewareSessionService
OAuthSessions SessionMiddlewareOAuthSessionService
States SessionMiddlewareStore
UIInfoResolver SessionMiddlewareUIInfoResolver
CookieDef SessionCookieDef
Cookies CookieManager
}
func (m *SessionMiddleware) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// The session is either created now, or read from cookie.
// Create the session now.
if oauthSessionID, ok := m.UIInfoResolver.GetOAuthSessionID(r, ""); ok {
result, session := m.createSession(oauthSessionID)
for _, c := range result.Cookies {
httputil.UpdateCookie(w, c)
}
// Remove oauth session ID so that we do not create again.
m.UIInfoResolver.RemoveOAuthSessionID(w, r)
r = r.WithContext(WithSession(r.Context(), session))
} else {
// Or read from cookie
session, err := m.loadSession(r)
if err != nil {
if errors.Is(err, ErrSessionNotFound) {
// fallthrough
} else if errors.Is(err, ErrInvalidSession) {
// Clear the session before continuing
cookie := m.Cookies.ClearCookie(m.CookieDef.Def)
httputil.UpdateCookie(w, cookie)
} else {
panic(err)
}
}
if session != nil {
r = r.WithContext(WithSession(r.Context(), session))
}
}
next.ServeHTTP(w, r)
})
}
func (m *SessionMiddleware) createSession(oauthSessionID string) (*Result, *Session) {
// When oauth session is not found, we fall back gracefully
// with a zero value of SessionOptions
sessionOptions := SessionOptions{}
entry, err := m.OAuthSessions.Get(oauthSessionID)
if err != nil && !errors.Is(err, oauthsession.ErrNotFound) {
panic(err)
}
// err == nil || err == oauthsession.ErrNotFound
if entry != nil {
req := entry.T.AuthorizationRequest
uiInfo, err := m.UIInfoResolver.ResolveForUI(req)
if err != nil {
panic(err)
}
sessionOptions = SessionOptions{
OAuthSessionID: oauthSessionID,
RedirectURI: uiInfo.RedirectURI,
Prompt: uiInfo.Prompt,
UserIDHint: uiInfo.UserIDHint,
CanUseIntentReauthenticate: uiInfo.CanUseIntentReauthenticate,
Page: uiInfo.Page,
SuppressIDPSessionCookie: uiInfo.SuppressIDPSessionCookie,
OAuthProviderAlias: uiInfo.OAuthProviderAlias,
LoginHint: uiInfo.LoginHint,
}
}
session := NewSession(sessionOptions)
// We do not need to redirect here so redirectURI is unimportant.
unimportant := ""
result, err := m.Sessions.CreateSession(session, unimportant)
if err != nil {
panic(err)
}
return result, session
}
func (m *SessionMiddleware) loadSession(r *http.Request) (*Session, error) {
cookie, err := m.Cookies.GetCookie(r, m.CookieDef.Def)
if err != nil {
return nil, ErrSessionNotFound
}
s, err := m.States.Get(cookie.Value)
if err != nil {
return nil, err
}
return s, nil
}