authn

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2019 License: BSD-3-Clause Imports: 8 Imported by: 1

Documentation

Index

Constants

View Source
const (
	AuthnHeaderKey = "Authorization"
)

Variables

This section is empty.

Functions

func AuthenticationHeaderTokenExtractor added in v0.3.0

func AuthenticationHeaderTokenExtractor(ctx context.Context, r *httpx.Request, scheme string) (token string, err merry.Error)

AuthenticationHeaderTokenExtractor returns the token from the "Authentication" header. An error is returned if the "Authentication" header is missing, its value is empty or presented scheme doesn't match the value of `scheme`.

func BasicAuthTokenExtractor added in v0.3.0

func BasicAuthTokenExtractor(ctx context.Context, r *httpx.Request) (token string, err merry.Error)

BasicAuthTokenExtractor returns the decoded credentials from a Basic Authentication challenge. The token returned is the colon-concatentated userid-password as specified in RFC 7617. An error is returned if the credentials cannot be extracted.

func URLTokenExtractor added in v0.3.0

func URLTokenExtractor(ctx context.Context, r *httpx.Request) (token string, err merry.Error)

URLTokenExtractor returns the credentials from the request URL concatenated together with a colon as specified in RFC 7617. An error is returned if the credentials cannot be extracted.

Types

type Authenticator added in v0.3.0

type Authenticator interface {
	// Authenticate extracts a token from the request and
	// resolves it into a user principal.
	Authenticate(context.Context, *httpx.Request) (models.User, merry.Error)
	// Challenge returns the value for the "WWW-Authenticate"
	// header if authentication fails.
	Challenge() string
}

Authenticator defines a provder for authenticating the principal of a request.

func NewAuthenticator added in v0.3.0

func NewAuthenticator(extractor httpx.StringExtractor, idp IdentityProvider, scheme, realm string) (Authenticator, merry.Error)

NewAuthenticator returns an authenticator using the given token extractor and identity provider. An error will be returned if the `idp` or `extractor` parameters are nil.

func NewBasicAuthenticator added in v0.3.0

func NewBasicAuthenticator(idp IdentityProvider, realm string) (Authenticator, merry.Error)

NewBasicAuthenticator returns an authenticator implementing Basic Access Authentication as specified in RFC 7617. An error will be returned if the `idp` parameter is nil.

func NewBearerAuthenticator added in v0.3.0

func NewBearerAuthenticator(idp IdentityProvider, realm string) (Authenticator, merry.Error)

NewBearerAuthenticatior returns an authenticator implementing Bearer Access Authentication as specified in RFC 7617. An error will be returned if the `idp` parameter is nil.

type AuthenticatorAuthenticateInvocation added in v0.3.0

type AuthenticatorAuthenticateInvocation struct {
	Parameters struct {
		Ident1 context.Context
		Ident2 *httpx.Request
	}
	Results struct {
		Ident3 models.User
		Ident4 merry.Error
	}
}

AuthenticatorAuthenticateInvocation represents a single call of FakeAuthenticator.Authenticate

func NewAuthenticatorAuthenticateInvocation added in v0.3.1

func NewAuthenticatorAuthenticateInvocation(ident1 context.Context, ident2 *httpx.Request, ident3 models.User, ident4 merry.Error) *AuthenticatorAuthenticateInvocation

NewAuthenticatorAuthenticateInvocation creates a new instance of AuthenticatorAuthenticateInvocation

type AuthenticatorChallengeInvocation added in v0.3.0

type AuthenticatorChallengeInvocation struct {
	Results struct {
		Ident1 string
	}
}

AuthenticatorChallengeInvocation represents a single call of FakeAuthenticator.Challenge

type AuthenticatorTestingT added in v0.3.0

type AuthenticatorTestingT interface {
	Error(...interface{})
	Errorf(string, ...interface{})
	Fatal(...interface{})
	Helper()
}

AuthenticatorTestingT represents the methods of "testing".T used by charlatan Fakes. It avoids importing the testing package.

type FakeAuthenticator added in v0.3.0

type FakeAuthenticator struct {
	AuthenticateHook func(context.Context, *httpx.Request) (models.User, merry.Error)
	ChallengeHook    func() string

	AuthenticateCalls []*AuthenticatorAuthenticateInvocation
	ChallengeCalls    []*AuthenticatorChallengeInvocation
}

