auth

package
v0.0.0-...-2d572df Latest Latest
Warning

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

Go to latest
Published: May 28, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildHandler

func BuildHandler(next func(*Claims, http.ResponseWriter, *http.Request) error) http.Handler

BuildHandler builds a handlerutils.Handler and passes in the *Claims if set, returns http.StatusUnauthorized if not.

func TestMiddleware

func TestMiddleware(claim *Claims) func(http.Handler) http.Handler

TestMiddleware will return a middleware which injects the given claim into requests.

Types

type Auth

type Auth struct {
	Providers []Provider
}

Auth contains the signing key for generation signed tokens

func (*Auth) Middleware

func (c *Auth) Middleware(next http.Handler) http.Handler

Middleware ensures that a request is authentic before passing the request on to the next middleware in the stack. It will inject a Claim struct into the request context on successful authentication This Claim can be retrieved in downsteam handlers via UserFromContext.

func (*Auth) Provider

func (c *Auth) Provider(r *http.Request) (*Claims, bool)

Provider returns a matching provider for a request, if any

type Claims

type Claims struct {
	UserID string `json:"user_id"`
	Name   string
}

Claims represents the custom JWT Claims struct

func UserFromContext

func UserFromContext(ctx context.Context) (*Claims, bool)

UserFromContext fetches the Claim from the current context

type CreateUser

type CreateUser struct {
	GithubID string `json:"github_id"`
	Name     string
}

CreateUser is the struct containing all parameters necessary to create a user

type Provider

type Provider interface {
	Valid(*http.Request) bool
	Authenticate(*http.Request) (*Claims, bool)
}

The Provider interface describes and authentication provider, it contains two methods: - Valid which checks whether the given request is attempting to authenticate with a given provider - Authenticate which attempts to authenticate the request

type SQLUserStore

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

SQLUserStore is an SQL implementation of UserStore

func NewSQLUserStore

func NewSQLUserStore(db *hnysqlx.DB) *SQLUserStore

NewSQLUserStore configures a new SQLUserStore

func TestSQLUserStore

func TestSQLUserStore(now time.Time, id string, db *hnysqlx.DB) *SQLUserStore

TestSQLUserStore builds a new test sql user store

func (*SQLUserStore) Create

func (s *SQLUserStore) Create(cu *CreateUser) (*User, error)

Create persists a new user to the store

func (*SQLUserStore) Get

func (s *SQLUserStore) Get(id string) (*User, error)

Get fetches a user from the store based on their ID

func (*SQLUserStore) GetByGithubID

func (s *SQLUserStore) GetByGithubID(id string) (*User, error)

GetByGithubID fetches a user from the store based on their GithubID

type User

type User struct {
	ID        string `json:"id"`
	GithubID  string `json:"githubId" db:"github_id"`
	Name      string
	CreatedAt time.Time `json:"createdAt" db:"created_at"`
}

User represents an auth user

type UserStore

type UserStore interface {
	Get(string) (*User, error)
	GetByGithubID(string) (*User, error)
	Create(*CreateUser) (*User, error)
}

UserStore defines the interface to manage users

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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