2015-08-18 05:57:27 +05:30
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2016-05-12 22:23:01 +05:30
|
|
|
"encoding/base64"
|
2016-04-15 04:27:53 +05:30
|
|
|
"encoding/json"
|
2015-08-18 05:57:27 +05:30
|
|
|
"errors"
|
2016-04-15 04:27:53 +05:30
|
|
|
"io"
|
2015-08-18 05:57:27 +05:30
|
|
|
"net/url"
|
|
|
|
"reflect"
|
2016-06-18 04:05:03 +05:30
|
|
|
"strings"
|
2015-08-18 05:57:27 +05:30
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
2016-05-12 03:05:24 +05:30
|
|
|
"github.com/coreos/dex/repo"
|
2015-08-18 05:57:27 +05:30
|
|
|
"github.com/coreos/go-oidc/oidc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-06-22 00:33:47 +05:30
|
|
|
ErrorInvalidClientID = errors.New("not a valid client ID")
|
|
|
|
|
|
|
|
ErrorInvalidClientSecret = errors.New("not a valid client Secret")
|
|
|
|
|
2016-06-29 04:39:20 +05:30
|
|
|
ErrorDuplicateClientID = errors.New("client ID already exists")
|
|
|
|
|
2016-06-16 22:27:44 +05:30
|
|
|
ErrorInvalidRedirectURL = errors.New("not a valid redirect url for the given client")
|
|
|
|
ErrorCantChooseRedirectURL = errors.New("must provide a redirect url; client has many")
|
|
|
|
ErrorNoValidRedirectURLs = errors.New("no valid redirect URLs for this client.")
|
|
|
|
|
|
|
|
ErrorPublicClientRedirectURIs = errors.New("public clients cannot have redirect URIs")
|
|
|
|
ErrorPublicClientMissingName = errors.New("public clients must have a name")
|
2016-06-16 04:09:04 +05:30
|
|
|
|
|
|
|
ErrorMissingRedirectURI = errors.New("no client redirect url given")
|
|
|
|
|
|
|
|
ErrorNotFound = errors.New("no data found")
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
2016-06-16 04:09:04 +05:30
|
|
|
type ValidationError struct {
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v ValidationError) Error() string {
|
|
|
|
return v.Err.Error()
|
|
|
|
}
|
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
const (
|
|
|
|
bcryptHashCost = 10
|
2016-06-18 04:05:03 +05:30
|
|
|
|
|
|
|
OOBRedirectURI = "urn:ietf:wg:oauth:2.0:oob"
|
2016-05-12 22:23:01 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func HashSecret(creds oidc.ClientCredentials) ([]byte, error) {
|
|
|
|
secretBytes, err := base64.URLEncoding.DecodeString(creds.Secret)
|
|
|
|
if err != nil {
|
2016-06-22 00:33:47 +05:30
|
|
|
return nil, ErrorInvalidClientSecret
|
2016-05-12 22:23:01 +05:30
|
|
|
}
|
|
|
|
hashed, err := bcrypt.GenerateFromPassword([]byte(
|
|
|
|
secretBytes),
|
|
|
|
bcryptHashCost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return hashed, nil
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:27:53 +05:30
|
|
|
type Client struct {
|
|
|
|
Credentials oidc.ClientCredentials
|
|
|
|
Metadata oidc.ClientMetadata
|
|
|
|
Admin bool
|
2016-06-16 04:09:04 +05:30
|
|
|
Public bool
|
2016-04-15 04:27:53 +05:30
|
|
|
}
|
|
|
|
|
2016-06-18 04:05:03 +05:30
|
|
|
func (c Client) ValidRedirectURL(u *url.URL) (url.URL, error) {
|
|
|
|
if c.Public {
|
|
|
|
if u == nil {
|
|
|
|
return url.URL{}, ErrorInvalidRedirectURL
|
|
|
|
}
|
|
|
|
if u.String() == OOBRedirectURI {
|
|
|
|
return *u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Scheme != "http" {
|
|
|
|
return url.URL{}, ErrorInvalidRedirectURL
|
|
|
|
}
|
|
|
|
|
|
|
|
hostPort := strings.Split(u.Host, ":")
|
|
|
|
if len(hostPort) != 2 {
|
|
|
|
return url.URL{}, ErrorInvalidRedirectURL
|
|
|
|
}
|
|
|
|
|
|
|
|
if hostPort[0] != "localhost" || u.Path != "" || u.RawPath != "" || u.RawQuery != "" || u.Fragment != "" {
|
|
|
|
return url.URL{}, ErrorInvalidRedirectURL
|
|
|
|
}
|
|
|
|
|
|
|
|
return *u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidRedirectURL(u, c.Metadata.RedirectURIs)
|
|
|
|
}
|
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
type ClientRepo interface {
|
2016-05-12 03:05:24 +05:30
|
|
|
Get(tx repo.Transaction, clientID string) (Client, error)
|
2016-04-15 04:27:53 +05:30
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
// GetSecret returns the (base64 encoded) hashed client secret
|
|
|
|
GetSecret(tx repo.Transaction, clientID string) ([]byte, error)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
// All returns all registered Clients
|
2016-05-12 03:05:24 +05:30
|
|
|
All(tx repo.Transaction) ([]Client, error)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
2016-04-15 04:57:57 +05:30
|
|
|
// New registers a Client with the repo.
|
2015-08-18 05:57:27 +05:30
|
|
|
// An unused ID must be provided. A corresponding secret will be returned
|
|
|
|
// in a ClientCredentials struct along with the provided ID.
|
2016-05-12 03:05:24 +05:30
|
|
|
New(tx repo.Transaction, client Client) (*oidc.ClientCredentials, error)
|
2015-08-18 05:57:27 +05:30
|
|
|
|
2016-05-12 22:23:01 +05:30
|
|
|
Update(tx repo.Transaction, client Client) error
|
2016-04-23 02:39:28 +05:30
|
|
|
|
|
|
|
// GetTrustedPeers returns the list of clients authorized to mint ID token for the given client.
|
|
|
|
GetTrustedPeers(tx repo.Transaction, clientID string) ([]string, error)
|
|
|
|
|
|
|
|
// SetTrustedPeers sets the list of clients authorized to mint ID token for the given client.
|
|
|
|
SetTrustedPeers(tx repo.Transaction, clientID string, clientIDs []string) error
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ValidRedirectURL returns the passed in URL if it is present in the redirectURLs list, and returns an error otherwise.
|
|
|
|
// If nil is passed in as the rURL and there is only one URL in redirectURLs,
|
|
|
|
// that URL will be returned. If nil is passed but theres >1 URL in the slice,
|
|
|
|
// then an error is returned.
|
|
|
|
func ValidRedirectURL(rURL *url.URL, redirectURLs []url.URL) (url.URL, error) {
|
|
|
|
if len(redirectURLs) == 0 {
|
|
|
|
return url.URL{}, ErrorNoValidRedirectURLs
|
|
|
|
}
|
|
|
|
|
|
|
|
if rURL == nil {
|
|
|
|
if len(redirectURLs) > 1 {
|
|
|
|
return url.URL{}, ErrorCantChooseRedirectURL
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirectURLs[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ru := range redirectURLs {
|
|
|
|
if reflect.DeepEqual(ru, *rURL) {
|
|
|
|
return ru, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return url.URL{}, ErrorInvalidRedirectURL
|
|
|
|
}
|
2016-04-15 04:27:53 +05:30
|
|
|
|
2016-06-09 00:01:50 +05:30
|
|
|
// LoadableClient contains sufficient information for creating a Client and its related entities.
|
|
|
|
type LoadableClient struct {
|
|
|
|
Client Client
|
|
|
|
TrustedPeers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClientsFromReader(r io.Reader) ([]LoadableClient, error) {
|
2016-04-15 04:27:53 +05:30
|
|
|
var c []struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Secret string `json:"secret"`
|
|
|
|
RedirectURLs []string `json:"redirectURLs"`
|
2016-06-09 00:01:50 +05:30
|
|
|
Admin bool `json:"admin"`
|
2016-06-16 04:09:04 +05:30
|
|
|
Public bool `json:"public"`
|
2016-06-09 00:01:50 +05:30
|
|
|
TrustedPeers []string `json:"trustedPeers"`
|
2016-04-15 04:27:53 +05:30
|
|
|
}
|
|
|
|
if err := json.NewDecoder(r).Decode(&c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-09 00:01:50 +05:30
|
|
|
clients := make([]LoadableClient, len(c))
|
2016-04-15 04:27:53 +05:30
|
|
|
for i, client := range c {
|
2016-04-15 23:53:48 +05:30
|
|
|
if client.ID == "" {
|
|
|
|
return nil, errors.New("clients must have an ID")
|
|
|
|
}
|
|
|
|
if len(client.Secret) == 0 {
|
|
|
|
return nil, errors.New("clients must have a Secret")
|
|
|
|
}
|
2016-04-15 04:27:53 +05:30
|
|
|
redirectURIs := make([]url.URL, len(client.RedirectURLs))
|
|
|
|
for j, u := range client.RedirectURLs {
|
|
|
|
uri, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
redirectURIs[j] = *uri
|
|
|
|
}
|
|
|
|
|
2016-06-09 00:01:50 +05:30
|
|
|
clients[i] = LoadableClient{
|
|
|
|
Client: Client{
|
|
|
|
Credentials: oidc.ClientCredentials{
|
|
|
|
ID: client.ID,
|
|
|
|
Secret: client.Secret,
|
|
|
|
},
|
|
|
|
Metadata: oidc.ClientMetadata{
|
|
|
|
RedirectURIs: redirectURIs,
|
|
|
|
},
|
2016-06-16 04:09:04 +05:30
|
|
|
Admin: client.Admin,
|
|
|
|
Public: client.Public,
|
2016-04-15 04:27:53 +05:30
|
|
|
},
|
2016-06-09 00:01:50 +05:30
|
|
|
TrustedPeers: client.TrustedPeers,
|
2016-04-15 04:27:53 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return clients, nil
|
|
|
|
}
|