utils

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	COOKIE_AUTH_KEY_ENV = "COOKIE_AUTH_KEY"
	COOKIE_ENC_KEY_ENV  = "COOKIE_ENC_KEY"
	ISSUER_URL_ENV      = "ISSUER_URL"
	MASTER_KEY_ENV      = "MASTER_KEY"

	DEFAULT_DATE_FORMAT = "2006-01-02"

	// Netlify-specific environment variables
	// https://docs.netlify.com/configure-builds/environment-variables/#deploy-urls-and-metadata
	// https://docs.netlify.com/configure-builds/environment-variables/#system-environment-variables
	CONTEXT_ENV          = "CONTEXT"
	URL_ENV              = "URL"
	DEPLOY_PRIME_URL_ENV = "DEPLOY_PRIME_URL"
)
View Source
const (
	AUTHORIZATION_CODE_TYPE TokenType = "authorization_code"
	ACCESS_TOKEN_TYPE       TokenType = "access_token"
	REFRESH_TOKEN_TYPE      TokenType = "refresh_token"
	CLIENT_CREDENTIALS_TYPE TokenType = "client_credentials"
	CUSTOM_TOKEN_TYPE       string    = "custom_token"
)
View Source
const (
	ACR_VALUE     = "acr"
	AMR_VALUE     = "amr"
	AUTH_METHOD   = "auth-method"
	AUTH_STATUS   = "auth-status"
	CLAIM_TYPE    = "claim-type"
	CONTAINS_ANY  = "contains-any"
	DATE          = "date"
	DISPLAY       = "display"
	GRANT_TYPE    = "grant-type"
	HTTPS_ORIGIN  = "https_origin"
	HTTPS_URL     = "https_url"
	JWS           = "jws"
	PKCE_METHOD   = "pkce-method"
	PROMPT        = "prompt"
	RESPONSE_MODE = "response-mode"
	RESPONSE_TYPE = "response-type"
	RESULT        = "result"
	SCOPE         = "scope"
	SUBJECT_TYPE  = "subject-type"
	TIME_GT_NOW   = "time-gt-now"
	TIME_LT_NOW   = "time-lt-now"
	TIME_GTE_NOW  = "time-gte-now"
	TIME_LTE_NOW  = "time-lte-now"
	TOKEN_TYPE    = "token-type"
)
View Source
const (
	BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
)
View Source
const MaxPasswordBytes = 1024

MaxPasswordBytes is a byte limit, not a character limit. Passwords are never trimmed, normalized, truncated, or passed through HashedString.

Variables

View Source
var (
	ErrInvalidPassword     = errors.New("password must contain between 1 and 1024 bytes")
	ErrInvalidPasswordHash = errors.New("invalid or unsupported password hash")
)

Functions

func ContainsValue

func ContainsValue[T comparable](arr []T, value T) bool

func Decrypt

func Decrypt(data []byte) ([]byte, error)

func DummyPasswordHash added in v0.2.0

func DummyPasswordHash() (string, error)

DummyPasswordHash is initialized once on demand, never by a panicking init. Initialize at service startup and verify against it for unknown/passwordless users, discarding the comparison result. Never use it to authenticate a user.

func Encrypt

func Encrypt(data []byte) ([]byte, error)

func GeneratePKCEChallenge

func GeneratePKCEChallenge(verifier string, isHashed ...bool) (challenge string)

func GetDeploymentURL

func GetDeploymentURL() string

GetDeploymentURL returns the deployment URL based on the CONTEXT environment variable. In production, it uses the URL environment variable. In other contexts (like deploy previews), it uses the DEPLOY_PRIME_URL variable.

func Hash

func Hash(data []byte) (hashed [32]byte)

func HashPassword added in v0.2.0

func HashPassword(password string) (string, error)

HashPassword encodes a random-salted Argon2id hash in PHC format.

func HashPasswordContext added in v0.2.0

func HashPasswordContext(ctx context.Context, password string) (string, error)

HashPasswordContext permits cancellation while waiting for a KDF slot. An already-running Argon2 computation cannot be interrupted.

func HashS256

func HashS256(data []byte) (hashed [32]byte)

func NewCookieStore

func NewCookieStore() *sessions.CookieStore

func NewCustomDecoder

func NewCustomDecoder() *schema.Decoder

func NewCustomEncoder

func NewCustomEncoder() *schema.Encoder

