-
Notifications
You must be signed in to change notification settings - Fork 22
/
clients.go
158 lines (136 loc) · 4.81 KB
/
clients.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
package uaa
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
)
// ClientsEndpoint is the path to the clients resource.
const ClientsEndpoint string = "/oauth/clients"
// paginatedClientList is the response from the API for a single page of clients.
type paginatedClientList struct {
Page
Resources []Client `json:"resources"`
Schemas []string `json:"schemas"`
}
// Client is a UAA client
// http://docs.cloudfoundry.org/api/uaa/version/4.19.0/index.html#clients.
type Client struct {
ClientID string `json:"client_id,omitempty" generator:"id"`
AuthorizedGrantTypes []string `json:"authorized_grant_types,omitempty"`
RedirectURI []string `json:"redirect_uri,omitempty"`
Scope []string `json:"scope,omitempty"`
ResourceIDs []string `json:"resource_ids,omitempty"`
Authorities []string `json:"authorities,omitempty"`
AutoApproveRaw interface{} `json:"autoapprove,omitempty"`
AccessTokenValidity int64 `json:"access_token_validity,omitempty"`
RefreshTokenValidity int64 `json:"refresh_token_validity,omitempty"`
AllowedProviders []string `json:"allowedproviders,omitempty"`
DisplayName string `json:"name,omitempty"`
TokenSalt string `json:"token_salt,omitempty"`
CreatedWith string `json:"createdwith,omitempty"`
ApprovalsDeleted bool `json:"approvals_deleted,omitempty"`
RequiredUserGroups []string `json:"required_user_groups,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
LastModified int64 `json:"lastModified,omitempty"`
AllowPublic bool `json:"allowpublic,omitempty"`
}
// Identifier returns the field used to uniquely identify a Client.
func (c Client) Identifier() string {
return c.ClientID
}
func (c Client) AutoApprove() []string {
switch t := c.AutoApproveRaw.(type) {
case bool:
return []string{strconv.FormatBool(t)}
case string:
return []string{t}
case []string:
return t
}
return []string{}
}
// GrantType is a type of oauth2 grant.
type GrantType string
// Valid GrantType values.
const (
REFRESHTOKEN = GrantType("refresh_token")
AUTHCODE = GrantType("authorization_code")
IMPLICIT = GrantType("implicit")
PASSWORD = GrantType("password")
CLIENTCREDENTIALS = GrantType("client_credentials")
)
func errorMissingValueForGrantType(value string, grantType GrantType) error {
return fmt.Errorf("%v must be specified for %v grant type", value, grantType)
}
func errorMissingValue(value string) error {
return fmt.Errorf("%v must be specified in the client definition", value)
}
func requireRedirectURIForGrantType(c *Client, grantType GrantType) error {
if contains(c.AuthorizedGrantTypes, string(grantType)) {
if len(c.RedirectURI) == 0 {
return errorMissingValueForGrantType("redirect_uri", grantType)
}
}
return nil
}
func requireClientSecretForGrantType(c *Client, grantType GrantType) error {
if contains(c.AuthorizedGrantTypes, string(grantType)) {
if c.ClientSecret == "" {
return errorMissingValueForGrantType("client_secret", grantType)
}
}
return nil
}
func knownGrantTypesStr() string {
grantTypeStrings := []string{}
knownGrantTypes := []GrantType{AUTHCODE, IMPLICIT, PASSWORD, CLIENTCREDENTIALS}
for _, grant := range knownGrantTypes {
grantTypeStrings = append(grantTypeStrings, string(grant))
}
return "[" + strings.Join(grantTypeStrings, ", ") + "]"
}
// Validate returns nil if the client is valid, or an error if it is invalid.
func (c *Client) Validate() error {
if len(c.AuthorizedGrantTypes) == 0 {
return fmt.Errorf("grant type must be one of %v", knownGrantTypesStr())
}
if c.ClientID == "" {
return errorMissingValue("client_id")
}
if err := requireRedirectURIForGrantType(c, AUTHCODE); err != nil {
return err
}
if err := requireClientSecretForGrantType(c, AUTHCODE); err != nil {
return err
}
if err := requireClientSecretForGrantType(c, CLIENTCREDENTIALS); err != nil {
return err
}
if err := requireRedirectURIForGrantType(c, IMPLICIT); err != nil {
return err
}
return nil
}
type changeSecretBody struct {
ClientID string `json:"clientId,omitempty"`
ClientSecret string `json:"secret,omitempty"`
}
// ChangeClientSecret updates the secret with the given value for the client
// with the given id
// http://docs.cloudfoundry.org/api/uaa/version/4.14.0/index.html#change-secret.
func (a *API) ChangeClientSecret(id string, newSecret string) error {
u := urlWithPath(*a.TargetURL, fmt.Sprintf("%s/%s/secret", ClientsEndpoint, id))
change := &changeSecretBody{ClientID: id, ClientSecret: newSecret}
j, err := json.Marshal(change)
if err != nil {
return err
}
err = a.doJSON(http.MethodPut, &u, bytes.NewBuffer([]byte(j)), nil, true)
if err != nil {
return err
}
return nil
}