1
1
/* Copyright © 2022-2023 Seneca Project Contributors, MIT License. */
2
2
3
+ const crypto = require ( 'crypto' ) ;
3
4
4
5
const Pkg = require ( '../package.json' )
5
6
@@ -11,6 +12,20 @@ type TilloProviderOptions = {
11
12
debug : boolean
12
13
}
13
14
15
+ type TilloSignatureOptions = {
16
+ apiKey : string
17
+ apiSecret : string
18
+ method : string
19
+ path ?: string
20
+ timestamp : string
21
+ }
22
+
23
+
24
+ function getAuthSignature ( signData : TilloSignatureOptions ) {
25
+ const authSign = `${ signData . apiKey } -${ signData . method } -${ signData . path } -${ signData . timestamp } `
26
+ const hashedSign = crypto . createHmac ( 'sha256' , signData . apiSecret ) . update ( authSign ) . digest ( 'hex' )
27
+ return hashedSign
28
+ }
14
29
15
30
function TilloProvider ( this : any , options : TilloProviderOptions ) {
16
31
const seneca : any = this
@@ -71,10 +86,24 @@ function TilloProvider(this: any, options: TilloProviderOptions) {
71
86
cmd : {
72
87
list : {
73
88
action : async function ( this : any , entize : any , msg : any ) {
89
+ const path = "brands"
90
+ const timestamp = new Date ( ) . getTime ( ) . toString ( )
91
+ const options : TilloSignatureOptions = {
92
+ apiKey : this . shared . headers [ "API-Key" ] ,
93
+ method : "GET" ,
94
+ path,
95
+ timestamp,
96
+ apiSecret : this . shared . secret
97
+ }
98
+
99
+ this . shared . headers . Signature = getAuthSignature ( options )
100
+ this . shared . headers . Timestamp = timestamp
101
+ this . shared . headers . Accept = "application/json"
102
+
74
103
let json : any =
75
- await getJSON ( makeUrl ( 'catalogs' , msg . q ) , makeConfig ( ) )
76
- let brands = json . brands
77
- let list = brands . map ( ( data : any ) => entize ( data ) )
104
+ await getJSON ( makeUrl ( path , msg . q ) , makeConfig ( ) )
105
+ let brands = json . data . brands
106
+ let list = Object . entries ( brands ) . map ( ( [ name , value ] : any ) => entize ( { name , value } ) )
78
107
return list
79
108
} ,
80
109
}
@@ -124,22 +153,15 @@ function TilloProvider(this: any, options: TilloProviderOptions) {
124
153
let res =
125
154
await this . post ( 'sys:provider,get:keymap,provider:tillo' )
126
155
127
- // if (!res.ok) {
128
- // throw this.fail('keymap')
129
- // }
130
- //
131
- // let src = res.keymap.name.value + ':' + res.keymap.key.value
132
- // let auth = Buffer.from(src).toString('base64')
133
- //
134
- // this.shared.headers = {
135
- // Authorization: 'Basic ' + auth
136
- // }
137
- //
138
- // this.shared.primary = {
139
- // customerIdentifier: res.keymap.cust.value,
140
- // accountIdentifier: res.keymap.acc.value,
141
- // }
156
+ if ( ! res . ok ) {
157
+ throw this . fail ( 'keymap' )
158
+ }
159
+
160
+ this . shared . headers = {
161
+ 'API-Key' : res . keymap . key . value ,
162
+ }
142
163
164
+ this . shared . secret = res . keymap . secret . value
143
165
} )
144
166
145
167
@@ -154,7 +176,7 @@ function TilloProvider(this: any, options: TilloProviderOptions) {
154
176
const defaults : TilloProviderOptions = {
155
177
156
178
// NOTE: include trailing /
157
- url : 'https://integration-api .tillo.com/raas /v2/' ,
179
+ url : 'https://sandbox .tillo.dev/api /v2/' ,
158
180
159
181
// Use global fetch by default - if exists
160
182
fetch : ( 'undefined' === typeof fetch ? undefined : fetch ) ,
0 commit comments