-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcorporate.go
211 lines (173 loc) · 5.27 KB
/
corporate.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
package mono
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
)
const (
// StatementPermission provides access to balance and statement itself.
StatementPermission byte = 's'
// PersonalPermission provides access to client's name and surname.
PersonalPermission byte = 'p'
)
type corporateAuth struct {
*SignTool
PrivateKey *ecdsa.PrivateKey
KeyID string
}
func (auth *corporateAuth) signStrings(params ...string) (string, error) {
return auth.Sign(auth.PrivateKey, strings.Join(params, ""))
}
func (auth *corporateAuth) Auth(r *http.Request) error {
timestamp := strconv.Itoa(int(time.Now().Unix()))
r.Header.Set("X-Key-Id", auth.KeyID)
r.Header.Set("X-Time", timestamp)
return nil
}
// Corporate gives access to corporate methods.
type Corporate struct {
authCore authCore
auth corporateAuth
}
func newCorporateAuth(
keyData []byte,
) (*corporateAuth, error) {
sign := DefaultSignTool()
privateKey, err := sign.DecodePrivateKey(keyData)
if err != nil {
return nil, errors.New("failed to decode private key")
}
publicKey := privateKey.PublicKey
data := elliptic.Marshal(publicKey, publicKey.X, publicKey.Y)
hash := sha1.New()
if _, err := hash.Write(data); err != nil {
return nil, errors.New("failed to encode public key with sha1")
}
keyID := hex.EncodeToString(hash.Sum(nil))
return &corporateAuth{
SignTool: sign,
PrivateKey: privateKey,
KeyID: keyID,
}, nil
}
// NewCorporate returns new client of MonoBank Corporate API.
func NewCorporate(keyData []byte) (*Corporate, error) {
auth, err := newCorporateAuth(keyData)
if err != nil {
return nil, err
}
return &Corporate{
auth: *auth,
authCore: *newAuthCore(auth),
}, nil
}
// Auth initializes access.
func (c *Corporate) Auth(ctx context.Context, callback string, permissions ...byte) (*TokenRequest, error) {
timestamp := strconv.Itoa(int(time.Now().Unix()))
pp := string(permissions)
endpoint := "/personal/auth/request"
sign, err := c.auth.signStrings(timestamp, pp, endpoint)
if err != nil {
return nil, err
}
headers := map[string]string{
"X-Permissions": pp,
"X-Sign": sign,
"X-Callback": callback,
}
body, status, err := c.authCore.PostJSON(ctx, endpoint, headers, nil)
if err != nil {
return nil, err
}
tokenRequest := new(TokenRequest)
if err := json.Unmarshal(body, tokenRequest); err != nil {
return nil, err
}
if status != http.StatusOK {
var msg Error
if err := json.Unmarshal(body, &msg); err != nil {
return nil, errors.New("invalid error payload")
}
return nil, msg
}
return tokenRequest, nil
}
// CheckAuth checks status of request for client's personal data.
func (c *Corporate) CheckAuth(ctx context.Context, reqID string) (bool, error) {
timestamp := strconv.Itoa(int(time.Now().Unix()))
endpoint := "/personal/auth/request"
sign, err := c.auth.signStrings(timestamp, reqID, endpoint)
if err != nil {
return false, err
}
headers := map[string]string{
"X-Sign": sign,
"X-Request-Id": reqID,
}
body, status, err := c.authCore.GetJSON(ctx, endpoint, headers)
if err != nil {
return false, err
}
if status != http.StatusOK {
var msg Error
if err := json.Unmarshal(body, &msg); err != nil {
return false, errors.New("invalid error payload")
}
return false, msg
}
return true, nil
}
// User returns user personal information from MonoBank API.
// See https://api.monobank.ua/docs/#/definitions/UserInfo for details.
func (c *Corporate) User(ctx context.Context, reqID string) (*UserInfo, error) {
timestamp := strconv.Itoa(int(time.Now().Unix()))
endpoint := "/personal/client-info"
sign, err := c.auth.signStrings(timestamp, reqID, endpoint)
if err != nil {
return nil, err
}
headers := map[string]string{
"X-Sign": sign,
"X-Request-Id": reqID,
}
return c.authCore.User(ctx, headers)
}
// Transactions returns list of transactions from {from} till {to} time.
// See https://api.monobank.ua/docs/#/definitions/StatementItems for details.
func (c *Corporate) Transactions(ctx context.Context, reqID string, account string, from, to time.Time) ([]Transaction, error) {
timestamp := strconv.Itoa(int(time.Now().Unix()))
fmt.Println()
path := fmt.Sprintf("/personal/statement/%s/%d/%d", account, from.Unix(), to.Unix())
sign, err := c.auth.signStrings(timestamp, reqID, path)
if err != nil {
return nil, err
}
headers := map[string]string{
"X-Sign": sign,
"X-Request-Id": reqID,
}
return c.authCore.Transactions(ctx, account, from, to, headers)
}
// Rates returns list of currencies rates from MonoBank API.
// See https://api.monobank.ua/docs/#/definitions/CurrencyInfo for details.
func (c *Corporate) Rates(ctx context.Context) ([]Exchange, error) {
return c.authCore.Rates(ctx)
}
// GetJSON builds the full endpoint path and gets the raw JSON.
func (c *Corporate) GetJSON(ctx context.Context, endpoint string, headers map[string]string) ([]byte, int, error) {
return c.authCore.GetJSON(ctx, endpoint, headers)
}
// PostJSON builds the full endpoint path and gets the raw JSON.
func (c *Corporate) PostJSON(ctx context.Context, endpoint string, headers map[string]string, payload io.Reader) ([]byte, int, error) {
return c.authCore.PostJSON(ctx, endpoint, headers, payload)
}