init: Initial commit
This commit is contained in:
41
app/models/application.go
Normal file
41
app/models/application.go
Normal file
@@ -0,0 +1,41 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user