Conditions | 9 |
Total Lines | 58 |
Code Lines | 33 |
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( |
||
44 | views.NewHTTPResponseVMFromError(errors.New("invalid permission"))) |
||
45 | } |
||
46 | |||
47 | // if the route is public, we don't need to check if the userModel is connected |
||
48 | if p == domain.PermissionNone { |
||
49 | return c.Next() |
||
50 | } |
||
51 | |||
52 | _, span := infrastructures.GetTracerInstance().Tracer().Start(c.UserContext(), "IsConnectedMiddleware") |
||
53 | defer span.End() |
||
54 | |||
55 | // get the token from the request header |
||
56 | tokenHeader := c.Get("Authorization") |
||
57 | // if the token is empty, the userModel is not connected, and we return an error |
||
58 | if tokenHeader == "" { |
||
59 | return c.Status(fiber.StatusUnauthorized).JSON( |
||
60 | views.NewHTTPResponseVMFromError(errors.New("unauthorized: token missing"))) |
||
61 | } |
||
62 | |||
63 | // get the userModel from the token |
||
64 | // if the token is invalid, we return an error |
||
65 | userID, err := config.GetJwtInstance().GetJwt().GetConnectedUserID(c.UserContext(), tokenHeader) |
||
66 | if err != nil { |
||
67 | return c.Status(fiber.StatusUnauthorized).JSON( |
||
68 | views.NewHTTPResponseVMFromError(errors.New("unauthorized: invalid token"))) |
||
69 | } |
||
70 | |||
71 | // get the userModel from the database |
||
72 | userModel, err := j.IUseCase.GetByID(c.UserContext(), userID) |
||
73 | if err != nil { |
||
74 | log.WithContext(c.UserContext()).Error("error getting user / not connected", slog.Any("error", err)) |
||
75 | return c.Status(fiber.StatusUnauthorized).JSON( |
||
76 | views.NewHTTPResponseVMFromError(errors.New("unauthorized: invalid user"))) |
||
77 | } |
||
78 | |||
79 | sentry.ConfigureScope(func(scope *sentry.Scope) { |
||
80 | scope.SetUser(sentry.User{ |
||
81 | ID: strconv.Itoa(int(userModel.ID)), |
||
82 | Username: userModel.Username, |
||
83 | Email: userModel.Email, |
||
84 | }) |
||
85 | }) |
||
86 | |||
87 | // Check permissions |
||
88 | if !j.VerifyPermissions(userModel, p) { |
||
89 | log.WithContext(c.UserContext()).Debug("Not authorized") |
||
90 | return c.Status(fiber.StatusUnauthorized).JSON(views.NewHTTPResponseVMFromError(errors.New("unauthorized: insufficient permissions"))) |
||
91 | } |
||
92 | |||
93 | // Set userModel in locals |
||
94 | SetUserToContext(c, userModel) |
||
95 | span.End() |
||
96 | return c.Next() |
||
97 | } |
||
99 |