forked from adam-hanna/jwt-auth
/
auth-utils.go
149 lines (127 loc) · 4.31 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
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
}
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 == http.ErrNoCookie {
a.myLog("Unauthorized attempt! No refresh cookie")
return "", "", newJwtError(errors.New("No refresh cookie"), 401)
} else if refreshErr != nil {
a.myLog(refreshErr)
return "", "", newJwtError(errors.New("Internal Server Error"), 500)
}
return AuthCookie.Value, RefreshCookie.Value, 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 {
authTokenString, err := c.AuthToken.Token.SignedString(a.signKey)
if err != nil {
return newJwtError(err, 500)
}
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)
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)
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)
}
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))
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)
}