Files
oauth2-api/app/models/application.go
Björn Benouarets 346100feb6 init: Initial commit
2026-01-21 06:36:38 +01:00

42 lines
1.2 KiB
Go

package models
import (
"time"
"git.secnex.io/secnex/oauth2-api/utils"
"github.com/google/uuid"
"gorm.io/gorm"
)
type Application struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
Name string `gorm:"not null" json:"name"`
Secret string `gorm:"not null" json:"secret"`
TenantID uuid.UUID `gorm:"type:uuid" json:"tenant_id"`
ExpiresAt *time.Time `gorm:"not null" json:"expires_at"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
Tenant *Tenant `gorm:"foreignKey:TenantID" json:"tenant"`
Authorizations []Authorization `gorm:"foreignKey:ClientID" json:"authorizations"`
}
func (Application) TableName() string {
return "applications"
}
func (application *Application) BeforeCreate(tx *gorm.DB) (err error) {
secretHash, err := utils.Hash(application.Secret)
if err != nil {
return err
}
application.Secret = secretHash
if application.ExpiresAt == nil {
expiresAt := time.Now().Add(time.Hour * 24 * 365)
application.ExpiresAt = &expiresAt
}
return nil
}