vestauth 0.4.0 → 0.4.3

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.4.0",
3
+ "version": "0.4.3",
4
4
  "description": "auth for agents–from the creator of dotenvx",
5
5
  "keywords": [
6
6
  "vestauth"
@@ -2,11 +2,11 @@ const { logger } = require('./../../../shared/logger')
2
2
 
3
3
  const agent = require('./../../../lib/agent')
4
4
 
5
- function init () {
5
+ async function init () {
6
6
  const options = this.opts()
7
7
  logger.debug(`options: ${JSON.stringify(options)}`)
8
8
 
9
- const output = agent.init()
9
+ const output = await agent.init()
10
10
 
11
11
  let space = 0
12
12
  if (options.prettyPrint) {
@@ -1,5 +1,6 @@
1
1
  const { http } = require('../helpers/http')
2
2
  const buildApiError = require('../helpers/buildApiError')
3
+ const agentHeaders = require('../helpers/agentHeaders')
3
4
 
4
5
  class PostAgentRegister {
5
6
  constructor (hostname, publicJwk) {
@@ -11,11 +12,13 @@ class PostAgentRegister {
11
12
  const url = `${this.hostname}/api/agent/register`
12
13
  const publicJwk = this.publicJwk
13
14
 
15
+ const httpMethod = 'POST'
16
+ const headers = await agentHeaders(httpMethod, url)
17
+ headers['Content-Type'] = 'application/json'
18
+
14
19
  const resp = await http(url, {
15
- method: 'POST',
16
- headers: {
17
- 'Content-Type': 'application/json'
18
- },
20
+ method: httpMethod,
21
+ headers,
19
22
  body: JSON.stringify({
20
23
  public_jwk: publicJwk
21
24
  })
@@ -1,13 +1,8 @@
1
1
  const headers = require('./headers')
2
- const dotenvx = require('@dotenvx/dotenvx')
2
+ const identity = require('./identity')
3
3
 
4
4
  async function agentHeaders (httpMethod, uri, tag = 'vestauth', nonce = null) {
5
- let publicKey = null
6
- let privateKey = null
7
- try { publicKey = dotenvx.get('AGENT_PUBLIC_KEY', { strict: true }) } catch (_e) {}
8
- try { privateKey = dotenvx.get('AGENT_PRIVATE_KEY', { strict: true }) } catch (_e) {}
9
-
10
- if (!publicKey && !privateKey) throw new Error('missing AGENT_PUBLIC_KEY and AGENT_PRIVATE_KEY. Run [vestauth agent init]')
5
+ const { privateKey } = identity()
11
6
 
12
7
  return await headers(httpMethod, uri, privateKey, tag, nonce)
13
8
  }
@@ -1,4 +1,5 @@
1
1
  const dotenvx = require('@dotenvx/dotenvx')
2
+ const identity = require('./identity')
2
3
  const keypair = require('./keypair')
3
4
  const touch = require('./touch')
4
5
  const PostAgentRegister = require('../api/postAgentRegister')
@@ -7,9 +8,7 @@ async function agentInit () {
7
8
  const envPath = '.env'
8
9
 
9
10
  // keypair
10
- let currentPrivateKey = null
11
- try { currentPrivateKey = dotenvx.get('AGENT_PRIVATE_KEY', { strict: true }) } catch (e) {}
12
-
11
+ const currentPrivateKey = identity(false).privateKey
13
12
  const kp = keypair(currentPrivateKey, 'agent')
14
13
 
15
14
  touch(envPath)
@@ -19,8 +18,7 @@ async function agentInit () {
19
18
  dotenvx.set('AGENT_PRIVATE_KEY', JSON.stringify(kp.privateKey), { path: envPath, plain: true, quiet: true })
20
19
 
21
20
  // register agent with api
22
- const postAgentRegister = new PostAgentRegister(null, kp.publicKey)
23
- await postAgentRegister.run()
21
+ await new PostAgentRegister(null, kp.publicKey).run()
24
22
 
25
23
  return {
26
24
  AGENT_PUBLIC_KEY: kp.publicKey,
@@ -0,0 +1,11 @@
1
+ const dotenvx = require('@dotenvx/dotenvx')
2
+
3
+ function env (key) {
4
+ try {
5
+ return dotenvx.get(key, { strict: true })
6
+ } catch {
7
+ return null
8
+ }
9
+ }
10
+
11
+ module.exports = env
@@ -0,0 +1,15 @@
1
+ const env = require('./env')
2
+
3
+ function identity (raiseError = true) {
4
+ const publicKey = env('AGENT_PUBLIC_KEY')
5
+ const privateKey = env('AGENT_PRIVATE_KEY')
6
+
7
+ if (raiseError && !(publicKey || !privateKey)) throw new Error('missing AGENT_PUBLIC_KEY and AGENT_PRIVATE_KEY. Run [vestauth agent init]')
8
+
9
+ return {
10
+ publicKey,
11
+ privateKey
12
+ }
13
+ }
14
+
15
+ module.exports = identity
@@ -4,7 +4,6 @@ const parseSignatureInputHeader = require('./parseSignatureInputHeader')
4
4
  const stripDictionaryKey = require('./stripDictionaryKey')
5
5
  const authorityMessage = require('./authorityMessage')
6
6
  const edPublicKeyObject = require('./edPublicKeyObject')
7
- const epoch = require('./epoch')
8
7
 
9
8
  function providerVerify (httpMetod, uri, signatureHeader, signatureInputHeader, publicKey) {
10
9
  const { values } = parseSignatureInputHeader(signatureInputHeader)
@@ -1,50 +0,0 @@
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
- // ok and if a success what should i do here? should i store the challenge to the .env file?
47
- return json
48
- }
49
-
50
- module.exports = agentAuth