-
Notifications
You must be signed in to change notification settings - Fork 22
/
request_idp.go
173 lines (158 loc) · 5.83 KB
/
request_idp.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
/**
* Copyright (c) 2018, 2019 National Digital ID COMPANY LIMITED
*
* This file is part of NDID software.
*
* NDID is the free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or any later
* version.
*
* NDID 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 Affero GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public License
* along with the NDID source code. If not, see https://www.gnu.org/licenses/agpl.txt.
*
* Please contact info@ndid.co.th for any further questions
*
*/
package app
import (
"encoding/json"
"fmt"
"github.com/tendermint/tendermint/abci/types"
"google.golang.org/protobuf/proto"
"github.com/ndidplatform/smart-contract/v7/abci/code"
"github.com/ndidplatform/smart-contract/v7/abci/utils"
data "github.com/ndidplatform/smart-contract/v7/protos/data"
)
type CreateIdpResponseParam struct {
Aal float64 `json:"aal"`
Ial float64 `json:"ial"`
RequestID string `json:"request_id"`
Signature string `json:"signature"`
Status string `json:"status"`
ErrorCode *int32 `json:"error_code"`
}
func (app *ABCIApplication) createIdpResponse(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("CreateIdpResponse, Parameter: %s", param)
var funcParam CreateIdpResponseParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// get request
key := requestKeyPrefix + keySeparator + funcParam.RequestID
value, err := app.state.GetVersioned([]byte(key), 0, false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if value == nil {
return app.ReturnDeliverTxLog(code.RequestIDNotFound, "Request ID not found", "")
}
var request data.Request
err = proto.Unmarshal([]byte(value), &request)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// Check min_idp
var nonErrorResponseCount int64 = 0
for _, response := range request.ResponseList {
if response.Status != "" {
nonErrorResponseCount++
}
}
if nonErrorResponseCount >= request.MinIdp {
return app.ReturnDeliverTxLog(code.RequestIsCompleted, "Can't response to a request that is completed", "")
}
var remainingPossibleResponseCount int64 = int64(len(request.IdpIdList)) - int64(len(request.ResponseList))
if nonErrorResponseCount+remainingPossibleResponseCount < request.MinIdp {
return app.ReturnDeliverTxLog(code.RequestCannotBeFulfilled, "Can't response to a request that cannot be fulfilled", "")
}
response := data.Response{
IdpId: nodeID,
}
// Check closed request
if request.Closed {
return app.ReturnDeliverTxLog(code.RequestIsClosed, "Can't response a request that's closed", "")
}
// Check timed out request
if request.TimedOut {
return app.ReturnDeliverTxLog(code.RequestIsTimedOut, "Can't response a request that's timed out", "")
}
if funcParam.ErrorCode == nil {
response.Ial = funcParam.Ial
response.Aal = funcParam.Aal
response.Status = funcParam.Status
response.Signature = funcParam.Signature
// Check AAL
if request.MinAal > response.Aal {
return app.ReturnDeliverTxLog(code.AALError, "Response's AAL is less than min AAL", "")
}
// Check IAL
if request.MinIal > response.Ial {
return app.ReturnDeliverTxLog(code.IALError, "Response's IAL is less than min IAL", "")
}
// Check AAL, IAL with MaxIalAal
nodeDetailKey := nodeIDKeyPrefix + keySeparator + nodeID
nodeDetailValue, err := app.state.Get([]byte(nodeDetailKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if nodeDetailValue == nil {
return app.ReturnDeliverTxLog(code.NodeIDNotFound, "Node ID not found", "")
}
var nodeDetail data.NodeDetail
err = proto.Unmarshal([]byte(nodeDetailValue), &nodeDetail)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
if response.Aal > nodeDetail.MaxAal {
return app.ReturnDeliverTxLog(code.AALError, "Response's AAL is greater than max AAL", "")
}
if response.Ial > nodeDetail.MaxIal {
return app.ReturnDeliverTxLog(code.IALError, "Response's IAL is greater than max IAL", "")
}
} else {
// Check error code exists
errorCodeKey := errorCodeKeyPrefix + keySeparator + "idp" + keySeparator + fmt.Sprintf("%d", *funcParam.ErrorCode)
hasErrorCodeKey, err := app.state.Has([]byte(errorCodeKey), false)
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
if !hasErrorCodeKey {
return app.ReturnDeliverTxLog(code.InvalidErrorCode, "ErrorCode does not exist", "")
}
response.ErrorCode = *funcParam.ErrorCode
}
// Check nodeID is exist in idp_id_list
exist := false
for _, idpID := range request.IdpIdList {
if idpID == nodeID {
exist = true
break
}
}
if exist == false {
return app.ReturnDeliverTxLog(code.NodeIDDoesNotExistInIdPList, "Node ID does not exist in IdP list", "")
}
// Check duplicate response from the same IdP
for _, oldResponse := range request.ResponseList {
if oldResponse.IdpId == nodeID {
return app.ReturnDeliverTxLog(code.DuplicateIdPResponse, "Duplicate IdP response", "")
}
}
request.ResponseList = append(request.ResponseList, &response)
value, err = utils.ProtoDeterministicMarshal(&request)
if err != nil {
return app.ReturnDeliverTxLog(code.MarshalError, err.Error(), "")
}
err = app.state.SetVersioned([]byte(key), []byte(value))
if err != nil {
return app.ReturnDeliverTxLog(code.AppStateError, err.Error(), "")
}
return app.ReturnDeliverTxLog(code.OK, "success", funcParam.RequestID)
}