42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.secnex.io/secnex/mgmt-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;uniqueIndex:idx_name_tenant_id" json:"name"`
|
|
Secret string `gorm:"not null" json:"secret"`
|
|
TenantID uuid.UUID `gorm:"type:uuid;uniqueIndex:idx_name_tenant_id" 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
|
|
}
|