MSP-7470 Init user auth

This commit is contained in:
Синельник Тимур Романович committed 2026-04-01 10:05:20 +03:00
1 parent b67eb8137c
commit eb6326c343
35 files changed
+1059 -142

No files matched your search

+45 -18
View File
@@ -2,54 +2,81 @@ package service
import (
"context"
"isp-gate-service/domain"
"isp-gate-service/entity"
"github.com/pkg/errors"
"isp-gate-service/domain"
)
type AuthenticationCache interface {
Get(ctx context.Context, token string) (*domain.AuthData, error)
Set(ctx context.Context, token string, data domain.AuthData) error
Get(ctx context.Context, token string) (*entity.AppAuthData, error)
Set(ctx context.Context, token string, data entity.AppAuthData) error
}
type AuthenticationRepo interface {
Authenticate(ctx context.Context, token string) (*domain.AuthenticateResponse, error)
Authenticate(ctx context.Context, token string) (*entity.AuthenticateResponse, error)
}
type Auth struct {
type Authentication struct {
cache AuthenticationCache
repo AuthenticationRepo
}
func NewAuthentication(cache AuthenticationCache, repo AuthenticationRepo) Auth {
return Auth{
func NewAuthentication(
cache AuthenticationCache,
repo AuthenticationRepo,
) Authentication {
return Authentication{
cache: cache,
repo: repo,
}
}
func (s Auth) Authenticate(ctx context.Context, token string) (*domain.AuthenticateResponse, error) {
func (s Authentication) Authenticate(ctx context.Context, token string) (*domain.AuthenticateAppResponse, error) {
authData, err := s.cache.Get(ctx, token)
if errors.Is(err, domain.ErrAuthenticationCacheMiss) {
switch {
case errors.Is(err, domain.ErrAuthenticationCacheMiss):
resp, err := s.repo.Authenticate(ctx, token)
if err != nil {
return nil, errors.WithMessage(err, "auth repo authenticate")
}
if !resp.Authenticated {
return resp, nil
return s.convertAuthReponse(resp), nil
}
err = s.cache.Set(ctx, token, *resp.AuthData)
if err != nil {
return nil, errors.WithMessage(err, "auth cache set")
}
return resp, nil
}
if err != nil {
return s.convertAuthReponse(resp), nil
case err != nil:
return nil, errors.WithMessage(err, "auth cache get")
default:
return &domain.AuthenticateAppResponse{
Authenticated: true,
ErrorReason: "",
AuthData: s.convertAuthData(authData),
}, nil
}
}
func (s Authentication) convertAuthReponse(resp *entity.AuthenticateResponse) *domain.AuthenticateAppResponse {
return &domain.AuthenticateAppResponse{
Authenticated: resp.Authenticated,
ErrorReason: resp.ErrorReason,
AuthData: s.convertAuthData(resp.AuthData),
}
}
func (Authentication) convertAuthData(authData *entity.AppAuthData) *domain.AppAuthData {
if authData == nil {
return nil
}
return &domain.AppAuthData{
AppName: authData.AppName,
SystemId: authData.SystemId,
DomainId: authData.DomainId,
ServiceId: authData.ServiceId,
ApplicationId: authData.ApplicationId,
}
return &domain.AuthenticateResponse{
Authenticated: true,
ErrorReason: "",
AuthData: authData,
}, nil
}