forked from cloud-gov/uaa-credentials-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uaa.go
176 lines (145 loc) · 4.61 KB
/
uaa.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
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"net/http"
"net/url"
"code.cloudfoundry.org/lager"
)
type Users struct {
Resources []User
TotalResults int
}
type User struct {
ID string `json:"id,omitempty"`
UserName string `json:"userName,omitempty"`
Password string `json:"password,omitempty"`
Active bool `json:"active,omitempty"`
Emails []Email `json:"emails"`
}
type Clients struct {
Resources []Client
TotalResults int
}
type Client struct {
ID string `json:"client_id,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
Name string `json:"name,omitempty"`
AuthorizedGrantTypes []string `json:"authorized_grant_types,omitempty"`
Scope []string `json:"scope,omitempty"`
RedirectURI []string `json:"redirect_uri,omitempty"`
Active bool `json:"active,omitempty"`
AccessTokenValidity int `json:"access_token_validity,omitempty"`
RefreshTokenValidity int `json:"refresh_token_validity,omitempty"`
}
type Email struct {
Value string `json:"value,omitempty"`
Primary bool `json:"primary"`
}
type AuthClient interface {
CreateClient(client Client) (Client, error)
DeleteClient(clientID string) error
GetUser(userID string) (User, error)
CreateUser(user User) (User, error)
DeleteUser(userID string) error
}
type UAAClient struct {
logger lager.Logger
client *http.Client
endpoint string
zone string
}
func (c *UAAClient) CreateClient(client Client) (Client, error) {
c.logger.Info("uaa-create-client", lager.Data{"clientID": client.Name})
body, _ := encodeBody(client)
req, _ := http.NewRequest("POST", fmt.Sprintf("%s/oauth/clients", c.endpoint), body)
req.Header.Add("X-Identity-Zone-Id", c.zone)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return Client{}, err
}
if resp.StatusCode != 201 {
return Client{}, fmt.Errorf("Expected status 201; got: %d", resp.StatusCode)
}
err = decodeBody(resp, &client)
if err != nil {
return Client{}, err
}
return client, nil
}
func (c *UAAClient) DeleteClient(clientID string) error {
c.logger.Info("uaa-delete-client", lager.Data{"clientID": clientID})
req, _ := http.NewRequest("DELETE", fmt.Sprintf("%s/oauth/clients/%s", c.endpoint, clientID), nil)
req.Header.Add("X-Identity-Zone-Id", c.zone)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("Expected status 200; got: %d", resp.StatusCode)
}
return nil
}
func (c *UAAClient) GetUser(userID string) (User, error) {
c.logger.Info("uaa-get-user", lager.Data{"userID": userID})
u, _ := url.Parse(fmt.Sprintf("%s/Users", c.endpoint))
q := u.Query()
q.Add("filter", fmt.Sprintf(`userName eq "%s"`, userID))
q.Add("count", "1")
u.RawQuery = q.Encode()
req, _ := http.NewRequest("GET", u.String(), nil)
req.Header.Add("X-Identity-Zone-Id", c.zone)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return User{}, err
}
users := Users{}
err = decodeBody(resp, &users)
if err != nil {
return User{}, err
}
if users.TotalResults != 1 {
return User{}, fmt.Errorf("Expected to find exactly one user; got %d", users.TotalResults)
}
return users.Resources[0], nil
}
func (c *UAAClient) CreateUser(user User) (User, error) {
c.logger.Info("uaa-create-user", lager.Data{"userID": user.UserName})
body, _ := encodeBody(user)
req, _ := http.NewRequest("POST", fmt.Sprintf("%s/Users", c.endpoint), body)
req.Header.Add("X-Identity-Zone-Id", c.zone)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return User{}, err
}
if resp.StatusCode != 201 {
return User{}, fmt.Errorf("Expected status 201; got: %d", resp.StatusCode)
}
err = decodeBody(resp, &user)
if err != nil {
return User{}, err
}
return user, nil
}
func (c *UAAClient) DeleteUser(userID string) error {
c.logger.Info("uaa-delete-user", lager.Data{"userID": userID})
req, _ := http.NewRequest("DELETE", fmt.Sprintf("%s/Users/%s", c.endpoint, userID), nil)
req.Header.Add("X-Identity-Zone-Id", c.zone)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("Expected status 200; got: %d", resp.StatusCode)
}
return nil
}