-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
298 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import AtpAgent from '@atproto/api' | ||
import { SeedClient, TestNetwork } from '@atproto/dev-env' | ||
import usersSeed from './seeds/users' | ||
import { createServiceJwt } from '@atproto/xrpc-server' | ||
import { Keypair, Secp256k1Keypair } from '@atproto/crypto' | ||
|
||
describe('auth', () => { | ||
let network: TestNetwork | ||
let agent: AtpAgent | ||
let sc: SeedClient | ||
|
||
beforeAll(async () => { | ||
network = await TestNetwork.create({ | ||
dbPostgresSchema: 'bsky_auth', | ||
}) | ||
agent = network.bsky.getClient() | ||
sc = network.getSeedClient() | ||
await usersSeed(sc) | ||
await network.processAll() | ||
}) | ||
|
||
afterAll(async () => { | ||
await network.close() | ||
}) | ||
|
||
it('handles signing key change for service auth.', async () => { | ||
const issuer = sc.dids.alice | ||
const attemptWithKey = async (keypair: Keypair) => { | ||
const jwt = await createServiceJwt({ | ||
iss: issuer, | ||
aud: network.bsky.ctx.cfg.serverDid, | ||
keypair, | ||
}) | ||
return agent.api.app.bsky.actor.getProfile( | ||
{ actor: sc.dids.carol }, | ||
{ headers: { authorization: `Bearer ${jwt}` } }, | ||
) | ||
} | ||
const origSigningKey = await network.pds.ctx.actorStore.keypair(issuer) | ||
const newSigningKey = await Secp256k1Keypair.create({ exportable: true }) | ||
// confirm original signing key works | ||
await expect(attemptWithKey(origSigningKey)).resolves.toBeDefined() | ||
// confirm next signing key doesn't work yet | ||
await expect(attemptWithKey(newSigningKey)).rejects.toThrow( | ||
'jwt signature does not match jwt issuer', | ||
) | ||
// update to new signing key | ||
await network.plc | ||
.getClient() | ||
.updateAtprotoKey( | ||
issuer, | ||
network.pds.ctx.plcRotationKey, | ||
newSigningKey.did(), | ||
) | ||
// old signing key still works due to did doc cache | ||
await expect(attemptWithKey(origSigningKey)).resolves.toBeDefined() | ||
// new signing key works | ||
await expect(attemptWithKey(newSigningKey)).resolves.toBeDefined() | ||
// old signing key no longer works after cache is updated | ||
await expect(attemptWithKey(origSigningKey)).rejects.toThrow( | ||
'jwt signature does not match jwt issuer', | ||
) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,26 +1,29 @@ | ||
import * as uint8arrays from 'uint8arrays' | ||
import { parseDidKey } from './did' | ||
import plugins from './plugins' | ||
import { VerifyOptions } from './types' | ||
|
||
export const verifySignature = ( | ||
didKey: string, | ||
data: Uint8Array, | ||
sig: Uint8Array, | ||
opts?: VerifyOptions, | ||
): Promise<boolean> => { | ||
const parsed = parseDidKey(didKey) | ||
const plugin = plugins.find((p) => p.jwtAlg === parsed.jwtAlg) | ||
if (!plugin) { | ||
throw new Error(`Unsupported signature alg: :${parsed.jwtAlg}`) | ||
throw new Error(`Unsupported signature alg: ${parsed.jwtAlg}`) | ||
} | ||
return plugin.verifySignature(didKey, data, sig) | ||
return plugin.verifySignature(didKey, data, sig, opts) | ||
} | ||
|
||
export const verifySignatureUtf8 = async ( | ||
didKey: string, | ||
data: string, | ||
sig: string, | ||
opts?: VerifyOptions, | ||
): Promise<boolean> => { | ||
const dataBytes = uint8arrays.fromString(data, 'utf8') | ||
const sigBytes = uint8arrays.fromString(sig, 'base64url') | ||
return verifySignature(didKey, dataBytes, sigBytes) | ||
return verifySignature(didKey, dataBytes, sigBytes, opts) | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.