-
Notifications
You must be signed in to change notification settings - Fork 28
/
oidc4vci_cmd.go
362 lines (300 loc) · 11.5 KB
/
oidc4vci_cmd.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
/*
Copyright Avast Software. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package cmd
import (
"context"
"fmt"
"log/slog"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"github.com/henvic/httpretty"
"github.com/piprate/json-gold/ld"
"github.com/spf13/cobra"
vdrapi "github.com/trustbloc/did-go/vdr/api"
storageapi "github.com/trustbloc/kms-go/spi/storage"
"github.com/trustbloc/kms-go/wrapper/api"
"github.com/trustbloc/vcs/component/wallet-cli/internal/formatter"
"github.com/trustbloc/vcs/component/wallet-cli/pkg/attestation"
"github.com/trustbloc/vcs/component/wallet-cli/pkg/oidc4vci"
"github.com/trustbloc/vcs/component/wallet-cli/pkg/trustregistry"
"github.com/trustbloc/vcs/component/wallet-cli/pkg/wallet"
"github.com/trustbloc/vcs/component/wallet-cli/pkg/wellknown"
vcsverifiable "github.com/trustbloc/vcs/pkg/doc/verifiable"
)
const (
authorizationCodeGrantType = "authorization_code"
preAuthorizedCodeGrantType = "urn:ietf:params:oauth:grant-type:pre-authorized_code"
)
type oidc4vciCommandFlags struct {
serviceFlags *walletFlags
grantType string
qrCodePath string
credentialOffer string
proofType string
demoIssuerURL string
vcFormat string
credentialType string
oidcCredentialFormat string
walletDIDIndex int
clientID string
scopes []string
redirectURI string
userLogin string
userPassword string
issuerState string
pin string
enableDiscoverableClientID bool
enableTracing bool
proxyURL string
trustRegistryHost string
attestationURL string
}
func NewOIDC4VCICommand() *cobra.Command {
flags := &oidc4vciCommandFlags{
serviceFlags: &walletFlags{},
}
cmd := &cobra.Command{
Use: "oidc4vci",
Short: "requests credential with OIDC4VCI authorization or pre-authorized code flows",
RunE: func(cmd *cobra.Command, args []string) error {
w, svc, err := initWallet(flags.serviceFlags)
if err != nil {
return fmt.Errorf("init wallet: %w", err)
}
emptyCredentialOffer := flags.credentialOffer == "" && flags.qrCodePath == "" && flags.demoIssuerURL == ""
walletInitiatedFlow := flags.issuerState != ""
if emptyCredentialOffer && !walletInitiatedFlow {
return fmt.Errorf("set --qr-code-path or --credential-offer or --demo-issuer-url " +
"to retrieve credential offer")
}
if !emptyCredentialOffer && walletInitiatedFlow {
slog.Error("set either --issuer-state for wallet-initiated flow or one of --qr-code-path or " +
"--credential-offer or --demo-issuer-url to retrieve credential offer")
}
httpTransport := &http.Transport{
TLSClientConfig: svc.TLSConfig(),
}
if flags.proxyURL != "" {
proxyURL, parseErr := url.Parse(flags.proxyURL)
if parseErr != nil {
return fmt.Errorf("parse proxy url: %w", parseErr)
}
httpTransport.Proxy = http.ProxyURL(proxyURL)
}
cookie, err := cookiejar.New(&cookiejar.Options{})
if err != nil {
return fmt.Errorf("init cookie jar: %w", err)
}
httpClient := &http.Client{
Jar: cookie,
Transport: httpTransport,
}
if flags.enableTracing {
httpLogger := &httpretty.Logger{
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
SkipSanitize: true,
Colors: true,
SkipRequestInfo: true,
Formatters: []httpretty.Formatter{&httpretty.JSONFormatter{}, &formatter.JWTFormatter{}},
MaxResponseBody: 1e+7,
}
httpClient.Transport = httpLogger.RoundTripper(httpClient.Transport)
}
if flags.demoIssuerURL != "" {
var offer *parsedCredentialOffer
offer, err = fetchCredentialOffer(flags.demoIssuerURL, httpClient)
if err != nil {
return fmt.Errorf("fetch credential offer from %s: %w", flags.demoIssuerURL, err)
}
flags.pin = offer.Pin
flags.credentialOffer = offer.CredentialOfferURL
}
var credentialOffer string
if flags.credentialOffer != "" {
credentialOffer = flags.credentialOffer
} else if flags.qrCodePath != "" {
credentialOffer, err = readQRCode(flags.qrCodePath)
if err != nil {
return fmt.Errorf("read qr code: %w", err)
}
}
var walletDIDIndex int
if flags.walletDIDIndex != -1 {
walletDIDIndex = flags.walletDIDIndex
} else {
walletDIDIndex = len(w.DIDs()) - 1
}
attestationService, err := attestation.NewService(
&attestationProvider{
storageProvider: svc.StorageProvider(),
httpClient: httpClient,
documentLoader: svc.DocumentLoader(),
cryptoSuite: svc.CryptoSuite(),
wallet: w,
},
flags.attestationURL,
walletDIDIndex,
)
if err != nil {
return fmt.Errorf("create attestation service: %w", err)
}
wellKnownService := &wellknown.Service{
HTTPClient: httpClient,
VDRRegistry: svc.VDR(),
}
provider := &oidc4vciProvider{
storageProvider: svc.StorageProvider(),
httpClient: httpClient,
documentLoader: svc.DocumentLoader(),
vdrRegistry: svc.VDR(),
cryptoSuite: svc.CryptoSuite(),
attestationService: attestationService,
wallet: w,
wellKnownService: wellKnownService,
}
if flags.trustRegistryHost != "" {
provider.trustRegistry = trustregistry.NewClient(httpClient, flags.trustRegistryHost)
}
var flow *oidc4vci.Flow
opts := []oidc4vci.Opt{
oidc4vci.WithClientID(flags.clientID),
}
if flags.credentialType != "" {
types := strings.Split(flags.credentialType, ",")
formats := strings.Split(flags.oidcCredentialFormat, ",")
if len(types) != len(formats) {
return fmt.Errorf(
"credential types and formats amount mismatch: types %d, formats %d", len(types), len(formats))
}
for i, t := range types {
opts = append(opts, oidc4vci.WithCredentialFilter(t, vcsverifiable.OIDCFormat(formats[i])))
}
}
if walletInitiatedFlow {
opts = append(opts, oidc4vci.WithIssuerState(flags.issuerState))
} else {
opts = append(opts, oidc4vci.WithCredentialOffer(credentialOffer))
}
if flags.proofType == "cwt" {
opts = append(opts, oidc4vci.WithProofBuilder(
oidc4vci.NewCWTProofBuilder(),
))
}
opts = append(opts, oidc4vci.WithWalletDIDIndex(walletDIDIndex))
switch flags.grantType {
case authorizationCodeGrantType:
if flags.clientID == "" {
return fmt.Errorf("--client-id not set")
}
if flags.redirectURI == "" {
return fmt.Errorf("--redirect-uri not set")
}
if len(flags.scopes) == 0 {
return fmt.Errorf("--scopes not set")
}
opts = append(opts,
oidc4vci.WithScopes(flags.scopes),
oidc4vci.WithRedirectURI(flags.redirectURI),
oidc4vci.WithUserLogin(flags.userLogin),
oidc4vci.WithUserPassword(flags.userPassword),
)
if walletInitiatedFlow {
opts = append(opts, oidc4vci.WithFlowType(oidc4vci.FlowTypeWalletInitiated))
} else {
opts = append(opts, oidc4vci.WithFlowType(oidc4vci.FlowTypeAuthorizationCode))
}
if flags.enableDiscoverableClientID {
opts = append(opts, oidc4vci.WithEnableDiscoverableClientID())
}
if flow, err = oidc4vci.NewFlow(provider, opts...); err != nil {
return err
}
if _, err = flow.Run(context.Background()); err != nil {
return err
}
case preAuthorizedCodeGrantType:
opts = append(opts,
oidc4vci.WithFlowType(oidc4vci.FlowTypePreAuthorizedCode),
oidc4vci.WithPin(flags.pin),
)
if flow, err = oidc4vci.NewFlow(provider, opts...); err != nil {
return err
}
if _, err = flow.Run(context.Background()); err != nil {
return err
}
default:
return fmt.Errorf("unsupported grant-type: %s", flags.grantType)
}
return nil
},
}
cmd.Flags().StringVar(&flags.serviceFlags.levelDBPath, "leveldb-path", "", "leveldb path")
cmd.Flags().StringVar(&flags.serviceFlags.mongoDBConnectionString, "mongodb-connection-string", "", "mongodb connection string")
cmd.Flags().StringVar(&flags.grantType, "grant-type", "authorization_code", "supported grant types: authorization_code,urn:ietf:params:oauth:grant-type:pre-authorized_code")
cmd.Flags().StringVar(&flags.qrCodePath, "qr-code-path", "", "path to file with qr code")
cmd.Flags().StringVar(&flags.credentialOffer, "credential-offer", "", "openid credential offer")
cmd.Flags().StringVar(&flags.demoIssuerURL, "demo-issuer-url", "", "demo issuer url for downloading qr code automatically")
cmd.Flags().StringVar(&flags.oidcCredentialFormat, "credential-format", "", "comma-separated supported OIDC credential formats: ldp_vc,jwt_vc_json-ld")
cmd.Flags().StringVar(&flags.credentialType, "credential-type", "", "comma-separated credential types")
cmd.Flags().StringVar(&flags.proofType, "proof-type", "", "proof-type. jwt or cwt. default jwt")
cmd.Flags().IntVar(&flags.walletDIDIndex, "wallet-did-index", -1, "index of wallet did, if not set the most recently created DID is used")
cmd.Flags().StringVar(&flags.clientID, "client-id", "", "vcs oauth2 client")
cmd.Flags().StringSliceVar(&flags.scopes, "scopes", []string{"openid"}, "vcs oauth2 scopes")
cmd.Flags().StringVar(&flags.redirectURI, "redirect-uri", "http://127.0.0.1/callback", "callback where the authorization code should be sent")
cmd.Flags().StringVar(&flags.userLogin, "user-login", "", "user login on issuer IdP")
cmd.Flags().StringVar(&flags.userPassword, "user-password", "", "user password on issuer IdP")
cmd.Flags().StringVar(&flags.issuerState, "issuer-state", "", "issuer state in wallet-initiated flow")
cmd.Flags().StringVar(&flags.pin, "pin", "", "pin for pre-authorized code flow")
cmd.Flags().BoolVar(&flags.enableDiscoverableClientID, "enable-discoverable-client-id", false, "enables discoverable client id scheme for dynamic client registration")
cmd.Flags().StringVar(&flags.attestationURL, "attestation-url", "", "attestation url, i.e. https://<host>/vcs/wallet/attestation")
cmd.Flags().StringVar(&flags.trustRegistryHost, "trust-registry-host", "", "trust registry host, i.e. https://<host>/trustregistry")
cmd.Flags().BoolVar(&flags.enableTracing, "enable-tracing", false, "enables http tracing")
cmd.Flags().StringVar(&flags.proxyURL, "proxy-url", "", "proxy url for http client")
return cmd
}
type oidc4vciProvider struct {
storageProvider storageapi.Provider
httpClient *http.Client
documentLoader ld.DocumentLoader
vdrRegistry vdrapi.Registry
cryptoSuite api.Suite
attestationService *attestation.Service
trustRegistry *trustregistry.Client
wallet *wallet.Wallet
wellKnownService *wellknown.Service
}
func (p *oidc4vciProvider) StorageProvider() storageapi.Provider {
return p.storageProvider
}
func (p *oidc4vciProvider) HTTPClient() *http.Client {
return p.httpClient
}
func (p *oidc4vciProvider) DocumentLoader() ld.DocumentLoader {
return p.documentLoader
}
func (p *oidc4vciProvider) VDRegistry() vdrapi.Registry {
return p.vdrRegistry
}
func (p *oidc4vciProvider) CryptoSuite() api.Suite {
return p.cryptoSuite
}
func (p *oidc4vciProvider) AttestationService() oidc4vci.AttestationService {
return p.attestationService
}
func (p *oidc4vciProvider) TrustRegistry() oidc4vci.TrustRegistry {
return p.trustRegistry
}
func (p *oidc4vciProvider) Wallet() *wallet.Wallet {
return p.wallet
}
func (p *oidc4vciProvider) WellKnownService() *wellknown.Service {
return p.wellKnownService
}