From 7d963967f94ca2ba70f11048b412c0411167c5b7 Mon Sep 17 00:00:00 2001 From: Tabuci Octavian Date: Thu, 4 Aug 2022 17:48:26 +0300 Subject: [PATCH] broker service auth fixes --- core/services/broker/authentication.go | 61 +++++++++++++------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/core/services/broker/authentication.go b/core/services/broker/authentication.go index 49982f2..d8150e2 100644 --- a/core/services/broker/authentication.go +++ b/core/services/broker/authentication.go @@ -8,39 +8,40 @@ import ( ) func (b *Broker) CheckIsAuthenticated() gin.HandlerFunc { - return authentication. - CheckIsAuthenticated(). - //ByHeaderKeys([]string{"Auth-Token"}). - //ByGetParams([]string{"AuthToken"}). - OnTokenInValid(func(a *authentication.Auth) { - warn := func() *zerolog.Event { - return b.LWarnF("CheckIsAuthenticated") - } - warn().Msg("authentication token is not available") + return func(context *gin.Context) { + authentication.New().SetGinContext(context). + //ByHeaderKeys([]string{"Auth-Token"}). + //ByGetParams([]string{"AuthToken"}). + OnTokenInValid(func(a *authentication.Auth) { + warn := func() *zerolog.Event { + return b.LWarnF("CheckIsAuthenticated") + } + warn().Msg("authentication token is not available") - a.Abort( - 1000, - 403, - "Authentication token is not available!", - ) - }). - OnTokenValid(func(a *authentication.Auth) { - // Check here - info := func() *zerolog.Event { - return b.LInfoF("CheckIsAuthenticated") - } - - token := a.GetToken() - if token != b.config.AuthToken { - info().Str("received_token", token).Msg(color.Style{color.LightRed, color.Bold}.Render("authentication failed")) a.Abort( - 2000, + 1000, 403, - "Authentication Token is Invalid!", + "Authentication token is not available!", ) - } - info().Msg(color.Style{color.LightGreen, color.Bold}.Render("authentication success")) + }). + OnTokenValid(func(a *authentication.Auth) { + // Check here + info := func() *zerolog.Event { + return b.LInfoF("CheckIsAuthenticated") + } + + token := a.GetToken() + if token != b.config.AuthToken { + info().Str("received_token", token).Msg(color.Style{color.LightRed, color.Bold}.Render("authentication failed")) + a.Abort( + 2000, + 403, + "Authentication Token is Invalid!", + ) + } + info().Msg(color.Style{color.LightGreen, color.Bold}.Render("authentication success")) - }). - GetHandlerFunc() + }). + Check() + } }