-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_data.go
47 lines (37 loc) · 1.54 KB
/
client_data.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
package niso
import "context"
// ClientData is the information stored for an OAuth2 client
type ClientData struct {
ClientID string // Unique identifier for this client (https://tools.ietf.org/html/rfc6749#section-2.2)
ClientSecret string // OAuth2 client secret (https://tools.ietf.org/html/rfc6749#section-2.3.1)
RedirectURI string // OAuth2 redirect URI
}
// ValidSecret checks if the given secret is valid for this OAuth2 client
func (c *ClientData) ValidSecret(secret string) bool {
// Consider doing constant time equality check
return secret == c.ClientSecret
}
// getClientDataAndValidate looks up and authenticates the basic auth using the given storage.
func getClientDataAndValidate(ctx context.Context, auth *BasicAuth, storage Storage) (*ClientData, error) {
clientData, err := getClientData(ctx, auth.Username, storage)
if err != nil {
return nil, err
}
if !clientData.ValidSecret(auth.Password) {
return nil, NewError(EUnauthorizedClient, "invalid secret for client")
}
return clientData, nil
}
func getClientData(ctx context.Context, clientID string, storage Storage) (*ClientData, error) {
clientData, err := storage.GetClientData(ctx, clientID)
if err != nil {
if _, ok := err.(*NotFoundError); ok {
return nil, NewWrappedError(EUnauthorizedClient, err, "could not find client")
}
return nil, NewWrappedError(EServerError, err, "failed to get client data from storage")
}
if clientData.RedirectURI == "" {
return nil, NewError(EServerError, "client does not have a valid redirect uri set")
}
return clientData, nil
}