forked from openshift/origin
/
helper.go
58 lines (46 loc) · 1.43 KB
/
helper.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
package example
import (
"encoding/json"
"errors"
"fmt"
"github.com/RangelReale/osin"
"net/http"
"net/url"
)
func HandleLoginPage(ar *osin.AuthorizeRequest, w http.ResponseWriter, r *http.Request) bool {
r.ParseForm()
if r.Method == "POST" && r.Form.Get("login") == "test" && r.Form.Get("password") == "test" {
return true
}
w.Write([]byte("<html><body>"))
w.Write([]byte(fmt.Sprintf("LOGIN %s (use test/test)<br/>", ar.Client.GetId())))
w.Write([]byte(fmt.Sprintf("<form action=\"/authorize?response_type=%s&client_id=%s&state=%s&redirect_uri=%s\" method=\"POST\">",
ar.Type, ar.Client.GetId(), ar.State, url.QueryEscape(ar.RedirectUri))))
w.Write([]byte("Login: <input type=\"text\" name=\"login\" /><br/>"))
w.Write([]byte("Password: <input type=\"password\" name=\"password\" /><br/>"))
w.Write([]byte("<input type=\"submit\"/>"))
w.Write([]byte("</form>"))
w.Write([]byte("</body></html>"))
return false
}
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 != 200 {
return errors.New("Invalid status code")
}
jdec := json.NewDecoder(presp.Body)
err = jdec.Decode(&output)
return err
}