auth

package
v0.0.0-...-0fda810 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddUserIDHeader

func AddUserIDHeader(r *http.Request, username string)

AddUserIDHeader adds a header to the request that represents the user ID

func Authorization

func Authorization(authorizer Authorizer) func(http.Handler) http.Handler

Authorization verifies a request has a valid token associated

func GetToken

func GetToken(r *http.Request) (string, error)

GetAuthToken extracts the auth token from a request

func GetUserIDFromRequest

func GetUserIDFromRequest(r *http.Request) (string, error)

GetUserIDFromRequest returns the user ID from the request

func NewSessionID

func NewSessionID() string

NewSessionID is used to generate unique session IDs for user authentication

func RemoveUserIDHeader

func RemoveUserIDHeader(r *http.Request)

RemoveUserIDHeader removes the header that contains the user ID

func SetAuthCookie

func SetAuthCookie(w http.ResponseWriter, tokenString string, expires time.Time)

SetAuthCookie add an auth cookie to the response writer

Types

type Authorizer

type Authorizer interface {
	GenerateSecurityString(username string) (string, time.Time, error)
	Invalidate(token string) error
	Validate(token string) (bool, string, error)
	Cleanup(cleanInterval time.Duration)
}

Authorizer is used to validate the user request

type Claim

type Claim struct {
	Username string `json:"username"`
	jwt.RegisteredClaims
}

Claim uses the standard JWT Claim to create a custom claim

type Endpoints

type Endpoints struct {
	// contains filtered or unexported fields
}

Endpoints used to authorize an end user

func NewEndpoints

func NewEndpoints(ctx context.Context, isPasswordCorrect checkPassword, token Authorizer) *Endpoints

NewEndpoints is used setup the autorization endpoints

func (*Endpoints) Register

func (e *Endpoints) Register(r *mux.Router)

Register is used to attach the authorization endpoints to a given mux router

type JWT

type JWT struct {
	// contains filtered or unexported fields
}

JWT is used to generate and verify JST tokens

func NewJWTHandler

func NewJWTHandler(keys Keys) *JWT

NewJWTHandler is used to setup a JWT authorizer

func (*JWT) Cleanup

func (j *JWT) Cleanup(cleanInterval time.Duration)

Cleanup starts a go routine that periodically clears the black list

func (*JWT) GenerateSecurityString

func (j *JWT) GenerateSecurityString(username string) (string, time.Time, error)

GenerateSecurityString generates a JWT token for the provided username

func (*JWT) Invalidate

func (j *JWT) Invalidate(token string) error

Invalidate blacklists a token so that it cannot be used anymore

func (*JWT) Validate

func (j *JWT) Validate(tknStr string) (bool, string, error)

Validate validates whether the value of the receoved key is a valid token

type Keys

type Keys interface {
	GetOne() ([]byte, error)
}

Keys is the interface that needs to be implemented to retrieve the key needed to sign a token

type LoginRequest

type LoginRequest struct {
	Username string
	Password string
	Duration int
}

LoginRequest is used to request access to the API

func (*LoginRequest) Validate

func (u *LoginRequest) Validate() error

Validate the Login Request

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session is used manage generated authentication sessions

func NewSessionHandler

func NewSessionHandler(db *sql.DB, log logger.Logger) *Session

NewSessionHandler creates a structure to handle session authentication

func (*Session) Cleanup

func (s *Session) Cleanup(cleanInterval time.Duration)

Cleanup cleans expired sessions from the store

func (*Session) GenerateSecurityString

func (s *Session) GenerateSecurityString(username string) (string, time.Time, error)

GenerateSecurityString creates a session id for the provided username

func (*Session) Invalidate

func (s *Session) Invalidate(session string) error

Invalidate removes a session ID from the list of accepted session IDs

func (*Session) Validate

func (s *Session) Validate(key string) (bool, string, error)

Validate checks if the provided session id is valid

Jump to

Keyboard shortcuts

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