initial commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package models
|
||||
|
||||
type APIError struct {
|
||||
Code int `json:"-"`
|
||||
Message string `json:"message"`
|
||||
Slug string `json:"error"`
|
||||
Cause error `json:"-"`
|
||||
}
|
||||
|
||||
var (
|
||||
ErrBadRequest = &APIError{Code: 400, Slug: "bad_request", Message: "Invalid request"}
|
||||
ErrUnauthorized = &APIError{Code: 401, Slug: "unauthorized", Message: "Incorrect credentials"}
|
||||
ErrForbidden = &APIError{Code: 403, Slug: "forbidden", Message: "Forbidden"}
|
||||
ErrInsufficientScope = &APIError{Code: 403, Slug: "insufficient_scope", Message: "The access token does not contain the required scope"}
|
||||
ErrNotFound = &APIError{Code: 404, Slug: "not_found", Message: "Resource not found"}
|
||||
ErrAlreadyExists = &APIError{Code: 409, Slug: "already_exists", Message: "Resource already exists"}
|
||||
ErrInternal = &APIError{Code: 500, Slug: "internal", Message: "Internal server error"}
|
||||
)
|
||||
|
||||
func (e *APIError) Error() string {
|
||||
return e.Slug
|
||||
}
|
||||
|
||||
func (e *APIError) WithMessage(msg string) *APIError {
|
||||
if e == nil {
|
||||
return (&APIError{Code: 500, Slug: "internal", Message: "Internal server error"}).WithMessage(msg)
|
||||
}
|
||||
cp := *e
|
||||
cp.Message = msg
|
||||
return &cp
|
||||
}
|
||||
|
||||
func (e *APIError) WithCause(cause error) *APIError {
|
||||
if e == nil {
|
||||
return (&APIError{Code: 500, Slug: "internal", Message: "Internal server error"}).WithCause(cause)
|
||||
}
|
||||
cp := *e
|
||||
cp.Cause = cause
|
||||
return &cp
|
||||
}
|
||||
|
||||
func (e *APIError) Unwrap() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.Cause
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type OAuthClient struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
ClientID string `db:"client_id" json:"client_id"`
|
||||
ClientSecret string `db:"client_secret" json:"client_secret"`
|
||||
Name string `db:"name" json:"name"`
|
||||
RedirectURIs json.RawMessage `db:"redirect_uris" json:"redirect_uris"`
|
||||
GrantTypes json.RawMessage `db:"grant_types" json:"grant_types"`
|
||||
Scopes json.RawMessage `db:"scopes" json:"scopes,omitempty"`
|
||||
IsActive bool `db:"is_active" json:"is_active"`
|
||||
Trusted bool `db:"trusted" json:"trusted"`
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
type RefreshTokenRow struct {
|
||||
ID uuid.UUID `db:"id"`
|
||||
TokenHash string `db:"token_hash"`
|
||||
UserID uuid.UUID `db:"user_id"`
|
||||
ClientID uuid.UUID `db:"client_id"`
|
||||
Scopes json.RawMessage `db:"scopes"`
|
||||
ExpiresAt time.Time `db:"expires_at"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
|
||||
type AuthorizationCodeRow struct {
|
||||
ID uuid.UUID `db:"id"`
|
||||
CodeHash string `db:"code_hash"`
|
||||
UserID uuid.UUID `db:"user_id"`
|
||||
ClientID uuid.UUID `db:"client_id"`
|
||||
RedirectURI string `db:"redirect_uri"`
|
||||
Scopes json.RawMessage `db:"scopes"`
|
||||
CodeChallenge string `db:"code_challenge"`
|
||||
CodeChallengeMethod string `db:"code_challenge_method"`
|
||||
ExpiresAt time.Time `db:"expires_at"`
|
||||
ConsumedAt *time.Time `db:"consumed_at"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
|
||||
type UserConsent struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
UserID uuid.UUID `json:"-"`
|
||||
ClientID uuid.UUID `json:"-"`
|
||||
ClientIdentifier string `json:"client_id"`
|
||||
ClientName string `json:"client_name"`
|
||||
Scopes []string `json:"scopes"`
|
||||
AuthorizedAt time.Time `json:"authorized_at"`
|
||||
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
|
||||
RevokedAt *time.Time `json:"revoked_at,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
Email string `db:"email" json:"email"`
|
||||
PasswordHash string `db:"password_hash" json:"-"`
|
||||
FirstName string `db:"first_name" json:"first_name"`
|
||||
SecondName string `db:"second_name" json:"second_name"`
|
||||
BirthDate *time.Time `db:"birth_date" json:"birth_date,omitempty"`
|
||||
Sex string `db:"sex" json:"sex"`
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
type UserRegisterInput struct {
|
||||
Email string `json:"email" binding:"required,min=3"`
|
||||
Password string `json:"password" binding:"required,min=8"`
|
||||
FirstName string `json:"first_name" binding:"required,min=2,max=64"`
|
||||
SecondName string `json:"second_name" binding:"required,min=2,max=64"`
|
||||
BirthDate *time.Time `json:"birth_date" binding:"omitempty"`
|
||||
Sex string `json:"sex" binding:"required,oneof=MALE FEMALE"`
|
||||
}
|
||||
|
||||
type UserUpdateInput struct {
|
||||
FirstName string `json:"first_name" binding:"omitempty,min=2,max=64"`
|
||||
SecondName string `json:"second_name" binding:"omitempty,min=2,max=64"`
|
||||
BirthDate *time.Time `json:"birth_date"`
|
||||
Sex string `json:"sex" binding:"omitempty,oneof=MALE FEMALE"`
|
||||
Password string `json:"password" binding:"omitempty,min=8"`
|
||||
}
|
||||
Reference in New Issue
Block a user