MSP-1070 add secure/authenticate method
This commit is contained in:
1 parent
b79ed21c10
commit
17559ce3ba
4 files changed
+68
No files matched your search
@@ -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,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package domain
|
||||
|
||||
type SecureAuthRequest struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
type SecureAuthResponse struct {
|
||||
Authenticated bool
|
||||
ErrorReason string
|
||||
AdminId int64
|
||||
}
|
||||
@@ -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,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user