forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresentation_jwt.go
93 lines (73 loc) · 2.21 KB
/
presentation_jwt.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable
import (
"encoding/json"
"fmt"
"github.com/hyperledger/aries-framework-go/pkg/doc/jwt"
)
// JWTPresClaims is JWT Claims extension by Verifiable Presentation (with custom "vp" claim).
type JWTPresClaims struct {
*jwt.Claims
Presentation *rawPresentation `json:"vp,omitempty"`
}
func (jpc *JWTPresClaims) refineFromJWTClaims() {
raw := jpc.Presentation
if jpc.Issuer != "" {
raw.Holder = jpc.Issuer
}
if jpc.ID != "" {
raw.ID = jpc.ID
}
}
// newJWTPresClaims creates JWT Claims of VP with an option to minimize certain fields put into "vp" claim.
func newJWTPresClaims(vp *Presentation, audience []string, minimizeVP bool) (*JWTPresClaims, error) {
// currently jwt encoding supports only single subject (by the spec)
jwtClaims := &jwt.Claims{
Issuer: vp.Holder, // iss
ID: vp.ID, // jti
}
if len(audience) > 0 {
jwtClaims.Audience = audience
}
var (
rawVP *rawPresentation
err error
)
if minimizeVP {
vpCopy := *vp
vpCopy.ID = ""
vpCopy.Holder = ""
rawVP, err = vpCopy.raw()
} else {
rawVP, err = vp.raw()
}
if err != nil {
return nil, err
}
presClaims := &JWTPresClaims{
Claims: jwtClaims,
Presentation: rawVP,
}
return presClaims, nil
}
// JWTPresClaimsUnmarshaller parses JWT of certain type to JWT Claims containing "vp" (Presentation) claim.
type JWTPresClaimsUnmarshaller func(vpJWT string) (*JWTPresClaims, error)
// decodePresJWT parses JWT from the specified bytes array in compact format using the unmarshaller.
// It returns decoded Verifiable Presentation refined by JWT Claims in raw byte array and rawPresentation form.
func decodePresJWT(vpJWT string, unmarshaller JWTPresClaimsUnmarshaller) ([]byte, *rawPresentation, error) {
presClaims, err := unmarshaller(vpJWT)
if err != nil {
return nil, nil, fmt.Errorf("decode Verifiable Presentation JWT claims: %w", err)
}
// Apply VC-related claims from JWT.
presClaims.refineFromJWTClaims()
vpRaw := presClaims.Presentation
rawBytes, err := json.Marshal(vpRaw)
if err != nil {
return nil, nil, fmt.Errorf("marshal \"vp\" claim of JWT: %w", err)
}
return rawBytes, vpRaw, nil
}