-
Notifications
You must be signed in to change notification settings - Fork 43
/
auth-utils.go
175 lines (150 loc) · 4.75 KB
/
auth-utils.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
package jwt
import (
"errors"
"log"
"net/http"
"strconv"
"strings"
"time"
)
// return is (authTokenString, refreshTokenString, err)
func (a *Auth) extractTokenStringsFromReq(r *http.Request) (string, string, *jwtError) {
// read cookies
if a.options.BearerTokens {
// tokens are not in cookies
// Note: we don't check for errors here, because we will check if the token is valid, later
return r.Header.Get(a.options.AuthTokenName), r.Header.Get(a.options.RefreshTokenName), nil
}
var (
authCookieValue string
refreshCookieValue string
)
AuthCookie, authErr := r.Cookie(a.options.AuthTokenName)
if authErr == http.ErrNoCookie {
a.myLog("Unauthorized attempt! No auth cookie")
return "", "", newJwtError(errors.New("No auth cookie"), 401)
} else if authErr != nil {
// a.myLog(authErr)
return "", "", newJwtError(errors.New("Internal Server Error"), 500)
}
RefreshCookie, refreshErr := r.Cookie(a.options.RefreshTokenName)
if refreshErr != nil && refreshErr != http.ErrNoCookie {
a.myLog(refreshErr)
return "", "", newJwtError(errors.New("Internal Server Error"), 500)
}
if AuthCookie != nil {
authCookieValue = AuthCookie.Value
}
if RefreshCookie != nil {
refreshCookieValue = RefreshCookie.Value
}
return authCookieValue, refreshCookieValue, nil
}
func (a *Auth) extractCsrfStringFromReq(r *http.Request) (string, *jwtError) {
csrfString := r.FormValue(a.options.CSRFTokenName)
if csrfString != "" {
return csrfString, nil
}
csrfString = r.Header.Get(a.options.CSRFTokenName)
if csrfString != "" {
return csrfString, nil
}
auth := r.Header.Get("Authorization")
csrfString = strings.Replace(auth, "Bearer", "", 1)
csrfString = strings.Replace(csrfString, " ", "", -1)
if csrfString == "" {
return csrfString, newJwtError(errors.New("No CSRF string"), 401)
}
return csrfString, nil
}
func (a *Auth) setCredentialsOnResponseWriter(w http.ResponseWriter, c *credentials) *jwtError {
var (
refreshTokenString string
refreshTokenClaims *ClaimsType
)
authTokenString, err := c.AuthToken.Token.SignedString(a.signKey)
if err != nil {
return newJwtError(err, 500)
}
if c.RefreshToken != nil && c.RefreshToken.Token != nil {
a.myLog(c.RefreshToken)
a.myLog(c.RefreshToken.Token)
refreshTokenString, err = c.RefreshToken.Token.SignedString(a.signKey)
if err != nil {
return newJwtError(err, 500)
}
}
if a.options.BearerTokens {
// tokens are not in cookies
setHeader(w, a.options.AuthTokenName, authTokenString)
if refreshTokenString != "" {
setHeader(w, a.options.RefreshTokenName, refreshTokenString)
}
} else {
// tokens are in cookies
// note: don't use an "Expires" in auth cookies bc browsers won't send expired cookies?
authCookie := http.Cookie{
Name: a.options.AuthTokenName,
Value: authTokenString,
Path: "/",
// Expires: time.Now().Add(a.options.AuthTokenValidTime),
HttpOnly: true,
Secure: !a.options.IsDevEnv,
}
http.SetCookie(w, &authCookie)
if refreshTokenString != "" {
refreshCookie := http.Cookie{
Name: a.options.RefreshTokenName,
Value: refreshTokenString,
Expires: time.Now().Add(a.options.RefreshTokenValidTime),
Path: "/",
HttpOnly: true,
Secure: !a.options.IsDevEnv,
}
http.SetCookie(w, &refreshCookie)
}
}
authTokenClaims, ok := c.AuthToken.Token.Claims.(*ClaimsType)
if !ok {
a.myLog("Cannot read auth token claims")
return newJwtError(errors.New("Cannot read token claims"), 500)
}
if c.RefreshToken != nil && c.RefreshToken.Token != nil {
refreshTokenClaims, ok = c.RefreshToken.Token.Claims.(*ClaimsType)
if !ok {
a.myLog("Cannot read refresh token claims")
return newJwtError(errors.New("Cannot read token claims"), 500)
}
}
w.Header().Set(a.options.CSRFTokenName, c.CsrfString)
// note @adam-hanna: this may not be correct when using a sep auth server?
// bc it checks the request?
w.Header().Set("Auth-Expiry", strconv.FormatInt(authTokenClaims.StandardClaims.ExpiresAt, 10))
if refreshTokenClaims != nil {
w.Header().Set("Refresh-Expiry", strconv.FormatInt(refreshTokenClaims.StandardClaims.ExpiresAt, 10))
}
return nil
}
func (a *Auth) buildCredentialsFromRequest(r *http.Request, c *credentials) *jwtError {
authTokenString, refreshTokenString, err := a.extractTokenStringsFromReq(r)
if err != nil {
return newJwtError(err, 500)
}
csrfString, err := a.extractCsrfStringFromReq(r)
if err != nil {
return newJwtError(err, 500)
}
err = a.buildCredentialsFromStrings(csrfString, authTokenString, refreshTokenString, c)
if err != nil {
return newJwtError(err, 500)
}
return nil
}
func (a *Auth) myLog(stoofs interface{}) {
if a.options.Debug {
log.Println(stoofs)
}
}
func setHeader(w http.ResponseWriter, header string, value string) {
w.Header().Set(header, value)
}