auth

package
v1.13.2 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package auth provides user authentication.

Index

Constants

View Source
const TemporaryUserSessionLifetimeInSeconds = int32(2 * time.Hour / time.Second) // 2 hours (7200 seconds)

TemporaryUserSessionLifetimeInSeconds specifies the lifetime of the access token for a temporary user.

Variables

This section is empty.

Functions

func BearerTokenFromContext

func BearerTokenFromContext(ctx context.Context) string

BearerTokenFromContext retrieves a bearer token from a context set by the middleware.

func CreateNewTempSession

func CreateNewTempSession(s *database.SessionStore, userID int64) (
	accessToken string, expiresIn int32, err error,
)

CreateNewTempSession creates a new session for a temporary user.

func GenerateKey

func GenerateKey() (string, error)

GenerateKey generate a random string that can be used as an access token for a temporary user's session. The entropy of the generated string (assuming "crypto/rand" is well implemented) is 36^32, so ~165 bits.

func GetAuthErrorCodeFromError added in v1.9.3

func GetAuthErrorCodeFromError(err error) int

GetAuthErrorCodeFromError gets the HTTP error code.

func GetOAuthConfig

func GetOAuthConfig(config *viper.Viper) *oauth2.Config

GetOAuthConfig generates the OAuth2 config from a configuration.

func MockUserMiddleware

func MockUserMiddleware(user *database.User) func(next http.Handler) http.Handler

MockUserMiddleware is a middleware to be used to mock a fixed user in the context.

func UserFromContext

func UserFromContext(ctx context.Context) *database.User

UserFromContext retrieves a user from a context set by the middleware.

func UserMiddleware

func UserMiddleware(service GetStorer) func(next http.Handler) http.Handler

UserMiddleware is a middleware retrieving a user from the request content. It takes the access token from the 'Authorization' header and loads the user info from the DB.

func ValidatesUserAuthentication added in v1.9.0

func ValidatesUserAuthentication(service GetStorer, w http.ResponseWriter, r *http.Request) (context.Context, bool, error)

ValidatesUserAuthentication checks the authentication in the Authorization header and in the "access_token" cookie. It returns:

  • A request context with the user authenticated on success
  • Whether the authentication was a success
  • The reason why the user couldn't be authenticated

Types

type GetStorer added in v1.3.0

type GetStorer interface {
	GetStore(r *http.Request) *database.DataStore
}

GetStorer is an interface allowing to get a data store bound to the context of the given request.

type SessionCookieAttributes

type SessionCookieAttributes struct {
	UseCookie bool
	Secure    bool
	SameSite  bool
	Domain    string
	Path      string
}

SessionCookieAttributes represents attributes of the session cookie.

func ParseSessionCookie

func ParseSessionCookie(r *http.Request) (accessToken string, cookieAttributes SessionCookieAttributes)

ParseSessionCookie parses the 'access_token' cookie (if given) and returns the access token among with cookie attributes.

func SessionCookieAttributesFromContext

func SessionCookieAttributesFromContext(ctx context.Context) *SessionCookieAttributes

SessionCookieAttributesFromContext retrieves session cookie attributes from a context set by the middleware. The nil result means the middleware hasn't been called.

func (*SessionCookieAttributes) SessionCookie

func (attributes *SessionCookieAttributes) SessionCookie(token string, secondsUntilExpiry int32) *http.Cookie

SessionCookie constructs a session cookie with the given attributes.

Jump to

Keyboard shortcuts

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