132 lines
3.0 KiB
Go
132 lines
3.0 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/dusiburg/kotosoft-id/internal/models"
|
|
"github.com/google/uuid"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type TokenInfo struct {
|
|
UserID uuid.UUID
|
|
Scopes []string
|
|
ClientID uuid.UUID
|
|
}
|
|
|
|
func (s *Service) GetUserByID(ctx context.Context, id uuid.UUID) (*models.User, error) {
|
|
return s.users.GetUserByID(ctx, id)
|
|
}
|
|
|
|
func (s *Service) Register(ctx context.Context, input models.UserRegisterInput) (*models.User, error) {
|
|
if s.users.IsUserExistsByEmail(ctx, input.Email) {
|
|
return nil, models.ErrAlreadyExists
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(input.Password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return nil, models.ErrInternal
|
|
}
|
|
|
|
user, err := s.users.CreateUser(ctx, models.User{
|
|
Email: input.Email,
|
|
PasswordHash: string(hash),
|
|
FirstName: input.FirstName,
|
|
SecondName: input.SecondName,
|
|
BirthDate: input.BirthDate,
|
|
Sex: input.Sex,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
count, err := s.users.CountUsers(ctx)
|
|
if err != nil {
|
|
return nil, models.ErrInternal
|
|
}
|
|
if count == 1 {
|
|
position, err := s.positions.GetPositionByCode(ctx, "kotosoft_id_staff")
|
|
if err == nil {
|
|
now := time.Now()
|
|
_ = s.positions.AssignPosition(ctx, user.ID, position.ID, nil, now, nil)
|
|
}
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
func (s *Service) ValidateAccessToken(ctx context.Context, token string) (*TokenInfo, error) {
|
|
token = strings.TrimSpace(token)
|
|
if token == "" {
|
|
return nil, models.ErrUnauthorized
|
|
}
|
|
|
|
data, err := s.redis.Get(ctx, accessTokenPrefix+token).Bytes()
|
|
if err != nil {
|
|
return nil, models.ErrUnauthorized
|
|
}
|
|
|
|
var info struct {
|
|
UserID uuid.UUID `json:"user_id"`
|
|
Scopes []string `json:"scopes"`
|
|
ClientID uuid.UUID `json:"client_id"`
|
|
}
|
|
if err := json.Unmarshal(data, &info); err != nil {
|
|
return nil, models.ErrUnauthorized
|
|
}
|
|
|
|
return &TokenInfo{UserID: info.UserID, Scopes: info.Scopes, ClientID: info.ClientID}, nil
|
|
}
|
|
|
|
func (s *Service) UpdateProfile(ctx context.Context, userID uuid.UUID, input models.UserUpdateInput) (*models.User, error) {
|
|
user, err := s.users.GetUserByID(ctx, userID)
|
|
if err != nil {
|
|
return nil, models.ErrNotFound
|
|
}
|
|
|
|
if input.FirstName != "" {
|
|
user.FirstName = input.FirstName
|
|
}
|
|
|
|
if input.SecondName != "" {
|
|
user.SecondName = input.SecondName
|
|
}
|
|
|
|
if input.BirthDate != nil {
|
|
user.BirthDate = input.BirthDate
|
|
}
|
|
|
|
if input.Sex != "" {
|
|
user.Sex = input.Sex
|
|
}
|
|
|
|
if input.Password != "" {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(input.Password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return nil, models.ErrInternal
|
|
}
|
|
user.PasswordHash = string(hash)
|
|
}
|
|
|
|
user.UpdatedAt = time.Now()
|
|
|
|
return s.users.UpdateUser(ctx, *user)
|
|
}
|
|
|
|
func (s *Service) DeleteProfile(ctx context.Context, userID uuid.UUID, accessToken string) error {
|
|
if err := s.oauth.DeleteRefreshTokensByUserID(ctx, userID); err != nil {
|
|
return models.ErrInternal
|
|
}
|
|
|
|
if err := s.users.DeleteUser(ctx, userID); err != nil {
|
|
return models.ErrInternal
|
|
}
|
|
|
|
_ = s.redis.Del(ctx, accessTokenPrefix+accessToken)
|
|
|
|
return nil
|
|
}
|