-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
173 lines (142 loc) · 4.51 KB
/
api.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
var linksEndpoint = "/v1/credentials/links"
var qrCodeEndpoint = "/qrcode"
func runApi() {
r := gin.Default()
api := r.Group("/api/v1")
api.POST("/create/event", createEvent)
api.POST("/create/profile", createProfile)
api.GET("/ping", func(ctx *gin.Context) { ctx.JSON(http.StatusOK, "pong") })
r.Run(fmt.Sprintf(":%d", config.Port))
}
func createEvent(c *gin.Context) {
var payload *NewEventRequest
if err := c.ShouldBindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "fail", "error": err.Error()})
return
}
// TODO: make mandatory
if payload.Signature != "" {
hasSubscription, err := HasValidSubscription(payload.Signature)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "fail", "error": err.Error()})
return
}
if !hasSubscription {
c.JSON(http.StatusBadRequest, gin.H{"status": "fail", "error": errors.New("user is not subscribed")})
return
}
}
eventLinkRequest := EventCredentialLinkRequest{
SchemaId: config.EventSchemaId,
SignatureProof: true,
MtProof: true,
ClaimLinkExpiration: payload.ValidUntil,
LimitedClaims: payload.ClaimLimit,
CredentialSubject: payload.EventInfo,
}
reqJson, err := json.Marshal(eventLinkRequest)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "fail", "error": err.Error()})
return
}
createRes, err := sendCreateLinkRequest(reqJson)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "fail", "error": err.Error()})
return
}
qrCodeUrl := config.IssuerApi + linksEndpoint + "/" + createRes.Id + qrCodeEndpoint
png, err := createQrCode(qrCodeUrl)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "fail", "error": err.Error()})
return
}
c.Header("Content-Type", "image/png")
c.Header("Content-Length", fmt.Sprint(len(png)))
c.Data(http.StatusOK, "image/png", png)
}
func createProfile(c *gin.Context) {
var payload *Profile
if err := c.ShouldBindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "fail", "error": err.Error()})
return
}
profileLinkRequest := ProfileCredentialLinkRequest{
SchemaId: config.ProfileSchemaId,
SignatureProof: true,
MtProof: true,
CredentialSubject: *payload,
}
reqJson, err := json.Marshal(profileLinkRequest)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "fail", "error": err.Error()})
return
}
createRes, err := sendCreateLinkRequest(reqJson)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "fail", "error": err.Error()})
return
}
qrCodeUrl := fmt.Sprintf("%s%s/%s%s", config.IssuerApi, linksEndpoint, createRes.Id, qrCodeEndpoint)
//qrCodeUrl := config.IssuerApi + linksEndpoint + "/" + createRes.Id + qrCodeEndpoint
png, err := createQrCode(qrCodeUrl)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "fail", "error": err.Error()})
return
}
c.Header("Content-Type", "image/png")
c.Header("Content-Length", fmt.Sprint(len(png)))
c.Data(http.StatusOK, "image/png", png)
}
func sendCreateLinkRequest(reqJson []byte) (CreateLinkResponse, error) {
url := config.IssuerApi + linksEndpoint
r, err := http.NewRequest("POST", url, bytes.NewBuffer(reqJson))
if err != nil {
return CreateLinkResponse{}, err
}
r.SetBasicAuth(config.IssuerUname, config.IssuerPass)
r.Header.Add("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(r)
if err != nil {
return CreateLinkResponse{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
return CreateLinkResponse{}, fmt.Errorf("request to issuer node failed with status code: %d", res.StatusCode)
}
var createRes CreateLinkResponse
if err = json.NewDecoder(res.Body).Decode(&createRes); err != nil {
return CreateLinkResponse{}, err
}
return createRes, nil
}
func sendGetQrRequest(url string) (string, error) {
r, err := http.NewRequest("POST", url, nil)
if err != nil {
return "", err
}
r.SetBasicAuth(config.IssuerUname, config.IssuerPass)
client := &http.Client{}
res, err := client.Do(r)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("auth QR code request to issuer node failed with status code: %d", res.StatusCode)
}
var qrRes QrCodeRequestResponse
if err = json.NewDecoder(res.Body).Decode(&qrRes); err != nil {
return "", err
}
return qrRes.QrCode, nil
}