Documentation ¶
Index ¶
- Constants
- Variables
- func ConfigMaster(id string, secret string)
- func GRPCAuthFunc(ctx context.Context) (authCtx context.Context, err error)
- func HTTPHandler(handler http.HandlerFunc) (authHandler http.HandlerFunc)
- func Hash(password string, salts ...string) string
- func IsMaster(id, secret string) (ok bool)
- func WithGroupRepository(r GroupRepository)
- func WithRBAC(r RBAC)
- func WithRepository(r Repository)
- type AWSCognitoRBAC
- type GRPCSession
- type GRPCUnaryInterceptorOverride
- type Group
- type GroupMySQLRepository
- func (repo *GroupMySQLRepository) Add(ctx context.Context, user User, role Role) (err error)
- func (repo *GroupMySQLRepository) Delete(ctx context.Context, user User, role Role) (err error)
- func (repo *GroupMySQLRepository) Find(ctx context.Context, role Role) (group Group, err error)
- func (repo *GroupMySQLRepository) Free(ctx context.Context, resource Resource) (err error)
- func (repo *GroupMySQLRepository) IsUserInAny(ctx context.Context, user User, roles Roles) (ok bool, err error)
- func (repo *GroupMySQLRepository) Resources(ctx context.Context, kind ResourceKind, user User) (roles Roles, err error)
- type GroupRepository
- type GroupRepositoryImpl
- type HTTPSession
- type Key
- type Option
- type Query
- type RBAC
- type RBACUser
- type Repository
- type Resource
- type ResourceID
- type ResourceKind
- type Role
- type RoleName
- type Roles
- type Session
- type User
Constants ¶
const ( // IgnoreUnverified and pass Auth. IgnoreUnverified = Option(1) )
const ( // UserKey for storing auth.Auth with context.WithValue(...). UserKey = Key("user") )
Variables ¶
var ( // ErrMissingUserCredentials when auth information isn't present in message. ErrMissingUserCredentials = fmt.Errorf("missing user credentials") // ErrInvalidUserCredentials when ID, Secret doesn't match that in the database. We // use this generic error by design so as to thwart malicious requests intended to // list user IDs by brute-force. ErrInvalidUserCredentials = fmt.Errorf("invalid user credentials") // ErrUserNotVerified when user hasn't verified email ID, phone, etc. ErrUserNotVerified = fmt.Errorf("user not verified") )
var AWSCognitoModule = fx.Options( fx.Provide( NewAWSCognitoRBAC, ), fx.Invoke( WithRBAC, ), )
AWSCognitoModule is an fx.Options that sets up our AWS Cognito RBAC.
var ( fmt.Errorf("user not authorized to make that request") )ErrUnauthorizedUser =
Some handy errors you can use to send out of packages that depend on auth.
var GRPCUnaryInterceptor grpc.UnaryServerInterceptor = func( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, ) (resp interface{}, err error) { if custom, ok := info.Server.(GRPCUnaryInterceptorOverride); ok { if ctx, err = custom.Auth(ctx, info.FullMethod); err != nil { return } } else if ctx, err = GRPCAuthFunc(ctx); err != nil { return } resp, err = handler(ctx, req) return }
GRPCUnaryInterceptor to Auth incoming requests.
var Module = fx.Options( fx.Provide( NewGroupMySQLRepositoryImpl, ), fx.Invoke( WithRepository, WithGroupRepository, ), )
Module is an fx.Options that includes provider (constructors) and invoke (register) functions of the package
var Owner = Role{ Name: RoleName("owner"), Resource: PlatformResource, }
Owner is the role that has access to all resources
var PlatformResource = resourceImpl{ // contains filtered or unexported fields }
PlatformResource is the top level Resource that contains all other Resources.
Functions ¶
func ConfigMaster ¶
ConfigMaster sets the details for the user that has access to everything
func GRPCAuthFunc ¶
GRPCAuthFunc matches grpc_auth.AuthFunc, in case you want to use github.com/grpc-ecosystem/go-grpc-middleware
func HTTPHandler ¶
func HTTPHandler(handler http.HandlerFunc) (authHandler http.HandlerFunc)
HTTPHandler to chain with our HandlerFuncs, performing Auth before invoking them.
func Hash ¶
Hash is a convenience function that appends the password along with any other passed salts to return an SHA1 hash.
func WithGroupRepository ¶
func WithGroupRepository(r GroupRepository)
WithGroupRepository configures the GroupRepository implementation that `auth` will refer.
func WithRBAC ¶
func WithRBAC(r RBAC)
WithRBAC configures the RBAC implementation that `auth` will refer.
func WithRepository ¶
func WithRepository(r Repository)
WithRepository configures the User Repository implementation that `auth` will refer.
Types ¶
type AWSCognitoRBAC ¶
type AWSCognitoRBAC struct {
// contains filtered or unexported fields
}
AWSCognitoRBAC implements the RBAC interface for AWS Cognito.
func (*AWSCognitoRBAC) Authenticate ¶
func (repo *AWSCognitoRBAC) Authenticate( ctx context.Context, username, accessToken string, ) (user User, ok bool, err error)
Authenticate implements Repository#Authenticate for AWS Cognito.
type GRPCSession ¶
type GRPCSession struct {
// contains filtered or unexported fields
}
GRPCSession is an implementation of Session for gRPC, and checks for User and Secret in the Auth protobuf-generated message-struct.
func NewGRPCSession ¶
func NewGRPCSession(ctx context.Context) (session GRPCSession)
NewGRPCSession is a constructor for GRPCSession. It mutates the Context pointed to, by inserting the User into it's Values when Auth() is called.
type GRPCUnaryInterceptorOverride ¶
type GRPCUnaryInterceptorOverride interface { Auth(ctx context.Context, fullMethodName string) ( authCtx context.Context, err error, ) }
GRPCUnaryInterceptorOverride is an interface a gRPC Server can implement to override the global server GRPCUnaryInterceptor installation and implement custom authentication.
type Group ¶
Group of Users with a common Role. Groups are how we interface with our persistence API to assign Roles to Users or list all Users with a given Role. In a Google Cloud Platform-like implementation, we would show the multiple groups for a given Resource and allow our end-user to configure them. In a BitBucket-like implementation, we'd mix up all the Groups for a given Resource while displaying them, with the relevant Role selected next to a Given user. This would allow us to change the Role assigned to a given User with a single click.
type GroupMySQLRepository ¶
type GroupMySQLRepository struct {
// contains filtered or unexported fields
}
GroupMySQLRepository implements GroupRepository in MySQL.
func (*GroupMySQLRepository) Add ¶
Add a User to a Group for the given Role, creating a Group if it doesn't exist. Add is an idempotent action and does nothing silently if the User already has the given Role.
func (*GroupMySQLRepository) Find ¶
Find a Group of Users that are assigned a given Role. This allows us to visually list them and allow for the end-user to reconfigure our Groups.
func (*GroupMySQLRepository) Free ¶
func (repo *GroupMySQLRepository) Free(ctx context.Context, resource Resource) ( err error, )
Free deletes all Groups attached to the given Resource. It is intended to be called at the end of the Resource's lifecycle.
func (*GroupMySQLRepository) IsUserInAny ¶
func (repo *GroupMySQLRepository) IsUserInAny(ctx context.Context, user User, roles Roles) ( ok bool, err error, )
IsUserInAny checks whether the given User has one or more of the given Roles. It's results can only be reliably consumed when `err` is `nil`.
func (*GroupMySQLRepository) Resources ¶
func (repo *GroupMySQLRepository) Resources( ctx context.Context, kind ResourceKind, user User, ) (roles Roles, err error)
Resources lists all of the Resources of a given ResourceKind that a User has access to via any Role assigned to her.
type GroupRepository ¶
type GroupRepository struct {
GroupRepositoryImpl
}
GroupRepository persists our Groups using an underlying GroupRepositoryImpl.
var Groups GroupRepository
Groups exposes our internal GroupRepository as a public API for our business layer.
func NewGroupMySQLRepositoryImpl ¶
func NewGroupMySQLRepositoryImpl(db *sql.DB) (repo GroupRepository, err error)
NewGroupMySQLRepositoryImpl is a constructor for GroupMySQLRepository.
type GroupRepositoryImpl ¶
type GroupRepositoryImpl interface { Add(ctx context.Context, user User, role Role) (err error) Delete(ctx context.Context, user User, role Role) (err error) Find(ctx context.Context, role Role) (group Group, err error) Free(ctx context.Context, resource Resource) (err error) IsUserInAny(ctx context.Context, user User, roles Roles) (ok bool, err error) Resources(ctx context.Context, kind ResourceKind, user User) ( roles Roles, err error, ) }
GroupRepositoryImpl defines an interface with which we can persist our Groups.
type HTTPSession ¶
type HTTPSession struct {
// contains filtered or unexported fields
}
HTTPSession implements Session over HTTP headers.
func NewHTTPSession ¶
func NewHTTPSession(rw http.ResponseWriter, req *http.Request) (session *HTTPSession)
NewHTTPSession is a constructor for HTTPSession.
func (*HTTPSession) Auth ¶
func (session *HTTPSession) Auth() (ctx context.Context, err error)
Auth authenticates a Session based.
func (*HTTPSession) Cancel ¶
func (session *HTTPSession) Cancel()
Cancel checks if an error has occurred thus far and writes it to the HTTP response.
type Key ¶
type Key string
Key is a non-simple type for keys in the context.Context operations by Sessions.
type Query ¶
type Query struct { UserEmail string Kind ResourceKind RoleName RoleName }
Query returns a fragment that can be used to authenticate resource access.
type RBAC ¶
type RBAC interface { Authenticate( ctx context.Context, username, password string, ) (user User, ok bool, err error) }
RBAC is an interface that any RBAC provider must implement.
type RBACUser ¶
type RBACUser struct { }
RBACUser provides stubs for User#Secret and User#IsVerified as RBAC-implementations often do not maintain these values as part of their business logic, but instead delegate it to their RBAC system.
func (RBACUser) GetIsVerified ¶
GetIsVerified stubs User#GetIsVerified.
type Repository ¶
Repository is the interface for your application's repository to implement.
type Resource ¶
type Resource interface { Identifier() (id ResourceID) Kind() (kind ResourceKind) }
Resource is an interface that must be implemented by entities.
type Role ¶
Role represents a realm of allowed actions that it allows upon a Resource. These actions themselves are 'mapped' to the Role in our business-logic, and aren't persisted. This allows us to iterate on our set of Roles without any 'migrations' upon our persisted data.
type RoleName ¶
type RoleName string
RoleName is a string-based key to ensure our Roles are named uniquely through our codebase.
type Roles ¶
type Roles []Role
Roles is a type-alias for []Role.
func RolesFor ¶
RolesFor is a convenience-constructor for constructing an array of Roles with the same name but for different Resources. This is handy when a Role 'propagates' hierarchically through a set of Resources. For example, if an "Editor" Role for an Account implies an "Editor" Role for all it's constituent entities, we can use auth.Roles to check if the User has the "Editor" Role for the Account, or the child entity being accessed. The `auth` package doesn't assume any such hierarchical relationships and may be considered more as a handy labelling tool rather than housing for actual authorization business-logic. It merely appends to a set of Groups and allows us to check on the presence of a User in an array of Roles.