BREAKING CHANGE: The OpenID Provider package is now able to handle multiple issuers with a single storage implementation. The issuer will be selected from the host of the request and passed into the context, where every function can read it from if necessary. This results in some fundamental changes: - `Configuration` interface: - `Issuer() string` has been changed to `IssuerFromRequest(r *http.Request) string` - `Insecure() bool` has been added - OpenIDProvider interface and dependants: - `Issuer` has been removed from Config struct - `NewOpenIDProvider` now takes an additional parameter `issuer` and returns a pointer to the public/default implementation and not an OpenIDProvider interface: `NewOpenIDProvider(ctx context.Context, config *Config, storage Storage, opOpts ...Option) (OpenIDProvider, error)` changed to `NewOpenIDProvider(ctx context.Context, issuer string, config *Config, storage Storage, opOpts ...Option) (*Provider, error)` - therefore the parameter type Option changed to the public type as well: `Option func(o *Provider) error` - `AuthCallbackURL(o OpenIDProvider) func(string) string` has been changed to `AuthCallbackURL(o OpenIDProvider) func(context.Context, string) string` - `IDTokenHintVerifier() IDTokenHintVerifier` (Authorizer, OpenIDProvider, SessionEnder interfaces), `AccessTokenVerifier() AccessTokenVerifier` (Introspector, OpenIDProvider, Revoker, UserinfoProvider interfaces) and `JWTProfileVerifier() JWTProfileVerifier` (IntrospectorJWTProfile, JWTAuthorizationGrantExchanger, OpenIDProvider, RevokerJWTProfile interfaces) now take a context.Context parameter `IDTokenHintVerifier(context.Context) IDTokenHintVerifier`, `AccessTokenVerifier(context.Context) AccessTokenVerifier` and `JWTProfileVerifier(context.Context) JWTProfileVerifier` - `OidcDevMode` (CAOS_OIDC_DEV) environment variable check has been removed, use `WithAllowInsecure()` Option - Signing: the signer is not kept in memory anymore, but created on request from the loaded key: - `Signer` interface and func `NewSigner` have been removed - `ReadySigner(s Signer) ProbesFn` has been removed - `CreateDiscoveryConfig(c Configuration, s Signer) *oidc.DiscoveryConfiguration` has been changed to `CreateDiscoveryConfig(r *http.Request, config Configuration, storage DiscoverStorage) *oidc.DiscoveryConfiguration` - `Storage` interface: - `GetSigningKey(context.Context, chan<- jose.SigningKey)` has been changed to `SigningKey(context.Context) (SigningKey, error)` - `KeySet(context.Context) ([]Key, error)` has been added - `GetKeySet(context.Context) (*jose.JSONWebKeySet, error)` has been changed to `KeySet(context.Context) ([]Key, error)` - `SigAlgorithms(s Signer) []string` has been changed to `SigAlgorithms(ctx context.Context, storage DiscoverStorage) []string` - KeyProvider interface: `GetKeySet(context.Context) (*jose.JSONWebKeySet, error)` has been changed to `KeySet(context.Context) ([]Key, error)` - `CreateIDToken`: the Signer parameter has been removed
183 lines
4.7 KiB
Go
183 lines
4.7 KiB
Go
package mock
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/caos/oidc/pkg/oidc"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/caos/oidc/pkg/op"
|
|
)
|
|
|
|
func NewStorage(t *testing.T) op.Storage {
|
|
return NewMockStorage(gomock.NewController(t))
|
|
}
|
|
|
|
func NewMockStorageExpectValidClientID(t *testing.T) op.Storage {
|
|
m := NewStorage(t)
|
|
ExpectValidClientID(m)
|
|
return m
|
|
}
|
|
|
|
func NewMockStorageExpectInvalidClientID(t *testing.T) op.Storage {
|
|
m := NewStorage(t)
|
|
ExpectInvalidClientID(m)
|
|
return m
|
|
}
|
|
|
|
func NewMockStorageAny(t *testing.T) op.Storage {
|
|
m := NewStorage(t)
|
|
mockS := m.(*MockStorage)
|
|
mockS.EXPECT().GetClientByClientID(gomock.Any(), gomock.Any()).AnyTimes().Return(&ConfClient{}, nil)
|
|
mockS.EXPECT().AuthorizeClientIDSecret(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
|
|
return m
|
|
}
|
|
|
|
func NewMockStorageSigningKeyInvalid(t *testing.T) op.Storage {
|
|
m := NewStorage(t)
|
|
//ExpectSigningKeyInvalid(m)
|
|
return m
|
|
}
|
|
func NewMockStorageSigningKey(t *testing.T) op.Storage {
|
|
m := NewStorage(t)
|
|
//ExpectSigningKey(m)
|
|
return m
|
|
}
|
|
|
|
func ExpectInvalidClientID(s op.Storage) {
|
|
mockS := s.(*MockStorage)
|
|
mockS.EXPECT().GetClientByClientID(gomock.Any(), gomock.Any()).Return(nil, errors.New("client not found"))
|
|
}
|
|
|
|
func ExpectValidClientID(s op.Storage) {
|
|
mockS := s.(*MockStorage)
|
|
mockS.EXPECT().GetClientByClientID(gomock.Any(), gomock.Any()).DoAndReturn(
|
|
func(_ context.Context, id string) (op.Client, error) {
|
|
var appType op.ApplicationType
|
|
var authMethod oidc.AuthMethod
|
|
var accessTokenType op.AccessTokenType
|
|
var responseTypes []oidc.ResponseType
|
|
switch id {
|
|
case "web_client":
|
|
appType = op.ApplicationTypeWeb
|
|
authMethod = oidc.AuthMethodBasic
|
|
accessTokenType = op.AccessTokenTypeBearer
|
|
responseTypes = []oidc.ResponseType{oidc.ResponseTypeCode}
|
|
case "native_client":
|
|
appType = op.ApplicationTypeNative
|
|
authMethod = oidc.AuthMethodNone
|
|
accessTokenType = op.AccessTokenTypeBearer
|
|
responseTypes = []oidc.ResponseType{oidc.ResponseTypeCode}
|
|
case "useragent_client":
|
|
appType = op.ApplicationTypeUserAgent
|
|
authMethod = oidc.AuthMethodBasic
|
|
accessTokenType = op.AccessTokenTypeJWT
|
|
responseTypes = []oidc.ResponseType{oidc.ResponseTypeIDToken}
|
|
}
|
|
return &ConfClient{id: id, appType: appType, authMethod: authMethod, accessTokenType: accessTokenType, responseTypes: responseTypes}, nil
|
|
})
|
|
}
|
|
|
|
//
|
|
//func ExpectSigningKeyInvalid(s op.Storage) {
|
|
// mockS := s.(*MockStorage)
|
|
// mockS.EXPECT().GetSigningKey(gomock.Any(), gomock.Any()).DoAndReturn(
|
|
// func(_ context.Context, keyCh chan<- jose.SigningKey) {
|
|
// keyCh <- jose.SigningKey{}
|
|
// },
|
|
// )
|
|
//}
|
|
//
|
|
//func ExpectSigningKey(s op.Storage) {
|
|
// mockS := s.(*MockStorage)
|
|
// mockS.EXPECT().GetSigningKey(gomock.Any(), gomock.Any()).DoAndReturn(
|
|
// func(_ context.Context, keyCh chan<- jose.SigningKey) {
|
|
// keyCh <- jose.SigningKey{Algorithm: jose.HS256, Key: []byte("key")}
|
|
// },
|
|
// )
|
|
//}
|
|
|
|
type ConfClient struct {
|
|
id string
|
|
appType op.ApplicationType
|
|
authMethod oidc.AuthMethod
|
|
accessTokenType op.AccessTokenType
|
|
responseTypes []oidc.ResponseType
|
|
grantTypes []oidc.GrantType
|
|
devMode bool
|
|
}
|
|
|
|
func (c *ConfClient) RedirectURIs() []string {
|
|
return []string{
|
|
"https://registered.com/callback",
|
|
"http://registered.com/callback",
|
|
"http://localhost:9999/callback",
|
|
"custom://callback",
|
|
}
|
|
}
|
|
func (c *ConfClient) PostLogoutRedirectURIs() []string {
|
|
return []string{}
|
|
}
|
|
|
|
func (c *ConfClient) LoginURL(id string) string {
|
|
return "login?id=" + id
|
|
}
|
|
|
|
func (c *ConfClient) ApplicationType() op.ApplicationType {
|
|
return c.appType
|
|
}
|
|
|
|
func (c *ConfClient) AuthMethod() oidc.AuthMethod {
|
|
return c.authMethod
|
|
}
|
|
|
|
func (c *ConfClient) GetID() string {
|
|
return c.id
|
|
}
|
|
|
|
func (c *ConfClient) AccessTokenLifetime() time.Duration {
|
|
return 5 * time.Minute
|
|
}
|
|
func (c *ConfClient) IDTokenLifetime() time.Duration {
|
|
return 5 * time.Minute
|
|
}
|
|
func (c *ConfClient) AccessTokenType() op.AccessTokenType {
|
|
return c.accessTokenType
|
|
}
|
|
func (c *ConfClient) ResponseTypes() []oidc.ResponseType {
|
|
return c.responseTypes
|
|
}
|
|
func (c *ConfClient) GrantTypes() []oidc.GrantType {
|
|
return c.grantTypes
|
|
}
|
|
func (c *ConfClient) DevMode() bool {
|
|
return c.devMode
|
|
}
|
|
func (c *ConfClient) AllowedScopes() []string {
|
|
return nil
|
|
}
|
|
func (c *ConfClient) RestrictAdditionalIdTokenScopes() func(scopes []string) []string {
|
|
return func(scopes []string) []string {
|
|
return scopes
|
|
}
|
|
}
|
|
func (c *ConfClient) RestrictAdditionalAccessTokenScopes() func(scopes []string) []string {
|
|
return func(scopes []string) []string {
|
|
return scopes
|
|
}
|
|
}
|
|
func (c *ConfClient) IsScopeAllowed(scope string) bool {
|
|
return false
|
|
}
|
|
|
|
func (c *ConfClient) IDTokenUserinfoClaimsAssertion() bool {
|
|
return false
|
|
}
|
|
|
|
func (c *ConfClient) ClockSkew() time.Duration {
|
|
return 0
|
|
}
|