-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdefaultAuthChecker.go
205 lines (196 loc) · 7.61 KB
/
defaultAuthChecker.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
package sniproxy
import (
"errors"
"fmt"
"net/http"
"time"
)
var (
//DefaultAuthCheckerErr is returned when an unknown error occurs during authChecker run
DefaultAuthCheckerErr = errors.New("Unknown Error occurred in DefaultAuthChecker")
//DefaultAuthCheckerTokenMakerErr is returned when an error occurs during tokenMaker run
DefaultAuthCheckerTokenMakerErr = errors.New("Error occurred while baking a token")
//DefaultAuthCheckerTokenValidationErr is returned when an error occurs during token validation
DefaultAuthCheckerTokenValidationErr = errors.New("Error occured while validating the token")
//DefaultAuthCheckerTokenExpiredErr is returned when an authToken has expired its lifetime
DefaultAuthCheckerTokenExpiredErr = errors.New("AuthToken has expired")
)
type defaultAuthChecker struct {
authToken AuthToken
}
//NewDefaultAuthChecker function returns a new instance of defaultAuthChecker
func NewDefaultAuthChecker(token AuthToken) *defaultAuthChecker {
authChecker := &defaultAuthChecker{
authToken: token,
}
return authChecker
}
func (c *defaultAuthChecker) CheckAuth(r *http.Request, w http.ResponseWriter,
authScheme string, tokenTypeString string) (bool, TokenSetter, bool, error) {
tokenType, enumerationError := enumerateDefaultAuthTokenType(tokenTypeString)
// if the tokenTypeString cannot be enumerated, we will check everywhere
if enumerationError != nil {
tokenType = EITHER
}
switch tokenType {
case COOKIE:
//check whether cookie value in the request matches up to the authTokenName
cookie, cookieReadErr := r.Cookie(c.authToken.GetTokenName())
if cookieReadErr != nil {
authenticated, tokenSetter, responded, authErr := c.authenticate(r, w, authScheme, tokenType)
if responded || authErr != nil || !authenticated {
return authenticated, tokenSetter, responded, authErr
}
// this means this is the very first request and hence there was no cookie
if tokenSetter != nil {
tokenSetter.SetToken(w, r, tokenType.String())
}
//we will temporarily redirect user back to the same location
//and hope the cookie is presented this time
http.Redirect(w, r, r.URL.RequestURI(), http.StatusTemporaryRedirect)
return authenticated, tokenSetter, true, authErr
} else {
tokenValidated, caller, validationErr := c.authToken.Validate(cookie.Value,
authScheme)
if validationErr != nil {
return false, nil, false, validationErr
}
if !tokenValidated {
authenticated, tokenSetter, responded, authErr := c.authenticate(r, w, authScheme, tokenType)
if responded || authErr != nil || !authenticated {
return authenticated, tokenSetter, responded, authErr
}
if tokenSetter != nil {
tokenSetter.SetToken(w, r, tokenType.String())
}
//we will temporarily redirect user back to the same location
//and hope the cookie is presented this time
http.Redirect(w, r, r.URL.RequestURI(), http.StatusTemporaryRedirect)
return authenticated, tokenSetter, true, authErr
}
//let's get rid of the authTokenCookie here
authCookieAsString := cookie.String()
allCookies := r.Cookies()
allCookiesAsString := ""
for i, eachOne := range allCookies {
if eachOne.String() != authCookieAsString {
if i < len(allCookies)-1 {
allCookiesAsString = allCookiesAsString + eachOne.String() + "; "
} else {
allCookiesAsString = allCookiesAsString + eachOne.String()
}
}
}
r.Header.Del("Cookie")
if allCookiesAsString != "" {
r.Header.Set("Cookie", allCookiesAsString)
}
//let's add the caller's identity to the request here
r.Header.Set("X-AuthIdentity", caller)
return true, nil, false, nil
}
case HEADER:
header := r.Header.Get(c.authToken.GetTokenName())
if header == "" {
authenticated, tokenSetter, responded, authErr := c.authenticate(r, w, authScheme, tokenType)
if responded || authErr != nil || !authenticated {
return authenticated, tokenSetter, responded, authErr
}
// this means this is the very first request and hence there was no header value
if tokenSetter != nil {
tokenSetter.SetToken(w, r, tokenType.String())
}
//we will temporarily redirect user back to the same location
//and hope the header value is presented this time
http.Redirect(w, r, r.URL.RequestURI(), http.StatusTemporaryRedirect)
return authenticated, tokenSetter, true, authErr
} else {
tokenValidated, caller, validationErr := c.authToken.Validate(header, authScheme)
if validationErr != nil {
return false, nil, false, validationErr
}
if !tokenValidated {
authenticated, tokenSetter, responded, authErr := c.authenticate(r, w, authScheme, tokenType)
if responded || authErr != nil || !authenticated {
return authenticated, tokenSetter, responded, authErr
}
if tokenSetter != nil {
tokenSetter.SetToken(w, r, tokenType.String())
}
//we will temporarily redirect user back to the same location
//and hope the cookie is presented this time
http.Redirect(w, r, r.URL.RequestURI(), http.StatusTemporaryRedirect)
return authenticated, tokenSetter, true, authErr
}
//let's get rid of the authtokenheader here
r.Header.Del(c.authToken.GetTokenName())
//let's add the caller's identity to the request here
r.Header.Set("X-AuthIdentity", caller)
return true, nil, false, nil
}
case EITHER:
//check whether cookie value in the request matches up to the authTokenName
cookie, cookieReadErr := r.Cookie(c.authToken.GetTokenName())
header := r.Header.Get(c.authToken.GetTokenName())
if cookieReadErr != nil && header == "" {
return c.authenticate(r, w, authScheme, tokenType)
}
if header != "" {
authorized, tokenSetter, responseFulfilled, authCheckErr :=
c.CheckAuth(r, w, authScheme, HEADER.String())
if authorized {
if cookieReadErr == nil {
//let's get rid of the authTokenCookie here
authCookieAsString := cookie.String()
allCookies := r.Cookies()
allCookiesAsString := ""
for i, eachOne := range allCookies {
if eachOne.String() != authCookieAsString {
if i < len(allCookies)-1 {
allCookiesAsString = allCookiesAsString + eachOne.String() + "; "
} else {
allCookiesAsString = allCookiesAsString + eachOne.String()
}
}
}
r.Header.Del("Cookie")
if allCookiesAsString != "" {
r.Header.Set("Cookie", allCookiesAsString)
}
}
return authorized, tokenSetter, responseFulfilled, authCheckErr
}
}
return c.CheckAuth(r, w, authScheme, COOKIE.String())
}
return false, nil, false, DefaultAuthCheckerErr
}
func (c *defaultAuthChecker) authenticate(r *http.Request, w http.ResponseWriter,
authScheme string, tokenType defaultAuthTokenType) (bool, TokenSetter, bool, error) {
if _, ok := authenticators[authScheme]; !ok {
return false, nil, false, fmt.Errorf("Authenticator not found for %v", authScheme)
}
principal, respondedFlag, authenticationError := authenticators[authScheme].Authenticate(r, w)
if authenticationError != nil {
//authentication could not be done on the request
return false, nil, bool(respondedFlag), DefaultAuthCheckerErr
}
if principal == "" {
return false, nil, bool(respondedFlag), nil
}
if bool(respondedFlag) == true {
return false, nil, true, nil
}
token, tokenMakerErr := c.authToken.TokenMaker(r, string(principal),
time.Now().Add(DefaultAuthTokenExpirationDurationInHours*time.Hour),
authScheme, tokenType)
if tokenMakerErr != nil {
return true, nil, bool(respondedFlag), DefaultAuthCheckerTokenMakerErr
}
var tokenSetter = &defaultTokenSetter{
tokenValue: token,
tokenName: c.authToken.GetTokenName(),
}
//tokenSetter.(TokenSetter)
return true, tokenSetter, bool(respondedFlag), nil
}