-
Notifications
You must be signed in to change notification settings - Fork 0
/
claims.go
85 lines (73 loc) · 1.86 KB
/
claims.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package claims
/*
claims is the content of a JWT token.
*/
import (
"context"
"errors"
"fmt"
"github.com/cristalhq/jwt/v3"
)
type contextKey string
var (
claimsKey = contextKey("claims")
)
/*
{
"namespace_id": "23",
"namespace_path": "factory",
"project_id": "605",
"project_path": "factory/check-my-web",
"user_id": "4",
"user_login": "mlecarme",
"user_email": "mlecarme@bearstech.com",
"pipeline_id": "20364",
"pipeline_source": "push",
"job_id": "106045",
"ref": "main",
"ref_type": "branch",
"ref_protected": "true",
"jti": "fdc1d726-4238-4830-9a3f-178f9d2ba6b0",
"iss": "gitlab.bearstech.com",
"iat": 1643491670,
"nbf": 1643491665,
"exp": 1643495270,
"sub": "job_106045"
}
*/
// Claims represents all the data found in JWT Claims
type Claims struct {
jwt.StandardClaims
NamespaceID string `json:"namespace_id"`
NamespacePath string `json:"namespace_path"`
ProjectID string `json:"project_id"`
ProjectPath string `json:"project_path"`
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserEmail string `json:"user_email"`
PipelineID string `json:"pipeline_id"`
JobID string `json:"job_id"`
Ref string `json:"ref"`
RefType string `json:"ref_type"`
RefProtected string `json:"ref_protected"`
}
// Validate data owner struct
func (c *Claims) Validate() error {
// FIXME: add standard claims check
if c.UserLogin == "" {
return fmt.Errorf("invalid owner name")
}
return nil
}
// ToCtx creates a context containing a user key
func (c *Claims) ToCtx(in context.Context) context.Context {
return context.WithValue(in, claimsKey, *c)
}
// FromCtx extract a user from a context
func FromCtx(ctx context.Context) (*Claims, error) {
c, ok := ctx.Value(claimsKey).(Claims)
if !ok {
return nil, errors.New("No claim found in this context")
}
return &c, nil
}