/
oauthCallback.go
45 lines (40 loc) · 1.2 KB
/
oauthCallback.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
package service
import (
"encoding/json"
"github.com/valyala/fasthttp"
)
func responseToMap(ctx *fasthttp.RequestCtx) map[string]interface{} {
res := make(map[string]interface{})
ctx.QueryArgs().VisitAll(func(key, val []byte) {
res[string(key)] = string(val)
})
ctx.PostArgs().VisitAll(func(key, val []byte) {
res[string(key)] = string(val)
})
mf, err := ctx.MultipartForm()
if err == nil && mf.Value != nil {
for key, val := range mf.Value {
// only one value is stored under key: https://openid.net/specs/oauth-v2-multiple-response-types-1_0.html
if len(val) == 1 {
res[key] = val[0]
}
}
}
return res
}
func (s *Service) HandleOAuthCallback(ctx *fasthttp.RequestCtx) {
state := ctx.FormValue("state")
_, ok := s.csrfTokens.Get(string(state))
if !ok {
setErrorResponse(&ctx.Response, fasthttp.StatusBadRequest, "invalid/expired OAuth state")
return
}
payload, _ := json.Marshal(responseToMap(ctx))
body := string(payload)
setHTMLResponse(&ctx.Response, body)
}
func setHTMLResponse(r *fasthttp.Response, html string) {
r.Header.SetContentType("application/json") // for IE - otherwise it want to download content as file
r.SetStatusCode(fasthttp.StatusOK)
r.SetBodyString(html)
}