notion-sdk-py icon indicating copy to clipboard operation
notion-sdk-py copied to clipboard

Add OAuth revoke and introspect endpoints

Open ramnes opened this issue 8 months ago • 13 comments

OAuth revoke and introspect endpoints got added to the JS SDK: https://github.com/makenotion/notion-sdk-js/pull/552

Let's add them here as well. :)

ramnes avatar Mar 14 '25 00:03 ramnes

I'd love to work on this! Are there any additional requirements I should be aware of?

vincedbowen avatar Mar 14 '25 16:03 vincedbowen

Go ahead!

The only requirements are that the DX/library API be identical to the JS SDK and that our Python code remain consistent with what we already have. :)

ramnes avatar Mar 14 '25 21:03 ramnes

Great, thanks :-) It looks like the tests I would write on this would be dependent on the access token's being generated. Is there anyway to generate these without a public integration?

vincedbowen avatar Mar 14 '25 21:03 vincedbowen

I don't think so, but we only need to burn one token to register the VCR.py cassette. Subsequent tests won't revoke the token as long as this cassette is being used, meaning that we only need to generate a token manually when we update this cassette in particular.

ramnes avatar Mar 15 '25 00:03 ramnes

Sorry if I am not understanding 😅 ! From my understanding of the Read me "To create new tests or run them without cassettes, you need to set up the environment variables NOTION_TOKEN and NOTION_TEST_PAGE_ID", this means I would have to generate my own token to develop any additional tests. The pytest-vcr docs are a bit minimal, so I am not totally sure how I would add new tests in the cassette.

vincedbowen avatar Mar 15 '25 00:03 vincedbowen

Yes, you need to create an integration so that you can generate a new token manually and burn it while generating the cassette for this test.

ramnes avatar Mar 15 '25 00:03 ramnes

Perfect! Thanks for the explanation

vincedbowen avatar Mar 15 '25 00:03 vincedbowen

I have the endpoints working with tests for each endpoint, using the method of burning a token to generate the cassette for the test. I did notice that in the client.py, the authorization is in the form Bearer {auth}, but from the docs, the authorization looks to be Basic '"$BASE64_ENCODED_ID_AND_SECRET"'. Will this be a problem for users?

I can push any changes for now if that would help :-)

vincedbowen avatar Mar 15 '25 22:03 vincedbowen

Oh, good catch! It looks like we missed this commit from two years ago: https://github.com/makenotion/notion-sdk-js/commit/0877d3478c680bf6b5860cfc21e7ab012084aad4

We should implement this "Get token" endpoint as well, and add the same changes for authorization, i.e. allow either the token or the client id and secret combo, which should be only usable for the OAuth-related endpoints, as in the JS SDK.

We can handle this in a separate issue and PR, or address everything at once, whichever you prefer.

ramnes avatar Mar 16 '25 16:03 ramnes

Also, if we implement the client id and secret authorization as well as the "Get token" endpoint, we should update the tests so that they run entirely with these environment variables and create their own token, rather than requiring the user to generate a token manually that will end up being revoked. :)

ramnes avatar Mar 16 '25 16:03 ramnes

We can handle that all here if that works for you!

I think we still will have to burn a token because I had to intercept the code from the 2nd step after visiting the authorization URL for my public integration. Ie, after visiting that auth URL, and being redirected to the redirect URI, I had to grab the code from here http://localhost:3000/callback?code=<GENERATED_CODE>&state=. I could then use that code to generate a token via a Curl or Postman request, then introspect and revoke in the SDK. I think if we implement the create token endpoint from the docs, a public integration would have to be set up and a code would have to be generated. If I am misunderstanding this, or you have a more streamlined approach, please let me know :-)!

Also from my testing, it looks like a the token can be "revoked" multiple times. Even though it isn't active after the first revocation, the API still returns a 200 response for revoking that token.

vincedbowen avatar Mar 16 '25 18:03 vincedbowen

Alright, let's keep it simple for now and say that we need both the token and the id / secret pair to run the tests. I'll see if we can work around this later. :)

ramnes avatar Mar 17 '25 13:03 ramnes

Sorry for the long hiatus, I have been incredibly busy with school. I can finish this in the coming weeks, or someone can help out if they so desire :-)! Thanks!

vincedbowen avatar Apr 18 '25 15:04 vincedbowen

Hi @ramnes! I'd like to work on this since PR #261 was closed.

I've been looking through the JS SDK and the discussion on the previous PR. From what I can see, this needs three OAuth endpoints: token for exchanging authorization codes, introspect for checking token status, and revoke for revoking tokens.They should use Basic auth with the client_id and client_secret base64 encoded.

I also saw your comment on PR #595 about adding refresh_token to the token endpoint, so I'll make sure that's included along with the other parameters.

For the tests, I noticed you suggested using pytest-mock instead of VCR cassettes since the OAuth flow is pretty cumbersome with the browser and manual code grabbing. Should I go with mocked tests for all three OAuth endpoints?

Let me know if this approach sounds good and I'll get started on it!

Sumeet213 avatar Nov 02 '25 07:11 Sumeet213

Hi @ramnes! I'd like to work on this since PR #261 was closed.

I've been looking through the JS SDK and the discussion on the previous PR. From what I can see, this needs three OAuth endpoints: token for exchanging authorization codes, introspect for checking token status, and revoke for revoking tokens.They should use Basic auth with the client_id and client_secret base64 encoded.

I also saw your comment on PR #595 about adding refresh_token to the token endpoint, so I'll make sure that's included along with the other parameters.

For the tests, I noticed you suggested using pytest-mock instead of VCR cassettes since the OAuth flow is pretty cumbersome with the browser and manual code grabbing. Should I go with mocked tests for all three OAuth endpoints?

Let me know if this approach sounds good and I'll get started on it!

Hi @Sumeet213 , I implemented the authorization and revoke, but never had the chance to do any of the future work because of school obligations. If you could build off the endpoints I already made that would be awesome. If @ramnes doesn't want this, or you would rather start from scratch, no worries :-)

vincedbowen avatar Nov 02 '25 07:11 vincedbowen

Hey @Sumeet213, awesome!

For the tests, I noticed you suggested using pytest-mock instead of VCR cassettes since the OAuth flow is pretty cumbersome with the browser and manual code grabbing. Should I go with mocked tests for all three OAuth endpoints?

I think so! Unless Notion made it a easier to work with this without a browser in the meantime. But just to be clear, I really want the as little mocks as possible; we should still test as much as possible against the real system with cassettes.

ramnes avatar Nov 03 '25 08:11 ramnes

Implemented in #301. Thanks everyone!

ramnes avatar Nov 14 '25 11:11 ramnes