forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorize.go
87 lines (75 loc) · 2.03 KB
/
authorize.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
package osincli
import (
"errors"
"net/http"
"net/url"
)
type AuthorizeRequestType string
const (
CODE AuthorizeRequestType = "code"
//TOKEN = "token" // token not supported in server applications (uses url fragment)
)
// Authorize request information
type AuthorizeRequest struct {
client *Client
Type AuthorizeRequestType
CustomParameters map[string]string
}
// Authorization data
type AuthorizeData struct {
Code string
State string
}
// Creates a new authorize request
func (c *Client) NewAuthorizeRequest(t AuthorizeRequestType) *AuthorizeRequest {
return &AuthorizeRequest{
client: c,
Type: t,
CustomParameters: make(map[string]string),
}
}
// Returns the authorize url
func (c *AuthorizeRequest) GetAuthorizeUrl() *url.URL {
return c.GetAuthorizeUrlWithParams("")
}
// Returns the authorize url
func (c *AuthorizeRequest) GetAuthorizeUrlWithParams(state string) *url.URL {
u := *c.client.configcache.authorizeUrl
uq := u.Query()
uq.Add("response_type", string(c.Type))
uq.Add("client_id", c.client.config.ClientId)
uq.Add("redirect_uri", c.client.config.RedirectUrl)
if c.client.config.Scope != "" {
uq.Add("scope", c.client.config.Scope)
}
if state != "" {
uq.Add("state", state)
}
if c.CustomParameters != nil {
for pn, pv := range c.CustomParameters {
uq.Add(pn, pv)
}
}
u.RawQuery = uq.Encode()
return &u
}
// Handle the authorization request
func (c *AuthorizeRequest) HandleRequest(r *http.Request) (*AuthorizeData, error) {
r.ParseForm()
var ad *AuthorizeData
if c.Type == CODE {
// detect error parameters
if r.Form.Get("error") != "" {
return nil, NewError(r.Form.Get("error"), r.Form.Get("error_description"), r.Form.Get("error_uri"), r.Form.Get("state"))
} else if r.Form.Get("code") == "" {
return nil, errors.New("Requested parameter not sent")
}
ad = &AuthorizeData{
Code: r.Form.Get("code"),
State: r.Form.Get("state"),
}
} else {
return nil, errors.New("Unsupported response type")
}
return ad, nil
}