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
|
||||
}
|
||||
40
app/models/authorization.go
Normal file
40
app/models/authorization.go
Normal 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
|
||||
}
|
||||
26
app/models/tenant.go
Normal file
26
app/models/tenant.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Tenant struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
Enabled bool `gorm:"not null;default:true" json:"enabled"`
|
||||
AllowSelfRegistration bool `gorm:"not null;default:false" json:"allow_self_registration"`
|
||||
AllowSelfRegistrationDomains []string `gorm:"type:jsonb;not null" json:"allow_self_registration_domains"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
|
||||
|
||||
Users []User `gorm:"foreignKey:TenantID" json:"users"`
|
||||
Applications []Application `gorm:"foreignKey:TenantID" json:"applications"`
|
||||
}
|
||||
|
||||
func (Tenant) TableName() string {
|
||||
return "tenants"
|
||||
}
|
||||
39
app/models/users.go
Normal file
39
app/models/users.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.secnex.io/secnex/oauth2-api/utils"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
|
||||
FirstName string `gorm:"not null" json:"first_name"`
|
||||
LastName string `gorm:"not null" json:"last_name"`
|
||||
Username string `gorm:"not null;unique" json:"username"`
|
||||
Password string `gorm:"not null" json:"password"`
|
||||
Email string `gorm:"not null;unique" json:"email"`
|
||||
Verified bool `gorm:"not null;default:false" json:"verified"`
|
||||
TenantID *uuid.UUID `gorm:"type:uuid" json:"tenant_id"`
|
||||
ExternalID *uuid.UUID `gorm:"type:uuid" json:"external_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"`
|
||||
|
||||
Tenant *Tenant `gorm:"foreignKey:TenantID" json:"tenant"`
|
||||
}
|
||||
|
||||
func (User) TableName() string {
|
||||
return "users"
|
||||
}
|
||||
|
||||
func (user *User) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
passwordHash, err := utils.Hash(user.Password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
user.Password = passwordHash
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user