| Conditions | 9 |
| Total Lines | 54 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | package controllers |
||
| 39 | func (j *JwtController) IsConnectedMiddleware(p domain.Permission) func(c *fiber.Ctx) error { |
||
| 40 | return func(c *fiber.Ctx) error { |
||
| 41 | // check if the permission is valid |
||
| 42 | if !p.IsValid() { |
||
| 43 | return c.Status(fiber.StatusInternalServerError).JSON(views.NewHTTPResponseVMFromError(errors.New("invalid permission"))) |
||
| 44 | } |
||
| 45 | |||
| 46 | // if the route is public, we don't need to check if the userModel is connected |
||
| 47 | if p == domain.PermissionNone { |
||
| 48 | return c.Next() |
||
| 49 | } |
||
| 50 | |||
| 51 | _, span := infrastructures.GetFiberTracer().Start(c.UserContext(), "IsConnectedMiddleware") |
||
| 52 | defer span.End() |
||
| 53 | |||
| 54 | // get the token from the request header |
||
| 55 | tokenHeader := c.Get("Authorization") |
||
| 56 | // if the token is empty, the userModel is not connected, and we return an error |
||
| 57 | if tokenHeader == "" { |
||
| 58 | return c.Status(fiber.StatusUnauthorized).JSON(views.NewHTTPResponseVMFromError(errors.New("unauthorized: token missing"))) |
||
| 59 | } |
||
| 60 | |||
| 61 | // get the userModel from the token |
||
| 62 | // if the token is invalid, we return an error |
||
| 63 | userID, err := config.GetJwtInstance().GetConnectedUserID(c.UserContext(), tokenHeader) |
||
| 64 | if err != nil { |
||
| 65 | return c.Status(fiber.StatusUnauthorized).JSON(views.NewHTTPResponseVMFromError(errors.New("unauthorized: invalid token"))) |
||
| 66 | } |
||
| 67 | |||
| 68 | // get the userModel from the database |
||
| 69 | userModel, err := j.IUseCase.GetByID(c.UserContext(), userID) |
||
| 70 | if err != nil { |
||
| 71 | otelzap.Ctx(c.UserContext()).Error("error getting user / not connected", zap.Error(err)) |
||
| 72 | return c.Status(fiber.StatusUnauthorized).JSON(views.NewHTTPResponseVMFromError(errors.New("unauthorized: invalid user"))) |
||
| 73 | } |
||
| 74 | |||
| 75 | sentry.ConfigureScope(func(scope *sentry.Scope) { |
||
| 76 | scope.SetUser(sentry.User{ |
||
| 77 | ID: strconv.Itoa(int(userModel.ID)), |
||
| 78 | Username: userModel.Username, |
||
| 79 | Email: userModel.Email, |
||
| 80 | }) |
||
| 81 | }) |
||
| 82 | |||
| 83 | // Check permissions |
||
| 84 | if !j.VerifyPermissions(userModel, p) { |
||
| 85 | otelzap.Ctx(c.UserContext()).Warn("Not authorized", zap.Error(errors.New("unauthorized: insufficient permissions"))) |
||
| 86 | return c.Status(fiber.StatusUnauthorized).JSON(views.NewHTTPResponseVMFromError(errors.New("unauthorized: insufficient permissions"))) |
||
| 87 | } |
||
| 88 | |||
| 89 | // Set userModel in locals |
||
| 90 | SetUserToContext(c, userModel) |
||
| 91 | span.End() |
||
| 92 | return c.Next() |
||
| 93 | } |
||
| 95 |