From 6da1f91c2f59f69820ecdfbe6933490162d6e6cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20P=C3=A9rez?= Date: Mon, 16 Sep 2024 20:34:56 +0100 Subject: [PATCH] chore: formating --- handlers.go | 24 ++++++++++++------------ jwt.go | 24 ++++++++++++------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/handlers.go b/handlers.go index d1a2ff9..d36be1c 100644 --- a/handlers.go +++ b/handlers.go @@ -15,7 +15,7 @@ func user_has_role(usr *User, c *gin.Context, rtenv *RouteEnv, role string) (*Us var okusr bool if usr == nil { idusr := rtenv.AuthMiddleware.IdentityHandler(c) - user, okusr = idusr.(*User) + user, okusr = idusr.(*User) if !okusr || len(user.UserName) == 0 { return nil,false } @@ -36,17 +36,17 @@ func user_has_role(usr *User, c *gin.Context, rtenv *RouteEnv, role string) (*Us return user,hasRole } func get_page_handle(c *gin.Context, rtenv *RouteEnv) { - tkn := "" - id := c.Params.ByName(rtenv.Cfg.Routes["page"].Param) - role := rtenv.Cfg.AdminRole - hasRole := false - mdlUsr, okmdl := rtenv.MdlsUsrs[id] - if okmdl { - if rtenv.Cfg.UseAuthz { - hasRole,_ = rtenv.Enforcer.HasRoleForUser(mdlUsr.User, role) - } - logRoute(c,rtenv,"page",fmt.Sprintf("get /page/%s", id),fmt.Sprintf("get %s (%s %v) %s",mdlUsr.User,role,hasRole,tkn)) - if rtenv.Cfg.UseJWT { + tkn := "" + id := c.Params.ByName(rtenv.Cfg.Routes["page"].Param) + role := rtenv.Cfg.AdminRole + hasRole := false + mdlUsr, okmdl := rtenv.MdlsUsrs[id] + if okmdl { + if rtenv.Cfg.UseAuthz { + hasRole,_ = rtenv.Enforcer.HasRoleForUser(mdlUsr.User, role) + } + logRoute(c,rtenv,"page",fmt.Sprintf("get /page/%s", id),fmt.Sprintf("get %s (%s %v) %s",mdlUsr.User,role,hasRole,tkn)) + if rtenv.Cfg.UseJWT { c.HTML(http.StatusOK, "welcome", gin.H{ "title": fmt.Sprintf("Main website %s for %s (%v)",id,mdlUsr.User,hasRole), "token": tkn, diff --git a/jwt.go b/jwt.go index 9b11480..1ca40e9 100644 --- a/jwt.go +++ b/jwt.go @@ -33,7 +33,7 @@ import ( // func makeTokenString(mw *jwt.GinJWTMiddleware,data interface{}) (string,error) { func makeTokenString(rtenv *RouteEnv,data interface{}) (string,error) { if rtenv.AuthMiddleware == nil { - return "", nil + return "", nil } token := gojwt.New(gojwt.GetSigningMethod(rtenv.AuthMiddleware.SigningAlgorithm)) claims := token.Claims.(gojwt.MapClaims) @@ -47,13 +47,13 @@ func makeTokenString(rtenv *RouteEnv,data interface{}) (string,error) { claims["orig_iat"] = rtenv.AuthMiddleware.TimeFunc().Unix() var tokenString string var err error - if rtenv.Cfg.SigningAlgorithm == "RS256" { + if rtenv.Cfg.SigningAlgorithm == "RS256" { // if mw.usingPublicKeyAlgo() { keyData, _ := ioutil.ReadFile(rtenv.Cfg.JwtKeyPem) - signKey, _ := gojwt.ParseRSAPrivateKeyFromPEM(keyData) - tokenString, err = token.SignedString(signKey) + signKey, _ := gojwt.ParseRSAPrivateKeyFromPEM(keyData) + tokenString, err = token.SignedString(signKey) } else { - tokenString, err = token.SignedString(rtenv.AuthMiddleware.Key) + tokenString, err = token.SignedString(rtenv.AuthMiddleware.Key) } return tokenString, err } @@ -95,9 +95,9 @@ func getJwt(rtenv *RouteEnv)*jwt.GinJWTMiddleware { password := loginVals.Password if val, ok := rtenv.Users.Accounts[userID]; ok { pasw,_ := b64.StdEncoding.DecodeString(password) - pass := string(pasw) - txtdata,err := decrypt(val.Passwd, string(CRYPTKEY)) - if err == nil && txtdata == pass { + pass := string(pasw) + txtdata,err := decrypt(val.Passwd, string(CRYPTKEY)) + if err == nil && txtdata == pass { return &User{ UserName: val.Id, LastName: "", @@ -110,12 +110,12 @@ func getJwt(rtenv *RouteEnv)*jwt.GinJWTMiddleware { Authorizator: func(data interface{}, c *gin.Context) bool { if v, ok := data.(*User); ok { if v.UserName == rtenv.Cfg.PubUser { - return true + return true } if _, ok := rtenv.Users.Accounts[v.UserName]; ok { - return true - } else { - return false + return true + } else { + return false } } return false