This repository has been archived by the owner on Nov 26, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth_test_client.go
165 lines (125 loc) · 4.12 KB
/
oauth_test_client.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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"github.com/RangelReale/osin"
)
type (
OAuthClient struct{ Id, Secret string }
)
const (
//just so it doesn't look super bad :)
basicCss = "<style type=\"text/css\">body{margin:40px auto;max-width:650px;line-height:1.6;font-size:18px;color:#444;padding:0 10px}h1,h2,h3{line-height:1.2}</style>"
clientUrl = "http://localhost:14000"
tidepoolUrl = "http://localhost:8009"
appauthPath = "/appauth/code"
)
// e.g. go run oauth_test_client.go -client_id=9f265bbf73 -client_secret=6c0efdc2b8e234e8a59c8fda4abb560601bb0387
//
func main() {
log.Print("Setting up Tidepools OAuth2 test client ...")
id := flag.String("client_id", "", "your registered client_id")
secret := flag.String("client_secret", "", "your registered client_secret")
flag.Parse()
if *id != "" && *secret != "" {
client := &OAuthClient{Id: *id, Secret: *secret}
log.Printf("running for client_id=%s client_secret=%s", client.Id, client.Secret)
http.HandleFunc(appauthPath, client.code)
http.HandleFunc("/", client.app)
log.Printf("go to [%s]", clientUrl)
http.ListenAndServe(":14000", nil)
}
log.Fatalln("Sorry but we need your registered apps client_id and client_secret")
}
func (o *OAuthClient) code(w http.ResponseWriter, r *http.Request) {
log.Print("OAuthClient: code")
r.ParseForm()
log.Printf("OAuthClient: all form data %v", r.Form)
code := r.Form.Get("code")
log.Printf("OAuthClient: code from form %s", code)
w.Write([]byte(fmt.Sprintf("<html><head>%s</head><body>", basicCss)))
//w.Write([]byte("APP AUTH - CODE<br/>"))
defer w.Write([]byte("</body></html>"))
if code == "" {
w.Write([]byte("What what - no code so nothing to do"))
return
}
jr := make(map[string]interface{})
// build access code url
aurl := fmt.Sprintf(
"/oauth/token?grant_type=authorization_code&client_id=%s&client_secret=%s&redirect_uri=%s&code=%s",
o.Id,
o.Secret,
url.QueryEscape(clientUrl+appauthPath),
url.QueryEscape(code),
)
log.Printf("OAuthClient: auth url %s", aurl)
// if parse, download and parse json
if r.Form.Get("doparse") == "1" {
err := downloadAccessToken(fmt.Sprintf(tidepoolUrl+"%s", aurl), &osin.BasicAuth{o.Id, o.Secret}, jr)
if err != nil {
w.Write([]byte("Error downloading token: " + err.Error()))
w.Write([]byte("<br/>"))
}
}
log.Printf("OAuthClient: details %v ", jr)
// show json error
if erd, ok := jr["error"]; ok {
w.Write([]byte(fmt.Sprintf("An error occurred: %s<br/>\n", erd)))
}
// show json access token
if at, ok := jr["access_token"]; ok {
w.Write([]byte("<h2>Access Token:</h2>"))
w.Write([]byte(fmt.Sprintf("%v", at)))
}
//show the full result
if jr["refresh_token"] != nil && jr["access_token"] != nil {
w.Write([]byte("<h2>Full Result:</h2>"))
w.Write([]byte(fmt.Sprintf("%v<br/>", jr)))
}
// output links
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Goto Token URL</a><br/>", tidepoolUrl+aurl)))
cururl := *r.URL
curq := cururl.Query()
curq.Add("doparse", "1")
cururl.RawQuery = curq.Encode()
w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Download Token</a><br/>", cururl.String())))
}
func (o *OAuthClient) app(w http.ResponseWriter, r *http.Request) {
log.Print("OAuthClient: app login")
w.Write([]byte(fmt.Sprintf("<html><head>%s</head><body>", basicCss)))
w.Write([]byte(fmt.Sprintf(
"<a href=\"%s/oauth/authorize?response_type=code&client_id=%s&redirect_uri=%s\">Tidepool Login</a><br/>",
tidepoolUrl,
o.Id,
url.QueryEscape(clientUrl+appauthPath),
)))
w.Write([]byte("</body></html>"))
}
func downloadAccessToken(url string, auth *osin.BasicAuth, output map[string]interface{}) error {
// download access token
preq, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
if auth != nil {
preq.SetBasicAuth(auth.Username, auth.Password)
}
pclient := &http.Client{}
presp, err := pclient.Do(preq)
if err != nil {
return err
}
if presp.StatusCode != http.StatusOK {
log.Printf("downloadAccessToken: %b", presp.StatusCode)
return errors.New("Invalid status code")
}
jdec := json.NewDecoder(presp.Body)
err = jdec.Decode(&output)
return err
}