vestauth 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const { logger } = require('./../../../shared/logger')
|
|
2
|
+
|
|
3
|
+
const agent = require('./../../../lib/agent')
|
|
4
|
+
|
|
5
|
+
async function auth (website) {
|
|
6
|
+
const options = this.opts()
|
|
7
|
+
logger.debug(`options: ${JSON.stringify(options)}`)
|
|
8
|
+
|
|
9
|
+
const output = await agent.auth(website)
|
|
10
|
+
|
|
11
|
+
let space = 0
|
|
12
|
+
if (options.prettyPrint) {
|
|
13
|
+
space = 2
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
console.log(JSON.stringify(output, null, space))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = auth
|
|
@@ -6,6 +6,14 @@ agent
|
|
|
6
6
|
.description('🪪 agent')
|
|
7
7
|
.allowUnknownOption()
|
|
8
8
|
|
|
9
|
+
// vestauth agent auth
|
|
10
|
+
const authAction = require('./../actions/agent/auth')
|
|
11
|
+
agent.command('auth')
|
|
12
|
+
.description('auth agent')
|
|
13
|
+
.argument('<website>', 'root url of website')
|
|
14
|
+
.option('-pp, --pretty-print', 'pretty print output')
|
|
15
|
+
.action(authAction)
|
|
16
|
+
|
|
9
17
|
// vestauth agent init
|
|
10
18
|
const initAction = require('./../actions/agent/init')
|
|
11
19
|
agent.command('init')
|
|
@@ -13,6 +21,7 @@ agent.command('init')
|
|
|
13
21
|
.option('-pp, --pretty-print', 'pretty print output')
|
|
14
22
|
.action(initAction)
|
|
15
23
|
|
|
24
|
+
|
|
16
25
|
// vestauth agent hello
|
|
17
26
|
const helloAction = require('./../actions/agent/hello')
|
|
18
27
|
agent.command('hello')
|
package/src/lib/agent.js
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const { http } = require('./http')
|
|
2
|
+
const buildApiError = require('./buildApiError')
|
|
3
|
+
const sign = require('./sign')
|
|
4
|
+
const dotenvx = require('@dotenvx/dotenvx')
|
|
5
|
+
|
|
6
|
+
async function agentAuth (website) {
|
|
7
|
+
let publicKey = null
|
|
8
|
+
let privateKey = null
|
|
9
|
+
try { publicKey = dotenvx.get('AGENT_PUBLIC_KEY', { strict: true }) } catch (_e) {}
|
|
10
|
+
try { privateKey = dotenvx.get('AGENT_PRIVATE_KEY', { strict: true }) } catch (_e) {}
|
|
11
|
+
|
|
12
|
+
if (!publicKey && !privateKey) throw new Error('missing AGENT_PUBLIC_KEY and AGENT_PRIVATE_KEY. Run [vestauth agent init]')
|
|
13
|
+
|
|
14
|
+
let signature
|
|
15
|
+
const url = `${website}/agent/auth`
|
|
16
|
+
|
|
17
|
+
if (!signature) {
|
|
18
|
+
const resp = await http(url, {
|
|
19
|
+
method: 'POST',
|
|
20
|
+
headers: {
|
|
21
|
+
Authorization: `Agent ${publicKey}:${signature}`,
|
|
22
|
+
'Content-Type': 'application/json'
|
|
23
|
+
},
|
|
24
|
+
body: JSON.stringify({})
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const json = await resp.body.json()
|
|
28
|
+
const challenge = json.challenge // grab challenge
|
|
29
|
+
signature = await sign(challenge, privateKey)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const resp = await http(url, {
|
|
33
|
+
method: 'POST',
|
|
34
|
+
headers: {
|
|
35
|
+
Authorization: `Agent ${publicKey}:${signature}`,
|
|
36
|
+
'Content-Type': 'application/json'
|
|
37
|
+
},
|
|
38
|
+
body: JSON.stringify({})
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
if (resp.statusCode >= 400) {
|
|
42
|
+
const json = await resp.body.json()
|
|
43
|
+
return json
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const json = await resp.body.json()
|
|
47
|
+
return json
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports = agentAuth
|