From 0910add54cebea8769c96dabc5a606235fd9f803 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 4 Oct 2022 17:59:37 +0300 Subject: [PATCH] MSP-1069 update middleware admin auth --- middleware/admin_authenticate.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/middleware/admin_authenticate.go b/middleware/admin_authenticate.go index 439d50b..7dfebde 100644 --- a/middleware/admin_authenticate.go +++ b/middleware/admin_authenticate.go @@ -1,12 +1,15 @@ package middleware import ( + "context" "net/http" "strings" - "github.com/pkg/errors" + "isp-gate-service/domain" "isp-gate-service/httperrors" "isp-gate-service/request" + + "github.com/pkg/errors" ) const ( @@ -14,7 +17,7 @@ const ( ) type AdminAuthenticator interface { - Authenticate(token string) (int, error) + AdminAuthenticate(ctx context.Context, token string) (*domain.AdminAuthenticateResponse, error) } func AdminAuthenticate(auth AdminAuthenticator) Middleware { @@ -24,7 +27,7 @@ func AdminAuthenticate(auth AdminAuthenticator) Middleware { if adminToken == "" { return next.Handle(ctx) } - adminId, err := auth.Authenticate(adminToken) + resp, err := auth.AdminAuthenticate(ctx.Context(), adminToken) if err != nil { return httperrors.New( http.StatusUnauthorized, @@ -32,7 +35,14 @@ func AdminAuthenticate(auth AdminAuthenticator) Middleware { errors.WithMessage(err, "admin authenticate: authenticate"), ) } - ctx.AuthenticateAdmin(adminId, adminToken) + if !resp.Authenticated { + return httperrors.New( + http.StatusUnauthorized, + "invalid admin token", + errors.WithMessage(errors.New(resp.ErrorReason), "admin authenticate: authenticate"), + ) + } + ctx.AuthenticateAdmin(resp.AdminId, adminToken) return next.Handle(ctx) }) }