init: Initial commit

This commit is contained in:
Björn Benouarets
2026-01-21 06:36:38 +01:00
commit 346100feb6
27 changed files with 1126 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
package models
import (
"time"
"git.secnex.io/secnex/oauth2-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
}