Skip to content

Commit

Permalink
Add did doc output to getSession for session resumption (#1806)
Browse files Browse the repository at this point in the history
* add optional did doc output to getSession lexicon

* add did doc output to getSession on pds, update client to use it

* api test fixes

* api changeset

* tidy
  • Loading branch information
devinivy authored Nov 2, 2023
1 parent 3598898 commit 772736a
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-apples-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@atproto/api': patch
---

respect pds endpoint during session resumption
3 changes: 2 additions & 1 deletion lexicons/com/atproto/server/getSession.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"handle": { "type": "string", "format": "handle" },
"did": { "type": "string", "format": "did" },
"email": { "type": "string" },
"emailConfirmed": { "type": "boolean" }
"emailConfirmed": { "type": "boolean" },
"didDoc": { "type": "unknown" }
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export class AtpAgent {
this.session.email = res.data.email
this.session.handle = res.data.handle
this.session.emailConfirmed = res.data.emailConfirmed
this._updateApiEndpoint(res.data.didDoc)
return res
} catch (e) {
this.session = undefined
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/client/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,9 @@ export const schemaDict = {
emailConfirmed: {
type: 'boolean',
},
didDoc: {
type: 'unknown',
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface OutputSchema {
did: string
email?: string
emailConfirmed?: boolean
didDoc?: {}
[k: string]: unknown
}

Expand Down
1 change: 0 additions & 1 deletion packages/api/src/moderation/accumulator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AppBskyGraphDefs } from '../client/index'
import { AtUri } from '@atproto/syntax'
import {
Label,
LabelPreference,
Expand Down
17 changes: 14 additions & 3 deletions packages/api/tests/agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import assert from 'assert'
import { defaultFetchHandler } from '@atproto/xrpc'
import {
AtpAgent,
Expand All @@ -6,6 +7,7 @@ import {
AtpSessionData,
} from '..'
import { TestNetworkNoAppView } from '@atproto/dev-env'
import { getPdsEndpoint, isValidDidDoc } from '@atproto/common-web'

describe('agent', () => {
let network: TestNetworkNoAppView
Expand Down Expand Up @@ -46,16 +48,19 @@ describe('agent', () => {
expect(agent.session?.did).toEqual(res.data.did)
expect(agent.session?.email).toEqual('[email protected]')
expect(agent.session?.emailConfirmed).toEqual(false)
assert(isValidDidDoc(res.data.didDoc))
expect(agent.api.xrpc.uri.origin).toEqual(getPdsEndpoint(res.data.didDoc))

const { data: sessionInfo } = await agent.api.com.atproto.server.getSession(
{},
)
expect(sessionInfo).toEqual({
expect(sessionInfo).toMatchObject({
did: res.data.did,
handle: res.data.handle,
email: '[email protected]',
emailConfirmed: false,
})
expect(isValidDidDoc(sessionInfo.didDoc)).toBe(true)

expect(events.length).toEqual(1)
expect(events[0]).toEqual('create')
Expand Down Expand Up @@ -93,15 +98,18 @@ describe('agent', () => {
expect(agent2.session?.did).toEqual(res1.data.did)
expect(agent2.session?.email).toEqual('[email protected]')
expect(agent2.session?.emailConfirmed).toEqual(false)
assert(isValidDidDoc(res1.data.didDoc))
expect(agent2.api.xrpc.uri.origin).toEqual(getPdsEndpoint(res1.data.didDoc))

const { data: sessionInfo } =
await agent2.api.com.atproto.server.getSession({})
expect(sessionInfo).toEqual({
expect(sessionInfo).toMatchObject({
did: res1.data.did,
handle: res1.data.handle,
email,
emailConfirmed: false,
})
expect(isValidDidDoc(sessionInfo.didDoc)).toBe(true)

expect(events.length).toEqual(2)
expect(events[0]).toEqual('create')
Expand Down Expand Up @@ -136,15 +144,18 @@ describe('agent', () => {
expect(agent2.hasSession).toEqual(true)
expect(agent2.session?.handle).toEqual(res1.data.handle)
expect(agent2.session?.did).toEqual(res1.data.did)
assert(isValidDidDoc(res1.data.didDoc))
expect(agent2.api.xrpc.uri.origin).toEqual(getPdsEndpoint(res1.data.didDoc))

const { data: sessionInfo } =
await agent2.api.com.atproto.server.getSession({})
expect(sessionInfo).toEqual({
expect(sessionInfo).toMatchObject({
did: res1.data.did,
handle: res1.data.handle,
email: res1.data.email,
emailConfirmed: false,
})
expect(isValidDidDoc(sessionInfo.didDoc)).toBe(true)

expect(events.length).toEqual(2)
expect(events[0]).toEqual('create')
Expand Down
3 changes: 3 additions & 0 deletions packages/bsky/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,9 @@ export const schemaDict = {
emailConfirmed: {
type: 'boolean',
},
didDoc: {
type: 'unknown',
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface OutputSchema {
did: string
email?: string
emailConfirmed?: boolean
didDoc?: {}
[k: string]: unknown
}

Expand Down
7 changes: 6 additions & 1 deletion packages/pds/src/api/com/atproto/server/getSession.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { InvalidRequestError } from '@atproto/xrpc-server'
import AppContext from '../../../../context'
import { Server } from '../../../../lexicon'
import { didDocForSession } from './util'

export default function (server: Server, ctx: AppContext) {
server.com.atproto.server.getSession({
auth: ctx.authVerifier.access,
handler: async ({ auth }) => {
const did = auth.credentials.did
const user = await ctx.services.account(ctx.db).getAccount(did)
const [user, didDoc] = await Promise.all([
ctx.services.account(ctx.db).getAccount(did),
didDocForSession(ctx, did),
])
if (!user) {
throw new InvalidRequestError(
`Could not find user info for account: ${did}`,
Expand All @@ -18,6 +22,7 @@ export default function (server: Server, ctx: AppContext) {
body: {
handle: user.handle,
did: user.did,
didDoc,
email: user.email,
emailConfirmed: !!user.emailConfirmedAt,
},
Expand Down
3 changes: 3 additions & 0 deletions packages/pds/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,9 @@ export const schemaDict = {
emailConfirmed: {
type: 'boolean',
},
didDoc: {
type: 'unknown',
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface OutputSchema {
did: string
email?: string
emailConfirmed?: boolean
didDoc?: {}
[k: string]: unknown
}

Expand Down

0 comments on commit 772736a

Please sign in to comment.