2016-07-26 01:30:28 +05:30
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base32"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2020-01-27 21:05:37 +05:30
|
|
|
"math/big"
|
2016-07-26 01:30:28 +05:30
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
jose "gopkg.in/square/go-jose.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-10-06 04:33:29 +05:30
|
|
|
// ErrNotFound is the error returned by storages if a resource cannot be found.
|
|
|
|
ErrNotFound = errors.New("not found")
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-10-06 04:33:29 +05:30
|
|
|
// ErrAlreadyExists is the error returned by storages if a resource ID is taken during a create.
|
|
|
|
ErrAlreadyExists = errors.New("ID already exists")
|
|
|
|
)
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
// Kubernetes only allows lower case letters for names.
|
|
|
|
//
|
|
|
|
// TODO(ericchiang): refactor ID creation onto the storage.
|
|
|
|
var encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567")
|
|
|
|
|
2020-10-18 03:24:27 +05:30
|
|
|
// Valid characters for user codes
|
2020-01-27 21:05:37 +05:30
|
|
|
const validUserCharacters = "BCDFGHJKLMNPQRSTVWXZ"
|
|
|
|
|
2020-01-16 21:25:07 +05:30
|
|
|
// NewDeviceCode returns a 32 char alphanumeric cryptographically secure string
|
|
|
|
func NewDeviceCode() string {
|
|
|
|
return newSecureID(32)
|
|
|
|
}
|
|
|
|
|
2016-08-03 10:27:36 +05:30
|
|
|
// NewID returns a random string which can be used as an ID for objects.
|
|
|
|
func NewID() string {
|
2020-01-16 21:25:07 +05:30
|
|
|
return newSecureID(16)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSecureID(len int) string {
|
2020-06-03 00:09:30 +05:30
|
|
|
buff := make([]byte, len) // random ID.
|
2016-07-26 01:30:28 +05:30
|
|
|
if _, err := io.ReadFull(rand.Reader, buff); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-07-06 17:58:48 +05:30
|
|
|
// Avoid the identifier to begin with number and trim padding
|
|
|
|
return string(buff[0]%26+'a') + strings.TrimRight(encoding.EncodeToString(buff[1:]), "=")
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-10-13 07:16:10 +05:30
|
|
|
// GCResult returns the number of objects deleted by garbage collection.
|
|
|
|
type GCResult struct {
|
2020-01-16 21:25:07 +05:30
|
|
|
AuthRequests int64
|
|
|
|
AuthCodes int64
|
|
|
|
DeviceRequests int64
|
|
|
|
DeviceTokens int64
|
2016-10-13 07:16:10 +05:30
|
|
|
}
|
|
|
|
|
2021-01-11 14:03:10 +05:30
|
|
|
// IsEmpty returns whether the garbage collection result is empty or not.
|
|
|
|
func (g *GCResult) IsEmpty() bool {
|
|
|
|
return g.AuthRequests == 0 &&
|
|
|
|
g.AuthCodes == 0 &&
|
|
|
|
g.DeviceRequests == 0 &&
|
|
|
|
g.DeviceTokens == 0
|
|
|
|
}
|
|
|
|
|
2016-12-17 00:33:36 +05:30
|
|
|
// Storage is the storage interface used by the server. Implementations are
|
|
|
|
// required to be able to perform atomic compare-and-swap updates and either
|
|
|
|
// support timezones or standardize on UTC.
|
2016-07-26 01:30:28 +05:30
|
|
|
type Storage interface {
|
|
|
|
Close() error
|
|
|
|
|
2016-08-01 11:55:06 +05:30
|
|
|
// TODO(ericchiang): Let the storages set the IDs of these objects.
|
2016-07-26 01:30:28 +05:30
|
|
|
CreateAuthRequest(a AuthRequest) error
|
|
|
|
CreateClient(c Client) error
|
|
|
|
CreateAuthCode(c AuthCode) error
|
2016-08-03 10:27:36 +05:30
|
|
|
CreateRefresh(r RefreshToken) error
|
2016-10-06 04:33:29 +05:30
|
|
|
CreatePassword(p Password) error
|
2017-02-01 05:41:59 +05:30
|
|
|
CreateOfflineSessions(s OfflineSessions) error
|
2017-03-23 22:29:33 +05:30
|
|
|
CreateConnector(c Connector) error
|
2020-01-16 21:25:07 +05:30
|
|
|
CreateDeviceRequest(d DeviceRequest) error
|
|
|
|
CreateDeviceToken(d DeviceToken) error
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
// TODO(ericchiang): return (T, bool, error) so we can indicate not found
|
2016-08-01 11:55:06 +05:30
|
|
|
// requests that way instead of using ErrNotFound.
|
2016-07-26 01:30:28 +05:30
|
|
|
GetAuthRequest(id string) (AuthRequest, error)
|
|
|
|
GetAuthCode(id string) (AuthCode, error)
|
|
|
|
GetClient(id string) (Client, error)
|
|
|
|
GetKeys() (Keys, error)
|
2016-08-03 10:27:36 +05:30
|
|
|
GetRefresh(id string) (RefreshToken, error)
|
2016-10-06 04:33:29 +05:30
|
|
|
GetPassword(email string) (Password, error)
|
2017-02-01 05:41:59 +05:30
|
|
|
GetOfflineSessions(userID string, connID string) (OfflineSessions, error)
|
2017-03-23 22:29:33 +05:30
|
|
|
GetConnector(id string) (Connector, error)
|
2020-01-29 00:44:30 +05:30
|
|
|
GetDeviceRequest(userCode string) (DeviceRequest, error)
|
2020-01-27 21:05:37 +05:30
|
|
|
GetDeviceToken(deviceCode string) (DeviceToken, error)
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
ListClients() ([]Client, error)
|
2016-08-03 10:27:36 +05:30
|
|
|
ListRefreshTokens() ([]RefreshToken, error)
|
2016-11-17 04:27:27 +05:30
|
|
|
ListPasswords() ([]Password, error)
|
2017-03-23 22:29:33 +05:30
|
|
|
ListConnectors() ([]Connector, error)
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
// Delete methods MUST be atomic.
|
|
|
|
DeleteAuthRequest(id string) error
|
|
|
|
DeleteAuthCode(code string) error
|
|
|
|
DeleteClient(id string) error
|
|
|
|
DeleteRefresh(id string) error
|
2016-10-06 04:33:29 +05:30
|
|
|
DeletePassword(email string) error
|
2017-02-01 05:41:59 +05:30
|
|
|
DeleteOfflineSessions(userID string, connID string) error
|
2017-03-23 22:29:33 +05:30
|
|
|
DeleteConnector(id string) error
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-10-29 01:30:13 +05:30
|
|
|
// Update methods take a function for updating an object then performs that update within
|
|
|
|
// a transaction. "updater" functions may be called multiple times by a single update call.
|
|
|
|
//
|
|
|
|
// Because new fields may be added to resources, updaters should only modify existing
|
|
|
|
// fields on the old object rather then creating new structs. For example:
|
|
|
|
//
|
|
|
|
// updater := func(old storage.Client) (storage.Client, error) {
|
|
|
|
// old.Secret = newSecret
|
|
|
|
// return old, nil
|
|
|
|
// }
|
|
|
|
// if err := s.UpdateClient(clientID, updater); err != nil {
|
|
|
|
// // update failed, handle error
|
|
|
|
// }
|
2016-09-15 05:08:12 +05:30
|
|
|
//
|
2016-07-26 01:30:28 +05:30
|
|
|
UpdateClient(id string, updater func(old Client) (Client, error)) error
|
|
|
|
UpdateKeys(updater func(old Keys) (Keys, error)) error
|
|
|
|
UpdateAuthRequest(id string, updater func(a AuthRequest) (AuthRequest, error)) error
|
2016-12-23 05:26:09 +05:30
|
|
|
UpdateRefreshToken(id string, updater func(r RefreshToken) (RefreshToken, error)) error
|
2016-10-06 04:33:29 +05:30
|
|
|
UpdatePassword(email string, updater func(p Password) (Password, error)) error
|
2017-02-01 05:41:59 +05:30
|
|
|
UpdateOfflineSessions(userID string, connID string, updater func(s OfflineSessions) (OfflineSessions, error)) error
|
2017-03-23 22:29:33 +05:30
|
|
|
UpdateConnector(id string, updater func(c Connector) (Connector, error)) error
|
2020-01-29 00:44:30 +05:30
|
|
|
UpdateDeviceToken(deviceCode string, updater func(t DeviceToken) (DeviceToken, error)) error
|
2016-09-15 05:08:12 +05:30
|
|
|
|
2020-06-03 00:09:30 +05:30
|
|
|
// GarbageCollect deletes all expired AuthCodes,
|
|
|
|
// AuthRequests, DeviceRequests, and DeviceTokens.
|
2016-10-13 07:16:10 +05:30
|
|
|
GarbageCollect(now time.Time) (GCResult, error)
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// Client represents an OAuth2 client.
|
2016-07-26 01:30:28 +05:30
|
|
|
//
|
|
|
|
// For further reading see:
|
|
|
|
// * Trusted peers: https://developers.google.com/identity/protocols/CrossClientAuth
|
|
|
|
// * Public clients: https://developers.google.com/api-client-library/python/auth/installed-app
|
|
|
|
type Client struct {
|
2016-09-15 05:08:12 +05:30
|
|
|
// Client ID and secret used to identify the client.
|
2019-04-18 17:25:08 +05:30
|
|
|
ID string `json:"id" yaml:"id"`
|
|
|
|
IDEnv string `json:"idEnv" yaml:"idEnv"`
|
|
|
|
Secret string `json:"secret" yaml:"secret"`
|
|
|
|
SecretEnv string `json:"secretEnv" yaml:"secretEnv"`
|
2016-09-15 05:08:12 +05:30
|
|
|
|
|
|
|
// A registered set of redirect URIs. When redirecting from dex to the client, the URI
|
|
|
|
// requested to redirect to MUST match one of these values, unless the client is "public".
|
2016-08-05 22:20:22 +05:30
|
|
|
RedirectURIs []string `json:"redirectURIs" yaml:"redirectURIs"`
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// TrustedPeers are a list of peers which can issue tokens on this client's behalf using
|
|
|
|
// the dynamic "oauth2:server:client_id:(client_id)" scope. If a peer makes such a request,
|
|
|
|
// this client's ID will appear as the ID Token's audience.
|
|
|
|
//
|
2016-07-26 01:30:28 +05:30
|
|
|
// Clients inherently trust themselves.
|
2016-08-05 22:20:22 +05:30
|
|
|
TrustedPeers []string `json:"trustedPeers" yaml:"trustedPeers"`
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
// Public clients must use either use a redirectURL 127.0.0.1:X or "urn:ietf:wg:oauth:2.0:oob"
|
2016-08-05 22:20:22 +05:30
|
|
|
Public bool `json:"public" yaml:"public"`
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// Name and LogoURL used when displaying this client to the end user.
|
2016-08-05 22:20:22 +05:30
|
|
|
Name string `json:"name" yaml:"name"`
|
|
|
|
LogoURL string `json:"logoURL" yaml:"logoURL"`
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-08-03 10:27:36 +05:30
|
|
|
// Claims represents the ID Token claims supported by the server.
|
|
|
|
type Claims struct {
|
2019-10-10 20:13:41 +05:30
|
|
|
UserID string
|
|
|
|
Username string
|
|
|
|
PreferredUsername string
|
|
|
|
Email string
|
|
|
|
EmailVerified bool
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
Groups []string
|
|
|
|
}
|
|
|
|
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 16:03:40 +05:30
|
|
|
// Data needed for PKCE (RFC 7636)
|
|
|
|
type PKCE struct {
|
|
|
|
CodeChallenge string
|
|
|
|
CodeChallengeMethod string
|
|
|
|
}
|
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
// AuthRequest represents a OAuth2 client authorization request. It holds the state
|
|
|
|
// of a single auth flow up to the point that the user authorizes the client.
|
|
|
|
type AuthRequest struct {
|
2016-09-15 05:08:12 +05:30
|
|
|
// ID used to identify the authorization request.
|
|
|
|
ID string
|
|
|
|
|
|
|
|
// ID of the client requesting authorization from a user.
|
2016-07-26 01:30:28 +05:30
|
|
|
ClientID string
|
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// Values parsed from the initial request. These describe the resources the client is
|
|
|
|
// requesting as well as values describing the form of the response.
|
2016-07-26 01:30:28 +05:30
|
|
|
ResponseTypes []string
|
|
|
|
Scopes []string
|
|
|
|
RedirectURI string
|
2016-09-15 05:08:12 +05:30
|
|
|
Nonce string
|
|
|
|
State string
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
// The client has indicated that the end user must be shown an approval prompt
|
|
|
|
// on all requests. The server cannot cache their initial action for subsequent
|
|
|
|
// attempts.
|
|
|
|
ForceApprovalPrompt bool
|
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
Expiry time.Time
|
|
|
|
|
|
|
|
// Has the user proved their identity through a backing identity provider?
|
|
|
|
//
|
|
|
|
// If false, the following fields are invalid.
|
|
|
|
LoggedIn bool
|
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
// The identity of the end user. Generally nil until the user authenticates
|
|
|
|
// with a backend.
|
2016-09-15 05:08:12 +05:30
|
|
|
Claims Claims
|
2016-08-03 09:44:24 +05:30
|
|
|
|
|
|
|
// The connector used to login the user and any data the connector wishes to persists.
|
|
|
|
// Set when the user authenticates.
|
2019-04-18 18:22:05 +05:30
|
|
|
ConnectorID string
|
|
|
|
ConnectorData []byte
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 16:03:40 +05:30
|
|
|
|
|
|
|
// PKCE CodeChallenge and CodeChallengeMethod
|
|
|
|
PKCE PKCE
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// AuthCode represents a code which can be exchanged for an OAuth2 token response.
|
2016-09-15 05:08:12 +05:30
|
|
|
//
|
|
|
|
// This value is created once an end user has authorized a client, the server has
|
|
|
|
// redirect the end user back to the client, but the client hasn't exchanged the
|
|
|
|
// code for an access_token and id_token.
|
2016-07-26 01:30:28 +05:30
|
|
|
type AuthCode struct {
|
2016-09-15 05:08:12 +05:30
|
|
|
// Actual string returned as the "code" value.
|
2016-07-26 01:30:28 +05:30
|
|
|
ID string
|
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// The client this code value is valid for. When exchanging the code for a
|
|
|
|
// token response, the client must use its client_secret to authenticate.
|
|
|
|
ClientID string
|
2016-08-03 09:44:24 +05:30
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// As part of the OAuth2 spec when a client makes a token request it MUST
|
|
|
|
// present the same redirect_uri as the initial redirect. This values is saved
|
|
|
|
// to make this check.
|
|
|
|
//
|
|
|
|
// https://tools.ietf.org/html/rfc6749#section-4.1.3
|
|
|
|
RedirectURI string
|
2016-07-26 01:30:28 +05:30
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// If provided by the client in the initial request, the provider MUST create
|
|
|
|
// a ID Token with this nonce in the JWT payload.
|
2016-07-26 01:30:28 +05:30
|
|
|
Nonce string
|
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// Scopes authorized by the end user for the client.
|
2016-07-26 01:30:28 +05:30
|
|
|
Scopes []string
|
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// Authentication data provided by an upstream source.
|
2019-04-18 18:22:05 +05:30
|
|
|
ConnectorID string
|
|
|
|
ConnectorData []byte
|
|
|
|
Claims Claims
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
Expiry time.Time
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 16:03:40 +05:30
|
|
|
|
|
|
|
// PKCE CodeChallenge and CodeChallengeMethod
|
|
|
|
PKCE PKCE
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// RefreshToken is an OAuth2 refresh token which allows a client to request new
|
|
|
|
// tokens on the end user's behalf.
|
2016-08-03 10:27:36 +05:30
|
|
|
type RefreshToken struct {
|
2016-12-23 05:26:09 +05:30
|
|
|
ID string
|
|
|
|
|
|
|
|
// A single token that's rotated every time the refresh token is refreshed.
|
|
|
|
//
|
|
|
|
// May be empty.
|
|
|
|
Token string
|
|
|
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
LastUsed time.Time
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
// Client this refresh token is valid for.
|
2016-08-03 09:44:24 +05:30
|
|
|
ClientID string
|
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// Authentication data provided by an upstream source.
|
2019-04-18 18:22:05 +05:30
|
|
|
ConnectorID string
|
|
|
|
ConnectorData []byte
|
|
|
|
Claims Claims
|
2016-07-26 01:30:28 +05:30
|
|
|
|
|
|
|
// Scopes present in the initial request. Refresh requests may specify a set
|
|
|
|
// of scopes different from the initial request when refreshing a token,
|
|
|
|
// however those scopes must be encompassed by this set.
|
|
|
|
Scopes []string
|
|
|
|
|
2016-09-15 05:08:12 +05:30
|
|
|
// Nonce value supplied during the initial redirect. This is required to be part
|
|
|
|
// of the claims of any future id_token generated by the client.
|
2016-07-26 01:30:28 +05:30
|
|
|
Nonce string
|
|
|
|
}
|
|
|
|
|
2017-02-01 05:41:59 +05:30
|
|
|
// RefreshTokenRef is a reference object that contains metadata about refresh tokens.
|
|
|
|
type RefreshTokenRef struct {
|
|
|
|
ID string
|
|
|
|
|
|
|
|
// Client the refresh token is valid for.
|
|
|
|
ClientID string
|
|
|
|
|
|
|
|
CreatedAt time.Time
|
|
|
|
LastUsed time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// OfflineSessions objects are sessions pertaining to users with refresh tokens.
|
|
|
|
type OfflineSessions struct {
|
|
|
|
// UserID of an end user who has logged in to the server.
|
|
|
|
UserID string
|
|
|
|
|
|
|
|
// The ID of the connector used to login the user.
|
|
|
|
ConnID string
|
|
|
|
|
|
|
|
// Refresh is a hash table of refresh token reference objects
|
|
|
|
// indexed by the ClientID of the refresh token.
|
|
|
|
Refresh map[string]*RefreshTokenRef
|
2018-01-29 04:07:07 +05:30
|
|
|
|
|
|
|
// Authentication data provided by an upstream source.
|
|
|
|
ConnectorData []byte
|
2017-02-01 05:41:59 +05:30
|
|
|
}
|
|
|
|
|
2016-10-06 04:33:29 +05:30
|
|
|
// Password is an email to password mapping managed by the storage.
|
|
|
|
type Password struct {
|
|
|
|
// Email and identifying name of the password. Emails are assumed to be valid and
|
|
|
|
// determining that an end-user controls the address is left to an outside application.
|
|
|
|
//
|
|
|
|
// Emails are case insensitive and should be standardized by the storage.
|
|
|
|
//
|
|
|
|
// Storages that don't support an extended character set for IDs, such as '.' and '@'
|
|
|
|
// (cough cough, kubernetes), must map this value appropriately.
|
2016-11-04 03:02:23 +05:30
|
|
|
Email string `json:"email"`
|
2016-10-06 04:33:29 +05:30
|
|
|
|
2016-10-28 04:58:11 +05:30
|
|
|
// Bcrypt encoded hash of the password. This package enforces a min cost value of 10
|
2016-11-04 03:02:23 +05:30
|
|
|
Hash []byte `json:"hash"`
|
2016-10-06 04:33:29 +05:30
|
|
|
|
2019-12-14 06:03:21 +05:30
|
|
|
// Bcrypt encoded hash of the password set in environment variable of this name.
|
|
|
|
HashFromEnv string `json:"hashFromEnv"`
|
|
|
|
|
2016-10-06 04:33:29 +05:30
|
|
|
// Optional username to display. NOT used during login.
|
2016-11-04 03:02:23 +05:30
|
|
|
Username string `json:"username"`
|
2016-10-06 04:33:29 +05:30
|
|
|
|
|
|
|
// Randomly generated user ID. This is NOT the primary ID of the Password object.
|
2016-11-04 03:02:23 +05:30
|
|
|
UserID string `json:"userID"`
|
2016-10-06 04:33:29 +05:30
|
|
|
}
|
|
|
|
|
2017-03-23 22:29:33 +05:30
|
|
|
// Connector is an object that contains the metadata about connectors used to login to Dex.
|
|
|
|
type Connector struct {
|
|
|
|
// ID that will uniquely identify the connector object.
|
2017-04-18 04:11:41 +05:30
|
|
|
ID string `json:"id"`
|
2017-03-23 22:29:33 +05:30
|
|
|
// The Type of the connector. E.g. 'oidc' or 'ldap'
|
2017-04-18 04:11:41 +05:30
|
|
|
Type string `json:"type"`
|
2017-03-23 22:29:33 +05:30
|
|
|
// The Name of the connector that is used when displaying it to the end user.
|
2017-04-18 04:11:41 +05:30
|
|
|
Name string `json:"name"`
|
2017-03-23 22:29:33 +05:30
|
|
|
// ResourceVersion is the static versioning used to keep track of dynamic configuration
|
|
|
|
// changes to the connector object made by the API calls.
|
2017-04-18 04:11:41 +05:30
|
|
|
ResourceVersion string `json:"resourceVersion"`
|
2017-03-23 22:29:33 +05:30
|
|
|
// Config holds all the configuration information specific to the connector type. Since there
|
|
|
|
// no generic struct we can use for this purpose, it is stored as a byte stream.
|
2017-04-18 04:11:41 +05:30
|
|
|
Config []byte `json:"email"`
|
2017-03-23 22:29:33 +05:30
|
|
|
}
|
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
// VerificationKey is a rotated signing key which can still be used to verify
|
|
|
|
// signatures.
|
|
|
|
type VerificationKey struct {
|
|
|
|
PublicKey *jose.JSONWebKey `json:"publicKey"`
|
|
|
|
Expiry time.Time `json:"expiry"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keys hold encryption and signing keys.
|
|
|
|
type Keys struct {
|
|
|
|
// Key for creating and verifying signatures. These may be nil.
|
|
|
|
SigningKey *jose.JSONWebKey
|
|
|
|
SigningKeyPub *jose.JSONWebKey
|
2016-09-15 05:08:12 +05:30
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
// Old signing keys which have been rotated but can still be used to validate
|
|
|
|
// existing signatures.
|
|
|
|
VerificationKeys []VerificationKey
|
|
|
|
|
|
|
|
// The next time the signing key will rotate.
|
|
|
|
//
|
|
|
|
// For caching purposes, implementations MUST NOT update keys before this time.
|
|
|
|
NextRotation time.Time
|
|
|
|
}
|
2020-01-16 21:25:07 +05:30
|
|
|
|
2020-01-27 21:05:37 +05:30
|
|
|
// NewUserCode returns a randomized 8 character user code for the device flow.
|
|
|
|
// No vowels are included to prevent accidental generation of words
|
|
|
|
func NewUserCode() (string, error) {
|
|
|
|
code, err := randomString(8)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return code[:4] + "-" + code[4:], nil
|
2020-01-16 21:25:07 +05:30
|
|
|
}
|
|
|
|
|
2020-01-27 21:05:37 +05:30
|
|
|
func randomString(n int) (string, error) {
|
|
|
|
v := big.NewInt(int64(len(validUserCharacters)))
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
c, _ := rand.Int(rand.Reader, v)
|
|
|
|
bytes[i] = validUserCharacters[c.Int64()]
|
2020-01-16 21:25:07 +05:30
|
|
|
}
|
2020-01-27 21:05:37 +05:30
|
|
|
return string(bytes), nil
|
2020-01-16 21:25:07 +05:30
|
|
|
}
|
|
|
|
|
2020-10-18 02:32:29 +05:30
|
|
|
// DeviceRequest represents an OIDC device authorization request. It holds the state of a device request until the user
|
|
|
|
// authenticates using their user code or the expiry time passes.
|
2020-01-16 21:25:07 +05:30
|
|
|
type DeviceRequest struct {
|
2020-10-18 02:32:29 +05:30
|
|
|
// The code the user will enter in a browser
|
2020-01-16 21:25:07 +05:30
|
|
|
UserCode string
|
2020-10-18 02:32:29 +05:30
|
|
|
// The unique device code for device authentication
|
2020-01-16 21:25:07 +05:30
|
|
|
DeviceCode string
|
2020-10-18 02:32:29 +05:30
|
|
|
// The client ID the code is for
|
2020-01-16 21:25:07 +05:30
|
|
|
ClientID string
|
2020-10-18 02:32:29 +05:30
|
|
|
// The Client Secret
|
2020-02-04 20:37:18 +05:30
|
|
|
ClientSecret string
|
2020-10-18 02:32:29 +05:30
|
|
|
// The scopes the device requests
|
2020-01-16 21:25:07 +05:30
|
|
|
Scopes []string
|
2020-10-18 02:32:29 +05:30
|
|
|
// The expire time
|
2020-01-16 21:25:07 +05:30
|
|
|
Expiry time.Time
|
|
|
|
}
|
|
|
|
|
2020-10-18 02:32:29 +05:30
|
|
|
// DeviceToken is a structure which represents the actual token of an authorized device and its rotation parameters
|
2020-01-16 21:25:07 +05:30
|
|
|
type DeviceToken struct {
|
2020-01-29 00:44:30 +05:30
|
|
|
DeviceCode string
|
|
|
|
Status string
|
|
|
|
Token string
|
|
|
|
Expiry time.Time
|
|
|
|
LastRequestTime time.Time
|
|
|
|
PollIntervalSeconds int
|
2020-01-16 21:25:07 +05:30
|
|
|
}
|