-
Notifications
You must be signed in to change notification settings - Fork 1
/
clients_service.go
165 lines (143 loc) · 5.03 KB
/
clients_service.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 warrant
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/pivotal-cf-experimental/warrant/internal/documents"
"github.com/pivotal-cf-experimental/warrant/internal/network"
)
// TODO: Pagination for List
// TODO: Change Secret
// TODO: Batch Create
// TODO: Batch Update
// TODO: Batch Secret Change
// TODO: Batch Delete
// TODO: Mixed Actions
// ClientsService provides access to the common client actions. Using this service, you can
// create, delete, or fetch a client. You can also fetch a client token.
type ClientsService struct {
config Config
}
// NewClientsService returns a ClientsService initialized with the given Config.
func NewClientsService(config Config) ClientsService {
return ClientsService{
config: config,
}
}
// Create will make a request to UAA to register a client with the given client resource and
// A token with the "clients.write" or "clients.admin" scope is required.
func (cs ClientsService) Create(client Client, secret, token string) error {
_, err := newNetworkClient(cs.config).MakeRequest(network.Request{
Method: "POST",
Path: "/oauth/clients",
Authorization: network.NewTokenAuthorization(token),
Body: network.NewJSONRequestBody(client.toDocument(secret)),
AcceptableStatusCodes: []int{http.StatusCreated},
})
if err != nil {
return translateError(err)
}
return nil
}
// Get will make a request to UAA to fetch the client matching the given id.
// A token with the "clients.read" scope is required.
func (cs ClientsService) Get(id, token string) (Client, error) {
resp, err := newNetworkClient(cs.config).MakeRequest(network.Request{
Method: "GET",
Path: fmt.Sprintf("/oauth/clients/%s", id),
Authorization: network.NewTokenAuthorization(token),
AcceptableStatusCodes: []int{http.StatusOK},
})
if err != nil {
return Client{}, translateError(err)
}
var document documents.ClientResponse
err = json.Unmarshal(resp.Body, &document)
if err != nil {
return Client{}, MalformedResponseError{err}
}
return newClientFromDocument(document), nil
}
// List will make a request to UAA to retrieve all client resources matching the given query.
// A token with the "clients.read" or "clients.admin" scope is required.
func (cs ClientsService) List(query Query, token string) ([]Client, error) {
requestPath := url.URL{
Path: "/oauth/clients",
RawQuery: url.Values{
"filter": []string{query.Filter},
"sortBy": []string{query.SortBy},
}.Encode(),
}
resp, err := newNetworkClient(cs.config).MakeRequest(network.Request{
Method: "GET",
Path: requestPath.String(),
Authorization: network.NewTokenAuthorization(token),
AcceptableStatusCodes: []int{http.StatusOK},
})
if err != nil {
return []Client{}, translateError(err)
}
var document documents.ClientListResponse
err = json.Unmarshal(resp.Body, &document)
if err != nil {
return []Client{}, MalformedResponseError{err}
}
var list []Client
for _, c := range document.Resources {
list = append(list, newClientFromDocument(c))
}
return list, nil
}
// Update will make a request to UAA to update the matching client resource.
// A token with the "clients.write" or "clients.admin" scope is required.
func (cs ClientsService) Update(client Client, token string) error {
_, err := newNetworkClient(cs.config).MakeRequest(network.Request{
Method: "PUT",
Path: fmt.Sprintf("/oauth/clients/%s", client.ID),
Authorization: network.NewTokenAuthorization(token),
Body: network.NewJSONRequestBody(client.toDocument("")),
AcceptableStatusCodes: []int{http.StatusOK},
})
if err != nil {
return translateError(err)
}
return nil
}
// Delete will make a request to UAA to delete the client matching the given id.
// A token with the "clients.write" or "clients.admin" scope is required.
func (cs ClientsService) Delete(id, token string) error {
_, err := newNetworkClient(cs.config).MakeRequest(network.Request{
Method: "DELETE",
Path: fmt.Sprintf("/oauth/clients/%s", id),
Authorization: network.NewTokenAuthorization(token),
AcceptableStatusCodes: []int{http.StatusOK},
})
if err != nil {
return translateError(err)
}
return nil
}
// GetToken will make a request to UAA to retrieve a client token using the
// "client_credentials" grant type. A client id and secret are required.
func (cs ClientsService) GetToken(id, secret string) (string, error) {
resp, err := newNetworkClient(cs.config).MakeRequest(network.Request{
Method: "POST",
Path: "/oauth/token",
Authorization: network.NewBasicAuthorization(id, secret),
Body: network.NewFormRequestBody(url.Values{
"client_id": []string{id},
"grant_type": []string{"client_credentials"},
}),
AcceptableStatusCodes: []int{http.StatusOK},
})
if err != nil {
return "", translateError(err)
}
var response documents.TokenResponse
err = json.Unmarshal(resp.Body, &response)
if err != nil {
return "", MalformedResponseError{err}
}
return response.AccessToken, nil
}