feat(auth): Add /token endpoint to request a access token
This commit is contained in:
43
app/models/token.go
Normal file
43
app/models/token.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.secnex.io/secnex/oauth2-api/utils"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Token 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"`
|
||||
RefreshToken string `gorm:"not null" json:"refresh_token"`
|
||||
SessionExpiresAt *time.Time `gorm:"not null" json:"session_expires_at"`
|
||||
RefreshTokenExpiresAt *time.Time `gorm:"not null" json:"refresh_token_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"`
|
||||
}
|
||||
|
||||
func (Token) TableName() string {
|
||||
return "tokens"
|
||||
}
|
||||
|
||||
func (token *Token) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
refreshTokenHash, err := utils.Hash(token.RefreshToken)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
token.RefreshToken = refreshTokenHash
|
||||
if token.SessionExpiresAt == nil {
|
||||
sessionExpiresAt := time.Now().Add(time.Hour * 24)
|
||||
token.SessionExpiresAt = &sessionExpiresAt
|
||||
}
|
||||
if token.RefreshTokenExpiresAt == nil {
|
||||
refreshTokenExpiresAt := time.Now().Add(time.Hour * 24 * 30)
|
||||
token.RefreshTokenExpiresAt = &refreshTokenExpiresAt
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user