-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
190 lines (166 loc) · 4.96 KB
/
index.js
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
const request = require("request-promise-native")
const messages = require("./messages.json")
const validation = require("./validation")
const parse = require("./parsing")
const dns = require("dns")
/**
* Requests and returns JWT from the server at the host provided in the alias.
* @param {string} alias
* @param {string} password
*/
async function authenticate(alias, password) {
if (!validation.isValidAlias(alias))
throw new Error(messages.INVALID_ALIAS_FORMAT)
const url = await getTargetUrl(alias, `/v1/auth`)
const result = await request({
method: 'post',
body: {
alias,
password
},
json: true,
url: url
})
if (result.jwt)
return result
throw result
}
/**
* Adds or Updates the given address type for the given user
* @param {string} alias The alias the address belongs to
* @param {number} type The address type e.g 100 (Bitcoin)
* @param {string} address The value of the address
* @param {string} jwt The jwt of the user behind the given alias
*
*/
async function putAddress(alias, type, address, jwt) {
if (!validation.isValidAlias(alias))
throw new Error(messages.INVALID_ALIAS_FORMAT)
const url = await getTargetUrl(alias, `/v1/addresses`)
return await request({
method: 'put',
headers: {
Authorization: `Bearer ${jwt}`
},
body: {
address_type: type,
address
},
json: true,
url: url
})
}
/**
* Requests and returns JWT from the server at the host provided in the alias.
* @param {string} alias The alias the address belongs to
* @param {number} type The address type e.g 100 (Bitcoin)
* @param {string} jwt The jwt of the user behind the given alias
*
*/
async function deleteAddress(alias, type, jwt) {
if (!validation.isValidAlias(alias))
throw new Error(messages.INVALID_ALIAS_FORMAT)
const url = await getTargetUrl(alias, `/v1/addresses/${type}`)
return await request({
method: 'delete',
headers: {
Authorization: `Bearer ${jwt}`
},
json: true,
url: url
})
}
/**
* Requests and returns JWT from the server at the host provided in the alias.
* @param {string} alias The alias the addresses belong to
* @param {string} jwt The jwt of the user behind the given alias
*
*/
async function deleteAllAddresses(alias, jwt) {
if (!validation.isValidAlias(alias))
throw new Error(messages.INVALID_ALIAS_FORMAT)
const url = await getTargetUrl(alias, "/v1/addresses")
return await request({
method: 'delete',
headers: {
Authorization: `Bearer ${jwt}`
},
json: true,
url: url
})
}
/**
* Fetches a specific address behind the given alias
* @param {string} alias The alias the addresses belong to
* @param {number} type The address type to be fetched
*
*
*/
async function getAddress(alias, type) {
if (!validation.isValidAlias(alias))
throw new Error(messages.INVALID_ALIAS_FORMAT)
const url = await getTargetUrl(alias, `/v1/addresses?alias=${alias}&address_type=${type}`)
return await request({
method: 'get',
json: true,
url: url
})
}
/**
* Fetches all addresses behind the given alias.
* @param {string} alias
*
*/
async function getAddresses(alias) {
if (!validation.isValidAlias(alias))
throw new Error(messages.INVALID_ALIAS_FORMAT)
const url = await getTargetUrl(alias, `/v1/addresses?alias=${alias}`)
return await request({
method: 'get',
json: true,
url: url
})
}
/**
* Resolves target host of the given alias and appends the given path.
* @param {string} alias
* @param {string} path
*/
async function getTargetUrl(alias, path) {
const target = await getTargetHost(alias)
return `https://${target}${path}`
}
/**
* Fetches the actual host behind an alias considering proxy domains via SRV Lookups.
* @param {string} alias
*/
async function getTargetHost(alias) {
const host = parse.hostFromAlias(alias)
const srvResult = await fetchOpenCAPSRVRecord(host)
const targetHost = srvResult[0].name
const targetPort = srvResult[0].port
return `${targetHost}:${targetPort}`
}
/**
* Fetches the opencap specific SRV record at the given domain.
* @param {string} domain
*/
function fetchOpenCAPSRVRecord(domain) {
const url = `_opencap._tcp.${domain}`
return new Promise((resolve, reject) => {
dns.resolveSrv(url, (error, result) => {
if (error) {
return reject(error)
}
resolve(result)
})
})
}
module.exports = {
authenticate,
getAddresses,
getAddress,
putAddress,
deleteAllAddresses,
deleteAddress,
}