FakeAuthenticator is a mock implementation of Authenticator for testing. Use it in your tests as in this example:

package example

func TestWithAuthenticator(t *testing.T) {
	f := &authn.FakeAuthenticator{
		AuthenticateHook: func(ident1 context.Context, ident2 *httpx.Request) (ident3 models.User, ident4 merry.Error) {
			// ensure parameters meet expections, signal errors using t, etc
			return
		},
	}

	// test code goes here ...

	// assert state of FakeAuthenticate ...
	f.AssertAuthenticateCalledOnce(t)
}

Create anonymous function implementations for only those interface methods that should be called in the code under test. This will force a panic if any unexpected calls are made to FakeAuthenticate.

func NewFakeAuthenticatorDefaultError added in v0.3.0

func NewFakeAuthenticatorDefaultError(t AuthenticatorTestingT) *FakeAuthenticator

NewFakeAuthenticatorDefaultError returns an instance of FakeAuthenticator with all hooks configured to call t.Error

func NewFakeAuthenticatorDefaultFatal added in v0.3.0

func NewFakeAuthenticatorDefaultFatal(t AuthenticatorTestingT) *FakeAuthenticator

NewFakeAuthenticatorDefaultFatal returns an instance of FakeAuthenticator with all hooks configured to call t.Fatal

func NewFakeAuthenticatorDefaultPanic added in v0.3.0

func NewFakeAuthenticatorDefaultPanic() *FakeAuthenticator

NewFakeAuthenticatorDefaultPanic returns an instance of FakeAuthenticator with all hooks configured to panic

func (*FakeAuthenticator) AssertAuthenticateCalled added in v0.3.0

func (f *FakeAuthenticator) AssertAuthenticateCalled(t AuthenticatorTestingT)

AssertAuthenticateCalled calls t.Error if FakeAuthenticator.Authenticate was not called

func (*FakeAuthenticator) AssertAuthenticateCalledN added in v0.3.0

func (f *FakeAuthenticator) AssertAuthenticateCalledN(t AuthenticatorTestingT, n int)

AssertAuthenticateCalledN calls t.Error if FakeAuthenticator.Authenticate was called less than n times

func (*FakeAuthenticator) AssertAuthenticateCalledOnce added in v0.3.0

func (f *FakeAuthenticator) AssertAuthenticateCalledOnce(t AuthenticatorTestingT)

AssertAuthenticateCalledOnce calls t.Error if FakeAuthenticator.Authenticate was not called exactly once

func (*FakeAuthenticator) AssertAuthenticateCalledOnceWith added in v0.3.0

func (_f9 *FakeAuthenticator) AssertAuthenticateCalledOnceWith(t AuthenticatorTestingT, ident1 context.Context, ident2 *httpx.Request)

AssertAuthenticateCalledOnceWith calls t.Error if FakeAuthenticator.Authenticate was not called exactly once with the given values

func (*FakeAuthenticator) AssertAuthenticateCalledWith added in v0.3.0

func (_f7 *FakeAuthenticator) AssertAuthenticateCalledWith(t AuthenticatorTestingT, ident1 context.Context, ident2 *httpx.Request)

AssertAuthenticateCalledWith calls t.Error if FakeAuthenticator.Authenticate was not called with the given values

func (*FakeAuthenticator) AssertAuthenticateNotCalled added in v0.3.0

func (f *FakeAuthenticator) AssertAuthenticateNotCalled(t AuthenticatorTestingT)

AssertAuthenticateNotCalled calls t.Error if FakeAuthenticator.Authenticate was called

func (*FakeAuthenticator) AssertChallengeCalled added in v0.3.0

func (f *FakeAuthenticator) AssertChallengeCalled(t AuthenticatorTestingT)

AssertChallengeCalled calls t.Error if FakeAuthenticator.Challenge was not called

func (*FakeAuthenticator) AssertChallengeCalledN added in v0.3.0

func (f *FakeAuthenticator) AssertChallengeCalledN(t AuthenticatorTestingT, n int)

AssertChallengeCalledN calls t.Error if FakeAuthenticator.Challenge was called less than n times

func (*FakeAuthenticator) AssertChallengeCalledOnce added in v0.3.0

func (f *FakeAuthenticator) AssertChallengeCalledOnce(t AuthenticatorTestingT)

