2024-09-13 15:31:40 +01:00
|
|
|
package auth
|
|
|
|
|
2024-10-06 21:30:39 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/1f349/lavender/database"
|
|
|
|
)
|
2024-09-13 15:31:40 +01:00
|
|
|
|
2024-12-06 18:41:03 +00:00
|
|
|
// State defines the currently reached authentication state
|
|
|
|
type State byte
|
2024-10-06 21:30:39 +01:00
|
|
|
|
|
|
|
const (
|
2024-12-06 18:41:03 +00:00
|
|
|
// StateUnauthorized defines the "unauthorized" state of a session
|
|
|
|
StateUnauthorized State = iota
|
2025-01-25 19:49:57 +00:00
|
|
|
// StateBase defines the "username" only user state
|
|
|
|
// This state is for providing a username to allow redirecting to oauth clients
|
|
|
|
StateBase
|
2024-12-06 18:41:03 +00:00
|
|
|
// StateBasic defines the "username and password with no OTP" user state
|
|
|
|
// This is skipped if OTP/passkey is optional and not enabled for the user
|
|
|
|
StateBasic
|
|
|
|
// StateExtended defines the "logged in" user state
|
|
|
|
StateExtended
|
|
|
|
// StateSudo defines the "sudo" user state
|
|
|
|
// This state is temporary and has a configurable duration
|
|
|
|
StateSudo
|
2024-10-06 21:30:39 +01:00
|
|
|
)
|
|
|
|
|
2024-12-09 18:40:18 +00:00
|
|
|
func (s State) IsLoggedIn() bool { return s >= StateExtended }
|
2024-12-06 18:41:03 +00:00
|
|
|
|
2024-12-09 18:40:18 +00:00
|
|
|
func (s State) IsSudoAvailable() bool { return s == StateSudo }
|
2024-12-06 18:41:03 +00:00
|
|
|
|
2024-10-06 21:30:39 +01:00
|
|
|
type Provider interface {
|
2024-12-06 18:41:03 +00:00
|
|
|
// AccessState defines the state at which the provider is allowed to show.
|
|
|
|
// Some factors might be unavailable due to user preference.
|
|
|
|
AccessState() State
|
2024-10-06 21:30:39 +01:00
|
|
|
|
2024-12-06 18:41:03 +00:00
|
|
|
// Name defines a string value for the provider.
|
2024-10-06 21:30:39 +01:00
|
|
|
Name() string
|
2024-10-25 15:08:56 +01:00
|
|
|
}
|
|
|
|
|
2024-12-06 18:41:03 +00:00
|
|
|
type LookupUserDB interface {
|
2024-10-06 21:30:39 +01:00
|
|
|
GetUser(ctx context.Context, subject string) (database.User, error)
|
2024-09-13 15:31:40 +01:00
|
|
|
}
|
|
|
|
|
2024-12-06 18:41:03 +00:00
|
|
|
func LookupUser(ctx context.Context, db LookupUserDB, subject string, user *database.User) error {
|
2024-10-06 21:30:39 +01:00
|
|
|
getUser, err := db.GetUser(ctx, subject)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*user = getUser
|
|
|
|
return nil
|
2024-09-13 15:31:40 +01:00
|
|
|
}
|