Documentation
¶
Index ¶
- Constants
- func EnsureAccountHandler(authenticator Authenticator, next go_http.Handler) go_http.Handler
- func RegisterAuthenticator(ctx context.Context, scheme string, init_func AuthenticatorInitializationFunc) error
- func Schemes() []string
- type Account
- type AccountNotExist
- type Authenticator
- func NewAuthenticator(ctx context.Context, uri string) (Authenticator, error)
- func NewJWTAuthenticator(ctx context.Context, uri string) (Authenticator, error)
- func NewNoneAuthenticator(ctx context.Context, uri string) (Authenticator, error)
- func NewNullAuthenticator(ctx context.Context, uri string) (Authenticator, error)
- func NewSharedSecretAuthenticator(ctx context.Context, uri string) (Authenticator, error)
- type AuthenticatorInitializationFunc
- type BasicAccount
- type JWTAuthenticator
- func (a *JWTAuthenticator) GetAccountForRequest(req *http.Request) (Account, error)
- func (a *JWTAuthenticator) SigninHandler() http.Handler
- func (a *JWTAuthenticator) SignoutHandler() http.Handler
- func (a *JWTAuthenticator) SignupHandler() http.Handler
- func (a *JWTAuthenticator) WrapHandler(next http.Handler) http.Handler
- type JWTAuthenticatorClaims
- type NoneAuthenticator
- func (a *NoneAuthenticator) GetAccountForRequest(req *http.Request) (Account, error)
- func (a *NoneAuthenticator) SigninHandler() http.Handler
- func (a *NoneAuthenticator) SignoutHandler() http.Handler
- func (a *NoneAuthenticator) SignupHandler() http.Handler
- func (a *NoneAuthenticator) WrapHandler(h http.Handler) http.Handler
- type NotAuthorized
- type NotLoggedIn
- type NullAuthenticator
- func (a *NullAuthenticator) GetAccountForRequest(req *http.Request) (Account, error)
- func (a *NullAuthenticator) SetLogger(logger *log.Logger)
- func (a *NullAuthenticator) SigninHandler() http.Handler
- func (a *NullAuthenticator) SignoutHandler() http.Handler
- func (a *NullAuthenticator) SignupHandler() http.Handler
- func (a *NullAuthenticator) WrapHandler(h http.Handler) http.Handler
- type SharedSecretAuthenticator
- func (a *SharedSecretAuthenticator) GetAccountForRequest(req *http.Request) (Account, error)
- func (a *SharedSecretAuthenticator) SigninHandler() http.Handler
- func (a *SharedSecretAuthenticator) SignoutHandler() http.Handler
- func (a *SharedSecretAuthenticator) SignupHandler() http.Handler
- func (a *SharedSecretAuthenticator) WrapHandler(next http.Handler) http.Handler
Constants ¶
const AUTHORIZATION_HEADER string = "Authentication"
const SHARED_SECRET_ACCOUNT_ID int64 = -1
SHARED_SECRET_ACCOUNT_ID is the account ID used for `Account` instances when shared secret authentication validates.
const SHARED_SECRET_ACCOUNT_NAME string = "sharedsecret"
SHARED_SECRET_ACCOUNT_NAME is the account name used for `Account` instances when shared secret authentication validates.
const SHARED_SECRET_HEADER string = "X-Shared-Secret"
SHARED_SECRET_HEADER is the name of the HTTP header to check for "shared secret" authentication.
Variables ¶
This section is empty.
Functions ¶
func EnsureAccountHandler ¶
func EnsureAccountHandler(authenticator Authenticator, next go_http.Handler) go_http.Handler
EnsureAccountHandler is a middleware `net/http` handler that wraps 'next' and ensures that the authenticator.GetAccountForRequest method does not return an error.
func RegisterAuthenticator ¶
func RegisterAuthenticator(ctx context.Context, scheme string, init_func AuthenticatorInitializationFunc) error
RegisterAuthenticator registers 'scheme' as a key pointing to 'init_func' in an internal lookup table used to create new `Authenticator` instances by the `NewAuthenticator` method.
Types ¶
type Account ¶
type Account interface { // The unique ID associated with this account. Id() int64 // The name associated with this account. Name() string }
type Account is an interface that defines minimal information for an account.
func NewAccount ¶
NewAccount returns a new instance of `BasicAccount` (which implements the `Account` interface) for 'id' and 'name'.
type AccountNotExist ¶
type AccountNotExist struct{}
AccountNotExist defines a well-known error for signaling that a given account does not exist.
func (AccountNotExist) Error ¶
func (e AccountNotExist) Error() string
Error() returns a human-readable representation of the `AccountNotExist` error.
type Authenticator ¶
type Authenticator interface { // WrapHandler wraps a `http.Handler` with any implementation-specific middleware. WrapHandler(http.Handler) http.Handler // GetAccountForRequest returns an `Account` instance for an HTTP request. GetAccountForRequest(*http.Request) (Account, error) // SigninHandler returns a `http.Handler` for implementing account signin. SigninHandler() http.Handler // SignoutHandler returns a `http.Handler` for implementing account signout. SignoutHandler() http.Handler // SignupHandler returns a `http.Handler` for implementing account signups. SignupHandler() http.Handler }
type Authenticator is a simple interface for enforcing authentication in HTTP handlers.
func NewAuthenticator ¶
func NewAuthenticator(ctx context.Context, uri string) (Authenticator, error)
NewAuthenticator returns a new `Authenticator` instance configured by 'uri'. The value of 'uri' is parsed as a `url.URL` and its scheme is used as the key for a corresponding `AuthenticatorInitializationFunc` function used to instantiate the new `Authenticator`. It is assumed that the scheme (and initialization function) have been registered by the `RegisterAuthenticator` method.
func NewJWTAuthenticator ¶
func NewJWTAuthenticator(ctx context.Context, uri string) (Authenticator, error)
NewJWTAuthenticator implements the Authenticator interface to ensure that requests contain a `Authorization: Bearer {JWT_TOKEN}` HTTP header configured by 'uri' which is expected to take the form of:
jwt://{SECRET}
Where {SECRET} is expected to be the shared JWT signing secret passed by HTTP requests. Or:
jwt://runtimevar?runtimevar-uri={GOCLOUD_DEV_RUNTIMEVAR_URI}
Where {GOCLOUD_DEV_RUNTIMEVAR_URI} is a valid `gocloud.dev/runtimevar` URI used to dereference the JWT signing secret. Under the hood this method using the `github.com/sfomuseum/runtimevar.StringVar` method to dereference runtimevar URIs.
By default a `JWTAuthenticator` instance looks for JWT Bearer tokens in the HTTP "Authorization" header. This behaviour can be customized by passing an "authorization-header" query parameter in 'uri'. For example:
jwt://?authorization-header=X-Custom-AuthHeader
func NewNoneAuthenticator ¶
func NewNoneAuthenticator(ctx context.Context, uri string) (Authenticator, error)
NewNoneAuthenticator implements the Authenticator interface that always returns a "not authorized" error. configured by 'uri' which is expected to take the form of:
none://
func NewNullAuthenticator ¶
func NewNullAuthenticator(ctx context.Context, uri string) (Authenticator, error)
NewNullAuthenticator implements the Authenticator interface such that no authentication is performed configured by 'uri' which is expected to take the form of:
null://
func NewSharedSecretAuthenticator ¶
func NewSharedSecretAuthenticator(ctx context.Context, uri string) (Authenticator, error)
NewSharedSecretAuthenticator implements the Authenticator interface to ensure that requests contain a `X-Shared-Secret` HTTP header configured by 'uri' which is expected to take the form of:
sharedsecret://{SECRET}
Where {SECRET} is expected to be the shared secret passed by HTTP requests.
type AuthenticatorInitializationFunc ¶
type AuthenticatorInitializationFunc func(ctx context.Context, uri string) (Authenticator, error)
AuthenticatorInitializationFunc is a function defined by individual authenticator package and used to create an instance of that authenticator
type BasicAccount ¶
type BasicAccount struct { Account `json:",omitempty"` // The unique ID associated with this account. AccountId int64 `json:"id"` // The name associated with this account. AccountName string `json:"name"` }
BasicAccount is the simplest (most basic) implementation of the `Account` interface for wrapping a unique account ID and an account name.
func (*BasicAccount) Id ¶
func (a *BasicAccount) Id() int64
Returns the unique ID associated with 'a'.
func (*BasicAccount) Name ¶
func (a *BasicAccount) Name() string
Returns the name associated with 'a'.
type JWTAuthenticator ¶
type JWTAuthenticator struct { Authenticator // contains filtered or unexported fields }
type JWTAuthenticator implements the Authenticator interface to require a valid JSON Web Token (JWT) be passed with all requests.
func (*JWTAuthenticator) GetAccountForRequest ¶
func (a *JWTAuthenticator) GetAccountForRequest(req *http.Request) (Account, error)
GetAccountForRequest returns an stub `Account` instance for requests that contain a valid `Authorization: Bearer {JWT_TOKEN}` HTTP header (or a custom header if defined in the `JWTAuthenticator` constuctor URI).
func (*JWTAuthenticator) SigninHandler ¶
func (a *JWTAuthenticator) SigninHandler() http.Handler
SigninHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*JWTAuthenticator) SignoutHandler ¶
func (a *JWTAuthenticator) SignoutHandler() http.Handler
SignoutHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*JWTAuthenticator) SignupHandler ¶
func (a *JWTAuthenticator) SignupHandler() http.Handler
SignoutHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*JWTAuthenticator) WrapHandler ¶
func (a *JWTAuthenticator) WrapHandler(next http.Handler) http.Handler
WrapHandler returns
type JWTAuthenticatorClaims ¶
type JWTAuthenticatorClaims struct { // The unique ID associated with this account. AccountId int64 `json:"account_id"` // The name associated with this account. AccountName string `json:"account_name"` jwt.RegisteredClaims }
type JWTAuthenticatorClaims are the custom claims for Authorization requests.
type NoneAuthenticator ¶
type NoneAuthenticator struct {
Authenticator
}
type NoneAuthenticator implements the Authenticator interface that always returns a "not authorized" error.
func (*NoneAuthenticator) GetAccountForRequest ¶
func (a *NoneAuthenticator) GetAccountForRequest(req *http.Request) (Account, error)
GetAccountForRequest returns an stub `Account` instance.
func (*NoneAuthenticator) SigninHandler ¶
func (a *NoneAuthenticator) SigninHandler() http.Handler
SigninHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*NoneAuthenticator) SignoutHandler ¶
func (a *NoneAuthenticator) SignoutHandler() http.Handler
SignoutHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*NoneAuthenticator) SignupHandler ¶
func (a *NoneAuthenticator) SignupHandler() http.Handler
SignoutHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*NoneAuthenticator) WrapHandler ¶
func (a *NoneAuthenticator) WrapHandler(h http.Handler) http.Handler
WrapHandler returns 'h' unchanged.
type NotAuthorized ¶
type NotAuthorized struct{}
NotAuthorized defines a well-known error for signaling that the request is not authorized.
func (NotAuthorized) Error ¶
func (e NotAuthorized) Error() string
Error() returns a human-readable representation of the `NotAuthorized` error.
type NotLoggedIn ¶
type NotLoggedIn struct{}
NotLoggedIn defines a well-known error for signaling that the account is not logged in.
func (NotLoggedIn) Error ¶
func (e NotLoggedIn) Error() string
Error() returns a human-readable representation of the `NotLoggedIn` error.
type NullAuthenticator ¶
type NullAuthenticator struct {
Authenticator
}
type NullAuthenticator implements the Authenticator interface such that no authentication is performed.
func (*NullAuthenticator) GetAccountForRequest ¶
func (a *NullAuthenticator) GetAccountForRequest(req *http.Request) (Account, error)
GetAccountForRequest returns an stub `Account` instance.
func (*NullAuthenticator) SetLogger ¶
func (a *NullAuthenticator) SetLogger(logger *log.Logger)
SetLogger is a no-op and does nothing.
func (*NullAuthenticator) SigninHandler ¶
func (a *NullAuthenticator) SigninHandler() http.Handler
SigninHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*NullAuthenticator) SignoutHandler ¶
func (a *NullAuthenticator) SignoutHandler() http.Handler
SignoutHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*NullAuthenticator) SignupHandler ¶
func (a *NullAuthenticator) SignupHandler() http.Handler
SignoutHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*NullAuthenticator) WrapHandler ¶
func (a *NullAuthenticator) WrapHandler(h http.Handler) http.Handler
WrapHandler returns 'h' unchanged.
type SharedSecretAuthenticator ¶
type SharedSecretAuthenticator struct { // contains filtered or unexported fields }
type SharedSecretAuthenticator implements the Authenticator interface to require a simple shared secret be passed with all requests. This is not a sophisticated handler. There are no nonces or hashing of requests or anything like that. It is a bare-bones supplementary authentication handler for environments that already implement their own measures of access control.
func (*SharedSecretAuthenticator) GetAccountForRequest ¶
func (a *SharedSecretAuthenticator) GetAccountForRequest(req *http.Request) (Account, error)
GetAccountForRequest returns an stub `Account` instance for requests that contain a valid `X-Shared-Secret` HTTP header.
func (*SharedSecretAuthenticator) SigninHandler ¶
func (a *SharedSecretAuthenticator) SigninHandler() http.Handler
SigninHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*SharedSecretAuthenticator) SignoutHandler ¶
func (a *SharedSecretAuthenticator) SignoutHandler() http.Handler
SignoutHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*SharedSecretAuthenticator) SignupHandler ¶
func (a *SharedSecretAuthenticator) SignupHandler() http.Handler
SignoutHandler returns an `http.Handler` instance that returns an HTTP "501 Not implemented" error.
func (*SharedSecretAuthenticator) WrapHandler ¶
func (a *SharedSecretAuthenticator) WrapHandler(next http.Handler) http.Handler
WrapHandler returns