forked from mtusi2026/mtusi2026lab2
145 lines
2.5 KiB
Go
145 lines
2.5 KiB
Go
package dto
|
|
|
|
import (
|
|
"errors"
|
|
"net/netip"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitea.privcloud.tech/mtusi2026/internal/pkg/authidentity"
|
|
"gitea.privcloud.tech/mtusi2026/internal/pkg/authprovider"
|
|
"gitea.privcloud.tech/mtusi2026/internal/pkg/role"
|
|
)
|
|
|
|
// ---- Tokens ----
|
|
|
|
type UserDataInToken struct {
|
|
TokenID uuid.UUID
|
|
UserID uuid.UUID
|
|
Role role.Role
|
|
IsAccess bool
|
|
|
|
PhoneNumber *string
|
|
Email *string
|
|
|
|
ExpiresAt time.Time
|
|
IssuedAt time.Time
|
|
}
|
|
|
|
func (u UserDataInToken) Validate() error {
|
|
if u.TokenID == uuid.Nil {
|
|
return errors.New("invalid token id")
|
|
}
|
|
if u.UserID == uuid.Nil {
|
|
return errors.New("invalid user id")
|
|
}
|
|
if !u.Role.IsValid() {
|
|
return errors.New("invalid role")
|
|
}
|
|
if u.ExpiresAt.IsZero() {
|
|
return errors.New("invalid expiry")
|
|
}
|
|
if u.IssuedAt.IsZero() {
|
|
return errors.New("invalid issued")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Token struct {
|
|
Payload string
|
|
}
|
|
|
|
type (
|
|
AccessToken Token
|
|
RefreshToken Token
|
|
)
|
|
|
|
type Tokens struct {
|
|
AccessToken AccessToken
|
|
RefreshToken RefreshToken
|
|
}
|
|
|
|
func (t AccessToken) Token() *Token {
|
|
return new(Token(t))
|
|
}
|
|
|
|
func (t RefreshToken) Token() *Token {
|
|
return new(Token(t))
|
|
}
|
|
|
|
type StoredRefreshToken struct {
|
|
ID uuid.UUID
|
|
UserID uuid.UUID
|
|
TokenHash string
|
|
UserAgent string
|
|
IPAddress netip.Addr
|
|
Role role.Role
|
|
ExpiresAt time.Time
|
|
RevokedAt *time.Time
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
type RevokeRefreshToken struct {
|
|
ID uuid.UUID
|
|
RevokedAt time.Time
|
|
}
|
|
|
|
type RevokeRefreshTokensByUserID struct {
|
|
UserID uuid.UUID
|
|
RevokedAt time.Time
|
|
}
|
|
|
|
// ---- Auth ----
|
|
|
|
type Login struct {
|
|
Email string
|
|
Password string
|
|
}
|
|
|
|
type Register struct {
|
|
Email string
|
|
Password string
|
|
RepeatPassword string
|
|
}
|
|
|
|
// ---- Auth Identities ----
|
|
|
|
type AuthIdentity struct {
|
|
ID uuid.UUID
|
|
Role role.Role
|
|
UserID uuid.UUID
|
|
Provider authprovider.Type
|
|
Identifier string
|
|
PasswordHash *string
|
|
Status authidentity.Status
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
type UpdateAuthIdentity struct {
|
|
ID uuid.UUID
|
|
Role *role.Role
|
|
Provider *authprovider.Type
|
|
Identifier *string
|
|
PasswordHash *string
|
|
Status *authidentity.Status
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
type UpdateAuthIdentityPasswordHash struct {
|
|
ID uuid.UUID
|
|
PasswordHash string
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
type UpdateAuthIdentityStatus struct {
|
|
ID uuid.UUID
|
|
Status authidentity.Status
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
type GetAuthIdentityByRoleAndUserID struct {
|
|
Role role.Role
|
|
UserID uuid.UUID
|
|
}
|