@rivascva/dt-idl 1.1.83 → 1.1.85
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/{jwt → auth}/jwt.go +1 -1
- package/go/auth/models.go +9 -0
- package/go/auth/utils.go +34 -0
- package/go/middlewares/middlewares.go +2 -2
- package/go/utils/context.go +18 -0
- package/package.json +1 -1
- package/go/jwt/constants.go +0 -6
package/go/{jwt → auth}/jwt.go
RENAMED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
package auth
|
|
2
|
+
|
|
3
|
+
import "time"
|
|
4
|
+
|
|
5
|
+
// DefaultServiceTokenDuration is the default duration for service tokens.
|
|
6
|
+
const DefaultServiceTokenDuration = 30 * time.Minute
|
|
7
|
+
|
|
8
|
+
// approvedServices is a list of services that are allowed to access sensitive data.
|
|
9
|
+
var approvedServices = []string{"dt-trade-service", "dt-portfolio-service", "dt-asset-service", "dt-user-service"}
|
package/go/auth/utils.go
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
package auth
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"fmt"
|
|
6
|
+
"slices"
|
|
7
|
+
|
|
8
|
+
"github.com/RivasCVA/dt-idl/go/utils"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
// CanActorAccessUserId checks if the actor is allowed to access data for the user id.
|
|
12
|
+
func CanActorAccessUserId(ctx context.Context, userId string) (bool, error) {
|
|
13
|
+
// get the actor id from the context
|
|
14
|
+
actorId, err := utils.GetActorIdFromContext(ctx)
|
|
15
|
+
if err != nil {
|
|
16
|
+
return false, fmt.Errorf("failed to get the actor id from the context: %w", err)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// get the token type from the context
|
|
20
|
+
tokenType, err := utils.GetTokenTypeFromContext(ctx)
|
|
21
|
+
if err != nil {
|
|
22
|
+
return false, fmt.Errorf("failed to get the token type from the context: %w", err)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// check if the actor is allowed to access the user id
|
|
26
|
+
switch tokenType {
|
|
27
|
+
case "user":
|
|
28
|
+
return actorId == userId, nil
|
|
29
|
+
case "service":
|
|
30
|
+
return slices.Contains(approvedServices, actorId), nil
|
|
31
|
+
default:
|
|
32
|
+
return false, fmt.Errorf("invalid token type %s", tokenType)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -6,7 +6,7 @@ import (
|
|
|
6
6
|
"slices"
|
|
7
7
|
"strings"
|
|
8
8
|
|
|
9
|
-
"github.com/RivasCVA/dt-idl/go/
|
|
9
|
+
"github.com/RivasCVA/dt-idl/go/auth"
|
|
10
10
|
"github.com/RivasCVA/dt-idl/go/log"
|
|
11
11
|
"github.com/RivasCVA/dt-idl/go/models"
|
|
12
12
|
"github.com/RivasCVA/dt-idl/go/write"
|
|
@@ -35,7 +35,7 @@ func GetAuthMiddleware(secret string) func(http.Handler) http.Handler {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
// validate the token
|
|
38
|
-
token, err :=
|
|
38
|
+
token, err := auth.ValidateToken(arr[1], secret)
|
|
39
39
|
if err != nil {
|
|
40
40
|
write.ResponseWithError(r.Context(), w, http.StatusUnauthorized, "invalid token")
|
|
41
41
|
return
|
package/go/utils/context.go
CHANGED
|
@@ -39,6 +39,24 @@ func GetActorIdFromContext(ctx context.Context) (string, error) {
|
|
|
39
39
|
return subject, nil
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
// GetTokenTypeFromContext retrieves the token type from the JWT token in the given context.
|
|
43
|
+
// The token type is a custom claim in the JWT token, and it represents the type of the token (e.g. user or service).
|
|
44
|
+
func GetTokenTypeFromContext(ctx context.Context) (string, error) {
|
|
45
|
+
// get the token from the context
|
|
46
|
+
token := GetTokenFromContext(ctx)
|
|
47
|
+
if token == nil {
|
|
48
|
+
return "", errors.New("token not found in the context")
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// get the token type from the token
|
|
52
|
+
tokenType, ok := token.Claims.(jwt.MapClaims)["type"].(string)
|
|
53
|
+
if !ok {
|
|
54
|
+
return "", errors.New("token type not found in the token")
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return tokenType, nil
|
|
58
|
+
}
|
|
59
|
+
|
|
42
60
|
// GetRequestPathFromContext retrieves the request path from the given context.
|
|
43
61
|
func GetRequestPathFromContext(ctx context.Context) (string, error) {
|
|
44
62
|
requestPath, ok := ctx.Value(models.RequestPathKey).(string)
|
package/package.json
CHANGED