@@ -100,7 +100,6 @@ func apiRequest(req *http.Request) (*simplejson.Json, error) {
100
100
}
101
101
102
102
func (p * OauthProxy ) redeemCode (code string ) (string , error ) {
103
-
104
103
params := url.Values {}
105
104
params .Add ("redirect_uri" , p .redirectUrl .String ())
106
105
params .Add ("client_id" , p .clientID )
@@ -125,6 +124,7 @@ func (p *OauthProxy) redeemCode(code string) (string, error) {
125
124
}
126
125
return access_token , nil
127
126
}
127
+
128
128
func (p * OauthProxy ) getUserInfo (token string ) (string , error ) {
129
129
params := url.Values {}
130
130
params .Add ("access_token" , token )
@@ -164,29 +164,33 @@ func ClearCookie(rw http.ResponseWriter, req *http.Request, key string) {
164
164
http .SetCookie (rw , cookie )
165
165
}
166
166
167
- func ErrorPage (rw http.ResponseWriter , code int , title string , message string , signinmessage string ) {
168
- log .Printf ("ErrorPage %d %s %s %s " , code , title , message , signinmessage )
167
+ func ( p * OauthProxy ) ErrorPage (rw http.ResponseWriter , code int , title string , message string ) {
168
+ log .Printf ("ErrorPage %d %s %s" , code , title , message )
169
169
rw .WriteHeader (code )
170
- t := getTemplates ()
171
- p := struct {
170
+ templates := getTemplates ()
171
+ t := struct {
172
172
Title string
173
173
Message string
174
- SignInMessage string
175
174
}{
176
175
Title : fmt .Sprintf ("%d %s" , code , title ),
177
176
Message : message ,
178
- SignInMessage : signinmessage ,
179
177
}
180
- t .ExecuteTemplate (rw , "error.html" , p )
178
+ templates .ExecuteTemplate (rw , "error.html" , t )
179
+ }
180
+
181
+ func (p * OauthProxy ) SignInPage (rw http.ResponseWriter , req * http.Request , code int ) {
182
+ // TODO: capture state for which url to redirect to at the end
183
+ rw .WriteHeader (code )
184
+ templates := getTemplates ()
185
+ t := struct { SignInMessage string }{SignInMessage : p .SignInMessage }
186
+ templates .ExecuteTemplate (rw , "sign_in.html" , t )
181
187
}
182
188
183
189
func (p * OauthProxy ) ServeHTTP (rw http.ResponseWriter , req * http.Request ) {
184
190
// check if this is a redirect back at the end of oauth
185
191
if req .URL .Path == signInPath {
186
192
ClearCookie (rw , req , p .CookieKey )
187
- t := getTemplates ()
188
- p := struct { SignInMessage string }{SignInMessage : p .SignInMessage }
189
- t .ExecuteTemplate (rw , "sign_in.html" , p )
193
+ p .SignInPage (rw , req , 200 )
190
194
return
191
195
}
192
196
if req .URL .Path == oauthStartPath {
@@ -197,31 +201,31 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
197
201
// finish the oauth cycle
198
202
reqParams , err := url .ParseQuery (req .URL .RawQuery )
199
203
if err != nil {
200
- ErrorPage (rw , 500 , "Internal Error" , err .Error (), p . SignInMessage )
204
+ p . ErrorPage (rw , 500 , "Internal Error" , err .Error ())
201
205
return
202
206
}
203
207
errorString , ok := reqParams ["error" ]
204
208
if ok && len (errorString ) == 1 {
205
- ErrorPage (rw , 403 , "Permission Denied" , errorString [0 ], p . SignInMessage )
209
+ p . ErrorPage (rw , 403 , "Permission Denied" , errorString [0 ])
206
210
return
207
211
}
208
212
code , ok := reqParams ["code" ]
209
213
if ! ok || len (code ) != 1 {
210
- ErrorPage (rw , 500 , "Internal Error" , "Invalid API response" , p . SignInMessage )
214
+ p . ErrorPage (rw , 500 , "Internal Error" , "Invalid API response" )
211
215
return
212
216
}
213
217
214
218
token , err := p .redeemCode (code [0 ])
215
219
if err != nil {
216
220
log .Printf ("error redeeming code %s" , err .Error ())
217
- ErrorPage (rw , 500 , "Internal Error" , err .Error (), p . SignInMessage )
221
+ p . ErrorPage (rw , 500 , "Internal Error" , err .Error ())
218
222
return
219
223
}
220
224
// validate user
221
225
email , err := p .getUserInfo (token )
222
226
if err != nil {
223
227
log .Printf ("error redeeming code %s" , err .Error ())
224
- ErrorPage (rw , 500 , "Internal Error" , err .Error (), p . SignInMessage )
228
+ p . ErrorPage (rw , 500 , "Internal Error" , err .Error ())
225
229
return
226
230
}
227
231
@@ -246,10 +250,11 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
246
250
http .Redirect (rw , req , "/" , 302 )
247
251
return
248
252
} else {
249
- ErrorPage (rw , 403 , "Permission Denied" , "Invalid Account" , p . SignInMessage )
253
+ p . ErrorPage (rw , 403 , "Permission Denied" , "Invalid Account" )
250
254
return
251
255
}
252
256
}
257
+
253
258
cookie , err := req .Cookie (p .CookieKey )
254
259
var ok bool
255
260
var email string
@@ -264,9 +269,8 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
264
269
}
265
270
266
271
if ! ok {
267
- log .Printf ("invalid cookie. redirecting to sign in" )
268
- // TODO: capture state for which url to redirect to at the end
269
- http .Redirect (rw , req , "/oauth2/sign_in" , 302 )
272
+ log .Printf ("invalid cookie" )
273
+ p .SignInPage (rw , req , 403 )
270
274
return
271
275
}
272
276
0 commit comments