-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: access-api uses DID env variable when building its ucanto serve…
…r id (#275) Motivation: * #237 Notes * This is intended to be safe to merge/deploy as is. The new functionality can be opted-into via env vars * The ucanto server will only be constructed with an opts.id that is a signer `withDID(...)` iff `process.env.DID` is set to a DID * If `process.env.DID` is unset, the ucanto server id signer will have a `did()` corresponding to `config.PRIVATE_KEY` (it will be a `did:key` of the corresponding public key) * Before this PR, many tests of the access-api worker were configured implicitly via dotenv with whatever the `/.env.local` file happened to contain. I added a worker test that asserts functionality that is dependent on the details of the env vars, so I made an affordance for [tests to be able to configure the access-api worker with environment variables defined in the test case itself](https://github.com/web3-storage/w3protocol/pull/275/files#diff-e20ffc550d006747790e7b67da280446c1cd1d2d4f72ccb5df04f62cbb6423daR149).
- Loading branch information
Showing
8 changed files
with
141 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import assert from 'assert' | ||
import * as configModule from '../src/config.js' | ||
|
||
/** keypair that can be used for testing */ | ||
const testKeypair = { | ||
private: { | ||
/** | ||
* Private key encoded as multiformats | ||
*/ | ||
multiformats: | ||
'MgCYWjE6vp0cn3amPan2xPO+f6EZ3I+KwuN1w2vx57vpJ9O0Bn4ci4jn8itwc121ujm7lDHkCW24LuKfZwIdmsifVysY=', | ||
}, | ||
public: { | ||
/** | ||
* Public key encoded as a did:key | ||
*/ | ||
did: 'did:key:z6MkqBzPG7oNu7At8fktasQuS7QR7Tj7CujaijPMAgzdmAxD', | ||
}, | ||
} | ||
|
||
describe('@web3-storage/access-api/src/config configureSigner', () => { | ||
it('creates a signer using config.{DID,PRIVATE_KEY}', async () => { | ||
const config = { | ||
PRIVATE_KEY: testKeypair.private.multiformats, | ||
DID: 'did:web:exampe.com', | ||
} | ||
const signer = configModule.configureSigner(config) | ||
assert.ok(signer) | ||
assert.equal(signer.did().toString(), config.DID) | ||
const { keys } = signer.toArchive() | ||
const didKeys = Object.keys(keys) | ||
assert.deepEqual(didKeys, [testKeypair.public.did]) | ||
}) | ||
it('errors if config.DID is provided but not a did', () => { | ||
assert.throws(() => { | ||
configModule.configureSigner({ | ||
DID: 'not a did', | ||
PRIVATE_KEY: testKeypair.private.multiformats, | ||
}) | ||
}, 'Invalid DID') | ||
}) | ||
it('infers did from config.PRIVATE_KEY when config.DID is omitted', async () => { | ||
const config = { | ||
PRIVATE_KEY: testKeypair.private.multiformats, | ||
} | ||
const signer = configModule.configureSigner(config) | ||
assert.ok(signer) | ||
assert.equal(signer.did().toString(), testKeypair.public.did) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,29 @@ describe('ucan', function () { | |
t.deepEqual(rsp, ['test pass']) | ||
}) | ||
|
||
test('should support ucan invoking to a did:web aud', async function () { | ||
const serviceDidWeb = 'did:web:example.com' | ||
const { mf, issuer, service } = await context({ | ||
environment: { | ||
...process.env, | ||
DID: serviceDidWeb, | ||
}, | ||
}) | ||
const ucan = await UCAN.issue({ | ||
issuer, | ||
audience: service.withDID(serviceDidWeb), | ||
capabilities: [{ can: 'testing/pass', with: 'mailto:[email protected]' }], | ||
}) | ||
const res = await mf.dispatchFetch('http://localhost:8787/raw', { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${UCAN.format(ucan)}`, | ||
}, | ||
}) | ||
const rsp = await res.json() | ||
t.deepEqual(rsp, ['test pass']) | ||
}) | ||
|
||
test('should handle exception in route handler', async function () { | ||
const { mf, service, issuer } = ctx | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.