/
handler_app_callback.go
83 lines (75 loc) · 2.49 KB
/
handler_app_callback.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
package pkg
import (
"fmt"
"net/http"
"net/url"
"github.com/parnurzeal/gorequest"
"golang.org/x/oauth2"
)
func CallbackHandler(c oauth2.Config) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(`<h1>Callback site</h1><a href="/">Go back</a>`))
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
if req.URL.Query().Get("error") != "" {
rw.Write([]byte(fmt.Sprintf(`<h1>Error!</h1>
Error: %s<br>
Description: %s<br>
<br>`,
req.URL.Query().Get("error"),
req.URL.Query().Get("error_description"),
)))
return
}
if req.URL.Query().Get("refresh") != "" {
_, body, errs := gorequest.New().Post(c.Endpoint.TokenURL).SetBasicAuth(c.ClientID, c.ClientSecret).SendString(url.Values{
"grant_type": {"refresh_token"},
"refresh_token": {req.URL.Query().Get("refresh")},
"scope": {"fosite"},
}.Encode()).End()
if len(errs) > 0 {
rw.Write([]byte(fmt.Sprintf(`<p>Could not refresh token %s</p>`, errs)))
return
}
rw.Write([]byte(fmt.Sprintf(`<p>Got a response from the refresh grant:<br><code>%s</code></p>`, body)))
return
}
if req.URL.Query().Get("code") == "" {
rw.Write([]byte(fmt.Sprintln(`<p>Could not find the authorize code. If you've used the implicit grant, check the
browser location bar for the
access token <small><a href="http://en.wikipedia.org/wiki/Fragment_identifier#Basics">(the server side does not have access to url fragments)</a></small>
</p>`,
)))
return
}
rw.Write([]byte(fmt.Sprintf(`<p>Amazing! You just got an authorize code!:<br><code>%s</code></p>
<p>Click <a href="/">here to return</a> to the front page</p>`,
req.URL.Query().Get("code"),
)))
token, err := c.Exchange(oauth2.NoContext, req.URL.Query().Get("code"))
if err != nil {
rw.Write([]byte(fmt.Sprintf(`<p>I tried to exchange the authorize code for an access token but it did not work but got error: %s</p>`, err.Error())))
return
}
rw.Write([]byte(fmt.Sprintf(`<p>Cool! You are now a proud token owner.<br>
<ul>
<li>
Access token (click to make <a href="%s">authorized call</a>):<br>
<code>%s</code>
</li>
<li>
Refresh token (click <a href="%s">here to use it</a>):<br>
<code>%s</code>
</li>
<li>
Extra info: <br>
<code>%s</code>
</li>
</ul>`,
"/protected-api?token="+token.AccessToken,
token.AccessToken,
"?refresh="+url.QueryEscape(token.RefreshToken),
token.RefreshToken,
token,
)))
}
}