mirror of
https://github.com/dagucloud/dagu.git
synced 2026-12-05 21:34:48 +00:00
142 lines
5.5 KiB
Go
142 lines
5.5 KiB
Go
// Copyright (C) 2026 Yota Hamada
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package auth
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const (
|
|
// AuthProviderBuiltin identifies users authenticated with a Dagu password.
|
|
AuthProviderBuiltin = "builtin"
|
|
// AuthProviderOIDC identifies users authenticated through OIDC.
|
|
AuthProviderOIDC = "oidc"
|
|
// AuthProviderProxy identifies users authenticated through proxy authentication.
|
|
AuthProviderProxy = "proxy"
|
|
)
|
|
|
|
// User represents a user in the system.
|
|
type User struct {
|
|
// ID is the unique identifier for the user (UUID).
|
|
ID string `json:"id"`
|
|
// Username is the unique login name.
|
|
Username string `json:"username"`
|
|
// PasswordHash is the bcrypt hash of the password.
|
|
// Excluded from JSON serialization for security.
|
|
PasswordHash string `json:"-"`
|
|
// Role determines the user's permissions.
|
|
Role Role `json:"role"`
|
|
// WorkspaceAccess restricts access to selected workspaces.
|
|
// Nil is treated as all-workspaces for backward compatibility.
|
|
WorkspaceAccess *WorkspaceAccess `json:"workspace_access,omitempty"`
|
|
// CreatedAt is the timestamp when the user was created.
|
|
CreatedAt time.Time `json:"created_at"`
|
|
// UpdatedAt is the timestamp when the user was last modified.
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
|
|
// AuthProvider indicates how the user authenticates.
|
|
// Empty string is treated as "builtin" for backward compatibility.
|
|
AuthProvider string `json:"auth_provider,omitempty"`
|
|
// OIDCIssuer is the OIDC provider issuer URL (set when AuthProvider is "oidc").
|
|
OIDCIssuer string `json:"oidc_issuer,omitempty"`
|
|
// OIDCSubject is the unique subject identifier from the OIDC provider.
|
|
OIDCSubject string `json:"oidc_subject,omitempty"`
|
|
// TrustedProxySource identifies the proxy identity source.
|
|
TrustedProxySource string `json:"trusted_proxy_source,omitempty"`
|
|
// TrustedProxyUser is the opaque identity supplied by the authenticating proxy.
|
|
TrustedProxyUser string `json:"trusted_proxy_user,omitempty"`
|
|
// IsDisabled indicates if the user account is disabled.
|
|
// Disabled users cannot log in.
|
|
IsDisabled bool `json:"is_disabled,omitempty"`
|
|
// PasswordChangedAt records when the password was last changed.
|
|
// Tokens issued before this time are rejected. Nil means never changed.
|
|
PasswordChangedAt *time.Time `json:"password_changed_at,omitempty"`
|
|
}
|
|
|
|
// NewUser creates a User with a new UUID and sets CreatedAt and UpdatedAt to the current UTC time.
|
|
// The provided username, passwordHash, and role are assigned to the corresponding fields.
|
|
func NewUser(username string, passwordHash string, role Role) *User {
|
|
now := time.Now().UTC()
|
|
return &User{
|
|
ID: uuid.New().String(),
|
|
Username: username,
|
|
PasswordHash: passwordHash,
|
|
Role: role,
|
|
WorkspaceAccess: AllWorkspaceAccess(),
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
}
|
|
|
|
// UserForStorage is used for JSON serialization to persistent storage.
|
|
// It includes the password hash which is excluded from the regular User JSON.
|
|
type UserForStorage struct {
|
|
ID string `json:"id"`
|
|
Username string `json:"username"`
|
|
PasswordHash string `json:"password_hash"`
|
|
Role Role `json:"role"`
|
|
WorkspaceAccess *WorkspaceAccess `json:"workspace_access,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
AuthProvider string `json:"auth_provider,omitempty"`
|
|
OIDCIssuer string `json:"oidc_issuer,omitempty"`
|
|
OIDCSubject string `json:"oidc_subject,omitempty"`
|
|
TrustedProxySource string `json:"trusted_proxy_source,omitempty"`
|
|
TrustedProxyUser string `json:"trusted_proxy_user,omitempty"`
|
|
IsDisabled bool `json:"is_disabled,omitempty"`
|
|
PasswordChangedAt *time.Time `json:"password_changed_at,omitempty"`
|
|
}
|
|
|
|
// ToStorage converts a User to UserForStorage for persistence.
|
|
func (u *User) ToStorage() *UserForStorage {
|
|
return &UserForStorage{
|
|
ID: u.ID,
|
|
Username: u.Username,
|
|
PasswordHash: u.PasswordHash,
|
|
Role: u.Role,
|
|
WorkspaceAccess: CloneWorkspaceAccess(u.WorkspaceAccess),
|
|
CreatedAt: u.CreatedAt,
|
|
UpdatedAt: u.UpdatedAt,
|
|
AuthProvider: u.AuthProvider,
|
|
OIDCIssuer: u.OIDCIssuer,
|
|
OIDCSubject: u.OIDCSubject,
|
|
TrustedProxySource: u.TrustedProxySource,
|
|
TrustedProxyUser: u.TrustedProxyUser,
|
|
IsDisabled: u.IsDisabled,
|
|
PasswordChangedAt: u.PasswordChangedAt,
|
|
}
|
|
}
|
|
|
|
// ToUser converts UserForStorage back to User.
|
|
func (s *UserForStorage) ToUser() *User {
|
|
return &User{
|
|
ID: s.ID,
|
|
Username: s.Username,
|
|
PasswordHash: s.PasswordHash,
|
|
Role: s.Role,
|
|
WorkspaceAccess: CloneWorkspaceAccess(s.WorkspaceAccess),
|
|
CreatedAt: s.CreatedAt,
|
|
UpdatedAt: s.UpdatedAt,
|
|
AuthProvider: s.AuthProvider,
|
|
OIDCIssuer: s.OIDCIssuer,
|
|
OIDCSubject: s.OIDCSubject,
|
|
TrustedProxySource: s.TrustedProxySource,
|
|
TrustedProxyUser: s.TrustedProxyUser,
|
|
IsDisabled: s.IsDisabled,
|
|
PasswordChangedAt: s.PasswordChangedAt,
|
|
}
|
|
}
|
|
|
|
// IsOIDCUser returns true if the user was created via OIDC auto-signup.
|
|
func (u *User) IsOIDCUser() bool {
|
|
return u.AuthProvider == AuthProviderOIDC
|
|
}
|
|
|
|
// CanUsePassword reports whether password authentication is available to the user.
|
|
func (u *User) CanUsePassword() bool {
|
|
return u.AuthProvider == "" || u.AuthProvider == AuthProviderBuiltin
|
|
}
|