From 17559ce3bada898014288d9e4ad883a5df5431c8 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Fri, 7 Oct 2022 15:38:36 +0300 Subject: [PATCH] MSP-1070 add secure/authenticate method --- assembly/locator.go | 2 ++ controller/secure.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ domain/secure.go | 11 ++++++++++ routes/routes.go | 7 +++++++ 4 files changed, 68 insertions(+) create mode 100644 controller/secure.go create mode 100644 domain/secure.go diff --git a/assembly/locator.go b/assembly/locator.go index 1e150ad..4efc6ed 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -43,6 +43,7 @@ func (l Locator) Handler(cfg conf.Remote) isp.BackendServiceServer { userController := controller.NewUser(userService) customizationController := controller.NewCustomization(cfg.UiDesign) authController := controller.NewAuth(authService, l.logger) + secureController := controller.NewSecure(tokenService) handler := routes.Handler( endpoint.DefaultWrapper( @@ -53,6 +54,7 @@ func (l Locator) Handler(cfg conf.Remote) isp.BackendServiceServer { User: userController, Customization: customizationController, Auth: authController, + Secure: secureController, }, ) diff --git a/controller/secure.go b/controller/secure.go new file mode 100644 index 0000000..310b57d --- /dev/null +++ b/controller/secure.go @@ -0,0 +1,48 @@ +package controller + +import ( + "context" + + "github.com/pkg/errors" + "msp-admin-service/domain" +) + +type SecureService interface { + GetUserId(ctx context.Context, token string) (int64, error) +} + +type Secure struct { + service SecureService +} + +func NewSecure(service SecureService) Secure { + return Secure{ + service: service, + } +} + +func (s Secure) Authenticate(ctx context.Context, req domain.SecureAuthRequest) (*domain.SecureAuthResponse, error) { + adminId, err := s.service.GetUserId(ctx, req.Token) + switch { + case errors.Is(err, domain.ErrTokenExpired): + return &domain.SecureAuthResponse{ + Authenticated: false, + ErrorReason: domain.ErrTokenExpired.Error(), + AdminId: 0, + }, nil + case errors.Is(err, domain.ErrTokenNotFound): + return &domain.SecureAuthResponse{ + Authenticated: false, + ErrorReason: domain.ErrTokenNotFound.Error(), + AdminId: 0, + }, nil + case err != nil: + return nil, err + default: + return &domain.SecureAuthResponse{ + Authenticated: true, + ErrorReason: "", + AdminId: adminId, + }, nil + } +} diff --git a/domain/secure.go b/domain/secure.go new file mode 100644 index 0000000..de92de7 --- /dev/null +++ b/domain/secure.go @@ -0,0 +1,11 @@ +package domain + +type SecureAuthRequest struct { + Token string +} + +type SecureAuthResponse struct { + Authenticated bool + ErrorReason string + AdminId int64 +} diff --git a/routes/routes.go b/routes/routes.go index eaeb563..552c2aa 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -12,6 +12,7 @@ type Controllers struct { Auth controller.Auth User controller.User Customization controller.Customization + Secure controller.Secure } func EndpointDescriptors() []cluster.EndpointDescriptor { @@ -82,5 +83,11 @@ func endpointDescriptors(c Controllers) []cluster.EndpointDescriptor { UserAuthRequired: false, Handler: c.User.DeleteUser, }, + { + Path: "admin/secure/authenticate", + Inner: true, + UserAuthRequired: false, + Handler: c.Secure.Authenticate, + }, } }