-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
296 lines (261 loc) · 7.34 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
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
package main
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
"crypto/sha1"
"embed"
"encoding/hex"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/lestrrat-go/jwx/jwk"
"github.com/lestrrat-go/jwx/jwt"
"github.com/nuts-foundation/go-did/did"
"github.com/nuts-foundation/nuts-node/core"
vcrApi "github.com/nuts-foundation/nuts-node/vcr/api/vcr/v2"
"io/fs"
"log"
"net/http"
"os"
"strings"
"time"
didmanAPI "github.com/nuts-foundation/nuts-node/didman/api/v1"
vdrAPI "github.com/nuts-foundation/nuts-node/vdr/api/v1"
"github.com/nuts-foundation/nuts-registry-admin-demo/domain/sp"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/lestrrat-go/jwx/jwa"
"github.com/nuts-foundation/nuts-registry-admin-demo/api"
"github.com/nuts-foundation/nuts-registry-admin-demo/domain/credentials"
"github.com/nuts-foundation/nuts-registry-admin-demo/domain/customers"
bolt "go.etcd.io/bbolt"
)
const assetPath = "web/dist"
//go:embed web/dist/*
var embeddedFiles embed.FS
const apiTimeout = 10 * time.Second
func getFileSystem(useFS bool) http.FileSystem {
if useFS {
log.Print("using live mode")
return http.FS(os.DirFS(assetPath))
}
log.Print("using embed mode")
fsys, err := fs.Sub(embeddedFiles, assetPath)
if err != nil {
panic(err)
}
return http.FS(fsys)
}
func main() {
config := loadConfig()
config.Print(log.Writer())
// load bbolt db
db, err := bolt.Open(config.DBFile, 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
var vendorDID *did.DID
if config.VendorDID != "" {
vendorDID, err = did.ParseDID(config.VendorDID)
if err != nil {
log.Fatal(err)
}
}
e := echo.New()
e.HideBanner = true
loggerConfig := middleware.DefaultLoggerConfig
loggerConfig.Skipper = requestsStatusEndpoint
e.Use(middleware.LoggerWithConfig(loggerConfig))
//e.Debug = true
//e.Use(middleware.Recover())
e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
Skipper: func(c echo.Context) bool {
protectedPaths := []string{
"/web/private",
}
for _, path := range protectedPaths {
if strings.HasPrefix(c.Request().RequestURI, path) {
return false
}
}
return true
},
SigningKey: &config.sessionKey.PublicKey,
SigningMethod: jwa.ES256.String(),
}))
e.HTTPErrorHandler = httpErrorHandler
// Initialize Auth
var account api.UserAccount
if config.Credentials.Empty() {
account = generateDefaultAccount(config)
log.Printf("Authentication credentials not configured, so they were generated (user=%s, password=%s)", account.Username, account.Password)
} else {
account = api.UserAccount{Username: config.Credentials.Username, Password: config.Credentials.Password}
}
auth := api.NewAuth(config.sessionKey, []api.UserAccount{account})
// API security
tokenGenerator := func() (string, error) {
return "", nil
}
if config.apiKey != nil {
tokenGenerator = createTokenGenerator(config)
}
// Initialize repos
vdrClient := vdrAPI.HTTPClient{
ClientConfig: core.ClientConfig{
Address: config.NutsNodeAddress,
Timeout: apiTimeout,
},
TokenGenerator: tokenGenerator,
}
didmanClient := didmanAPI.HTTPClient{
ClientConfig: core.ClientConfig{
Address: config.NutsNodeAddress,
Timeout: apiTimeout,
},
TokenGenerator: tokenGenerator,
}
vcrClient := vcrApi.HTTPClient{
ClientConfig: core.ClientConfig{
Address: config.NutsNodeAddress,
Timeout: apiTimeout,
},
TokenGenerator: tokenGenerator,
}
spService := sp.Service{
Repository: sp.NewBBoltRepository(db),
VDRClient: vdrClient,
DIDManClient: didmanClient,
VendorDID: vendorDID,
}
// Initialize services
customerService := customers.Service{
VDRClient: vdrClient,
Repository: customers.NewFlatFileRepository(config.CustomersFile),
DIDManClient: didmanClient,
}
credentialService := credentials.Service{
NutsNodeAddr: config.NutsNodeAddress,
SPService: spService,
DIDManClient: didmanClient,
VCRClient: vcrClient,
}
// Initialize wrapper
apiWrapper := api.Wrapper{Auth: auth, SPService: spService, CustomerService: customerService, CredentialService: credentialService}
api.RegisterHandlers(e, apiWrapper)
// Setup asset serving:
// Check if we use live mode from the file system or using embedded files
useFS := len(os.Args) > 1 && os.Args[1] == "live"
assetHandler := http.FileServer(getFileSystem(useFS))
e.GET("/branding/logo", (&api.LogoHandler{FilePath: config.Branding.Logo}).Handle)
e.GET("/status", func(context echo.Context) error {
return context.String(http.StatusOK, "OK")
})
e.GET("/*", echo.WrapHandler(assetHandler))
// Start server
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", config.HTTPPort)))
}
func generateDefaultAccount(config Config) api.UserAccount {
pkHashBytes := sha1.Sum(elliptic.Marshal(config.sessionKey.Curve, config.sessionKey.X, config.sessionKey.Y))
return api.UserAccount{Username: "[email protected]", Password: hex.EncodeToString(pkHashBytes[:])}
}
// httpErrorHandler includes the err.Err() string in a { "error": "msg" } json hash
func httpErrorHandler(err error, c echo.Context) {
var (
code = http.StatusInternalServerError
msg interface{}
)
type Map map[string]interface{}
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
msg = he.Message
if he.Internal != nil {
err = fmt.Errorf("%v, %v", err, he.Internal)
}
} else {
msg = err.Error()
}
if _, ok := msg.(string); ok {
msg = Map{"error": msg}
}
// Send response
if !c.Response().Committed {
if c.Request().Method == http.MethodHead {
err = c.NoContent(code)
} else {
err = c.JSON(code, msg)
}
if err != nil {
c.Logger().Error(err)
}
}
}
func requestsStatusEndpoint(context echo.Context) bool {
return context.Request().RequestURI == "/status"
}
// createTokenGenerator generates valid API tokens for the Nuts node and signs them with the private key
func createTokenGenerator(config Config) core.AuthorizationTokenGenerator {
return func() (string, error) {
key, err := jwkKey(config.apiKey)
if err != nil {
return "", err
}
issuedAt := time.Now()
notBefore := issuedAt
expires := notBefore.Add(time.Second * time.Duration(5))
token, err := jwt.NewBuilder().
Issuer(config.NutsNodeAPIUser).
Subject(config.Credentials.Username).
Audience([]string{config.NutsNodeAPIAudience}).
IssuedAt(issuedAt).
NotBefore(notBefore).
Expiration(expires).
JwtID(uuid.New().String()).
Build()
bytes, err := jwt.Sign(token, jwa.SignatureAlgorithm(key.Algorithm()), key)
if err != nil {
return "", err
}
return string(bytes), nil
}
}
func jwkKey(signer crypto.Signer) (key jwk.Key, err error) {
// ssh key format
key, err = jwk.New(signer)
if err != nil {
return nil, err
}
switch k := signer.(type) {
case *rsa.PrivateKey:
key.Set(jwk.AlgorithmKey, jwa.PS512)
case *ecdsa.PrivateKey:
var alg jwa.SignatureAlgorithm
alg, err = ecAlg(k)
key.Set(jwk.AlgorithmKey, alg)
default:
err = fmt.Errorf("unsupported signing private key: %T", k)
return
}
err = jwk.AssignKeyID(key)
return
}
func ecAlg(key *ecdsa.PrivateKey) (alg jwa.SignatureAlgorithm, err error) {
alg, err = ecAlgUsingPublicKey(key.PublicKey)
return
}
func ecAlgUsingPublicKey(key ecdsa.PublicKey) (alg jwa.SignatureAlgorithm, err error) {
switch key.Params().BitSize {
case 256:
alg = jwa.ES256
case 384:
alg = jwa.ES384
case 521:
alg = jwa.ES512
default:
err = errors.New("unsupported key")
}
return
}