-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
87 lines (73 loc) · 3.11 KB
/
client.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 warrant
import (
"time"
"github.com/pivotal-cf-experimental/warrant/internal/documents"
)
// Client is the representation of a client resource within UAA.
type Client struct {
// ID is the unique identifier for the client resource.
ID string
Name string
// Scope contains a list of scope values describing the level of permissions for a
// user token requested by this client.
Scope []string
// Authorities is a list of scope values describing the level of permissions granted
// to this client in a token requested with the "client_credentials" grant type.
Authorities []string
// ResourceIDs is a white list of resource identifiers to be included in the decoded
// tokens granted to this client. The UAA does not store any data here (it should be
// "none" for all clients), but instead creates a list of resource identifiers
// dynamically from the scope values when a token is granted.
ResourceIDs []string
// AuthorizedGrantTypes is a list of OAuth2 grant types, as defined in the spec.
// Valid fields are:
// - client_credentials
// - password
// - implicit
// - refresh_token
// - authorization_code
AuthorizedGrantTypes []string
// AccessTokenValidity is the number of seconds before a token granted to this client
// will expire.
AccessTokenValidity time.Duration
// RedirectURI is the location address to redirect the resource owner's user-agent
// back to after completing its interaction with the resource owner.
RedirectURI []string
// Autoapprove is a list of scopes to automatically approve when making an implicit
// grant for a user token.
Autoapprove []string
}
func newClientFromDocument(document documents.ClientResponse) Client {
return Client{
ID: document.ClientID,
Name: document.Name,
Scope: sort(document.Scope),
ResourceIDs: sort(document.ResourceIDs),
Authorities: sort(document.Authorities),
AuthorizedGrantTypes: sort(document.AuthorizedGrantTypes),
Autoapprove: sort(document.Autoapprove),
AccessTokenValidity: time.Duration(document.AccessTokenValidity) * time.Second,
RedirectURI: document.RedirectURI,
}
}
func (c Client) toDocument(secret string) documents.CreateUpdateClientRequest {
client := documents.CreateUpdateClientRequest{
ClientID: c.ID,
ClientSecret: secret,
Name: c.Name,
AccessTokenValidity: int(c.AccessTokenValidity.Seconds()),
Scope: make([]string, 0),
ResourceIDs: make([]string, 0),
Authorities: make([]string, 0),
AuthorizedGrantTypes: make([]string, 0),
RedirectURI: make([]string, 0),
Autoapprove: make([]string, 0),
}
client.Scope = append(client.Scope, c.Scope...)
client.ResourceIDs = append(client.ResourceIDs, c.ResourceIDs...)
client.Authorities = append(client.Authorities, c.Authorities...)
client.AuthorizedGrantTypes = append(client.AuthorizedGrantTypes, c.AuthorizedGrantTypes...)
client.RedirectURI = append(client.RedirectURI, c.RedirectURI...)
client.Autoapprove = append(client.Autoapprove, c.Autoapprove...)
return client
}