func NewCustomValidator

func NewCustomValidator() *validator.Validate

func ParseClientIP

func ParseClientIP(r *http.Request) string

func RandomBase58String

func RandomBase58String(n int, prefix ...string) (string, error)

func RandomBytes

func RandomBytes(n int) ([]byte, error)

func RandomDigitString

func RandomDigitString(n int) (string, error)

func SanitizeString

func SanitizeString(input string) string

func ValidatePasswordHash added in v0.2.0

func ValidatePasswordHash(encoded string) error

ValidatePasswordHash checks syntax and bounded KDF costs without doing KDF work.

func VerifyPassword added in v0.2.0

func VerifyPassword(encoded, password string) (bool, error)

func VerifyPasswordContext added in v0.2.0

func VerifyPasswordContext(ctx context.Context, encoded, password string) (bool, error)

Types

type ACR

type ACR string
const (
	ACR_BRONZE ACR = "urn:mace:incommon:iap:bronze"
	ACR_SILVER ACR = "urn:mace:incommon:iap:silver"
	ACR_GOLD   ACR = "urn:mace:incommon:iap:gold"
)

type AMR

type AMR string
const (
	AMR_PASSWORD    AMR = "pwd"
	AMR_OTP         AMR = "otp"
	AMR_MULTIFACTOR AMR = "mfa"
	AMR_SMS         AMR = "sms"
	AMR_EMAIL       AMR = "email"
	AMR_PUSH        AMR = "push"
	AMR_FIDO        AMR = "fido"
	AMR_BIOMETRIC   AMR = "biometric"
)

type AuthMethod

type AuthMethod string
const (
	CLIENT_SECRET_BASIC         AuthMethod = "client_secret_basic"
	CLIENT_SECRET_POST          AuthMethod = "client_secret_post"
	CLIENT_SECRET_JWT           AuthMethod = "client_secret_jwt"
	PRIVATE_KEY_JWT             AuthMethod = "private_key_jwt"
	TLS_CLIENT_AUTH             AuthMethod = "tls_client_auth"
	SELF_SIGNED_TLS_CLIENT_AUTH AuthMethod = "self_signed_tls_client_auth"
	AUTH_METHOD_NONE            AuthMethod = "none"
)

type AuthStatus

type AuthStatus string
const (
	APPROVED AuthStatus = "approved"
	PENDING  AuthStatus = "pending"
	DENIED   AuthStatus = "denied"
	REVOKED  AuthStatus = "revoked"
)

type ClaimType

type ClaimType string
const (
	CLAIM_TYPE_NORMAL      ClaimType = "normal"
	CLAIM_TYPE_AGGREGATED  ClaimType = "aggregated"
	CLAIM_TYPE_DISTRIBUTED ClaimType = "distributed"
)

type Display

type Display string
const (
	PAGE  Display = "page"
	POPUP Display = "popup"
	TOUCH Display = "touch"
	WAP   Display = "wap"
)

type EncryptedDate

type EncryptedDate struct {
	time.Time
}

func (EncryptedDate) MarshalJSON

func (e EncryptedDate) MarshalJSON() ([]byte, error)

func (*EncryptedDate) Scan

func (e *EncryptedDate) Scan(value interface{}) error

func (*EncryptedDate) UnmarshalJSON

func (e *EncryptedDate) UnmarshalJSON(data []byte) (err error)

func (EncryptedDate) Value

func (e EncryptedDate) Value() (driver.Value, error)

type EncryptedString

type EncryptedString string

func (*EncryptedString) Scan

func (e *EncryptedString) Scan(value interface{}) error

func (EncryptedString) Value

func (e EncryptedString) Value() (driver.Value, error)

type Epoch

type Epoch time.Time

func (Epoch) MarshalJSON

func (e Epoch) MarshalJSON() ([]byte, error)

func (*Epoch) Scan

func (e *Epoch) Scan(value interface{}) error

func (*Epoch) UnmarshalJSON

func (e *Epoch) UnmarshalJSON(data []byte) (err error)

func (Epoch) Value

func (e Epoch) Value() (driver.Value, error)

type EpochMillis

type EpochMillis time.Time

func (EpochMillis) MarshalJSON

func (e EpochMillis) MarshalJSON() ([]byte, error)

func (*EpochMillis) Scan

func (e *EpochMillis) Scan(value interface{}) error

