-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (87 loc) · 2.65 KB
/
main.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
package main
import (
"bufio"
"fmt"
"github.com/alecthomas/kong"
"github.com/nuts-foundation/go-didx509-toolkit/credential_issuer"
"github.com/nuts-foundation/go-didx509-toolkit/internal"
"github.com/nuts-foundation/go-didx509-toolkit/x509_cert"
"os"
)
type VC struct {
CertificateFile string `arg:"" name:"certificate_file" help:"Certificate PEM file. If the file contains a chain, the chain will be used for signing." type:"existingfile"`
SigningKey string `arg:"" name:"signing_key" help:"PEM key for signing." type:"existingfile"`
SubjectDID string `arg:"" name:"subject_did" help:"The subject DID of the VC."`
SubjectAttributes []x509_cert.SubjectTypeName `short:"s" name:"subject_attr" help:"A list of Subject Attributes u in the VC." default:"O,L"`
IncludePermanent bool `short:"p" help:"Include the permanent identifier in the did:x509."`
}
var CLI struct {
Version string `help:"Show version."`
Vc VC `cmd:"" help:"Create a new VC."`
}
func main() {
cli := &CLI
parser, err := kong.New(cli)
if err != nil {
panic(err)
}
ctx, err := parser.Parse(os.Args[1:])
if err != nil {
parser.FatalIfErrorf(err)
}
switch ctx.Command() {
case "vc <certificate_file> <signing_key> <subject_did>":
vc := cli.Vc
jwt, err := issueVc(vc)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
err = printLineAndFlush(jwt)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
default:
fmt.Println("Unknown command")
os.Exit(-1)
}
}
// printLineAndFlush writes a JWT (JSON Web Token) to the standard output and flushes the buffered writer.
func printLineAndFlush(jwt string) error {
f := bufio.NewWriter(os.Stdout)
// Make sure to flush
defer func(f *bufio.Writer) {
_ = f.Flush()
}(f)
// Write the JWT
_, err := f.WriteString(jwt + "\n")
return err
}
func issueVc(vc VC) (string, error) {
certFileData, err := os.ReadFile(vc.CertificateFile)
if err != nil {
return "", fmt.Errorf("failed to read certificate file: %w", err)
}
certs, err := internal.ParseCertificatesFromPEM(certFileData)
if err != nil {
return "", err
}
chain, err := internal.ParseCertificateChain(certs)
if err != nil {
return "", err
}
keyFileData, err := os.ReadFile(vc.SigningKey)
if err != nil {
return "", fmt.Errorf("failed to read key file: %w", err)
}
key, err := internal.ParseRSAPrivateKeyFromPEM(keyFileData)
if err != nil {
return "", err
}
credential, err := credential_issuer.Issue(chain, key, vc.SubjectDID, credential_issuer.SubjectAttributes(vc.SubjectAttributes...))
if err != nil {
return "", err
}
return credential.Raw(), nil
}