forked from openshift/osin
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclient.go
More file actions
120 lines (99 loc) · 2.98 KB
/
client.go
File metadata and controls
120 lines (99 loc) · 2.98 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
package osin
import (
"github.com/AccelByte/go-jose/jwt"
"strings"
)
// Client information
type Client interface {
// Client id
GetID() string
// Client secret
GetSecret() string
// Base client uri
GetRedirectURI() string
// Data to be passed to storage. Not used by the library.
GetUserData() interface{}
}
// ClientSecretMatcher is an optional interface clients can implement
// which allows them to be the one to determine if a secret matches.
// If a Client implements ClientSecretMatcher, the framework will never call GetSecret
type ClientSecretMatcher interface {
// SecretMatches returns true if the given secret matches
ClientSecretMatches(secret string) bool
}
type ClientIDMatcher interface {
// ClientIDMatches returns true if the given ID matches
ClientIDMatches(id string) bool
}
// DefaultClient stores all data in struct variables
type DefaultClient struct {
Id string
Secret string
RedirectUri string
UserData interface{}
}
func (d *DefaultClient) GetID() string {
return d.Id
}
func (d *DefaultClient) GetSecret() string {
return d.Secret
}
func (d *DefaultClient) GetRedirectURI() string {
return d.RedirectUri
}
func (d *DefaultClient) GetUserData() interface{} {
return d.UserData
}
// ClientSecretMatches implement the ClientSecretMatcher interface
func (d *DefaultClient) ClientSecretMatches(secret string) bool {
return d.Secret == secret
}
// ComboClient implements osin.Client interface
// This type of client is intended to handle multiple audience
// in the token
type ComboClient struct {
Audience jwt.Audience
Clients []Client
}
// GetID satisfies osin.Client interface.
func (client *ComboClient) GetID() string {
return strings.Join(client.Audience, ",")
}
// GetSecret satisfies osin.Client interface
func (client *ComboClient) GetSecret() string {
secrets := make([]string, 0, len(client.Clients))
for _, c := range client.Clients {
secrets = append(secrets, c.GetSecret())
}
return strings.Join(secrets, ",")
}
// GetRedirectURI satisfies osin.Client interface
func (client *ComboClient) GetRedirectURI() string {
uris := make([]string, 0, len(client.Clients))
for _, c := range client.Clients {
uris = append(uris, c.GetRedirectURI())
}
return strings.Join(uris, ",")
}
// GetUserData satisfies osin.Client interface
func (client *ComboClient) GetUserData() interface{} {
data := make([]interface{}, 0, len(client.Clients))
for _, c := range client.Clients {
data = append(data, c.GetUserData())
}
return data
}
// ClientSecretMatches satisfies the ClientSecretMatcher interface
func (client *ComboClient) ClientSecretMatches(secret string) bool {
return client.GetSecret() == secret
}
// ClientIDMatches satisfies the ClientIDMatcher interface
func (client *ComboClient) ClientIDMatches(id string) bool {
return client.Audience.Contains(id)
}
func (d *DefaultClient) CopyFrom(client Client) {
d.Id = client.GetID()
d.Secret = client.GetSecret()
d.RedirectUri = client.GetRedirectURI()
d.UserData = client.GetUserData()
}