auth

package
v0.0.0-...-a012da0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2018 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func (s *DefaultService) Login(ctx context.Context, user, pass string) (token string, err error)

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.

func (*DefaultService) Signup

func (s *DefaultService) Signup(ctx context.Context, user, pass string) (err error)

Signup creates a user with the given pass. The user still needs to login.

func (*DefaultService) Validate

func (s *DefaultService) Validate(ctx context.Context, user, token string) (err error)

Validate returns a nil error if the user is logged in and provides the correct token.

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

func (r *SQLiteRepository) Auth(ctx context.Context, user, pass string) (token string, err error)

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.

func (*SQLiteRepository) Deauth

func (r *SQLiteRepository) Deauth(ctx context.Context, user, token string) error

Deauth a user, if the token is correct.

func (*SQLiteRepository) Validate

func (r *SQLiteRepository) Validate(ctx context.Context, user, token string) error

Validate the user and token.

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.

Jump to

Keyboard shortcuts

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