veza/veza-backend-api/internal/models/api_key.go

36 lines
1 KiB
Go
Raw Normal View History

package models
import (
"time"
"github.com/google/uuid"
"github.com/lib/pq"
"gorm.io/gorm"
)
// APIKey represents a user API key for developer portal (v0.102 Lot C)
type APIKey struct {
2026-03-05 22:03:43 +00:00
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
UserID uuid.UUID `gorm:"type:uuid;not null;index" json:"user_id"`
Name string `gorm:"size:100;not null" json:"name"`
Prefix string `gorm:"size:16;not null;index" json:"prefix"`
HashedKey string `gorm:"size:128;not null" json:"-"` // Never expose
Scopes pq.StringArray `gorm:"type:text[]" json:"scopes"`
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// TableName defines the table name for GORM
func (APIKey) TableName() string {
return "api_keys"
}
// BeforeCreate GORM hook to generate UUID if not set
func (k *APIKey) BeforeCreate(tx *gorm.DB) error {
if k.ID == uuid.Nil {
k.ID = uuid.New()
}
return nil
}