github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/verifiable/presentation_jwt.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 SPDX-License-Identifier: Apache-2.0 4 */ 5 6 package verifiable 7 8 import ( 9 "encoding/json" 10 "fmt" 11 12 "github.com/hyperledger/aries-framework-go/pkg/doc/jwt" 13 ) 14 15 // JWTPresClaims is JWT Claims extension by Verifiable Presentation (with custom "vp" claim). 16 type JWTPresClaims struct { 17 *jwt.Claims 18 19 Presentation *rawPresentation `json:"vp,omitempty"` 20 } 21 22 func (jpc *JWTPresClaims) refineFromJWTClaims() { 23 raw := jpc.Presentation 24 25 if jpc.Issuer != "" { 26 raw.Holder = jpc.Issuer 27 } 28 29 if jpc.ID != "" { 30 raw.ID = jpc.ID 31 } 32 } 33 34 // newJWTPresClaims creates JWT Claims of VP with an option to minimize certain fields put into "vp" claim. 35 func newJWTPresClaims(vp *Presentation, audience []string, minimizeVP bool) (*JWTPresClaims, error) { 36 // currently jwt encoding supports only single subject.([]Subject) (by the spec) 37 jwtClaims := &jwt.Claims{ 38 Issuer: vp.Holder, // iss 39 ID: vp.ID, // jti 40 } 41 if len(audience) > 0 { 42 jwtClaims.Audience = audience 43 } 44 45 var ( 46 rawVP *rawPresentation 47 err error 48 ) 49 50 if minimizeVP { 51 vpCopy := *vp 52 vpCopy.ID = "" 53 vpCopy.Holder = "" 54 rawVP, err = vpCopy.raw() 55 } else { 56 rawVP, err = vp.raw() 57 } 58 59 if err != nil { 60 return nil, err 61 } 62 63 rawVP.JWT = "" 64 65 presClaims := &JWTPresClaims{ 66 Claims: jwtClaims, 67 Presentation: rawVP, 68 } 69 70 return presClaims, nil 71 } 72 73 // JWTPresClaimsUnmarshaller parses JWT of certain type to JWT Claims containing "vp" (Presentation) claim. 74 type JWTPresClaimsUnmarshaller func(vpJWT string) (*JWTPresClaims, error) 75 76 // decodePresJWT parses JWT from the specified bytes array in compact format using the unmarshaller. 77 // It returns decoded Verifiable Presentation refined by JWT Claims in raw byte array and rawPresentation form. 78 func decodePresJWT(vpJWT string, unmarshaller JWTPresClaimsUnmarshaller) ([]byte, *rawPresentation, error) { 79 presClaims, err := unmarshaller(vpJWT) 80 if err != nil { 81 return nil, nil, fmt.Errorf("decode Verifiable Presentation JWT claims: %w", err) 82 } 83 84 // Apply VC-related claims from JWT. 85 presClaims.refineFromJWTClaims() 86 87 vpRaw := presClaims.Presentation 88 89 rawBytes, err := json.Marshal(vpRaw) 90 if err != nil { 91 return nil, nil, fmt.Errorf("marshal \"vp\" claim of JWT: %w", err) 92 } 93 94 return rawBytes, vpRaw, nil 95 }