-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
165 lines (132 loc) · 4.26 KB
/
Copy pathclient.go
File metadata and controls
165 lines (132 loc) · 4.26 KB
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 sql provides reference implementations of the authkit model
// interfaces backed by a SQL-friendly struct layout. These types are intended
// as a starting point; copy and adapt them to match your own database schema.
package sql
import (
"crypto/subtle"
"time"
"github.com/alkeyio/authkit/models"
"github.com/alkeyio/authkit/types"
)
// Compile-time check that *Client implements models.Client.
var _ models.Client = (*Client)(nil)
type Client struct {
ClientName string `json:"client_name"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURIs []string `json:"redirect_uris"`
ResponseTypes []string `json:"response_types"`
GrantTypes []string `json:"grant_types"`
Scopes []string `json:"scopes"`
TokenEndpointAuthMethod string `json:"token_endpoint_auth_method"`
ClientURI string `json:"client_uri"`
LogoURI string `json:"logo_uri"`
Contacts []string `json:"contacts"`
TosURI string `json:"tos_uri"`
PolicyURI string `json:"policy_uri"`
JWKsURI string `json:"jwks_uri"`
SoftwareID string `json:"software_id"`
SoftwareVersion string `json:"software_version"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func (c *Client) GetClientName() string {
return c.ClientName
}
func (c *Client) SetClientName(name string) {
c.ClientName = name
}
func (c *Client) GetClientID() string {
return c.ClientID
}
func (c *Client) SetClientID(id string) {
c.ClientID = id
}
func (c *Client) GetClientSecret() string {
return c.ClientSecret
}
func (c *Client) SetClientSecret(secret string) {
c.ClientSecret = secret
}
func (c *Client) GetRedirectURIs() []string {
return c.RedirectURIs
}
func (c *Client) GetResponseTypes() types.ResponseTypes {
return types.NewResponseTypes(c.ResponseTypes)
}
func (c *Client) SetResponseTypes(resTypes types.ResponseTypes) {
c.ResponseTypes = resTypes.String()
}
func (c *Client) GetGrantTypes() types.GrantTypes {
return types.NewGrantTypes(c.GrantTypes)
}
func (c *Client) SetGrantTypes(gTypes types.GrantTypes) {
c.GrantTypes = gTypes.String()
}
func (c *Client) GetScopes() types.Scopes {
return types.NewScopes(c.Scopes)
}
func (c *Client) SetScopes(scopes types.Scopes) {
c.Scopes = scopes.String()
}
func (c *Client) GetAllowedScopes(scopes types.Scopes) types.Scopes {
allowed := make(map[string]bool)
for _, s := range c.Scopes {
allowed[s] = true
}
ret := make(types.Scopes, 0)
for _, s := range scopes {
if _, ok := allowed[s.String()]; ok {
ret = append(ret, s)
}
}
return ret
}
func (c *Client) GetTokenEndpointAuthMethod() types.ClientAuthMethod {
return types.NewClientAuthMethod(c.TokenEndpointAuthMethod)
}
func (c *Client) SetTokenEndpointAuthMethod(m types.ClientAuthMethod) {
c.TokenEndpointAuthMethod = m.String()
}
func (c *Client) GetDefaultRedirectURI() string {
if len(c.RedirectURIs) == 0 {
return ""
}
return c.RedirectURIs[0]
}
func (c *Client) CheckRedirectURI(redirectURI string) bool {
for i := range c.RedirectURIs {
if c.RedirectURIs[i] == redirectURI {
return true
}
}
return false
}
func (c *Client) CheckGrantType(gt types.GrantType) bool {
for i := range c.GrantTypes {
if c.GrantTypes[i] == gt.String() {
return true
}
}
return false
}
func (c *Client) CheckResponseType(rt types.ResponseType) bool {
for i := range c.ResponseTypes {
if c.ResponseTypes[i] == rt.String() {
return true
}
}
return false
}
// CheckTokenEndpointAuthMethod reports whether method matches the client's
// configured auth method. The endpoint parameter is intentionally ignored:
// this implementation uses a single auth method for all endpoints.
func (c *Client) CheckTokenEndpointAuthMethod(method types.ClientAuthMethod, endpoint string) bool {
return c.TokenEndpointAuthMethod == method.String()
}
func (c *Client) CheckClientSecret(secret string) bool {
return subtle.ConstantTimeCompare([]byte(c.ClientSecret), []byte(secret)) == 1
}
func (c *Client) IsPublic() bool {
return c.TokenEndpointAuthMethod == string(types.ClientNoneAuthentication)
}