oneauth

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2025 License: Apache-2.0 Imports: 12 Imported by: 3

README

oneauth

A simple pluggable auth framework for go apps and services.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicUser

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

func (*BasicUser) Id

func (b *BasicUser) Id() string

type Middleware

type Middleware struct {
	UserParamName      string
	CallbackURLParam   string
	SessionGetter      func(r *http.Request, param string) any
	GetRedirURL        func(r *http.Request) string
	DefaultRedirectURL string
	VerifyToken        func(tokenString string) (loggedInUserId string, token any, err error)
}

func (*Middleware) EnsureReasonableDefaults

func (a *Middleware) EnsureReasonableDefaults()

*

  • Ensures that config values have reasonable defaults.

func (*Middleware) EnsureUser

func (a *Middleware) EnsureUser(next http.Handler) http.Handler

func (*Middleware) ExtractUser

func (a *Middleware) ExtractUser(next http.Handler) http.Handler

*

  • Fetches the user from the request and loads the UserId and User variables
  • available for other handlers. *
  • Note this does not perform any redirects if a valid user does not exist.
  • To also enforce a user exists, use the EnsureUser handler which both
  • calls ExgractUser and ensures that user is logged in.

func (*Middleware) GetLoggedInUserId

func (a *Middleware) GetLoggedInUserId(r *http.Request) string

Get the ID of the logged in user from the current request

type OneAuth

type OneAuth struct {
	Session    *scs.SessionManager
	Middleware Middleware

	// Optional name that can be used as a prefix for all required vars
	AppName string

	// Name of the session variable where the auth token is stored
	AuthTokenSessionVar string

	// Must be passed in
	UserStore UserStore

	// All the domains where the auth token cookies will be set on a login success or logout
	CookieDomains []string

	// A function that can validate username/passwords
	ValidateUsernamePassword func(username string, password string) bool

	// JWT related fields
	JwtIssuer    string
	JWTSecretKey string
	// contains filtered or unexported fields
}

func New

func New() *OneAuth

func (*OneAuth) AddAuth added in v0.0.2

func (a *OneAuth) AddAuth(prefix string, handler http.Handler) *OneAuth

func (*OneAuth) EnsureDefaults

func (a *OneAuth) EnsureDefaults() *OneAuth

func (*OneAuth) Handler added in v0.0.2

func (a *OneAuth) Handler() http.Handler

func (*OneAuth) SaveUserAndRedirect

func (a *OneAuth) SaveUserAndRedirect(authtype, provider string, token *oauth2.Token, userInfo map[string]interface{}, w http.ResponseWriter, r *http.Request)

*

  • Called by the oauth callback handler with auth token and user info after
  • a successful auth flow and redirect. *
  • Here is our opportunity to:
  • 1. Create a userId that is unique to our system based on userInfo
  • 2. Set the right session cookies from this.

type User

type User interface {
	Id() string
}

type UserStore

type UserStore interface {
	GetUserByID(userId string) (User, error)
	EnsureAuthUser(authtype string, provider string, token *oauth2.Token, userInfo map[string]any) (User, error)
}

Directories

Path Synopsis
cmd
oneauth module
grpc module
stores
gae module
gorm module

Jump to

Keyboard shortcuts

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