forked from louketo/louketo-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
130 lines (110 loc) · 3.38 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
/*
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 (
"bytes"
"net/http"
"strconv"
"strings"
"github.com/coreos/go-oidc/jose"
"go.uber.org/zap"
)
// getIdentity retrieves the user identity from a request, either from a session cookie or a bearer token
func (r *oauthProxy) getIdentity(req *http.Request) (*userContext, error) {
var isBearer bool
// step: check for a bearer token or cookie with jwt token
access, isBearer, err := getTokenInRequest(req, r.config.CookieAccessName)
if err != nil {
return nil, err
}
if r.config.EnableEncryptedToken || r.config.ForceEncryptedCookie && !isBearer {
if access, err = decodeText(access, r.config.EncryptionKey); err != nil {
return nil, ErrDecryption
}
}
token, err := jose.ParseJWT(access)
if err != nil {
return nil, err
}
user, err := extractIdentity(token)
if err != nil {
return nil, err
}
user.bearerToken = isBearer
r.log.Debug("found the user identity",
zap.String("id", user.id),
zap.String("name", user.name),
zap.String("email", user.email),
zap.String("roles", strings.Join(user.roles, ",")),
zap.String("groups", strings.Join(user.groups, ",")))
return user, nil
}
// getRefreshTokenFromCookie returns the refresh token from the cookie if any
func (r *oauthProxy) getRefreshTokenFromCookie(req *http.Request) (string, error) {
token, err := getTokenInCookie(req, r.config.CookieRefreshName)
if err != nil {
return "", err
}
return token, nil
}
// getTokenInRequest returns the access token from the http request
func getTokenInRequest(req *http.Request, name string) (string, bool, error) {
bearer := true
// step: check for a token in the authorization header
token, err := getTokenInBearer(req)
if err != nil {
if err != ErrSessionNotFound {
return "", false, err
}
if token, err = getTokenInCookie(req, name); err != nil {
return token, false, err
}
bearer = false
}
return token, bearer, nil
}
// getTokenInBearer retrieves a access token from the authorization header
func getTokenInBearer(req *http.Request) (string, error) {
token := req.Header.Get(authorizationHeader)
if token == "" {
return "", ErrSessionNotFound
}
items := strings.Split(token, " ")
if len(items) != 2 {
return "", ErrInvalidSession
}
if items[0] != authorizationType {
return "", ErrSessionNotFound
}
return items[1], nil
}
// getTokenInCookie retrieves the access token from the request cookies
func getTokenInCookie(req *http.Request, name string) (string, error) {
var token bytes.Buffer
if cookie := findCookie(name, req.Cookies()); cookie != nil {
token.WriteString(cookie.Value)
}
// add also divided cookies
for i := 1; i < 600; i++ {
cookie := findCookie(name+"-"+strconv.Itoa(i), req.Cookies())
if cookie == nil {
break
} else {
token.WriteString(cookie.Value)
}
}
if token.Len() == 0 {
return "", ErrSessionNotFound
}
return token.String(), nil
}