vestauth 0.2.1 → 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/README.md CHANGED
@@ -1,3 +1,32 @@
1
- # vestauth
1
+ [![vestauth](https://vestauth.com/banner.png)](https://vestauth.com)
2
2
 
3
- coming soon
3
+ *auth for agents*–from the creator of [`dotenvx`](https://github.com/dotenvx/dotenvx).
4
+
5
+ * auth
6
+
7
+  
8
+
9
+ ### Quickstart [![npm version](https://img.shields.io/npm/v/vestauth.svg)](https://www.npmjs.com/package/vestauth) [![downloads](https://img.shields.io/npm/dw/vestauth)](https://www.npmjs.com/package/vestauth)
10
+
11
+ ```sh
12
+ npm install vestauth --save
13
+ ```
14
+ ```js
15
+ // index.js
16
+ // TODO
17
+ ```
18
+
19
+  
20
+
21
+ or install globally - *unlocks vestauth for any language, framework, or platform!*
22
+
23
+ <details><summary>with curl 🌐 </summary><br>
24
+
25
+ ```sh
26
+ curl -sfS https://vestauth.sh | sh
27
+ vestauth help
28
+ ```
29
+
30
+ &nbsp;
31
+
32
+ </details>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vestauth",
3
- "version": "0.2.1",
4
- "description": "auth for agents",
3
+ "version": "0.2.4",
4
+ "description": "auth for agents–from the creator of dotenvx",
5
5
  "keywords": [
6
6
  "vestauth"
7
7
  ],
@@ -37,6 +37,7 @@
37
37
  "release": "standard-version"
38
38
  },
39
39
  "dependencies": {
40
+ "@dotenvx/dotenvx": "^1.52.0",
40
41
  "@noble/hashes": "^1.8.0",
41
42
  "@noble/secp256k1": "^1.7.2",
42
43
  "commander": "^11.1.0",
@@ -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
@@ -0,0 +1,22 @@
1
+ const { logger } = require('./../../../shared/logger')
2
+
3
+ const agent = require('./../../../lib/agent')
4
+
5
+ function hello () {
6
+ const options = this.opts()
7
+ logger.debug(`options: ${JSON.stringify(options)}`)
8
+
9
+
10
+ const output = {
11
+ hello: agent.hello()
12
+ }
13
+
14
+ let space = 0
15
+ if (options.prettyPrint) {
16
+ space = 2
17
+ }
18
+
19
+ console.log(JSON.stringify(output, null, space))
20
+ }
21
+
22
+ module.exports = hello
@@ -0,0 +1,19 @@
1
+ const { logger } = require('./../../../shared/logger')
2
+
3
+ const agent = require('./../../../lib/agent')
4
+
5
+ function init () {
6
+ const options = this.opts()
7
+ logger.debug(`options: ${JSON.stringify(options)}`)
8
+
9
+ const output = agent.init()
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 = init
@@ -1,12 +1,12 @@
1
1
  const { logger } = require('./../../../shared/logger')
2
2
 
3
- const main = require('./../../../lib/main')
3
+ const primitives = require('./../../../lib/primitives')
4
4
 
5
5
  function challenge () {
6
6
  const options = this.opts()
7
7
  logger.debug(`options: ${JSON.stringify(options)}`)
8
8
 
9
- const chal = main.challenge()
9
+ const chal = primitives.challenge()
10
10
 
11
11
  const output = {
12
12
  challenge: chal
@@ -1,6 +1,6 @@
1
1
  const { logger } = require('./../../../shared/logger')
2
2
 
3
- const main = require('./../../../lib/main')
3
+ const primitives = require('./../../../lib/primitives')
4
4
 
5
5
  function hash (message) {
6
6
  logger.debug(`message: ${message}`)
@@ -8,7 +8,7 @@ function hash (message) {
8
8
  const options = this.opts()
9
9
  logger.debug(`options: ${JSON.stringify(options)}`)
10
10
 
11
- const hashMessage = main.hash(message)
11
+ const hashMessage = primitives.hash(message)
12
12
  const hashBase64Url = Buffer.from(hashMessage).toString('base64url')
13
13
 
14
14
  const output = {
@@ -1,12 +1,12 @@
1
1
  const { logger } = require('./../../../shared/logger')
2
2
 
3
- const main = require('./../../../lib/main')
3
+ const primitives = require('./../../../lib/primitives')
4
4
 
5
5
  function keypair (existingPrivateKey) {
6
6
  const options = this.opts()
7
7
  logger.debug(`options: ${JSON.stringify(options)}`)
8
8
 
9
- const kp = main.keypair(existingPrivateKey, options.prefix)
9
+ const kp = primitives.keypair(existingPrivateKey, options.prefix)
10
10
 
11
11
  const output = {
12
12
  public_key: kp.publicKey,
@@ -1,6 +1,6 @@
1
1
  const { logger } = require('./../../../shared/logger')
2
2
 
3
- const main = require('./../../../lib/main')
3
+ const primitives = require('./../../../lib/primitives')
4
4
 
5
5
  async function sign (challenge, privateKeyHex) {
6
6
  logger.debug(`challenge: ${challenge}`)
@@ -9,7 +9,7 @@ async function sign (challenge, privateKeyHex) {
9
9
  const options = this.opts()
10
10
  logger.debug(`options: ${JSON.stringify(options)}`)
11
11
 
12
- const signature = await main.sign(challenge, privateKeyHex)
12
+ const signature = await primitives.sign(challenge, privateKeyHex)
13
13
 
14
14
  const output = {
15
15
  signature
@@ -1,6 +1,6 @@
1
1
  const { logger } = require('./../../../shared/logger')
2
2
 
3
- const main = require('./../../../lib/main')
3
+ const primitives = require('./../../../lib/primitives')
4
4
 
5
5
  async function verify (challenge, signatureBase64, publicKeyHex) {
6
6
  logger.debug(`challenge: ${challenge}`)
@@ -10,7 +10,7 @@ async function verify (challenge, signatureBase64, publicKeyHex) {
10
10
  const options = this.opts()
11
11
  logger.debug(`options: ${JSON.stringify(options)}`)
12
12
 
13
- const success = main.verify(challenge, signatureBase64, publicKeyHex)
13
+ const success = primitives.verify(challenge, signatureBase64, publicKeyHex)
14
14
 
15
15
  const output = {
16
16
  success,
@@ -0,0 +1,31 @@
1
+ const { Command } = require('commander')
2
+
3
+ const agent = new Command('agent')
4
+
5
+ agent
6
+ .description('🪪 agent')
7
+ .allowUnknownOption()
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
+
17
+ // vestauth agent init
18
+ const initAction = require('./../actions/agent/init')
19
+ agent.command('init')
20
+ .description('create agent')
21
+ .option('-pp, --pretty-print', 'pretty print output')
22
+ .action(initAction)
23
+
24
+
25
+ // vestauth agent hello
26
+ const helloAction = require('./../actions/agent/hello')
27
+ agent.command('hello')
28
+ .description('say hello')
29
+ .action(helloAction)
30
+
31
+ module.exports = agent
@@ -38,6 +38,9 @@ program
38
38
  .version(packageJson.version)
39
39
  .allowUnknownOption()
40
40
 
41
+ // dotenvx agent
42
+ program.addCommand(require('./commands/agent'))
43
+
41
44
  // vestauth verifyAgent
42
45
  const verifyAgentAction = require('./actions/verifyAgent')
43
46
  program.command('verifyagent')
@@ -0,0 +1,9 @@
1
+ const agentAuth = require('./helpers/agentAuth')
2
+ const agentInit = require('./helpers/agentInit')
3
+ const hello = require('./helpers/hello')
4
+
5
+ module.exports = {
6
+ auth: agentAuth,
7
+ init: agentInit,
8
+ hello
9
+ }
@@ -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
@@ -0,0 +1,27 @@
1
+ const dotenvx = require('@dotenvx/dotenvx')
2
+ const keypair = require('./keypair')
3
+ const touch = require('./touch')
4
+
5
+ function agentInit () {
6
+ const envPath = '.env'
7
+
8
+ // keypair
9
+ let currentPrivateKey = null
10
+ try { currentPrivateKey = dotenvx.get('AGENT_PRIVATE_KEY', { strict: true }) } catch (e) {}
11
+
12
+ const kp = keypair(currentPrivateKey, 'agent')
13
+
14
+ touch(envPath)
15
+
16
+ // place in .env file
17
+ dotenvx.set('AGENT_PUBLIC_KEY', kp.publicKey, { path: envPath, plain: true, quiet: true })
18
+ dotenvx.set('AGENT_PRIVATE_KEY', kp.privateKey, { path: envPath, plain: true, quiet: true })
19
+
20
+ return {
21
+ AGENT_PUBLIC_KEY: kp.publicKey,
22
+ AGENT_PRIVATE_KEY: kp.privateKey,
23
+ path: envPath
24
+ }
25
+ }
26
+
27
+ module.exports = agentInit
@@ -0,0 +1,5 @@
1
+ function hello () {
2
+ return 'hello'
3
+ }
4
+
5
+ module.exports = hello
@@ -0,0 +1,7 @@
1
+ const fs = require('fs')
2
+
3
+ function touch (filepath) {
4
+ fs.closeSync(fs.openSync(filepath, 'a'))
5
+ }
6
+
7
+ module.exports = touch