Skip to content

Commit

Permalink
fix: make login idempotent (#1149)
Browse files Browse the repository at this point in the history
Logging with the same account several times creates and stores redundant
proofs and then sends them over during invocations.

Changes here avoid this redundancy by returning an account using
existing proofs if you already happen to have them, essentially making
login idempotent.
  • Loading branch information
Gozala authored Nov 16, 2023
1 parent d1a9c78 commit c1df8d8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
26 changes: 26 additions & 0 deletions packages/w3up-client/src/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ export const list = ({ agent }, { account } = {}) => {
*/
export const login = async ({ agent }, email) => {
const account = fromEmail(email)

// If we already have a session for this account we
// skip the authentication process, otherwise we will
// end up adding more UCAN proofs and attestations to
// the store which we then will be sending when using
// this account.
// Note: This is not a robust solution as there may be
// reasons to re-authenticate e.g. previous session is
// no longer valid because it was revoked. But dropping
// revoked UCANs from store is something we should do
// anyway.
const session = list({ agent }, { account })[account]
if (session) {
return { ok: session }
}

const result = await Access.request(
{ agent },
{
Expand Down Expand Up @@ -140,6 +156,16 @@ export class Account {
this.proofs.push(proof)
}

toJSON() {
return {
id: this.did(),
proofs: this.proofs
// we sort proofs to get a deterministic JSON representation.
.sort((a, b) => a.cid.toString().localeCompare(b.cid.toString()))
.map((proof) => proof.toJSON()),
}
}

/**
* Provisions given `space` with this account.
*
Expand Down
29 changes: 27 additions & 2 deletions packages/w3up-client/test/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,33 @@ export const testAccount = {
assert.ok(two[Account.fromEmail(bobEmail)].toEmail(), bobEmail)
},

'login idempotence': async (assert, { client, mail, grantAccess }) => {
const email = 'alice@web.mail'
const login = client.login(email)
await grantAccess(await mail.take())
const alice = await login

assert.deepEqual(
Object.keys(client.accounts()),
[alice.did()],
'no accounts have been saved'
)

const retry = await client.login(email)
assert.deepEqual(
alice.toJSON(),
retry.toJSON(),
'same account view is returned'
)

const loginResult = await Account.login(client, email)
assert.deepEqual(
alice.toJSON(),
loginResult.ok?.toJSON(),
'same account is returned with low level API'
)
},

'client.login': async (assert, { client, mail, grantAccess }) => {
const account = client.login('alice@web.mail')

Expand Down Expand Up @@ -217,8 +244,6 @@ export const testAccount = {
const space = await client.createSpace('test')
assert.deepEqual(client.spaces(), [])

console.log(space)

const result = await space.save()
assert.ok(result.ok)

Expand Down

0 comments on commit c1df8d8

Please sign in to comment.