auth

package module
v0.1.25-development Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

README

Branche Online Auth

Authentication module for a backend of api that supports frontend web and mobile applications that need to implement token and or database backed session authentication for user profiles stored locally or with external identity providers. Data storage is managed by adapters and the module provides features for OAuth, Credentials, Email, and JWT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandomFloat32

func RandomFloat32() float32

Generates a random 32 bit floating number between 0 and 1. The function uses the crypto/rand package to generate 23 random bits for the mantissa

func RandomFloat64

func RandomFloat64() float64

Generates a random 64 bit floating number between 0 and 1. The function uses the crypto/rand package to generate 52 random bits for the mantissa

func RandomInteger

func RandomInteger(max *big.Int) *big.Int

Generates a random integer value of arbitrary size that is less than the given max value. The function uses a rejection sampling technique based on comparing the random value after it is bit shifted with the max value.

func RandomString

func RandomString(enc *EncodingScheme, strLen uint, charSubset *string) (string, error)

Generates either a base10, 16, 32 or 64 encoded string of the specified length. The charSubset parameter is used when the encoding scheme is base32 or base64. It allows the user to specify a custom character set for the encoding. If charSubset is nil, the default encoding scheme will be used.

func RandomUint32

func RandomUint32(max uint32) uint32

Generates a random unsigned 32bit integer between [0, max). `max` should not be a very large number.

func RandomUint64

func RandomUint64(max uint64) uint64

Generates a random unsigned 32bit integer between [0, max). `max` should not be a very large number.

Types

type AccountManager

type AccountManager interface {
	CreateUser(user UserData) (*User, error)
	ReadUser(uid UID) (*User, error)
	UpdateUser(uid UID, data *UserData) error
	ConnectProfile(uid UID, profile *UserProfile) error
	DisconnectProfile(uid UID, prid string) error
	DeleteUser(uid UID) error
}

type AuthenticationAdapter

type AuthenticationAdapter interface {
	TokenMaker
	SessionManager
	AccountManager
}

type Digest

type Digest string

Digest is a primitive of the string data type representing a hash digest.

type EncodingScheme

type EncodingScheme string
const (
	HEX EncodingScheme = "hex"
	B32 EncodingScheme = "base32"
	B64 EncodingScheme = "base64"
	B10 EncodingScheme = "base10"
)

type Key

type Key string

Key is a primitive of the string data type representing a secret key.

func GenerateSecret

func GenerateSecret(keySize uint) (Key, error)

GenerateSecret generates a random secret key of the specified size in bytes.

type KeyPair

type KeyPair struct {
	PrivateKey Key
	PublicKey  Key
}

KeyPair is a struct that contains a private key and a public key.

type OTP

type OTP struct {
	Token  Token
	UID    UID
	Expiry Time
}

type ProviderType

type ProviderType string
const (
	OIDCIDP      ProviderType = "oidc"
	SAMLIDP      ProviderType = "saml"
	LDAPIDP      ProviderType = "ldap"
	LocalCredIDP ProviderType = "local"
	OtherIDP     ProviderType = "other"
)

type SID

type SID string

type Session

type Session struct {
	ID        SID
	UserId    UID
	ExpiresAt Time
}

type SessionManager

type SessionManager interface {
	CreateSession(token Token, user *User) (*Session, error)
	ReadSession(token Token) (*Session, error)
	UpdateSession(expiry Time) error
	DeleteSession(sid SID) error
	DeleteUserSessions(uid UID) error
}

type Time

type Time time.Duration

Time is a primitive of the time data type representing a duration in nanoseconds.

func NewTime

func NewTime(hour, min, sec, nsec int) Time

NewTime is a constructor for Time and returns new Time.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler to convert Time to json serialization.

func (Time) String

func (t Time) String() string

String implements fmt.Stringer interface.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler to deserialize json data.

type Token

type Token string

type TokenMaker

type TokenMaker interface {
	GenerateToken() (Token, error)
	SignToken(token Token, secret Key) (Digest, error)
	VerifyToken(token Token, signature Digest, publicKey Key) (bool, error)
	CreateOTP(uid UID, expiry Time) (*OTP, error)
	ReadOTP(token Token) (UID, error)
	DestroyOTP(token Token) error
}

type UID

type UID string

type User

type User interface {
	ID() UID
	Status() UserStatus
	DisplayName() string
}

type UserData

type UserData interface {
	any
}

type UserProfile

type UserProfile interface {
	ID() string
	UserId() UID
	IdPType() ProviderType
	IdPName() string
	IdPAccountId() string
}

type UserStatus

type UserStatus int
const (
	UNKNOWN UserStatus = iota
	ACTIVE
	PENDING
	DISABLED
	ARCHIVED
)

Jump to

Keyboard shortcuts

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