-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Authenticated Fetch #45
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
Conversation
I think there's a lot of value in presenting developers with a familiar API so a nice one would make this all transparent. Either something like configuring the (optional) remote peer verification ahead of time: import { authenticatedFetch } from '@libp2p/authenticated-fetch'
const fetch = authenticatedFetch(privateKey, {
verifyPeer: async (peerId: PeerId, request: Request, options: AbortOptions) => {
return true
}
})
// just like regular fetch
const result = await fetch('http://example.com', {
method: 'POST',
body: 'hello',
signal: AbortSignal.timeout(5000)
})or possibly more flexibly by extending the import { authenticatedFetch } from '@libp2p/authenticated-fetch'
const fetch = authenticatedFetch(privateKey)
// almost just like regular fetch
const result = await fetch('http://example.com', {
method: 'POST',
body: 'hello',
signal: AbortSignal.timeout(5000),
verifyPeer: async (peerId: PeerId, options: AbortOptions) => {
return true
}
})We could also do both - accept an option to the I've put the above as One other thing is that if the If the body is a
|
Agreed
I'm a bit torn here. It does increase friction, but isn't this something that we want the user to think about? Even if they think "I don't care", that's probably better than "wasn't I suppose to check the id somewhere?" Of the two APIs, I prefer the second as it doesn't involve creating a new
I think this would only happen once: in the case where our bearer token is expired or rejected by the server. (well twice in the case of redirects, but that should hopefully be caught in the first handshake message). Remember that we don't send the request body on the first (unauthenticated) handshake message. We don't have a standard way to ask the user to give us a body factory for us to retry the request (Go's
Of the options 3 feels annoying, but might be the simplest option for now. [1]: Went for a little detour as I wrote this:
|
a6ab735 to
76761c1
Compare
achingbrain
left a comment
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.
LGTM, just a few last bits to tidy up.
src/auth/client.ts
Outdated
| } | ||
|
|
||
| export interface AuthenticateServerOptions extends AbortOptions { | ||
| export interface AuthenticateServerOptions { |
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 thinking behind making the VerifyPeerOptions a single-member interface?
Certainly the generated docs are easier to read if the options types accepted by methods are not unions, which is the approach taken elsewhere in the JS stack.
This also avoids a breaking change here due to the removal of extends AbortOptions which means any signal prop passed as a member of an object of this type would fail to compile.
| export interface AuthenticateServerOptions { | |
| export interface AuthenticateServerOptions extends RequestInit { | |
| /** | |
| * Accepts the PeerId of the server, return true if it was the expected | |
| * peer or false if not. | |
| */ | |
| verifyPeer?(peerId: PeerId, options: AbortOptions): boolean | Promise<boolean> | |
This suggestion implies also removing the VerifyPeerOptions interface and removing the AbortOptions union from the authenticateServer method definition.
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.
No Opinion here. I'll defer to your opinion on what is more ergonomic to use.
## [2.1.0](v2.0.2...v2.1.0) (2024-11-01) ### Features * Authenticated Fetch ([#45](#45)) ([ca69dc4](ca69dc4))
|
🎉 This PR is included in version 2.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Title
Description
Adds the ability to send application request earlier in the handshake, after authenticating the server (but before the server has authenticated us).
Notes & open questions
checkID? I iterated a bit here, and found this API would avoid some duplication and traps in other approaches. But I'm open to a different api.Change checklist