This repository has been archived by the owner on May 17, 2024. It is now read-only.
generated from findy-network/findy-template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_issue_credential_service.go
182 lines (155 loc) · 5.63 KB
/
api_issue_credential_service.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
/*
* Aries Agent Test Harness Backchannel API
*
* This page documents the backchannel API the test harness uses to communicate with agents under tests. For more information checkout the [Aries Interoperability Information](http://aries-interop.info) page.
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package openapi
import (
"context"
"log"
"net/http"
"github.com/findy-network/findy-agent-backchannel/agent"
)
func getCredStatus(issuer bool, state agent.IssueCredentialState) IssueCredentialState {
res := CREDENTIAL_RECEIVED
switch state {
case agent.PROPOSAL:
res = PROPOSAL_RECEIVED
if !issuer {
res = PROPOSAL_SENT
}
case agent.OFFER:
res = OFFER_SENT
if !issuer {
res = OFFER_RECEIVED
}
case agent.REQUEST:
res = REQUEST_RECEIVED
if !issuer {
res = REQUEST_SENT
}
case agent.CREDENTIAL:
res = CREDENTIAL_ISSUED
if !issuer {
res = CREDENTIAL_RECEIVED
}
case agent.DONE:
res = DONE
}
return res
}
// IssueCredentialApiService is a service that implents the logic for the IssueCredentialApiServicer
// This service should implement the business logic for every endpoint for the IssueCredentialApi API.
// Include any external packages or services that will be required by this service.
type IssueCredentialApiService struct {
a *agent.Agent
}
// NewIssueCredentialApiService creates a default api service
func NewIssueCredentialApiService(a *agent.Agent) IssueCredentialApiServicer {
return &IssueCredentialApiService{
a: a,
}
}
// IssueCredentialGetByThreadId - Get credential exchange record by thread id
func (s *IssueCredentialApiService) IssueCredentialGetByThreadId(ctx context.Context, credentialExchangeThreadId string) (ImplResponse, error) {
issuer, state, err := s.a.GetCredential(credentialExchangeThreadId)
if err != nil {
return Response(http.StatusNotFound, nil), err
}
res := getCredStatus(issuer, state)
log.Println("IssueCredentialGetByThreadId Credential status ", res, credentialExchangeThreadId)
return Response(200, IssueCredentialOperationResponse{
State: res,
ThreadId: credentialExchangeThreadId,
}), nil
}
// IssueCredentialIssue - Issue Credential
func (s *IssueCredentialApiService) IssueCredentialIssue(ctx context.Context, req IssueCredentialIssueRequest) (ImplResponse, error) {
err := s.a.IssueCredential(req.Id)
if err != nil {
return Response(http.StatusInternalServerError, nil), err
}
issuer, state, err := s.a.GetCredential(req.Id)
if err != nil {
return Response(http.StatusNotFound, nil), err
}
res := getCredStatus(issuer, state)
log.Println("IssueCredentialIssue Credential status ", res, req.Id)
return Response(200, IssueCredentialOperationResponse{
State: res,
ThreadId: req.Id,
}), nil
}
// IssueCredentialSendOffer - Send credential offer
func (s *IssueCredentialApiService) IssueCredentialSendOffer(ctx context.Context, req IssueCredentialOfferRequest) (r ImplResponse, err error) {
if req.Id == "" {
connectionID := req.Data.ConnectionId
credDefID := req.Data.CredDefId
previewAttributes := req.Data.CredentialProposal.Attributes
attributes := make([]*agent.CredentialAttribute, 0)
for _, attr := range previewAttributes {
attributes = append(attributes, &agent.CredentialAttribute{
Name: attr.Name,
Value: attr.Value,
})
}
var threadId string
threadId, err = s.a.OfferCredential(connectionID, credDefID, attributes)
if err == nil {
return Response(200, IssueCredentialOperationResponse{State: OFFER_SENT, ThreadId: threadId}), nil
}
} else { // offer to proposal
_, err = s.a.AcceptCredentialProposal(req.Id)
if err == nil {
return Response(200, IssueCredentialOperationResponse{ThreadId: req.Id, State: OFFER_SENT}), nil
}
}
return Response(http.StatusInternalServerError, nil), err
}
// IssueCredentialSendProposal - Send credential proposal
func (s *IssueCredentialApiService) IssueCredentialSendProposal(ctx context.Context, inlineObject6 InlineObject6) (ImplResponse, error) {
connectionID := inlineObject6.Data.ConnectionId
credDefID := inlineObject6.Data.CredDefId
previewAttributes := inlineObject6.Data.CredentialProposal.Attributes
attributes := make([]*agent.CredentialAttribute, 0)
for _, attr := range previewAttributes {
attributes = append(attributes, &agent.CredentialAttribute{
Name: attr.Name,
Value: attr.Value,
})
}
threadId, err := s.a.ProposeCredential(connectionID, credDefID, attributes)
if err == nil {
return Response(200, IssueCredentialOperationResponse{State: PROPOSAL_SENT, ThreadId: threadId}), nil
}
return Response(http.StatusInternalServerError, nil), err
}
// IssueCredentialSendRequest - Send credential request
func (s *IssueCredentialApiService) IssueCredentialSendRequest(ctx context.Context, req SendIssueCredentialRequest) (ImplResponse, error) {
threadId, err := s.a.RequestCredential(req.Id)
if err == nil {
return Response(200, IssueCredentialOperationResponse{State: REQUEST_SENT, ThreadId: threadId}), nil
}
return Response(http.StatusInternalServerError, nil), err
}
// IssueCredentialStore - Store Credential
func (s *IssueCredentialApiService) IssueCredentialStore(ctx context.Context, req IssueCredentialStoreRequest) (ImplResponse, error) {
err := s.a.ReceiveCredential(req.Id)
if err != nil {
return Response(http.StatusNotFound, nil), err
}
issuer, state, err := s.a.GetCredential(req.Id)
if err != nil {
return Response(http.StatusNotFound, nil), err
}
res := getCredStatus(issuer, state)
log.Println("IssueCredentialStore Credential status ", res, req.Id)
return Response(200, IssueCredentialOperationResponse{
State: res,
ThreadId: req.Id,
CredentialId: req.Id,
}), nil
}