136 lines
3.3 KiB
Go
136 lines
3.3 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
|
|
}
|
|
|
|
return s.users.CreateUser(ctx, models.User{
|
|
Email: input.Email,
|
|
PasswordHash: string(hash),
|
|
FirstName: input.FirstName,
|
|
SecondName: input.SecondName,
|
|
BirthDate: input.BirthDate,
|
|
Sex: input.Sex,
|
|
})
|
|
}
|
|
|
|
func (s *Service) RefreshTokens(ctx context.Context, rawToken string) (*TokenPair, error) {
|
|
row, err := s.oauth.GetRefreshTokenByHash(ctx, sha256Hex(rawToken))
|
|
if err != nil {
|
|
return nil, models.ErrUnauthorized
|
|
}
|
|
|
|
if time.Now().After(row.ExpiresAt) {
|
|
return nil, models.ErrUnauthorized
|
|
}
|
|
|
|
client, err := s.oauth.GetOAuthClientByID(ctx, row.ClientID)
|
|
if err != nil {
|
|
return nil, models.ErrInternal
|
|
}
|
|
|
|
scopes := scopesFromJSON(row.Scopes)
|
|
if !ScopesAllowed(client.Scopes, scopes) {
|
|
return nil, ErrInvalidGrant
|
|
}
|
|
|
|
if err := s.oauth.DeleteRefreshTokenByID(ctx, row.ID); err != nil {
|
|
return nil, models.ErrInternal
|
|
}
|
|
|
|
user, err := s.users.GetUserByID(ctx, row.UserID)
|
|
if err != nil {
|
|
return nil, models.ErrUnauthorized
|
|
}
|
|
|
|
return s.issuePair(ctx, user, client.ID, scopes)
|
|
}
|
|
|
|
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.users.DeleteUser(ctx, userID); err != nil {
|
|
return models.ErrInternal
|
|
}
|
|
_ = s.redis.Del(ctx, accessTokenPrefix+accessToken)
|
|
return nil
|
|
}
|