func (*EpochMillis) UnmarshalJSON

func (e *EpochMillis) UnmarshalJSON(data []byte) (err error)

func (EpochMillis) Value

func (e EpochMillis) Value() (driver.Value, error)

type GrantType

type GrantType string
const (
	AUTHORIZATION_CODE GrantType = "authorization_code"
	IMPLICIT           GrantType = "implicit"
	CLIENT_CREDENTIALS GrantType = "client_credentials"
	REFRESH_TOKEN      GrantType = "refresh_token"
	DEVICE_CODE        GrantType = "urn:ietf:params:oauth:grant-type:device_code"
	JWT_BEARER         GrantType = "urn:ietf:params:oauth:grant-type:jwt-bearer"
	CIBA               GrantType = "urn:openid:params:grant-type:ciba"
)

type HashedString

type HashedString string

func (*HashedString) Compare

func (h *HashedString) Compare(plain []byte) bool

func (*HashedString) Scan

func (h *HashedString) Scan(value interface{}) error

func (HashedString) String

func (h HashedString) String() string

func (HashedString) Value

func (h HashedString) Value() (driver.Value, error)

type Marshalable

type Marshalable interface {
	MarshalJSON() ([]byte, error)
	UnmarshalJSON(data []byte) error
}

type PKCEMethod

type PKCEMethod string
const (
	S256      PKCEMethod = "S256"
	PLAIN     PKCEMethod = "plain"
	PKCE_NONE PKCEMethod = "none"
)

type Prompt

type Prompt string
const (
	NONE           Prompt = "none"
	LOGIN          Prompt = "login"
	CONSENT        Prompt = "consent"
	SELECT_ACCOUNT Prompt = "select_account"
)

type ResponseMode

type ResponseMode string
const (
	QUERY    ResponseMode = "query"
	FRAGMENT ResponseMode = "fragment"
)

type ResponseType

type ResponseType string
const (
	CODE                ResponseType = "code"
	TOKEN               ResponseType = "token"
	ID_TOKEN            ResponseType = "id_token"
	CODE_TOKEN          ResponseType = "code token"
	CODE_ID_TOKEN       ResponseType = "code id_token"
	ID_TOKEN_TOKEN      ResponseType = "id_token token"
	CODE_ID_TOKEN_TOKEN ResponseType = "code id_token token"
	FORM_POST           ResponseType = "form_post"
)

type Result

type Result string
const (
	SUCCESS Result = "success"
	FAILED  Result = "failed"
	EXPIRED Result = "expired"
)

type Scope

type Scope string
const (
	OPENID         Scope = "openid"
	PROFILE        Scope = "profile"
	EMAIL          Scope = "email"
	PHONE          Scope = "phone"
	ADDRESS        Scope = "address"
	READ           Scope = "read"
	WRITE          Scope = "write"
	UPDATE         Scope = "update"
	DELETE         Scope = "delete"
	OFFLINE_ACCESS Scope = "offline_access"
)

type ScopeSlice

type ScopeSlice []Scope

func (ScopeSlice) MarshalJSON

func (s ScopeSlice) MarshalJSON() ([]byte, error)

func (*ScopeSlice) UnmarshalJSON

func (s *ScopeSlice) UnmarshalJSON(data []byte) error

type SigningAlgorithm

type SigningAlgorithm string
const (
	HS256 SigningAlgorithm = "HS256"
	HS384 SigningAlgorithm = "HS384"
	HS512 SigningAlgorithm = "HS512"

	RS256 SigningAlgorithm = "RS256"
	RS384 SigningAlgorithm = "RS384"
	RS512 SigningAlgorithm = "RS512"

	ES256 SigningAlgorithm = "ES256"
	ES384 SigningAlgorithm = "ES384"
	ES512 SigningAlgorithm = "ES512"

	PS256 SigningAlgorithm = "PS256"
	PS384 SigningAlgorithm = "PS384"
	PS512 SigningAlgorithm = "PS512"

	EdDSA SigningAlgorithm = "EdDSA"
)

type SubjectType

type SubjectType string
const (
	PAIRWISE SubjectType = "pairwise"
	PUBLIC   SubjectType = "public"
)

type TokenType

type TokenType string

type Validatable

type Validatable interface {
	Validate() error
}

type Writable

type Writable interface {
	Write(w http.ResponseWriter)
}

Jump to

Keyboard shortcuts

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