Documentation
¶
Index ¶
- Variables
- type DefaultService
- func (s *DefaultService) Login(ctx context.Context, user, pass string) (token string, err error)
- func (s *DefaultService) Logout(ctx context.Context, user, token string) (err error)
- func (s *DefaultService) Signup(ctx context.Context, user, pass string) (err error)
- func (s *DefaultService) Validate(ctx context.Context, user, token string) (err error)
- type HTTPServer
- type Repository
- type SQLiteRepository
- func (r *SQLiteRepository) Auth(ctx context.Context, user, pass string) (token string, err error)
- func (r *SQLiteRepository) Create(ctx context.Context, user, pass string) error
- func (r *SQLiteRepository) Deauth(ctx context.Context, user, token string) error
- func (r *SQLiteRepository) Validate(ctx context.Context, user, token string) error
- type Service
Constants ¶
This section is empty.
Variables ¶
var ErrBadAuth = errors.New("bad auth")
ErrBadAuth is returned when authentication fails for any reason.
Functions ¶
This section is empty.
Types ¶
type DefaultService ¶
type DefaultService struct {
// contains filtered or unexported fields
}
DefaultService provides authentication via a repository (DB). It's a very thin layer around the repository.
func NewDefaultService ¶
func NewDefaultService(repo Repository) *DefaultService
NewDefaultService returns a usable service, wrapping a repository.
func (*DefaultService) Login ¶
Login logs the user in, if the pass is correct. The returned token should be passed to Logout or Validate.
func (*DefaultService) Logout ¶
func (s *DefaultService) Logout(ctx context.Context, user, token string) (err error)
Logout logs the user out, if the token is valid.
type HTTPServer ¶
type HTTPServer struct {
// contains filtered or unexported fields
}
HTTPServer wraps a Service and implements http.Handler.
func NewHTTPServer ¶
func NewHTTPServer(service Service) *HTTPServer
NewHTTPServer returns an HTTPServer wrapping the Service.
func (*HTTPServer) ServeHTTP ¶
func (s *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler, delegating to the mux.Router.
type Repository ¶
type Repository interface { Create(ctx context.Context, user, pass string) error Auth(ctx context.Context, user, pass string) (token string, err error) Deauth(ctx context.Context, user, token string) error Validate(ctx context.Context, user, token string) error }
Repository models the data access layer required by the auth service. It's very similar to the service interface, because authentication doesn't involve much business logic.
type SQLiteRepository ¶
type SQLiteRepository struct {
// contains filtered or unexported fields
}
SQLiteRepository for persistence of user credential data.
func NewSQLiteRepository ¶
func NewSQLiteRepository(urn string) (*SQLiteRepository, error)
NewSQLiteRepository connects to the DB represented by URN.
func (*SQLiteRepository) Auth ¶
Auth a user, if the pass is correct, and return a token. If the user is already authed, overwrites the token.
func (*SQLiteRepository) Create ¶
func (r *SQLiteRepository) Create(ctx context.Context, user, pass string) error
Create a user with associated password. The user still needs to log in.
type Service ¶
type Service interface { Signup(ctx context.Context, user, pass string) error Login(ctx context.Context, user, pass string) (token string, err error) Logout(ctx context.Context, user, token string) error Validate(ctx context.Context, user, token string) error }
Service describes the expected behavior of the authentication service. Users can create accounts, log in, and log out; other services can validate user tokens (sessions) that they've received.