AssertChallengeCalledOnce calls t.Error if FakeAuthenticator.Challenge was not called exactly once

func (*FakeAuthenticator) AssertChallengeNotCalled added in v0.3.0

func (f *FakeAuthenticator) AssertChallengeNotCalled(t AuthenticatorTestingT)

AssertChallengeNotCalled calls t.Error if FakeAuthenticator.Challenge was called

func (*FakeAuthenticator) Authenticate added in v0.3.0

func (_f1 *FakeAuthenticator) Authenticate(ident1 context.Context, ident2 *httpx.Request) (ident3 models.User, ident4 merry.Error)

func (*FakeAuthenticator) AuthenticateCalled added in v0.3.0

func (f *FakeAuthenticator) AuthenticateCalled() bool

AuthenticateCalled returns true if FakeAuthenticator.Authenticate was called

func (*FakeAuthenticator) AuthenticateCalledN added in v0.3.0

func (f *FakeAuthenticator) AuthenticateCalledN(n int) bool

AuthenticateCalledN returns true if FakeAuthenticator.Authenticate was called at least n times

func (*FakeAuthenticator) AuthenticateCalledOnce added in v0.3.0

func (f *FakeAuthenticator) AuthenticateCalledOnce() bool

AuthenticateCalledOnce returns true if FakeAuthenticator.Authenticate was called exactly once

func (*FakeAuthenticator) AuthenticateCalledOnceWith added in v0.3.0

func (_f8 *FakeAuthenticator) AuthenticateCalledOnceWith(ident1 context.Context, ident2 *httpx.Request) bool

AuthenticateCalledOnceWith returns true if FakeAuthenticator.Authenticate was called exactly once with the given values

func (*FakeAuthenticator) AuthenticateCalledWith added in v0.3.0

func (_f6 *FakeAuthenticator) AuthenticateCalledWith(ident1 context.Context, ident2 *httpx.Request) (found bool)

AuthenticateCalledWith returns true if FakeAuthenticator.Authenticate was called with the given values

func (*FakeAuthenticator) AuthenticateNotCalled added in v0.3.0

func (f *FakeAuthenticator) AuthenticateNotCalled() bool

AuthenticateNotCalled returns true if FakeAuthenticator.Authenticate was not called

func (*FakeAuthenticator) AuthenticateResultsForCall added in v0.3.0

func (_f10 *FakeAuthenticator) AuthenticateResultsForCall(ident1 context.Context, ident2 *httpx.Request) (ident3 models.User, ident4 merry.Error, found bool)

AuthenticateResultsForCall returns the result values for the first call to FakeAuthenticator.Authenticate with the given values

func (*FakeAuthenticator) Challenge added in v0.3.0

func (_f11 *FakeAuthenticator) Challenge() (ident1 string)

func (*FakeAuthenticator) ChallengeCalled added in v0.3.0

func (f *FakeAuthenticator) ChallengeCalled() bool

ChallengeCalled returns true if FakeAuthenticator.Challenge was called

func (*FakeAuthenticator) ChallengeCalledN added in v0.3.0

func (f *FakeAuthenticator) ChallengeCalledN(n int) bool

ChallengeCalledN returns true if FakeAuthenticator.Challenge was called at least n times

func (*FakeAuthenticator) ChallengeCalledOnce added in v0.3.0

func (f *FakeAuthenticator) ChallengeCalledOnce() bool

ChallengeCalledOnce returns true if FakeAuthenticator.Challenge was called exactly once

func (*FakeAuthenticator) ChallengeNotCalled added in v0.3.0

func (f *FakeAuthenticator) ChallengeNotCalled() bool

ChallengeNotCalled returns true if FakeAuthenticator.Challenge was not called

func (*FakeAuthenticator) Reset added in v0.3.0

func (f *FakeAuthenticator) Reset()

func (*FakeAuthenticator) SetAuthenticateInvocation added in v0.3.1

func (_f3 *FakeAuthenticator) SetAuthenticateInvocation(calls_f4 []*AuthenticatorAuthenticateInvocation, fallback_f5 func() (models.User, merry.Error))

SetAuthenticateInvocation configures Authenticator.Authenticate to return the given results when called with the given parameters If no match is found for an invocation the result(s) of the fallback function are returned

func (*FakeAuthenticator) SetAuthenticateStub added in v0.3.1

