@rivascva/dt-idl 1.1.65 → 1.1.67

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.
@@ -0,0 +1,7 @@
1
+ package models
2
+
3
+ // ContextKey is the type used to define keys for the request context.
4
+ type ContextKey string
5
+
6
+ // TokenKey is the key used to store the JWT token in the request context.
7
+ const TokenKey = ContextKey("token")
@@ -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
+ // Returns a middleware function that authenticates all requests except for the login endpoints.
16
+ // It attaches the token to the request context.
17
+ func GetAuthMiddleware(secret string) func(http.Handler) http.Handler {
18
+ responder := responder.NewStandardResponder(logger.NewConsoleLoggerWithPrefix("GetAuthMiddleware:"), "ERROR")
19
+ return func(next http.Handler) http.Handler {
20
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21
+ // check if the request is for the login endpoint
22
+ if slices.Contains([]string{"/v1/login", "/v1/login/provider"}, r.URL.Path) {
23
+ next.ServeHTTP(w, r)
24
+ return
25
+ }
26
+
27
+ // check if the request has a bearer authorization header
28
+ authorizationHeader := r.Header.Get("Authorization")
29
+ arr := strings.Split(authorizationHeader, " ")
30
+ if len(arr) != 2 || arr[0] != "Bearer" {
31
+ responder.WriteError(w, http.StatusUnauthorized, "invalid authorization header")
32
+ return
33
+ }
34
+
35
+ // validate the JWT token
36
+ token, err := jwt.ValidateToken(arr[1], secret)
37
+ if err != nil {
38
+ responder.WriteError(w, http.StatusUnauthorized, "invalid token")
39
+ return
40
+ }
41
+
42
+ // set the token in the request context
43
+ ctx := context.WithValue(r.Context(), models.TokenKey, token)
44
+ r = r.WithContext(ctx)
45
+
46
+ next.ServeHTTP(w, r)
47
+ })
48
+ }
49
+ }
50
+
51
+ // Adds shared headers to all responses.
52
+ func CommonHeaders(next http.Handler) http.Handler {
53
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
54
+ w.Header().Add("content-type", "application/json")
55
+ next.ServeHTTP(w, r)
56
+ })
57
+ }
package/go.mod CHANGED
@@ -1,3 +1,5 @@
1
1
  module github.com/RivasCVA/dt-idl
2
2
 
3
3
  go 1.23
4
+
5
+ require github.com/golang-jwt/jwt/v5 v5.2.2
package/go.sum ADDED
@@ -0,0 +1,2 @@
1
+ github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
2
+ github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivascva/dt-idl",
3
- "version": "1.1.65",
3
+ "version": "1.1.67",
4
4
  "description": "Dream Trade - Interface Definition Language",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",