forked from louketo/louketo-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
287 lines (243 loc) · 8.49 KB
/
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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Copyright 2015 All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/base64"
"fmt"
"net/http"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/gambol99/go-oidc/jose"
"github.com/gambol99/go-oidc/oidc"
"github.com/gin-gonic/gin"
)
const (
claimPreferredName = "preferred_username"
claimAudience = "aud"
claimResourceAccess = "resource_access"
claimResourceRoles = "roles"
)
// sessionState holds the state related data
type sessionState struct {
// the max time the session is permitted
expireOn time.Time
// the refresh token if any
refreshToken string
}
// refreshUserSessionToken is responsible for retrieving the session state cookie and attempting to
// refresh the access token for the user
func (r *KeycloakProxy) refreshUserSessionToken(cx *gin.Context) (jose.JWT, error) {
// step: grab the session state cooke
state, err := r.getSessionState(cx)
if err != nil {
return jose.JWT{}, ErrNoSessionStateFound
}
// step: has the refresh token expired
if time.Now().After(state.expireOn) {
log.Warningf("failed to refresh the access token, the refresh token has expired, expiration: %s", state.expireOn)
return jose.JWT{}, ErrAccessTokenExpired
}
// step: attempts to refresh the access token
token, expires, err := r.refreshAccessToken(state.refreshToken)
if err != nil {
// step: has the refresh token expired
if err == ErrRefreshTokenExpired {
log.WithFields(log.Fields{"token": token}).Warningf("the refresh token has expired")
// clear the session
clearSessionState(cx)
}
log.WithFields(log.Fields{"error": err.Error()}).Errorf("failed to refresh the access token")
return jose.JWT{}, err
}
// step: inject the refreshed access token
log.Infof("injecting the refreshed access token into seesion, expires on: %s", expires)
// step: create the session
if err := r.createSession(token, expires, cx); err != nil {
return token, err
}
return token, nil
}
// getSessionToken retrieves the authentication cookie from the request and parse's into a JWT token
// The token can come either a session cookie or a Bearer header
func (r *KeycloakProxy) getSessionToken(cx *gin.Context) (jose.JWT, bool, error) {
var session string
isBearer := false
// step: look for a authorization header
if authHeader := cx.Request.Header.Get(authorizationHeader); authHeader != "" {
isBearer = true
items := strings.Split(authHeader, " ")
if len(items) != 2 {
return jose.JWT{}, isBearer, fmt.Errorf("invalid authorizarion header")
}
session = items[1]
} else {
// step: find the authentication cookie from the request
cookie := findCookie(sessionCookieName, cx.Request.Cookies())
if cookie == nil {
return jose.JWT{}, isBearer, ErrSessionNotFound
}
session = cookie.Value
}
// step: parse the token
jwt, err := jose.ParseJWT(session)
if err != nil {
return jose.JWT{}, isBearer, err
}
return jwt, isBearer, nil
}
// getSessionState retrieves the session state from the request
func (r *KeycloakProxy) getSessionState(cx *gin.Context) (*sessionState, error) {
// step: find the session data cookie
cookie := findCookie(sessionStateCookieName, cx.Request.Cookies())
if cookie == nil {
return nil, ErrNoCookieFound
}
return r.decodeState(cookie.Value)
}
// getUserContext parse the jwt token and extracts the various elements is order to construct
func (r *KeycloakProxy) getUserContext(token jose.JWT) (*userContext, error) {
// step: decode the claims from the tokens
claims, err := token.Claims()
if err != nil {
return nil, err
}
// step: extract the identity
identity, err := oidc.IdentityFromClaims(claims)
if err != nil {
return nil, err
}
// step: ensure we have and can extract the preferred name of the user, if not, we set to the ID
preferredName, found, err := claims.StringClaim(claimPreferredName)
if err != nil || !found {
log.WithFields(log.Fields{
"id": identity.ID,
"email": identity.Email,
}).Warnf("the token does not container a %s", claimPreferredName)
// choice: set the preferredName to the ID if claim not found
preferredName = identity.ID
}
// step: retrieve the audience from access token
audience, found, err := claims.StringClaim(claimAudience)
if err != nil || !found {
return nil, fmt.Errorf("the access token does not container a audience claim")
}
var list []string
// step: extract the roles from the access token
if accesses, found := claims[claimResourceAccess].(map[string]interface{}); found {
for roleName, roleList := range accesses {
scopes := roleList.(map[string]interface{})
if roles, found := scopes[claimResourceRoles]; found {
for _, r := range roles.([]interface{}) {
list = append(list, fmt.Sprintf("%s:%s", roleName, r))
}
}
}
}
return &userContext{
id: identity.ID,
name: preferredName,
audience: audience,
preferredName: preferredName,
email: identity.Email,
expiresAt: identity.ExpiresAt,
roles: list,
token: token,
claims: claims,
}, nil
}
// createSession creates a session cookie with the access token
func (r *KeycloakProxy) createSession(token jose.JWT, expires time.Time, cx *gin.Context) error {
http.SetCookie(cx.Writer, createSessionCookie(token.Encode(), cx.Request.Host, expires))
return nil
}
// createSessionState creates a session state cookie, used to hold the refresh cookie and the expiration time
func (r *KeycloakProxy) createSessionState(state *sessionState, cx *gin.Context) error {
// step: we need to encode the state
encoded, err := r.encodeState(state)
if err != nil {
return err
}
// step: create a session state cookie
http.SetCookie(cx.Writer, createSessionStateCookie(encoded, cx.Request.Host, state.expireOn))
return nil
}
// encodeState encodes the session state information into a value for a cookie to consume
func (r *KeycloakProxy) encodeState(session *sessionState) (string, error) {
// step: encode the session into a string
encoded := fmt.Sprintf("%d|%s", session.expireOn.Unix(), session.refreshToken)
// step: encrypt the cookie
cipherText, err := encryptDataBlock([]byte(encoded), []byte(r.config.EncryptionKey))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(cipherText), nil
}
// decodeState decodes the session state cookie value
func (r *KeycloakProxy) decodeState(state string) (*sessionState, error) {
// step: decode the base64 encrypted cookie
cipherText, err := base64.StdEncoding.DecodeString(state)
if err != nil {
return nil, err
}
// step: decrypt the cookie back in the expiration|token
plainText, err := decryptDataBlock(cipherText, []byte(r.config.EncryptionKey))
if err != nil {
return nil, ErrInvalidSession
}
// step: extracts the sections from the state
sections := strings.Split(string(plainText), "|")
if len(sections) != 2 {
return nil, ErrInvalidSession
}
// step: convert the unit timestamp
expiration, err := convertUnixTime(sections[0])
if err != nil {
return nil, ErrInvalidSession
}
return &sessionState{
expireOn: expiration,
refreshToken: sections[1],
}, nil
}
// createSessionCookie creates a new session cookie
func createSessionCookie(token, hostname string, expires time.Time) *http.Cookie {
return &http.Cookie{
Name: sessionCookieName,
Domain: strings.Split(hostname, ":")[0],
Path: "/",
Expires: expires,
HttpOnly: true,
// Secure: true,
Value: token,
}
}
// createSessionStateCookie creates a new session state cookie
func createSessionStateCookie(token, hostname string, expires time.Time) *http.Cookie {
return &http.Cookie{
Name: sessionStateCookieName,
Domain: strings.Split(hostname, ":")[0],
Path: "/",
HttpOnly: true,
//Secure: true,
Value: token,
}
}
// clearSessionState clears the session cookie
func clearSessionState(cx *gin.Context) {
http.SetCookie(cx.Writer, createSessionStateCookie("", cx.Request.Host, time.Now()))
}
// clearSession clears the session cookie
func clearSession(cx *gin.Context) {
http.SetCookie(cx.Writer, createSessionCookie("", cx.Request.Host, time.Now()))
}