@rivascva/dt-idl 1.1.81 → 1.1.82
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/constants.go +6 -0
- package/go/jwt/jwt.go +29 -0
- package/go/log/log.go +4 -4
- package/go/log/utils.go +17 -26
- package/go/models/constants.go +7 -0
- package/go/utils/context.go +8 -7
- package/package.json +1 -1
package/go/jwt/jwt.go
CHANGED
|
@@ -2,6 +2,7 @@ package jwt
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"fmt"
|
|
5
|
+
"time"
|
|
5
6
|
|
|
6
7
|
"github.com/golang-jwt/jwt/v5"
|
|
7
8
|
)
|
|
@@ -19,3 +20,31 @@ func ValidateToken(token string, secret string) (*jwt.Token, error) {
|
|
|
19
20
|
|
|
20
21
|
return parsedToken, nil
|
|
21
22
|
}
|
|
23
|
+
|
|
24
|
+
// NewUserToken creates a new JWT token for the given user id.
|
|
25
|
+
func NewUserToken(secret string, issuer string, userId string, duration time.Duration) (string, error) {
|
|
26
|
+
return newToken(secret, "user", issuer, userId, duration)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// NewServiceToken creates a new JWT token for the given service.
|
|
30
|
+
func NewServiceToken(secret string, issuer string, service string, duration time.Duration) (string, error) {
|
|
31
|
+
return newToken(secret, "service", issuer, service, duration)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// newToken creates a new JWT token.
|
|
35
|
+
func newToken(secret string, ttype string, issuer string, subject string, duration time.Duration) (string, error) {
|
|
36
|
+
t := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
37
|
+
"type": ttype,
|
|
38
|
+
"iss": issuer,
|
|
39
|
+
"sub": subject,
|
|
40
|
+
"iat": time.Now().UnixMilli(),
|
|
41
|
+
"exp": time.Now().Add(duration).UnixMilli(),
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
s, err := t.SignedString([]byte(secret))
|
|
45
|
+
if err != nil {
|
|
46
|
+
return "", fmt.Errorf("unable to sign the JWT token: %w", err)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return s, nil
|
|
50
|
+
}
|
package/go/log/log.go
CHANGED
|
@@ -39,19 +39,19 @@ func NewLog() *Log {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
func (l *Log) Info(ctx context.Context, m any) {
|
|
42
|
-
l.info.Println(toJson(
|
|
42
|
+
l.info.Println(toJson(ctx, m, l.kvs, "info"))
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
func (l *Log) Warn(ctx context.Context, m any) {
|
|
46
|
-
l.warn.Println(toJson(
|
|
46
|
+
l.warn.Println(toJson(ctx, m, l.kvs, "warn"))
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
func (l *Log) Error(ctx context.Context, m any) {
|
|
50
|
-
l.error.Println(toJson(
|
|
50
|
+
l.error.Println(toJson(ctx, m, l.kvs, "error"))
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
func (l *Log) Fatal(ctx context.Context, m any) {
|
|
54
|
-
l.error.Fatal(toJson(
|
|
54
|
+
l.error.Fatal(toJson(ctx, m, l.kvs, "error"))
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
func (l *Log) With(kvs ...any) Logger {
|
package/go/log/utils.go
CHANGED
|
@@ -10,49 +10,40 @@ import (
|
|
|
10
10
|
)
|
|
11
11
|
|
|
12
12
|
// toJson converts the message and key-value pairs to a JSON string.
|
|
13
|
-
func toJson(m any, kvs map[string]any,
|
|
13
|
+
func toJson(ctx context.Context, m any, kvs map[string]any, lvl string) string {
|
|
14
14
|
// copy the key-value pairs into a new map
|
|
15
|
-
out := make(map[string]any, len(kvs)+
|
|
15
|
+
out := make(map[string]any, len(kvs)+10)
|
|
16
16
|
maps.Copy(out, kvs)
|
|
17
17
|
|
|
18
18
|
// add the message
|
|
19
19
|
out["msg"] = m
|
|
20
20
|
|
|
21
21
|
// add the log level
|
|
22
|
-
out["level"] =
|
|
22
|
+
out["level"] = lvl
|
|
23
23
|
|
|
24
24
|
// add the timestamp
|
|
25
25
|
out["timestamp"] = time.Now().UTC().Format(time.RFC3339)
|
|
26
26
|
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return err.Error()
|
|
27
|
+
// add the actor id
|
|
28
|
+
if actorId, err := utils.GetActorIdFromContext(ctx); err == nil {
|
|
29
|
+
out["actorId"] = actorId
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// kvsWithContext returns a new kvs map with added context metadata.
|
|
37
|
-
func kvsWithContext(ctx context.Context, kvs map[string]any) map[string]any {
|
|
38
|
-
// copy the existing key-value pairs into a new map
|
|
39
|
-
newkvs := make(map[string]any, len(kvs)+3)
|
|
40
|
-
maps.Copy(newkvs, kvs)
|
|
41
|
-
|
|
42
|
-
// add the user id to the key-value pairs
|
|
43
|
-
if userId, err := utils.GetUserIdFromContext(ctx); err == nil {
|
|
44
|
-
newkvs["userId"] = userId
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// add the request path to the key-value pairs
|
|
32
|
+
// add the request path
|
|
48
33
|
if requestPath, err := utils.GetRequestPathFromContext(ctx); err == nil {
|
|
49
|
-
|
|
34
|
+
out["requestPath"] = requestPath
|
|
50
35
|
}
|
|
51
36
|
|
|
52
|
-
// add the request id
|
|
37
|
+
// add the request id
|
|
53
38
|
if requestId, err := utils.GetRequestIdFromContext(ctx); err == nil {
|
|
54
|
-
|
|
39
|
+
out["requestId"] = requestId
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// marshal the map to a JSON byte array
|
|
43
|
+
data, err := json.Marshal(out)
|
|
44
|
+
if err != nil {
|
|
45
|
+
return err.Error()
|
|
55
46
|
}
|
|
56
47
|
|
|
57
|
-
return
|
|
48
|
+
return string(data)
|
|
58
49
|
}
|
package/go/models/constants.go
CHANGED
package/go/utils/context.go
CHANGED
|
@@ -18,24 +18,25 @@ func GetTokenFromContext(ctx context.Context) *jwt.Token {
|
|
|
18
18
|
return token
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
//
|
|
22
|
-
|
|
21
|
+
// GetActorIdFromContext retrieves the actor id from the JTW token in the given context.
|
|
22
|
+
// The actor id is the subject of the JWT token, and it represents the user id or service name that initiated the request.
|
|
23
|
+
func GetActorIdFromContext(ctx context.Context) (string, error) {
|
|
23
24
|
// get the token from the context
|
|
24
25
|
token := GetTokenFromContext(ctx)
|
|
25
26
|
if token == nil {
|
|
26
27
|
return "", errors.New("token not found in the context")
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
// get the subject
|
|
30
|
-
|
|
30
|
+
// get the subject from the token
|
|
31
|
+
subject, err := token.Claims.GetSubject()
|
|
31
32
|
if err != nil {
|
|
32
33
|
return "", fmt.Errorf("failed to get the subject from the token: %w", err)
|
|
33
34
|
}
|
|
34
|
-
if
|
|
35
|
-
return "", errors.New("
|
|
35
|
+
if subject == "" {
|
|
36
|
+
return "", errors.New("subject is empty in the token")
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
return
|
|
39
|
+
return subject, nil
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
// GetRequestPathFromContext retrieves the request path from the given context.
|