func (_f2 *FakeAuthenticator) SetAuthenticateStub(ident3 models.User, ident4 merry.Error)

SetAuthenticateStub configures Authenticator.Authenticate to always return the given values

func (*FakeAuthenticator) SetChallengeStub added in v0.3.1

func (_f12 *FakeAuthenticator) SetChallengeStub(ident1 string)

SetChallengeStub configures Authenticator.Challenge to always return the given values

type FakeIdentityProvider added in v0.3.0

type FakeIdentityProvider struct {
	AuthenticateHook func(context.Context, string) (models.User, merry.Error)

	AuthenticateCalls []*IdentityProviderAuthenticateInvocation
}

FakeIdentityProvider is a mock implementation of IdentityProvider for testing. Use it in your tests as in this example:

package example

func TestWithIdentityProvider(t *testing.T) {
	f := &authn.FakeIdentityProvider{
		AuthenticateHook: func(ident1 context.Context, ident2 string) (ident3 models.User, ident4 merry.Error) {
			// ensure parameters meet expections, signal errors using t, etc
			return
		},
	}

	// test code goes here ...

	// assert state of FakeAuthenticate ...
	f.AssertAuthenticateCalledOnce(t)
}

Create anonymous function implementations for only those interface methods that should be called in the code under test. This will force a panic if any unexpected calls are made to FakeAuthenticate.

func NewFakeIdentityProviderDefaultError added in v0.3.0

func NewFakeIdentityProviderDefaultError(t IdentityProviderTestingT) *FakeIdentityProvider

NewFakeIdentityProviderDefaultError returns an instance of FakeIdentityProvider with all hooks configured to call t.Error

func NewFakeIdentityProviderDefaultFatal added in v0.3.0

func NewFakeIdentityProviderDefaultFatal(t IdentityProviderTestingT) *FakeIdentityProvider

NewFakeIdentityProviderDefaultFatal returns an instance of FakeIdentityProvider with all hooks configured to call t.Fatal

func NewFakeIdentityProviderDefaultPanic added in v0.3.0

func NewFakeIdentityProviderDefaultPanic() *FakeIdentityProvider

NewFakeIdentityProviderDefaultPanic returns an instance of FakeIdentityProvider with all hooks configured to panic

func (*FakeIdentityProvider) AssertAuthenticateCalled added in v0.3.0

func (f *FakeIdentityProvider) AssertAuthenticateCalled(t IdentityProviderTestingT)

AssertAuthenticateCalled calls t.Error if FakeIdentityProvider.Authenticate was not called

func (*FakeIdentityProvider) AssertAuthenticateCalledN added in v0.3.0

func (f *FakeIdentityProvider) AssertAuthenticateCalledN(t IdentityProviderTestingT, n int)

AssertAuthenticateCalledN calls t.Error if FakeIdentityProvider.Authenticate was called less than n times

func (*FakeIdentityProvider) AssertAuthenticateCalledOnce added in v0.3.0

func (f *FakeIdentityProvider) AssertAuthenticateCalledOnce(t IdentityProviderTestingT)

AssertAuthenticateCalledOnce calls t.Error if FakeIdentityProvider.Authenticate was not called exactly once

func (*FakeIdentityProvider) AssertAuthenticateCalledOnceWith added in v0.3.0

func (_f9 *FakeIdentityProvider) AssertAuthenticateCalledOnceWith(t IdentityProviderTestingT, ident1 context.Context, ident2 string)

AssertAuthenticateCalledOnceWith calls t.Error if FakeIdentityProvider.Authenticate was not called exactly once with the given values

func (*FakeIdentityProvider) AssertAuthenticateCalledWith added in v0.3.0

func (_f7 *FakeIdentityProvider) AssertAuthenticateCalledWith(t IdentityProviderTestingT, ident1 context.Context, ident2 string)

AssertAuthenticateCalledWith calls t.Error if FakeIdentityProvider.Authenticate was not called with the given values

func (*FakeIdentityProvider) AssertAuthenticateNotCalled added in v0.3.0

func (f *FakeIdentityProvider) AssertAuthenticateNotCalled(t IdentityProviderTestingT)

AssertAuthenticateNotCalled calls t.Error if FakeIdentityProvider.Authenticate was called

func (*FakeIdentityProvider) Authenticate added in v0.3.0

