oneauth

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2025 License: Apache-2.0 Imports: 14 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

func (*BasicUser) Profile added in v0.0.9

func (b *BasicUser) Profile() map[string]any

type HandleUserFunc added in v0.0.9

type HandleUserFunc func(authtype string, provider string, token *oauth2.Token, userInfo map[string]any, w http.ResponseWriter, r *http.Request)

type LocalAuth added in v0.0.9

type LocalAuth struct {
	// A function that can validate username/passwords
	ValidateUsernamePassword func(username string, password string) (User, error)
	Provider                 string
	UsernameField            string
	PasswordField            string
	HandleUser               HandleUserFunc
}

Allows local username/password based authentication

func (*LocalAuth) ServeHTTP added in v0.0.9

func (a *LocalAuth) ServeHTTP(w http.ResponseWriter, r *http.Request)

For now we only accept JSON encoded username/password (as a proxy for Grafana)

type Middleware

type Middleware struct {
	AuthTokenHeaderName string
	AuthTokenCookieName string
	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

	// JWT related fields
	JwtIssuer    string
	JWTSecretKey string

	// How long is a session cookie valid for.  Defaults to 1 day
	SessionTimeoutInSeconds int
	// contains filtered or unexported fields
}

func New

func New(appName string) *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]any, 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
	Profile() map[string]any
}

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