goaway

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2023 License: MIT Imports: 9 Imported by: 0

README

GoAway

A Go package so simplify JWT authentication with net/http + gorilla/mux.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCredentials = Response{Status: "error", Message: "1 or more credentials are invalid"}
	ResSuccessfulLogout   = Response{Status: "success", Message: "successfully logged out"}
)
View Source
var DefaultGoAwayConfig = GoAwayConfig{
	AccessTokenTTL:           15 * time.Minute,
	RefreshTokenTTL:          24 * time.Hour,
	EnvAccessTokenPrivateKey: "ACCESS_TOKEN_PRIVATE_KEY",
	EnvAccessTokenPublicKey:  "ACCESS_TOKEN_PUBLIC_KEY",
	CookieAccessToken:        "access_token",
	CookieRefreshToken:       "refresh_token",
	ContextPayload:           "payload",
	CookieDomain:             "",
	CookiePath:               "/",
	CookieHttpOnly:           true,
	CookieSecure:             false,
}

Functions

func GenerateAccessToken

func GenerateAccessToken[P interface{}](expiresAt time.Time, payload P, id, privateKey string) (string, error)

Returns an access token that contains the given payload of the specified type. It expires at the given expiresAt time and is encoded with RS256 using b64 encoded privateKey. The id is used as a unique identifier of the token.

func JSONResponse

func JSONResponse(w http.ResponseWriter, code int, payload interface{})

func Merge

func Merge[T interface{}](a, b T) (*T, error)

func MustParseRequest

func MustParseRequest(b io.ReadCloser, v interface{}) error

func NewCookie

func NewCookie(name, value, domain, path string, expires time.Time, httpOnly, secure bool) *http.Cookie

Types

type GoAway

type GoAway[U, P interface{}] struct {
	GoAwayFunctions[U, P]
	GoAwayConfig
}

func NewGoAway

func NewGoAway[U, P interface{}](
	UfC func(string, string) (U, error),
	UfRT func(string) (U, error),
	NPfU func(U) (P, error),
	NRTfU func(U) (string, error),
	VRTfP func(string, P) error,
	RRT func(string) error,
	configs ...GoAwayConfig,
) (*GoAway[U, P], error)

Create a new GoAway object with the given User and Payload type and the functions:

UfC: UserFromCredentials: returns the user if username and password are valid
UfRT: UserFromRefreshToken: returns the user by checking the refresh tokens owner
NPfU: NewPayloadFromuser: returns a new payload by generating it from the users data
NRTfU: NewRefreshTokenFromUser: generates a new refresh token from the users data and returns the token string
VRTfP: ValidateRefreshTokenFromPayload: compares the attached data of the refresh token to the payload
RRT: RevokeRefreshToken: revokes the refresh token

func (*GoAway[U, P]) Login

func (g *GoAway[U, P]) Login(w http.ResponseWriter, r *http.Request)

Login handler that takes username and password from the request body and generates a token pair if the credentials are valid.

func (*GoAway[U, P]) Logout

func (g *GoAway[U, P]) Logout(w http.ResponseWriter, r *http.Request)

Logout handler that revokes the refresh token and removes the access and refresh token from the cookie.

func (*GoAway[U, P]) Refresh

func (g *GoAway[U, P]) Refresh(w http.ResponseWriter, r *http.Request)

Refresh handler revokes the old refresh token and generates a new token pair from the old pair.

func (*GoAway[U, P]) ValidateAccessToken

func (g *GoAway[U, P]) ValidateAccessToken(next http.Handler) http.Handler

Middleware that validates the access token from the cookie and attaches its paylod to the context.

type GoAwayClaims

type GoAwayClaims[P interface{}] struct {
	Data P `json:"data"`
	jwt.RegisteredClaims
}

func ValidateAccessToken

func ValidateAccessToken[P interface{}](token, publicKey string) (*GoAwayClaims[P], error)

Returns the claims of the given jwt access token after decoding using RSA with the b64 encoded publicKey. The type of the payload in use has to be specified as a generic type.

type GoAwayConfig

type GoAwayConfig struct {
	AccessTokenTTL           time.Duration
	RefreshTokenTTL          time.Duration
	EnvAccessTokenPrivateKey string
	EnvAccessTokenPublicKey  string
	CookieAccessToken        string
	CookieRefreshToken       string
	ContextPayload           string
	CookieDomain             string
	CookiePath               string
	CookieHttpOnly           bool
	CookieSecure             bool
}

type GoAwayFunctions

type GoAwayFunctions[U, P interface{}] struct {
	UserFromCredentials             func(string, string) (U, error)
	UserFromRefreshToken            func(string) (U, error)
	NewPayloadFromUser              func(U) (P, error)
	NewRefreshTokenFromUser         func(U) (string, error)
	ValidateRefreshTokenFromPayload func(string, P) error
	RevokeRefreshToken              func(string) error
}

type LoginRequest

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

type Response

type Response struct {
	Status       string `json:"status"`
	Message      string `json:"message,omitempty"`
	AccessToken  string `json:"access_token,omitempty"`
	RefreshToken string `json:"refresh_token,omitempty"`
}

func ErrCookieIsMissing

func ErrCookieIsMissing(err error) Response

func ErrFailGenerateTokenPair

func ErrFailGenerateTokenPair(err error) Response

func ErrFailRevokeRefreshToken

func ErrFailRevokeRefreshToken(err error) Response

func ErrFailTokenRotation

func ErrFailTokenRotation(err error) Response

func ErrInvalidAccessToken

func ErrInvalidAccessToken(err error) Response

func ErrInvalidRefreshToken

func ErrInvalidRefreshToken(err error) Response

func ErrInvalidRequestBody

func ErrInvalidRequestBody(err error) Response

func ErrMethodNotAllowed

func ErrMethodNotAllowed(method string) Response

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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