func (_f1 *FakeIdentityProvider) Authenticate(ident1 context.Context, ident2 string) (ident3 models.User, ident4 merry.Error)

func (*FakeIdentityProvider) AuthenticateCalled added in v0.3.0

func (f *FakeIdentityProvider) AuthenticateCalled() bool

AuthenticateCalled returns true if FakeIdentityProvider.Authenticate was called

func (*FakeIdentityProvider) AuthenticateCalledN added in v0.3.0

func (f *FakeIdentityProvider) AuthenticateCalledN(n int) bool

AuthenticateCalledN returns true if FakeIdentityProvider.Authenticate was called at least n times

func (*FakeIdentityProvider) AuthenticateCalledOnce added in v0.3.0

func (f *FakeIdentityProvider) AuthenticateCalledOnce() bool

AuthenticateCalledOnce returns true if FakeIdentityProvider.Authenticate was called exactly once

func (*FakeIdentityProvider) AuthenticateCalledOnceWith added in v0.3.0

func (_f8 *FakeIdentityProvider) AuthenticateCalledOnceWith(ident1 context.Context, ident2 string) bool

AuthenticateCalledOnceWith returns true if FakeIdentityProvider.Authenticate was called exactly once with the given values

func (*FakeIdentityProvider) AuthenticateCalledWith added in v0.3.0

func (_f6 *FakeIdentityProvider) AuthenticateCalledWith(ident1 context.Context, ident2 string) (found bool)

AuthenticateCalledWith returns true if FakeIdentityProvider.Authenticate was called with the given values

func (*FakeIdentityProvider) AuthenticateNotCalled added in v0.3.0

func (f *FakeIdentityProvider) AuthenticateNotCalled() bool

AuthenticateNotCalled returns true if FakeIdentityProvider.Authenticate was not called

func (*FakeIdentityProvider) AuthenticateResultsForCall added in v0.3.0

func (_f10 *FakeIdentityProvider) AuthenticateResultsForCall(ident1 context.Context, ident2 string) (ident3 models.User, ident4 merry.Error, found bool)

AuthenticateResultsForCall returns the result values for the first call to FakeIdentityProvider.Authenticate with the given values

func (*FakeIdentityProvider) Reset added in v0.3.0

func (f *FakeIdentityProvider) Reset()

func (*FakeIdentityProvider) SetAuthenticateInvocation added in v0.3.1

func (_f3 *FakeIdentityProvider) SetAuthenticateInvocation(calls_f4 []*IdentityProviderAuthenticateInvocation, fallback_f5 func() (models.User, merry.Error))

SetAuthenticateInvocation configures IdentityProvider.Authenticate to return the given results when called with the given parameters If no match is found for an invocation the result(s) of the fallback function are returned

func (*FakeIdentityProvider) SetAuthenticateStub added in v0.3.1

func (_f2 *FakeIdentityProvider) SetAuthenticateStub(ident3 models.User, ident4 merry.Error)

SetAuthenticateStub configures IdentityProvider.Authenticate to always return the given values

type IdentityProvider added in v0.3.0

type IdentityProvider interface {
	Authenticate(context.Context, string) (models.User, merry.Error)
}

IdentityProvider is a service that resolves tokens into principals.

type IdentityProviderAuthenticateInvocation added in v0.3.0

type IdentityProviderAuthenticateInvocation struct {
	Parameters struct {
		Ident1 context.Context
		Ident2 string
	}
	Results struct {
		Ident3 models.User
		Ident4 merry.Error
	}
}

IdentityProviderAuthenticateInvocation represents a single call of FakeIdentityProvider.Authenticate

func NewIdentityProviderAuthenticateInvocation added in v0.3.1

func NewIdentityProviderAuthenticateInvocation(ident1 context.Context, ident2 string, ident3 models.User, ident4 merry.Error) *IdentityProviderAuthenticateInvocation

NewIdentityProviderAuthenticateInvocation creates a new instance of IdentityProviderAuthenticateInvocation

type IdentityProviderTestingT added in v0.3.0

type IdentityProviderTestingT interface {
	Error(...interface{})
	Errorf(string, ...interface{})
	Fatal(...interface{})
	Helper()
}

IdentityProviderTestingT represents the methods of "testing".T used by charlatan Fakes. It avoids importing the testing package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL