-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_id.go
228 lines (178 loc) · 7.44 KB
/
client_id.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) 2021-2023, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package tokens
import (
"crypto/ed25519"
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"strings"
"time"
"github.com/golang-jwt/jwt/v4"
)
type ClientPermissions struct {
// StreamsAdmin enables full access to Choria Streams for all APIs
StreamsAdmin bool `json:"streams_admin,omitempty"`
// StreamsUser enables user level access to Choria Streams, no stream admin features
StreamsUser bool `json:"streams_user,omitempty"`
// EventsViewer allows viewing lifecycle and auto agent events
EventsViewer bool `json:"events_viewer,omitempty"`
// ElectionUser allows using leader elections
ElectionUser bool `json:"election_user,omitempty"`
// SystemUser allows accessing the Choria Broker system account without verified TLS
SystemUser bool `json:"system_user,omitempty"`
// Governor enables access to Governors, cannot make new ones, also requires Streams permission
Governor bool `json:"governor,omitempty"`
// OrgAdmin has access to all subjects and broker system account
OrgAdmin bool `json:"org_admin,omitempty"`
// FleetManagement enables access to the choria server fleet for RPCs
FleetManagement bool `json:"fleet_management,omitempty"`
// SignedFleetManagement requires a user to have a valid signature by an AuthenticationDelegator to interact with the fleet
SignedFleetManagement bool `json:"signed_fleet_management,omitempty"`
// ExtendedServiceLifetime allows a token to have a longer than common lifetime, suitable for services users
ExtendedServiceLifetime bool `json:"service,omitempty"`
// AuthenticationDelegator has the right to sign requests on behalf of others
AuthenticationDelegator bool `json:"authentication_delegator,omitempty"`
// ServerProvisioner is required by Choria Provisioner to connect to the account where unprovisioned nodes exist
ServerProvisioner bool `json:"provisioner,omitempty"`
}
// ClientIDClaims represents a user and all AAA Authenticators should create a JWT using this format
//
// The "purpose" claim should be set to ClientIDPurpose
type ClientIDClaims struct {
// CallerID is the choria caller id that will be set for this user for AAA purposes, typically provider=caller format
CallerID string `json:"callerid"`
// AllowedAgents is a list of agent names or agent.action names this user can perform
AllowedAgents []string `json:"agents,omitempty"`
// OrganizationUnit broker account a user should belong to, set to 'choria' now and issuing organization
OrganizationUnit string `json:"ou,omitempty"`
// UserProperties is a list of arbitrary properties that can be set for a user, OPA Policies in the token can access these
UserProperties map[string]string `json:"user_properties,omitempty"`
// OPAPolicy is a Open Policy Agent document to be used by the signer to limit the users actions
OPAPolicy string `json:"opa_policy,omitempty"`
// Permissions sets additional permissions for a client
Permissions *ClientPermissions `json:"permissions,omitempty"`
// AdditionalPublishSubjects are additional subjects the client can publish to
AdditionalPublishSubjects []string `json:"pub_subjects,omitempty"`
// AdditionalSubscribeSubjects are additional subjects the client can subscribe to
AdditionalSubscribeSubjects []string `json:"sub_subjects,omitempty"`
StandardClaims
}
var (
ErrNotAClientToken = fmt.Errorf("not a client token")
ErrInvalidClientCallerID = fmt.Errorf("invalid caller id in token")
)
// UniqueID returns the caller id and unique id used to generate private inboxes
func (c *ClientIDClaims) UniqueID() (id string, uid string) {
return c.CallerID, fmt.Sprintf("%x", md5.Sum([]byte(c.CallerID)))
}
// NewClientIDClaims generates new ClientIDClaims
func NewClientIDClaims(callerID string, allowedAgents []string, org string, properties map[string]string, opaPolicy string, issuer string, validity time.Duration, perms *ClientPermissions, pk ed25519.PublicKey) (*ClientIDClaims, error) {
if callerID == "" {
return nil, fmt.Errorf("caller id is required")
}
stdClaims, err := newStandardClaims(issuer, ClientIDPurpose, validity, false)
if err != nil {
return nil, err
}
pubKey := ""
if pk != nil {
pubKey = hex.EncodeToString(pk)
}
if org == "" {
org = defaultOrg
}
stdClaims.PublicKey = pubKey
return &ClientIDClaims{
CallerID: callerID,
AllowedAgents: allowedAgents,
OrganizationUnit: org,
UserProperties: properties,
OPAPolicy: opaPolicy,
Permissions: perms,
StandardClaims: *stdClaims,
}, nil
}
// UnverifiedCallerFromClientIDToken extracts the caller id from a client token.
//
// The token is not verified as this is mainly used on clents who might not have
// the signer public key to verify the certificate. This is safe as the signer
// will later verify the token anyway.
//
// # Further, at the moment, we do not verity the Purpose for backward compatibility
//
// An empty callerid will result in an error
func UnverifiedCallerFromClientIDToken(token string) (*jwt.Token, string, error) {
claims := &ClientIDClaims{}
t, _, err := new(jwt.Parser).ParseUnverified(token, claims)
if err != nil {
return nil, "", err
}
if !IsClientIDToken(claims.StandardClaims) {
return nil, "", ErrNotAClientToken
}
if claims.CallerID == "" {
return nil, "", ErrInvalidClientCallerID
}
return t, claims.CallerID, nil
}
// IsClientIDTokenString calls IsClientIDToken on the token in a string
func IsClientIDTokenString(token string) (bool, error) {
claims := &ClientIDClaims{}
_, _, err := new(jwt.Parser).ParseUnverified(token, claims)
if err != nil {
return false, err
}
return IsClientIDToken(claims.StandardClaims), nil
}
// IsClientIDToken determines if this is a client identifying token
func IsClientIDToken(claims StandardClaims) bool {
return claims.Purpose == ClientIDPurpose
}
// ParseClientIDTokenUnverified parses the client token in an unverified manner.
func ParseClientIDTokenUnverified(token string) (*ClientIDClaims, error) {
claims := &ClientIDClaims{}
_, _, err := new(jwt.Parser).ParseUnverified(token, claims)
if err != nil {
return nil, err
}
if !IsClientIDToken(claims.StandardClaims) {
return nil, ErrNotAClientToken
}
return claims, nil
}
// ParseClientIDToken parses token and verifies it with pk
func ParseClientIDToken(token string, pk any, verifyPurpose bool) (*ClientIDClaims, error) {
claims := &ClientIDClaims{}
err := ParseToken(token, claims, pk)
if err != nil {
return nil, fmt.Errorf("could not parse client id token: %w", err)
}
if verifyPurpose && !IsClientIDToken(claims.StandardClaims) {
return nil, ErrNotAClientToken
}
// if we have a tcs we require an issuer expiry to be set and it to not have expired
if claims.TrustChainSignature != "" && strings.HasPrefix(claims.Issuer, ChainIssuerPrefix) {
if !claims.verifyIssuerExpiry(true) {
return nil, jwt.ErrTokenExpired
}
}
return claims, nil
}
// ParseClientIDTokenWithKeyfile parses token and verifies it with the RSA Public key in pkFile, does not support ed25519 public keys in a file
func ParseClientIDTokenWithKeyfile(token string, pkFile string, verifyPurpose bool) (*ClientIDClaims, error) {
if pkFile == "" {
return nil, fmt.Errorf("invalid public key file")
}
certdat, err := os.ReadFile(pkFile)
if err != nil {
return nil, fmt.Errorf("could not read validation certificate: %s", err)
}
pk, err := readRSAOrED25519PublicData(certdat)
if err != nil {
return nil, err
}
return ParseClientIDToken(token, pk, verifyPurpose)
}