This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
credential.go
388 lines (367 loc) · 13.3 KB
/
credential.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package openidvc
import (
"crypto/ecdsa"
"crypto/sha512"
"encoding/json"
"errors"
"net/http"
"strings"
"time"
"github.com/gossif/diddoc"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jws"
"github.com/lestrrat-go/jwx/v2/jwt"
)
type ProofOfPossession struct {
ProofType ProofType `json:"proof_type"`
Jwt string `json:"jwt"`
}
type CredentialRequest struct {
ClientId string `json:"client_id"`
AuthorizationDetails string `json:"authorization_details"`
CNonce string `json:"c_nonce"`
CNonceExpiresIn time.Duration `json:"c_nonce_expires_in"`
BearerTokenSHA512 [64]byte `json:"token_hash"`
BearerTokenExpiresIn time.Duration `json:"token_type"`
Deferred bool `json:"deferred"`
Format CredentialFormat `json:"format"`
// object containing the proof of posession of the did
Proof ProofOfPossession `json:"proof"`
// the following two attriutes are according to appendix E of the open id specification
CredentialTypes []string `json:"types"`
CredentialDefinition json.RawMessage `json:"credential_definition"`
}
type CredentialResponse struct {
Format CredentialFormat `json:"format"`
Credential string `json:"credential"`
AcceptanceToken string `json:"acceptance_token"`
CNonce string `json:"c_nonce"`
CNonceExpiresIn time.Duration `json:"c_nonce_expires_in"`
}
type CredentialResponseOption func(*optionalParameters)
// WithVerifiableCredentialn is the option to pass the credential for issuance
func WithVerifiableCredentialn(vc string, f CredentialFormat) CredentialResponseOption {
return func(o *optionalParameters) {
o.verifiableCredential = vc
o.format = f
}
}
// WithCredentialDeferred is the option to defer the issuance of the credential
func WithCredentialDeferred() CredentialResponseOption {
return func(o *optionalParameters) {
o.deferred = true
}
}
// WithAcceptanceTokenSigningKey is the option for the algorithm and signing key
func WithAcceptanceTokenSigningKey(alg jwa.KeyAlgorithm, sigKey interface{}) CredentialResponseOption {
return func(o *optionalParameters) {
o.signingKey = sigKey
o.algorithm = alg
}
}
// NewCredentialRequest receives an open id for credential issuance request
// the options must contain the signing key of the access token
func (i *credentialIssuance) NewCredentialRequest(r *http.Request, options ...TokenResponseOption) (CredentialRequest, error) {
authorizationHeader := r.Header.Get("Authorization")
authorizedCredentialRequest, err := validateBearerToken(authorizationHeader, options...)
if err != nil {
return CredentialRequest{}, errInvalidToken
}
contentType := r.Header.Get("Content-Type")
if !strings.Contains(contentType, "application/json") {
// Deferred credential request has application/x-www-form-urlencoded
if !strings.Contains(contentType, "application/x-www-form-urlencoded") {
return CredentialRequest{}, errors.New("invalid_content_type")
}
}
credRequest := CredentialRequest{}
decoder := json.NewDecoder(r.Body)
// the json must match the known attributes in the struct
decoder.DisallowUnknownFields()
err = decoder.Decode(&credRequest)
if err != nil {
return CredentialRequest{}, errInternalServerError
}
if err = credRequest.Validate(); err != nil {
return CredentialRequest{}, err
}
credRequest.AuthorizationDetails = authorizedCredentialRequest.AuthorizationDetails
return credRequest, nil
}
// NewDeferredCredentialRequest receives a issuance request for a deferred credential
func (i *credentialIssuance) NewDeferredCredentialRequest(r *http.Request, options ...TokenResponseOption) (CredentialRequest, error) {
authorizationHeader := r.Header.Get("Authorization")
authorizedCredentialRequest, err := validateBearerToken(authorizationHeader, options...)
if err != nil {
return CredentialRequest{}, errInvalidToken
}
contentType := r.Header.Get("Content-Type")
// Deferred credential request has application/x-www-form-urlencoded
if !strings.Contains(contentType, "application/x-www-form-urlencoded") {
return CredentialRequest{}, errors.New("invalid_content_type")
}
return authorizedCredentialRequest, nil
}
// Validate validates the credential request (only content)
// The client id should be the host of the
func (c *CredentialRequest) Validate() error {
// openid specificies three Credential formats for W3C Verifiable Credentials
// https://openid.net/specs/openid-4-verifiable-credential-issuance-1_0.html#name-w3c-verifiable-credentials
if !contains([]string{VCJwtFormat.String(), VCJwtJsonldFormat.String(), VCJsonldFormat.String()}, c.Format.String()) {
return errUnsupportedCredFormat
}
if c.Proof != (ProofOfPossession{}) {
if c.Proof.ProofType != JwtProofType {
return errInvalidProof
}
// parse the jwt
proofOfPossession, err := jwt.Parse([]byte(c.Proof.Jwt), jwt.WithVerify(false), jwt.WithValidate(false))
if err != nil {
return errInternalServerError
}
privateClaims := proofOfPossession.PrivateClaims()
if nonceValue, ok := privateClaims["nonce"].(string); ok {
// validate if the proof of possession is issued with the same c_nonce of the credential request authorized
// when the c_nonce is expired, the authorized credential request is not found
authorizedCredentialRequest, err := GetCredentialIssuanceByCNonce(nonceValue)
if err != nil {
return errors.New("invalid_nonce")
}
// check if the client is the samen as in the authorization request
if proofOfPossession.Issuer() != authorizedCredentialRequest.ClientId {
return errors.New("invalid_client_id")
}
} else {
// if nonce not in the jwt, then invalid request
return errInvalidProof
}
if jwkValue, ok := privateClaims["jwk"]; ok {
switch jwkValue := jwkValue.(type) {
case map[string]interface{}:
jwkBytes, err := json.Marshal(jwkValue)
if err != nil {
return err
}
err = c.validateNaturalPerson(string(jwkBytes))
if err != nil {
return err
}
case string:
err = c.validateNaturalPerson(jwkValue)
if err != nil {
return errInvalidProof
}
}
} else {
// proof of possession for legal entity
return c.validateLegalEntity()
//return errors.New("legal entity not supported")
}
}
return nil
}
// ValidateNaturalPerson validates the proof of possession for a natural person
func (c *CredentialRequest) validateNaturalPerson(key string) error {
var publicKey interface{}
if err := jwk.ParseRawKey([]byte(key), &publicKey); err != nil {
return errInternalServerError
}
switch publicKey.(type) {
case *ecdsa.PublicKey:
_, err := jwt.Parse([]byte(c.Proof.Jwt), jwt.WithKey(jwa.ES256, publicKey), jwt.WithAudience(GetProviderMetadata().Issuer))
if err != nil {
return err
}
default:
return errors.New("unsupported_key_type")
}
return nil
}
// ValidateLegalEntity validates the proof of possession for a legal entity
func (c *CredentialRequest) validateLegalEntity() error {
token, err := jws.Parse([]byte(c.Proof.Jwt))
if err != nil {
return err
}
// While JWT enveloped with JWS in compact format only has 1 signature,
// a generic JWS message may have multiple signatures. Therefore we
// need to access the first element
keyId := token.Signatures()[0].ProtectedHeaders().KeyID()
if keyId == "" {
return errors.New("missing_key_id")
}
jwkKey, err := c.getJWKKeyFromDid(keyId)
if err != nil {
return err
}
_, err = jwt.Parse([]byte(c.Proof.Jwt), jwt.WithAudience(GetProviderMetadata().Issuer), jwt.WithKey(jwa.ES256, jwkKey))
if err != nil {
return err
}
return nil
}
func (c *CredentialRequest) getJWKKeyFromDid(keyId string) (jwk.Key, error) {
// get the did from the key id
arrKeyId := strings.Split(keyId, "#")
if len(arrKeyId) == 0 {
return nil, errors.New("invalid_key_id")
}
// Proof posession for legal entity
diddoc, err := c.ResolveDid(arrKeyId[0])
if err != nil {
return nil, err
}
verificationMethod, err := diddoc.GetVerificationMethodById(keyId)
if err != nil {
return nil, err
}
// Serialize the key to json
jsonbuf, err := json.Marshal(verificationMethod.PubicKeyJWK)
if err != nil {
return nil, err
}
// Parse the json to jwk.Key
return jwk.ParseKey(jsonbuf)
}
// ResolveDid resolves a did from ebsi
func (c *CredentialRequest) ResolveDid(didString string) (diddoc.Document, error) {
// requester must be a legal entity, as natural person are not registered on ebsi
registry := NewDecetralizedIdentifierRegistry()
rawdoc, err := registry.ResolveDid(didString)
if err != nil {
return diddoc.Document{}, err
}
var inputjson []byte
switch r := rawdoc.(type) {
case string:
inputjson = []byte(r)
case []byte:
inputjson = r
case map[string]interface{}:
inputjson, _ = json.Marshal(r)
}
var doc diddoc.Document
json.Unmarshal(inputjson, &doc)
if err != nil {
return diddoc.Document{}, err
}
return doc, nil
}
// CreateCredentialResponse creates the credential response that can be used in the http response
// the function uses the verifiableCredential and deferred parameters to indicate a direct issuance or a deferred issuance
func (c *CredentialRequest) CreateCredentialResponse(options ...CredentialResponseOption) (CredentialResponse, error) {
o := optionalParameters{}
// Push the options into the config
for _, opt := range options {
opt(&o)
}
if o.deferred {
if err := o.checkRequired([]string{"signingKey", "algorithm"}); err != nil {
return CredentialResponse{}, err
}
authorizedCredentialRequest := CredentialRequest{
ClientId: c.ClientId,
AuthorizationDetails: c.AuthorizationDetails,
CNonceExpiresIn: GetServerConfig().ExpirationTime,
BearerTokenExpiresIn: GetServerConfig().ExpirationTime,
Deferred: true,
}
authorizedCredentialRequest.CNonce, _ = generateNonce()
acceptanceToken, err := authorizedCredentialRequest.createBearerToken(WithAccessTokenSigningKey(o.algorithm, o.signingKey))
if err != nil {
return CredentialResponse{}, err
}
authorizedCredentialRequest.BearerTokenSHA512 = sha512.Sum512(acceptanceToken)
// Store the bearer token, so we can verify it up when the credential requests arrives
if err := authorizedCredentialRequest.StoreCredentialRequest(); err != nil {
return CredentialResponse{}, err
}
return CredentialResponse{
AcceptanceToken: string(acceptanceToken),
CNonce: authorizedCredentialRequest.CNonce,
CNonceExpiresIn: authorizedCredentialRequest.CNonceExpiresIn,
}, nil
}
if err := o.checkRequired([]string{"verifiableCredential"}); err != nil {
return CredentialResponse{}, err
}
if !contains([]string{VCJsonldFormat.String(), VCJwtFormat.String(), string(VCJwtJsonldFormat)}, o.format.String()) {
return CredentialResponse{}, errors.New("invalid_format")
}
// return the request_uri and expiration time
return CredentialResponse{
Format: o.format,
Credential: o.verifiableCredential,
}, nil
}
// generateAccessToken generates the access token for the token response
// but also the acceptance token for a deferred credential request, acceptance token is used as access token
func (c *CredentialRequest) createBearerToken(options ...TokenResponseOption) ([]byte, error) {
o := optionalParameters{}
// Push the options into the config
for _, opt := range options {
opt(&o)
}
issuer := GetProviderMetadata().Issuer
if err := o.checkRequired([]string{"signingKey", "algorithm"}); err != nil {
return nil, err
}
accessToken, err := jwt.NewBuilder().
Issuer(issuer).
Audience([]string{issuer}).
Subject(c.ClientId).
IssuedAt(time.Now()).
Expiration(time.Now().Add(GetServerConfig().ExpirationTime)).
Claim("nonce", c.CNonce).
Claim("scope", "credential_issuance").
Build()
if err != nil {
return nil, err
}
serialized, err := jwt.Sign(accessToken, jwt.WithKey(o.algorithm, o.signingKey))
if err != nil {
return nil, err
}
return serialized, nil
}
// ValidateAuthorization validates the access token
func validateBearerToken(authorizationHeader string, options ...TokenResponseOption) (CredentialRequest, error) {
// Remove the prefix from the authorization header
bearerToken := strings.TrimPrefix(authorizationHeader, "Bearer ")
if len(bearerToken) <= 0 {
return CredentialRequest{}, errors.New("invalid_authorization_header")
}
// Check if the authorization header has bearer as prefix
if !strings.HasPrefix(authorizationHeader, "Bearer ") {
return CredentialRequest{}, errors.New("bearer_not_found")
}
credRequest, err := GetCredentialRequestSecure(bearerToken)
if err != nil {
return CredentialRequest{}, errors.New("invalid_token")
}
// Verify the validity of the token
o := optionalParameters{}
// Push the options into the config
for _, opt := range options {
opt(&o)
}
issuer := GetProviderMetadata().Issuer
if err = o.checkRequired([]string{"signingKey", "algorithm"}); err != nil {
return CredentialRequest{}, err
}
verifiedToken, err := jwt.Parse([]byte(bearerToken), jwt.WithKey(o.algorithm, o.signingKey), jwt.WithValidate(true), jwt.WithAudience(issuer))
if err != nil {
return CredentialRequest{}, err
}
privateClaims := verifiedToken.PrivateClaims()
// check if the nonce is equal as issued with the token respnse
nonce, ok := privateClaims["nonce"].(string)
if !ok {
return CredentialRequest{}, errors.New("missing_nonce")
}
if nonce != credRequest.CNonce {
return CredentialRequest{}, errors.New("invalid_nonce")
}
return *credRequest, nil
}