@rivascva/dt-idl 1.1.65 → 1.1.66
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/go/models/constants.go +7 -0
- package/go/utils/jwt/jwt.go +21 -0
- package/go/utils/middlewares/middlewares.go +57 -0
- package/go.mod +2 -0
- package/go.sum +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
package jwt
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"fmt"
|
|
5
|
+
|
|
6
|
+
"github.com/golang-jwt/jwt/v5"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
// Validates the given JWT token using the provided secret.
|
|
10
|
+
func ValidateToken(token string, secret string) (*jwt.Token, error) {
|
|
11
|
+
// create a JWT parser with the HS256 signing method
|
|
12
|
+
parser := jwt.NewParser(jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))
|
|
13
|
+
|
|
14
|
+
// validate the JWT token
|
|
15
|
+
parsedToken, err := parser.Parse(token, func(t *jwt.Token) (any, error) { return []byte(secret), nil })
|
|
16
|
+
if err != nil {
|
|
17
|
+
return nil, fmt.Errorf("ValidateToken: unable to validate the JWT token: %w", err)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return parsedToken, nil
|
|
21
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
package middlewares
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"net/http"
|
|
6
|
+
"slices"
|
|
7
|
+
"strings"
|
|
8
|
+
|
|
9
|
+
"github.com/RivasCVA/dt-idl/go/models"
|
|
10
|
+
"github.com/RivasCVA/dt-idl/go/utils/jwt"
|
|
11
|
+
"github.com/RivasCVA/dt-idl/go/utils/logger"
|
|
12
|
+
"github.com/RivasCVA/dt-idl/go/utils/responder"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
// Adds shared headers to all responses.
|
|
16
|
+
func CommonHeaders(next http.Handler) http.Handler {
|
|
17
|
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
18
|
+
w.Header().Add("content-type", "application/json")
|
|
19
|
+
next.ServeHTTP(w, r)
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Returns a middleware function that perform authentication checks for a request.
|
|
24
|
+
// It attaches the token to the request context.
|
|
25
|
+
func GetAuthMiddleware(secret string) func(http.Handler) http.Handler {
|
|
26
|
+
responder := responder.NewStandardResponder(logger.NewConsoleLoggerWithPrefix("GetAuthMiddleware:"), "ERROR")
|
|
27
|
+
return func(next http.Handler) http.Handler {
|
|
28
|
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
29
|
+
// check if the request is for the login endpoint
|
|
30
|
+
if slices.Contains([]string{"/v1/login", "/v1/login/provider"}, r.URL.Path) {
|
|
31
|
+
next.ServeHTTP(w, r)
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// check if the request has a bearer authorization header
|
|
36
|
+
authorizationHeader := r.Header.Get("Authorization")
|
|
37
|
+
arr := strings.Split(authorizationHeader, " ")
|
|
38
|
+
if len(arr) != 2 || arr[0] != "Bearer" {
|
|
39
|
+
responder.WriteError(w, http.StatusUnauthorized, "invalid authorization header")
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// validate the JWT token
|
|
44
|
+
token, err := jwt.ValidateToken(arr[1], secret)
|
|
45
|
+
if err != nil {
|
|
46
|
+
responder.WriteError(w, http.StatusUnauthorized, "invalid token")
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// set the token in the request context
|
|
51
|
+
ctx := context.WithValue(r.Context(), models.TokenKey, token)
|
|
52
|
+
r = r.WithContext(ctx)
|
|
53
|
+
|
|
54
|
+
next.ServeHTTP(w, r)
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}
|
package/go.mod
CHANGED
package/go.sum
ADDED