This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
/
engine.go
188 lines (157 loc) · 5.06 KB
/
engine.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
/*
* Nuts consent store
* Copyright (C) 2019. Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package engine
import (
"context"
"errors"
"strings"
_ "github.com/golang-migrate/migrate/v4/database/sqlite3"
_ "github.com/jinzhu/gorm/dialects/sqlite"
_ "github.com/mattn/go-sqlite3"
"github.com/nuts-foundation/nuts-consent-store/api"
"github.com/nuts-foundation/nuts-consent-store/client"
"github.com/nuts-foundation/nuts-consent-store/pkg"
engine "github.com/nuts-foundation/nuts-go-core"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func NewConsentStoreEngine() *engine.Engine {
cs := pkg.ConsentStoreInstance()
return &engine.Engine{
Name: "ConsentStore",
Cmd: cmd(),
Configure: cs.Configure,
Config: &cs.Config,
ConfigKey: "cstore",
Diagnostics: cs.Diagnostics,
FlagSet: flagSet(),
Routes: func(router engine.EchoRouter) {
api.RegisterHandlers(router, &api.Wrapper{Cs: cs})
},
Start: cs.Start,
Shutdown: cs.Shutdown,
}
}
func flagSet() *pflag.FlagSet {
flags := pflag.NewFlagSet("cstore", pflag.ContinueOnError)
flags.String(pkg.ConfigConnectionString, pkg.ConfigConnectionStringDefault, "Db connectionString")
flags.String(pkg.ConfigAddress, "localhost:1323", "Address of the server when in client mode")
flags.String(pkg.ConfigMode, "", "server or client, when client it uses the HttpClient")
return flags
}
func cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "consent-store",
Short: "consent store commands",
}
cmd.AddCommand(&cobra.Command{
Use: "list [actor] [subject]?",
Example: "list urn:oid:2.16.840.1.113883.2.4.6.1:00000007",
Short: "lists all consent records for the given actor and optional subject",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires an actor argument")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
csc := client.NewConsentStoreClient()
var (
consentList []pkg.PatientConsent
err error
)
if len(args) > 1 {
consentList, err = csc.QueryConsent(context.TODO(), &args[0], nil, &args[1], nil)
} else {
consentList, err = csc.QueryConsent(context.TODO(), &args[0], nil, nil, nil)
}
if err != nil {
logrus.Errorf("Error finding consent records: %s\n", err.Error())
return
}
logrus.Errorf("Found %d records\n\n", len(consentList))
for _, c := range consentList {
logrus.Errorln(c.String())
}
},
})
cmd.AddCommand(&cobra.Command{
Use: "record [subject] [custodian] [actor] [dataClasses]",
Example: "record urn:oid:2.16.840.1.113883.2.4.6.3:999999990 urn:oid:2.16.840.1.113883.2.4.6.1:00000007 urn:oid:2.16.840.1.113883.2.4.6.1:00000007 urn:oid:1.3.6.1.4.1.54851:1:MEDICAL",
Short: "record a new consent in store, dataClasses is comma-separated",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 4 {
return errors.New("requires 4 arguments")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
csc := client.NewConsentStoreClient()
resources := pkg.DataClassesFromStrings(strings.Split(args[3], ","))
err := csc.RecordConsent(context.TODO(), []pkg.PatientConsent{
{
Subject: args[0],
Custodian: args[1],
Actor: args[2],
Records: []pkg.ConsentRecord{
{
DataClasses: resources,
},
},
},
})
if err != nil {
logrus.Errorf("Error recording consent: %s\n", err.Error())
return
}
logrus.Errorln("Consent recorded")
},
})
cmd.AddCommand(&cobra.Command{
Use: "check [subject] [custodian] [actor] [dataClass]",
Example: "check urn:oid:2.16.840.1.113883.2.4.6.3:999999990 urn:oid:2.16.840.1.113883.2.4.6.1:00000007 urn:oid:2.16.840.1.113883.2.4.6.1:00000007 urn:oid:1.3.6.1.4.1.54851:1:MEDICAL",
Short: "check if there's an active consent record for the given combination",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 4 {
return errors.New("requires 4 arguments")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
csc := client.NewConsentStoreClient()
auth, err := csc.ConsentAuth(
context.TODO(),
args[0],
args[1],
args[2],
args[3],
nil)
if err != nil {
logrus.Errorf("Error checking consent: %s", err.Error())
return
}
if auth {
logrus.Errorln("Consent given")
} else {
logrus.Errorln("No consent given")
}
},
})
return cmd
}