-
Notifications
You must be signed in to change notification settings - Fork 35
/
login.go
308 lines (251 loc) · 8.11 KB
/
login.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
oidc "github.com/coreos/go-oidc"
jwt "github.com/dgrijalva/jwt-go"
"github.com/google/uuid"
"golang.org/x/oauth2"
)
var ctx context.Context
var oauth2Config oauth2.Config
var oidcProvider *oidc.Provider
var oidcConfig *oidc.Config
var hmacSecret []byte
var nonceChars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
var loginSessions []*loginSession
func init() {
hostname = strings.Split(parseEnvURL("SELF_URL").Host, ":")[0] // Because Host still has a port if it was in URL
clientID := parseEnvVar("CLIENT_ID")
clientSecret := parseEnvVar("CLIENT_SECRET")
ctx = context.Background()
provider, err := oidc.NewProvider(ctx, parseEnvURL("OIDC_PROVIDER").String())
if err != nil {
log.Fatal("OIDC provider setup failed: ", err)
}
oidcConfig = &oidc.Config{
ClientID: clientID,
}
var oidcScopes []string
// "openid" (oidc.ScopeOpenID) is a required scope for OpenID Connect flows.
oidcScopes = append(oidcScopes, oidc.ScopeOpenID)
for _, elem := range strings.Split(parseEnvVar("OIDC_SCOPES"), " ") {
oidcScopes = append(oidcScopes, elem)
}
var redirURL = parseEnvURL("SELF_URL").String()
if string(redirURL[len(redirURL)-1]) == "/" {
redirURL = string(redirURL[:len(redirURL)-1])
}
redirURL = redirURL + "/login/oidc"
oauth2Config = oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: redirURL,
// Discovery returns the OAuth2 endpoints.
Endpoint: provider.Endpoint(),
Scopes: oidcScopes,
}
oidcProvider = provider
rand.Seed(time.Now().UnixNano())
// 64 char(512 bit) key is needed for HS512
hmacSecret = initialiseHMACSecretFromEnv("JWT_HMAC_SECRET", 64)
}
type loginSession struct {
State string
Validity time.Time
OrigURL string
}
// OIDCHandler processes authn responses from OpenID Provider, exchanges token to userinfo and establishes user session with cookie containing JWT token
func OIDCHandler(w http.ResponseWriter, r *http.Request) {
var authCode = r.FormValue("code")
if len(authCode) == 0 {
log.Println(getUserIP(r), "Missing url parameter: code")
returnStatus(w, http.StatusBadRequest, "Missing url parameter: code")
return
}
var state = r.FormValue("state")
if len(state) == 0 {
log.Println(getUserIP(r), "Missing url parameter: state")
returnStatus(w, http.StatusBadRequest, "Missing url parameter: state")
return
}
// Getting original destination from DB with state
var destination = ""
if redisdb != nil {
var err error
destination, err = redisdb.Get("state-" + state).Result()
if err != nil {
if err.Error() == "redis: nil" { // State didn't exist, redirecting to new login
log.Print(getUserIP(r), " No state found with ", state, ", starting new auth session.\n")
beginOIDCLogin(w, r, "/")
return
}
returnStatus(w, http.StatusInternalServerError, "Error fetching state from DB.")
panic(err)
}
} else {
session, err := findLocalLoginSession(state)
if err != nil {
log.Print(getUserIP(r), " No state found with ", state, ", starting new auth session.\n")
beginOIDCLogin(w, r, "/")
return
}
destination = session.OrigURL
}
oauth2Token, err := oauth2Config.Exchange(ctx, authCode)
if err != nil {
log.Println("Failed to exchange token:", err.Error())
returnStatus(w, http.StatusInternalServerError, "Failed to exchange token.")
return
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
log.Println("No id_token field available.")
returnStatus(w, http.StatusInternalServerError, "No id_token field in OAuth 2.0 token.")
return
}
// Verifying received ID token
verifier := oidcProvider.Verifier(oidcConfig)
idToken, err := verifier.Verify(ctx, rawIDToken)
if err != nil {
log.Println("Not able to verify ID token:", err.Error())
returnStatus(w, http.StatusInternalServerError, "Unable to verify ID token.")
return
}
claims := json.RawMessage{}
if disableUserInfo {
if err = idToken.Claims(&claims); err != nil {
log.Println("Problem getting id_token claims:", err.Error())
returnStatus(w, http.StatusInternalServerError, "Not able to fetch id_token claims.")
return
}
} else {
userInfo, err := oidcProvider.UserInfo(ctx, oauth2.StaticTokenSource(oauth2Token))
if err != nil {
log.Println("Problem fetching userinfo:", err.Error())
returnStatus(w, http.StatusInternalServerError, "Not able to fetch userinfo.")
return
}
if err = userInfo.Claims(&claims); err != nil {
log.Println("Problem getting userinfo claims:", err.Error())
returnStatus(w, http.StatusInternalServerError, "Not able to fetch userinfo claims.")
return
}
}
userJwt := createSignedJWT(claims, idToken.Expiry)
cookie := createCookie(userJwt, idToken.Expiry, hostname)
// Removing OIDC flow state from DB
if redisdb != nil {
err = redisdb.Del("state-" + state).Err()
if err != nil {
log.Println("WARNING: Unable to remove state from DB,", err.Error())
}
} else {
removeLoginSession(state)
}
log.Println(getUserIP(r), "Login validated with ID token, redirecting with JWT cookie.") // TODO add user from userinfo
http.SetCookie(w, cookie)
http.Redirect(w, r, destination, http.StatusFound)
}
// beginOIDCLogin starts the login sequence by creating state and forwarding user to OIDC provider for verification
func beginOIDCLogin(w http.ResponseWriter, r *http.Request, origURL string) {
var state = createNonce(8)
if redisdb != nil {
err := redisdb.Set("state-"+state, origURL, time.Hour).Err()
if err != nil {
panic(err)
}
} else {
session := &loginSession{State: state, Validity: time.Now().Add(time.Hour), OrigURL: origURL}
loginSessions = append(loginSessions, session)
}
http.Redirect(w, r, oauth2Config.AuthCodeURL(state), http.StatusFound)
}
func createCookie(sessionJwt string, expiration time.Time, domain string) *http.Cookie {
cookie := &http.Cookie{
Name: "auth",
Value: sessionJwt,
Path: "/",
Domain: domain,
Expires: expiration,
}
return cookie
}
func createSignedJWT(userinfo []byte, expiration time.Time) string {
token := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims{
"jti": uuid.New().String(),
"iss": hostname,
"iat": time.Now().Unix(),
"exp": expiration.Unix(),
"uif": base64encode(userinfo), // Userinfo will be readable to user
})
tokenString, err := token.SignedString(hmacSecret)
if err != nil {
panic(err)
}
return tokenString
}
func createNonce(length int) string {
var nonce = make([]rune, length)
for i := range nonce {
nonce[i] = nonceChars[rand.Intn(len(nonceChars))]
}
return string(nonce)
}
func parseJWT(tokenstr string) (*jwt.Token, error) {
token, err := jwt.Parse(tokenstr, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return hmacSecret, nil
})
if err != nil {
return nil, err
}
if token.Valid {
return token, nil
}
return nil, errors.New("Token not valid")
}
func initialiseHMACSecretFromEnv(secEnv string, reqLen int) []byte {
envContent := os.Getenv(secEnv)
if len(envContent) < reqLen {
log.Println("WARNING: HMAC secret not provided or secret too short. Generating a random one from nonce characters.")
return []byte(createNonce(reqLen))
}
return []byte(envContent)
}
func findLocalLoginSession(state string) (*loginSession, error) {
for _, elem := range loginSessions {
if elem.State == state {
return elem, nil
}
}
return nil, errors.New("state not found")
}
func removeLoginSession(state string) {
for i, elem := range loginSessions {
if elem.State == state {
loginSessions[len(loginSessions)-1], loginSessions[i] = loginSessions[i], loginSessions[len(loginSessions)-1]
loginSessions = loginSessions[:len(loginSessions)-1]
return
}
}
log.Println("Tried to delete a nonexistent session, nothing found.")
}
func removeOldLoginSessions() {
for _, elem := range loginSessions {
if elem.Validity.Before(time.Now()) {
log.Println("Removing expired state", elem.State, "from active login sessions.")
removeLoginSession(elem.State)
}
}
}