46 lines
1.8 KiB
Go
46 lines
1.8 KiB
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
type APIKey struct {
|
|
ID uint64 `gorm:"column:id;primaryKey;autoIncrement"`
|
|
UserID uint64 `gorm:"column:user_id;not null"`
|
|
Name string `gorm:"column:name;size:80;not null"`
|
|
PublicID string `gorm:"column:public_id;size:24;not null;uniqueIndex:uq_api_keys_public_id"`
|
|
KeyPrefix string `gorm:"column:key_prefix;size:32;not null"`
|
|
SecretHash []byte `gorm:"column:secret_hash;type:binary(32);not null" json:"-"`
|
|
ExpiresAt *time.Time `gorm:"column:expires_at"`
|
|
LastUsedAt *time.Time `gorm:"column:last_used_at"`
|
|
RevokedAt *time.Time `gorm:"column:revoked_at"`
|
|
CreatedAt time.Time `gorm:"column:created_at;not null"`
|
|
UpdatedAt time.Time `gorm:"column:updated_at;not null"`
|
|
}
|
|
|
|
func (APIKey) TableName() string { return "api_keys" }
|
|
|
|
func (key APIKey) UsableAt(now time.Time) bool {
|
|
if key.RevokedAt != nil {
|
|
return false
|
|
}
|
|
return key.ExpiresAt == nil || key.ExpiresAt.After(now)
|
|
}
|
|
|
|
type APIAuditEvent struct {
|
|
ID uint64 `gorm:"column:id;primaryKey;autoIncrement"`
|
|
UserID *uint64 `gorm:"column:user_id"`
|
|
APIKeyID *uint64 `gorm:"column:api_key_id"`
|
|
GenerationID *uint64 `gorm:"column:generation_id"`
|
|
Action string `gorm:"column:action;size:128;not null"`
|
|
Result string `gorm:"column:result;size:16;not null"`
|
|
RequestID string `gorm:"column:request_id;size:128;not null"`
|
|
StatusCode *uint16 `gorm:"column:status_code"`
|
|
ErrorCode *string `gorm:"column:error_code;size:64"`
|
|
Summary json.RawMessage `gorm:"column:summary;type:json;not null"`
|
|
CreatedAt time.Time `gorm:"column:created_at;not null"`
|
|
}
|
|
|
|
func (APIAuditEvent) TableName() string { return "api_audit_events" }
|