This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.go
214 lines (181 loc) · 5.57 KB
/
auth.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
package okta
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/google/uuid"
"github.com/gorilla/sessions"
verifier "github.com/okta/okta-jwt-verifier-golang"
)
const (
sessionName = "okta-session"
sessionIDTokenKey = "id_token"
)
// AuthHandler manages okta based authentication
type AuthHandler struct {
sessionStore *sessions.CookieStore
clientID string
clientSecret string
issuer string
redirectURI string
errorWriter func(w http.ResponseWriter, r *http.Request, err error, status int)
}
func NewAuthHandler(sessionKey []byte, clientID, clientSecret, issuer, redirectURI string, errorWriter func(w http.ResponseWriter, r *http.Request, err error, status int)) *AuthHandler {
return &AuthHandler{
sessionStore: sessions.NewCookieStore(sessionKey),
clientID: clientID,
clientSecret: clientSecret,
issuer: issuer,
errorWriter: errorWriter,
redirectURI: redirectURI,
}
}
func (h *AuthHandler) isAuthenticated(r *http.Request) bool {
session, err := h.sessionStore.Get(r, sessionName)
if err != nil || session.Values[sessionIDTokenKey] == nil || session.Values[sessionIDTokenKey] == "" {
return false
}
return true
}
func (h *AuthHandler) Ensure(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if h.isAuthenticated(r) {
next.ServeHTTP(w, r)
return
}
nonce, err := generateNonce()
if err != nil {
log.Printf("failed to start okta auth: %s", err)
h.errorWriter(w, r, err, http.StatusInternalServerError)
return
}
q := r.URL.Query()
q.Add("client_id", h.clientID)
q.Add("response_type", "code")
q.Add("response_mode", "query")
q.Add("scope", "openid profile email")
q.Add("redirect_uri", h.redirectURI)
q.Add("state", uuid.New().String())
q.Add("nonce", nonce)
session, err := h.sessionStore.Get(r, sessionName)
if err != nil {
h.errorWriter(w, r, err, http.StatusInternalServerError)
return
}
session.Values["nonce"] = nonce
err = session.Save(r, w)
if err != nil {
h.errorWriter(w, r, err, http.StatusInternalServerError)
return
}
redirectPath := h.issuer + "/v1/authorize?" + q.Encode()
http.Redirect(w, r, redirectPath, http.StatusFound)
})
}
func (h *AuthHandler) AuthCodeCallbackHandler(w http.ResponseWriter, r *http.Request) {
// Make sure the code was provided
if r.URL.Query().Get("code") == "" {
h.errorWriter(w, r, errors.New("missing auth code"), http.StatusInternalServerError)
return
}
exchange, err := h.exchangeCode(r.URL.Query().Get("code"), r)
if err != nil {
h.errorWriter(w, r, err, http.StatusInternalServerError)
return
}
session, err := h.sessionStore.Get(r, sessionName)
if err != nil {
h.errorWriter(w, r, err, http.StatusInternalServerError)
return
}
var nonce string
// treat missing or invalid nonce as ""
value := session.Values["nonce"]
nonce, _ = value.(string)
_, err = h.verifyToken(exchange.IDToken, nonce)
if err != nil {
h.errorWriter(w, r, err, http.StatusForbidden)
return
}
session.Values["id_token"] = exchange.IDToken
session.Values["access_token"] = exchange.AccessToken
err = session.Save(r, w)
if err != nil {
h.errorWriter(w, r, err, http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
func generateNonce() (string, error) {
nonceBytes := make([]byte, 32)
_, err := rand.Read(nonceBytes)
if err != nil {
return "", fmt.Errorf("generating nonce: %w", err)
}
return base64.URLEncoding.EncodeToString(nonceBytes), nil
}
type exchange struct {
Error string `json:"error,omitempty"`
ErrorDescription string `json:"error_description,omitempty"`
AccessToken string `json:"access_token,omitempty"`
TokenType string `json:"token_type,omitempty"`
ExpiresIn int `json:"expires_in,omitempty"`
Scope string `json:"scope,omitempty"`
IDToken string `json:"id_token,omitempty"`
}
func (h *AuthHandler) exchangeCode(code string, r *http.Request) (*exchange, error) {
authHeader := base64.StdEncoding.EncodeToString(
[]byte(h.clientID + ":" + h.clientSecret))
q := r.URL.Query()
q.Add("grant_type", "authorization_code")
q.Add("code", code)
q.Add("redirect_uri", h.redirectURI)
url := h.issuer + "/v1/token?" + q.Encode()
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return nil, fmt.Errorf("constructing auth code exchange request: %w", err)
}
req.Header.Add("Authorization", "Basic "+authHeader)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Connection", "close")
req.Header.Add("Content-Length", "0")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("exchanging auth code: %w", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading response body: %w", err)
}
defer resp.Body.Close()
var exchange exchange
err = json.Unmarshal(body, &exchange)
if err != nil {
return nil, fmt.Errorf("parsing auth code exchange response: %w", err)
}
return &exchange, nil
}
func (h *AuthHandler) verifyToken(token, nonce string) (*verifier.Jwt, error) {
tv := map[string]string{}
tv["nonce"] = nonce
tv["aud"] = h.clientID
jv := verifier.JwtVerifier{
Issuer: h.issuer,
ClaimsToValidate: tv,
}
result, err := jv.New().VerifyIdToken(token)
if err != nil {
return nil, fmt.Errorf("verifying token: %w", err)
}
if result != nil {
return result, nil
}
return nil, errors.New("token could not be verified")
}