-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtoken.go
More file actions
153 lines (135 loc) · 4.16 KB
/
token.go
File metadata and controls
153 lines (135 loc) · 4.16 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Copyright 2024-2026 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package token
import (
v1 "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1"
"github.com/golang-jwt/jwt/v4"
)
const (
UserAudience = "user-auth.chainloop"
APIAudience = "api-token-auth.chainloop"
)
type ParsedToken struct {
ID string
OrgID string
TokenType v1.Attestation_Auth_AuthType
}
const (
userAudience = "user-auth.chainloop"
//nolint:gosec
apiTokenAudience = "api-token-auth.chainloop"
federatedTokenAudience = "chainloop"
)
func chooseAudience(audiences []string) string {
// Priority matters for mixed-audience tokens: user/api token identity
// should win over generic federated audience.
for _, candidate := range []string{apiTokenAudience, userAudience, federatedTokenAudience} {
for _, aud := range audiences {
if aud == candidate {
return candidate
}
}
}
return ""
}
// Parse the token and return the type of token. At the moment in Chainloop we have 3 types of tokens:
// 1. User account token
// 2. API token
// 3. Federated token
// Each one of them have an associated audience claim that we use to identify the type of token. If the token is not
// present, nor we cannot match it with one of the expected audience, return nil.
func Parse(token string) (*ParsedToken, error) {
if token == "" {
return nil, nil
}
// Create a parser without claims validation
parser := jwt.NewParser(jwt.WithoutClaimsValidation())
// Parse the token without verification
t, _, err := parser.ParseUnverified(token, jwt.MapClaims{})
if err != nil {
return nil, err
}
// Extract generic claims otherwise, we would have to parse
// the token again to get the claims for each type
claims, ok := t.Claims.(jwt.MapClaims)
if !ok {
return nil, nil
}
// Supports both string and array formats per JWT RFC 7519.
// For multi-audience tokens, prefer the most specific Chainloop auth audience
// instead of taking the first element (order can vary by issuer/runtime).
var audiences []string
switch aud := claims["aud"].(type) {
case string:
audiences = []string{aud}
case []interface{}:
for _, a := range aud {
if s, ok := a.(string); ok && s != "" {
audiences = append(audiences, s)
}
}
default:
return nil, nil
}
if len(audiences) == 0 {
return nil, nil
}
audience := chooseAudience(audiences)
if audience == "" {
return nil, nil
}
pToken := &ParsedToken{}
// Determines token type and id based on audience:
// 1. API Tokens:
// - Type: AUTH_TYPE_API_TOKEN
// - ID: 'jti' claim (JWT ID)
// - OrgID: 'org_id' claim
// 2. User Tokens:
// - Type: AUTH_TYPE_USER
// - ID: 'user_id' claim
// 3. Federated Tokens:
// - Type: AUTH_TYPE_FEDERATED
// - ID: 'iss' claim (issuer URL)
switch audience {
case apiTokenAudience:
pToken.TokenType = v1.Attestation_Auth_AUTH_TYPE_API_TOKEN
if tokenID, ok := claims["jti"].(string); ok {
pToken.ID = tokenID
}
if orgID, ok := claims["org_id"].(string); ok {
pToken.OrgID = orgID
}
case userAudience:
pToken.TokenType = v1.Attestation_Auth_AUTH_TYPE_USER
if userID, ok := claims["user_id"].(string); ok {
pToken.ID = userID
}
case federatedTokenAudience:
pToken.TokenType = v1.Attestation_Auth_AUTH_TYPE_FEDERATED
if issuer, ok := claims["iss"].(string); ok {
pToken.ID = issuer
} else {
return nil, nil
}
default:
return nil, nil
}
// Avoid returning partially filled tokens that would create invalid auth metadata
// in crafting state (e.g. type=UNSPECIFIED or empty ID).
if pToken.TokenType == v1.Attestation_Auth_AUTH_TYPE_UNSPECIFIED || pToken.ID == "" {
return nil, nil
}
return pToken, nil
}