41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.secnex.io/secnex/mgmt-api/utils"
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Authorization struct {
|
|
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
|
|
UserID uuid.UUID `gorm:"type:uuid;not null" json:"user_id"`
|
|
ClientID uuid.UUID `gorm:"type:uuid;not null" json:"client_id"`
|
|
Code string `gorm:"not null" json:"code"`
|
|
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"`
|
|
|
|
User *User `gorm:"foreignKey:UserID" json:"user"`
|
|
Application *Application `gorm:"foreignKey:ClientID" json:"application"`
|
|
}
|
|
|
|
func (Authorization) TableName() string {
|
|
return "authorizations"
|
|
}
|
|
|
|
func (authorization *Authorization) BeforeCreate(tx *gorm.DB) (err error) {
|
|
codeHash, err := utils.Hash(authorization.Code)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
authorization.Code = codeHash
|
|
if authorization.ExpiresAt == nil {
|
|
expiresAt := time.Now().Add(time.Minute * 10)
|
|
authorization.ExpiresAt = &expiresAt
|
|
}
|
|
return nil
|
|
}
|