vestauth 0.2.3 → 0.2.5

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vestauth",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "auth for agents–from the creator of dotenvx",
5
5
  "keywords": [
6
6
  "vestauth"
@@ -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,7 +6,6 @@ function hello () {
6
6
  const options = this.opts()
7
7
  logger.debug(`options: ${JSON.stringify(options)}`)
8
8
 
9
-
10
9
  const output = {
11
10
  hello: agent.hello()
12
11
  }
@@ -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')
package/src/lib/agent.js CHANGED
@@ -1,7 +1,9 @@
1
+ const agentAuth = require('./helpers/agentAuth')
1
2
  const agentInit = require('./helpers/agentInit')
2
3
  const hello = require('./helpers/hello')
3
4
 
4
5
  module.exports = {
6
+ auth: agentAuth,
5
7
  init: agentInit,
6
8
  hello
7
9
  }
@@ -0,0 +1,49 @@
1
+ const { http } = require('./http')
2
+ const sign = require('./sign')
3
+ const dotenvx = require('@dotenvx/dotenvx')
4
+
5
+ async function agentAuth (website) {
6
+ let publicKey = null
7
+ let privateKey = null
8
+ try { publicKey = dotenvx.get('AGENT_PUBLIC_KEY', { strict: true }) } catch (_e) {}
9
+ try { privateKey = dotenvx.get('AGENT_PRIVATE_KEY', { strict: true }) } catch (_e) {}
10
+
11
+ if (!publicKey && !privateKey) throw new Error('missing AGENT_PUBLIC_KEY and AGENT_PRIVATE_KEY. Run [vestauth agent init]')
12
+
13
+ let signature
14
+ const url = `${website}/agent/auth`
15
+
16
+ if (!signature) {
17
+ const resp = await http(url, {
18
+ method: 'POST',
19
+ headers: {
20
+ Authorization: `Agent ${publicKey}:${signature}`,
21
+ 'Content-Type': 'application/json'
22
+ },
23
+ body: JSON.stringify({})
24
+ })
25
+
26
+ const json = await resp.body.json()
27
+ const challenge = json.challenge // grab challenge
28
+ signature = await sign(challenge, privateKey)
29
+ }
30
+
31
+ const resp = await http(url, {
32
+ method: 'POST',
33
+ headers: {
34
+ Authorization: `Agent ${publicKey}:${signature}`,
35
+ 'Content-Type': 'application/json'
36
+ },
37
+ body: JSON.stringify({})
38
+ })
39
+
40
+ if (resp.statusCode >= 400) {
41
+ const json = await resp.body.json()
42
+ return json
43
+ }
44
+
45
+ const json = await resp.body.json()
46
+ return json
47
+ }
48
+
49
+ module.exports = agentAuth
@@ -2,9 +2,6 @@ const dotenvx = require('@dotenvx/dotenvx')
2
2
  const keypair = require('./keypair')
3
3
  const touch = require('./touch')
4
4
 
5
- const PUBLIC_KEYNAME = 'AGENT_PUBLIC_KEY'
6
- const PRIVATE_KEYNAME = 'AGENT_PRIVATE_KEY'
7
-
8
5
  function agentInit () {
9
6
  const envPath = '.env'
10
7