feat(auth): Add /token endpoint to request a access token
This commit is contained in:
@@ -33,7 +33,7 @@ func (authorization *Authorization) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
}
|
||||
authorization.Code = codeHash
|
||||
if authorization.ExpiresAt == nil {
|
||||
expiresAt := time.Now().Add(time.Minute * 10)
|
||||
expiresAt := time.Now().Add(time.Minute * 2)
|
||||
authorization.ExpiresAt = &expiresAt
|
||||
}
|
||||
return nil
|
||||
|
||||
22
app/models/session.go
Normal file
22
app/models/session.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Session 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"`
|
||||
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 (Session) TableName() string {
|
||||
return "sessions"
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -22,7 +22,11 @@ type User struct {
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
|
||||
|
||||
Tenant *Tenant `gorm:"foreignKey:TenantID" json:"tenant"`
|
||||
Tenant *Tenant `gorm:"foreignKey:TenantID"`
|
||||
|
||||
Authorizations []Authorization `gorm:"foreignKey:UserID" json:"-"`
|
||||
Sessions []Session `gorm:"foreignKey:UserID" json:"-"`
|
||||
Tokens []Token `gorm:"foreignKey:UserID" json:"-"`
|
||||
}
|
||||
|
||||
func (User) TableName() string {
|
||||
Reference in New Issue
Block a user