Passport/pkg/internal/models/auth.go

72 lines
2.1 KiB
Go
Raw Normal View History

2024-01-06 17:56:32 +00:00
package models
import (
"fmt"
"time"
"gorm.io/datatypes"
)
type AuthFactorType = int8
const (
PasswordAuthFactor = AuthFactorType(iota)
EmailPasswordFactor
)
type AuthFactor struct {
BaseModel
2024-08-24 07:17:26 +00:00
Type int8 `json:"type"`
Secret string `json:"-"`
Config JSONMap `json:"config"`
Account Account `json:"account"`
2024-01-06 17:56:32 +00:00
AccountID uint `json:"account_id"`
}
2024-04-20 11:04:33 +00:00
type AuthTicket struct {
2024-01-06 17:56:32 +00:00
BaseModel
2024-04-20 11:04:33 +00:00
Location string `json:"location"`
IpAddress string `json:"ip_address"`
UserAgent string `json:"user_agent"`
RequireMFA bool `json:"require_mfa"`
RequireAuthenticate bool `json:"require_authenticate"`
Claims datatypes.JSONSlice[string] `json:"claims"`
Audiences datatypes.JSONSlice[string] `json:"audiences"`
GrantToken *string `json:"grant_token"`
AccessToken *string `json:"access_token"`
RefreshToken *string `json:"refresh_token"`
ExpiredAt *time.Time `json:"expired_at"`
AvailableAt *time.Time `json:"available_at"`
LastGrantAt *time.Time `json:"last_grant_at"`
2024-07-28 14:30:51 +00:00
Nonce *string `json:"nonce"`
2024-04-20 11:04:33 +00:00
ClientID *uint `json:"client_id"`
2024-08-24 07:17:26 +00:00
Account Account `json:"account"`
AccountID uint `json:"account_id"`
IsApiKey bool `json:"is_api_key"`
2024-01-06 17:56:32 +00:00
}
2024-04-20 11:04:33 +00:00
func (v AuthTicket) IsAvailable() error {
if v.RequireMFA || v.RequireAuthenticate {
2024-04-21 04:20:06 +00:00
return fmt.Errorf("ticket isn't authenticated yet")
2024-04-20 11:04:33 +00:00
}
2024-01-06 17:56:32 +00:00
if v.AvailableAt != nil && time.Now().Unix() < v.AvailableAt.Unix() {
2024-04-21 04:20:06 +00:00
return fmt.Errorf("ticket isn't available yet")
2024-01-06 17:56:32 +00:00
}
if v.ExpiredAt != nil && time.Now().Unix() > v.ExpiredAt.Unix() {
2024-04-21 04:20:06 +00:00
return fmt.Errorf("ticket expired")
2024-01-06 17:56:32 +00:00
}
return nil
}
type AuthContext struct {
2024-05-17 11:37:58 +00:00
Ticket AuthTicket `json:"ticket"`
Account Account `json:"account"`
LastUsedAt time.Time `json:"last_used_at"`
}