105 lines
3.6 KiB
Go
105 lines
3.6 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/dusiburg/kotosoft-id/internal/models"
|
|
"github.com/google/uuid"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidRequest = errors.New("invalid_request")
|
|
ErrUnauthorizedClient = errors.New("unauthorized_client")
|
|
ErrInvalidClient = errors.New("invalid_client")
|
|
ErrInvalidGrant = errors.New("invalid_grant")
|
|
ErrUnsupportedGrantType = errors.New("unsupported_grant_type")
|
|
ErrAccessDenied = errors.New("access_denied")
|
|
)
|
|
|
|
type UserRepo interface {
|
|
CreateUser(ctx context.Context, user models.User) (*models.User, error)
|
|
UpdateUser(ctx context.Context, user models.User) (*models.User, error)
|
|
DeleteUser(ctx context.Context, id uuid.UUID) error
|
|
GetUserByEmail(ctx context.Context, email string) (*models.User, error)
|
|
GetUserByID(ctx context.Context, id uuid.UUID) (*models.User, error)
|
|
IsUserExistsByEmail(ctx context.Context, email string) bool
|
|
}
|
|
|
|
type OAuthRepo interface {
|
|
GetOAuthClientByClientID(ctx context.Context, clientID string) (*models.OAuthClient, error)
|
|
GetOAuthClientByID(ctx context.Context, id uuid.UUID) (*models.OAuthClient, error)
|
|
CreateAuthorizationCode(ctx context.Context, row models.AuthorizationCodeRow) (*models.AuthorizationCodeRow, error)
|
|
GetAuthorizationCodeByHash(ctx context.Context, codeHash string) (*models.AuthorizationCodeRow, error)
|
|
ConsumeAuthorizationCodeByID(ctx context.Context, id uuid.UUID) error
|
|
CreateRefreshToken(ctx context.Context, row models.RefreshTokenRow) (*models.RefreshTokenRow, error)
|
|
GetRefreshTokenByHash(ctx context.Context, tokenHash string) (*models.RefreshTokenRow, error)
|
|
DeleteRefreshTokenByID(ctx context.Context, id uuid.UUID) error
|
|
DeleteRefreshTokensByUserID(ctx context.Context, userID uuid.UUID) error
|
|
RecordUserConsent(ctx context.Context, userID, clientID uuid.UUID, scopes []string) error
|
|
GetUserConsents(ctx context.Context, userID uuid.UUID) ([]models.UserConsent, error)
|
|
UpdateConsentLastUsed(ctx context.Context, userID, clientID uuid.UUID) error
|
|
GetUserConsentsByClientID(ctx context.Context, userID uuid.UUID, clientID uuid.UUID) ([]models.UserConsent, error)
|
|
RevokeConsent(ctx context.Context, userID, clientID uuid.UUID, revokedBy *uuid.UUID) error
|
|
}
|
|
|
|
type PositionRepo interface {
|
|
GetUserPositions(ctx context.Context, userID uuid.UUID, locale string) ([]models.PositionLocalized, error)
|
|
GetAllPositions(ctx context.Context, locale string) ([]models.PositionLocalized, error)
|
|
UpsertPositionForLocale(ctx context.Context, code, locale, name string, description *string) error
|
|
UserHasPosition(ctx context.Context, userID uuid.UUID, code string) (bool, error)
|
|
}
|
|
|
|
type TokenConfig struct {
|
|
AccessTTL time.Duration
|
|
RefreshTTL time.Duration
|
|
}
|
|
|
|
type ClientAuth struct {
|
|
ClientID string
|
|
ClientSecret string
|
|
}
|
|
|
|
type Service struct {
|
|
users UserRepo
|
|
oauth OAuthRepo
|
|
positions PositionRepo
|
|
redis *redis.Client
|
|
tokens TokenConfig
|
|
}
|
|
|
|
func New(
|
|
users UserRepo,
|
|
oauth OAuthRepo,
|
|
positions PositionRepo,
|
|
rdb *redis.Client,
|
|
tokenCfg TokenConfig,
|
|
) *Service {
|
|
return &Service{
|
|
users: users,
|
|
oauth: oauth,
|
|
positions: positions,
|
|
redis: rdb,
|
|
tokens: tokenCfg,
|
|
}
|
|
}
|
|
|
|
func (s *Service) AuthenticateClient(ctx context.Context, auth ClientAuth) (*models.OAuthClient, error) {
|
|
client, err := s.oauth.GetOAuthClientByClientID(ctx, auth.ClientID)
|
|
if err != nil {
|
|
return nil, ErrInvalidClient
|
|
}
|
|
if !verifyClientSecret(client.ClientSecret, auth.ClientSecret) {
|
|
return nil, ErrInvalidClient
|
|
}
|
|
return client, nil
|
|
}
|
|
|
|
const (
|
|
oauthSessionPrefix = "oauth_session:"
|
|
accessTokenPrefix = "access_token:"
|
|
oauthSessionTTL = 10 * time.Minute
|
|
)
|