-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: access-api uses DID env variable when building its ucanto server id #275
Changes from 3 commits
6504306
a4785dd
91edd06
795582a
b9533c9
d204331
5184a55
a629263
852951f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,8 +1,7 @@ | ||||||
import { Signer } from '@ucanto/principal/ed25519' | ||||||
import { Logging } from '@web3-storage/worker-utils/logging' | ||||||
import Toucan from 'toucan-js' | ||||||
import pkg from '../../package.json' | ||||||
import { loadConfig } from '../config.js' | ||||||
import { configureSigner, loadConfig } from '../config.js' | ||||||
import { Spaces } from '../kvs/spaces.js' | ||||||
import { Validations } from '../kvs/validations.js' | ||||||
import { Email } from './email.js' | ||||||
|
@@ -42,12 +41,12 @@ export function getContext(request, env, ctx) { | |||||
env: config.ENV, | ||||||
}) | ||||||
|
||||||
const keypair = Signer.parse(config.PRIVATE_KEY) | ||||||
const signer = configureSigner(config) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you do this inside the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 b9533c9 |
||||||
const url = new URL(request.url) | ||||||
const db = new D1QB(config.DB) | ||||||
return { | ||||||
log, | ||||||
signer: keypair, | ||||||
signer, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
can't we just do this ? any error will be caught at dev time or CI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want the advertised DID to come from env variable. A '.web3.storage' domain won't be the best choice 100% of the times, e.g. on localhost or testing various scenarios. env variable gives me flexibility to get something working. once something works/tests end to end, can narrow down the configurability. |
||||||
config, | ||||||
url, | ||||||
kvs: { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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: testKeypair.public.did, | ||
} | ||
const signer = configModule.configureSigner(config) | ||
assert.ok(signer) | ||
assert.equal(signer.did().toString(), config.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) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,31 @@ describe('ucan', function () { | |
t.deepEqual(rsp, ['test pass']) | ||
}) | ||
|
||
test('should support ucan invoking to a did:web aud', async function () { | ||
const serviceDidWeb = 'did:web:web3.storage' | ||
const { mf, issuer, service } = await context({ | ||
environment: { | ||
...process.env, | ||
PRIVATE_KEY: | ||
'MgCYWjE6vp0cn3amPan2xPO+f6EZ3I+KwuN1w2vx57vpJ9O0Bn4ci4jn8itwc121ujm7lDHkCW24LuKfZwIdmsifVysY=', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you need a new key here ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't. Good catch. 🙏 d204331 |
||
DID: serviceDidWeb, | ||
}, | ||
}) | ||
const ucan = await UCAN.issue({ | ||
issuer, | ||
audience: service.withDID('did:web:web3.storage'), | ||
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 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What’s the point of catching here to then throw ? You can simply just do .from instead of whole isDID thing.
Alternatively there’s also .is method which you could instead which will return boolean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 5184a55