@@ -128,8 +128,6 @@ func authHandler(req events.Request) (events.Response, error) {
128
128
129
129
func callbackHandler (req events.Request ) (events.Response , error ) {
130
130
// TODO: Break this method apart
131
- errorRedirect , _ := events .Redirect ("https://" + req .Headers ["Host" ], 303 )
132
-
133
131
sess , err := sm .Read (req )
134
132
if err != nil {
135
133
log .Printf ("Failed loading session cookie: %s" , err )
@@ -140,34 +138,34 @@ func callbackHandler(req events.Request) (events.Response, error) {
140
138
141
139
if sess .Nonce == "" {
142
140
log .Print ("callback hit with no nonce" )
143
- return errorRedirect , nil
141
+ return events . Redirect ( "https://" + req . Headers [ "Host" ], 303 )
144
142
} else if sess .Nonce != actual {
145
143
log .Print ("nonce mismatch; possible csrf OR cookies not enabled" )
146
- return errorRedirect , nil
144
+ return events . Fail ( "error; aborting" )
147
145
}
148
146
149
147
code := req .QueryStringParameters ["code" ]
150
148
token , err := oauthCfg .Exchange (oauth2 .NoContext , code )
151
149
if err != nil {
152
150
log .Print ("there was an issue getting your token" )
153
- return errorRedirect , nil
151
+ return events . Fail ( "error; aborting" )
154
152
}
155
153
156
154
if ! token .Valid () {
157
155
log .Print ("retreived invalid token" )
158
- return errorRedirect , nil
156
+ return events . Fail ( "error; aborting" )
159
157
}
160
158
161
159
client := github .NewClient (oauthCfg .Client (oauth2 .NoContext , token ))
162
160
user , _ , err := client .Users .Get (context .Background (), "" )
163
161
if err != nil {
164
162
log .Print ("error getting name" )
165
- return errorRedirect , nil
163
+ return events . Fail ( "error; aborting" )
166
164
}
167
165
orgs , _ , err := client .Organizations .List (context .Background (), "" , & github.ListOptions {})
168
166
if err != nil {
169
167
log .Print ("error getting orgs" )
170
- return errorRedirect , nil
168
+ return events . Fail ( "error; aborting" )
171
169
}
172
170
var orgList []string
173
171
for _ , i := range orgs {
@@ -181,7 +179,7 @@ func callbackHandler(req events.Request) (events.Response, error) {
181
179
cookie , err := sm .Write (sess )
182
180
if err != nil {
183
181
log .Print ("error encoding cookie" )
184
- return errorRedirect , nil
182
+ return events . Fail ( "error; aborting" )
185
183
}
186
184
return events.Response {
187
185
StatusCode : 303 ,
@@ -196,39 +194,39 @@ func indexHandler(req events.Request) (events.Response, error) {
196
194
// TODO: use nicer homepage template
197
195
// TODO: Show if you're already auth'd
198
196
// TODO: Show link to auth page
199
- //return events.Succeed("Index!")
200
197
return events .Succeed (fmt .Sprintf ("%+v\n " , req ))
201
198
}
202
199
203
200
func defaultHandler (req events.Request ) (events.Response , error ) {
204
201
return events .Redirect ("https://" + req .Headers ["Host" ], 303 )
205
202
}
206
203
207
- func loadConfig () {
204
+ func loadConfig () (* configFile , error ) {
205
+ c := configFile {}
206
+
208
207
bucket := os .Getenv ("S3_BUCKET" )
209
208
path := os .Getenv ("S3_KEY" )
210
209
if bucket == "" || path == "" {
211
- log .Print ("variables not provided" )
212
- return
210
+ return & c , fmt .Errorf ("variables not provided" )
213
211
}
214
212
215
213
obj , err := s3 .GetObject (bucket , path )
216
214
if err != nil {
217
- log .Print (err )
218
- return
215
+ return & c , err
219
216
}
220
217
221
- c := configFile {}
222
218
err = yaml .Unmarshal (obj , & c )
219
+ return & c , err
220
+ }
221
+
222
+ func main () {
223
+ var err error
224
+
225
+ config , err = loadConfig ()
223
226
if err != nil {
224
227
log .Print (err )
225
228
return
226
229
}
227
- config = & c
228
- }
229
-
230
- func main () {
231
- loadConfig ()
232
230
233
231
sm = & sessionManager {
234
232
Name : "session" ,
0 commit comments