Documentation ¶
Index ¶
- Variables
- func GenerateAccessToken[P interface{}](expiresAt time.Time, payload P, id, privateKey string) (string, error)
- func JSONResponse(w http.ResponseWriter, code int, payload interface{})
- func Merge[T interface{}](a, b T) (*T, error)
- func MustParseRequest(b io.ReadCloser, v interface{}) error
- func NewCookie(name, value, domain, path string, expires time.Time, httpOnly, secure bool) *http.Cookie
- type GoAway
- type GoAwayClaims
- type GoAwayConfig
- type GoAwayFunctions
- type LoginRequest
- type Response
- func ErrCookieIsMissing(err error) Response
- func ErrFailGenerateTokenPair(err error) Response
- func ErrFailRevokeRefreshToken(err error) Response
- func ErrFailTokenRotation(err error) Response
- func ErrInvalidAccessToken(err error) Response
- func ErrInvalidRefreshToken(err error) Response
- func ErrInvalidRequestBody(err error) Response
- func ErrMethodNotAllowed(method string) Response
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidCredentials = Response{Status: "error", Message: "1 or more credentials are invalid"} ResSuccessfulLogout = Response{Status: "success", Message: "successfully logged out"} )
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 MustParseRequest ¶
func MustParseRequest(b io.ReadCloser, v interface{}) error
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.
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 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 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"` }