forked from hesusruiz/VCBackend
/
signer_routes.go
210 lines (165 loc) · 5.38 KB
/
signer_routes.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
package issuernew
import (
"encoding/json"
"log"
"net/http"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
)
func (is *IssuerServer) addSignerRoutes(e *core.ServeEvent) {
// Add special Issuer routes with a common prefix
// All requests for this group should be authenticated as Admin or with x509 certificate
signerApiGroup := e.Router.Group(signerApiGroupPrefix, RequireAdminOrX509Auth())
// Return to caller the x509 certificate that was used to do client authentication
signerApiGroup.GET("/getcertinfo", func(c echo.Context) error {
_, subject, err := getX509UserFromHeader(c.Request())
if err != nil {
return err
}
log.Println(subject)
return c.JSON(http.StatusOK, subject)
})
// Create a LEARCredential with the info in the request
signerApiGroup.POST("/createjsoncredential", func(c echo.Context) error {
return is.createJSONCredential(c)
})
// Sign in the server the credential passed in the request
signerApiGroup.POST("/signcredential", func(c echo.Context) error {
return is.signCredential(c)
})
// Retrieve all credentials, applying the proper access control depending on the status
signerApiGroup.GET("/retrievecredentials", func(c echo.Context) error {
return is.retrieveAllCredentials(c)
})
// Update the credential
signerApiGroup.POST("/updatesignedcredential", func(c echo.Context) error {
return is.updateSignedCredential(c)
})
// Send a reminder to the user that there is a credential waiting
signerApiGroup.GET("/sendreminder/:credid", func(c echo.Context) error {
return is.sendReminder(c)
})
}
// createJSONCredential is the Echo route to create a credential from the Mandate in the body of the request.
// We get the Mandate object, create a LEARCredential and insert the Mandate into the 'credentialSubject' object.
func (is *IssuerServer) createJSONCredential(c echo.Context) error {
info := apis.RequestInfo(c)
// We received the body data in form of a map[string]any
data := info.Data
log.Println("data", data)
// Serialize to JSON
raw, err := json.Marshal(data)
if err != nil {
return err
}
log.Println("RAW", string(raw))
// Create the Mandate struct from the serialized JSON data
mandate := Mandate{}
err = json.Unmarshal(raw, &mandate)
if err != nil {
return err
}
// Generate the dates for the Mandate
now := time.Now()
nowPlusOneYear := now.AddDate(1, 0, 0)
nowUTC := now.UTC().String()
nowPlusOneYearUTC := nowPlusOneYear.UTC().String()
mandate.Id = newRandomString()
for i := range mandate.Power {
mandate.Power[i].Id = newRandomString()
}
mandate.LifeSpan.StartDateTime = nowUTC
mandate.LifeSpan.EndDateTime = nowPlusOneYearUTC
// didkey, _, err := GenDIDKey()
// if err != nil {
// return err
// }
// mandate.Mandatee.Id = didkey
// Print the Mandate struct to check it is OK
raw, err = json.MarshalIndent(mandate, "", " ")
if err != nil {
return err
}
log.Println("===== Mandate struct Marshalled")
log.Println(string(raw))
// Create the LEARCredential struct
lc := LEARCredentialEmployee{}
lc.CredentialSubject.Mandate = mandate
// Complete the LEARCredential
lc.Context = []string{"https://www.w3.org/ns/credentials/v2", "https://www.evidenceledger.eu/2022/credentials/employee/v1"}
lc.Id = newRandomString()
lc.TypeCredential = []string{"VerifiableCredential", "LEARCredentialEmployee"}
lc.Issuer.Id = "did:elsi:" + mandate.Mandator.OrganizationIdentifier
lc.IssuanceDate = nowUTC
lc.ValidFrom = nowUTC
lc.ExpirationDate = nowPlusOneYearUTC
raw, err = json.MarshalIndent(lc, "", " ")
if err != nil {
return err
}
log.Println("===== LEARCredential struct Marshalled")
log.Println(string(raw))
return c.JSON(http.StatusOK, lc)
}
func (is *IssuerServer) signCredential(c echo.Context) error {
// Get the private key from the configured x509 certificate
privateKey, err := getConfigPrivateKey()
if err != nil {
return err
}
var learCred LEARCredentialEmployee
// The body of the HTTP request should be a LEARCredentialEmployee
err = echo.BindBody(c, &learCred)
if err != nil {
return err
}
// Sign the credential
tok, err := CreateLEARCredentialJWTtoken(learCred, jwt.SigningMethodRS256, privateKey)
if err != nil {
return err
}
return c.JSON(http.StatusOK, map[string]any{"signed": tok})
}
func (is *IssuerServer) sendReminder(c echo.Context) error {
id := c.PathParam("credid")
return is.sendEmailReminder(id)
}
type updateSignedCredentialRequest struct {
Id string
Status string
Raw string
}
func (is *IssuerServer) updateSignedCredential(c echo.Context) error {
app := is.App
var request updateSignedCredentialRequest
err := echo.BindBody(c, &request)
if err != nil {
return err
}
out, err := json.MarshalIndent(request, "", " ")
if err != nil {
return err
}
log.Println(string(out))
record, err := app.Dao().FindRecordById("credentials", request.Id)
if err != nil {
return err
}
// set individual fields
// or bulk load with record.Load(map[string]any{...})
record.Set("title", "Lorem ipsum")
record.Set("status", request.Status)
record.Set("raw", request.Raw)
if err := app.Dao().SaveRecord(record); err != nil {
return err
}
// Send an email to the user
err = is.sendEmailReminder(record.Id)
if err != nil {
log.Printf("error sending reminder %s", err.Error())
}
return c.JSONPretty(http.StatusOK, map[string]any{"result": "OK"}, " ")
}