Skip to content
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

PKCE support for SSO #5189

Merged
merged 3 commits into from
Dec 2, 2024
Merged

PKCE support for SSO #5189

merged 3 commits into from
Dec 2, 2024

Conversation

avdb13
Copy link
Contributor

@avdb13 avdb13 commented Nov 12, 2024

Description

In continuation of #4881.

Implements PKCE support, in order to mitigate against the threat of authorization code interception attacks.

Background reading: https://www.oauth.com/oauth2-servers/pkce

@avdb13 avdb13 force-pushed the pkce branch 2 times, most recently from 5dda4a1 to 7fc1ead Compare November 12, 2024 08:15
@Nutomic
Copy link
Member

Nutomic commented Nov 12, 2024

@privacyguard Could you have a look at this?

@avdb13
Copy link
Contributor Author

avdb13 commented Nov 12, 2024

Frontend PR: LemmyNet/lemmy-ui#2806
Trying to figure out how to test it now, hints are appreciated.

@@ -57,6 +57,8 @@ pub struct OAuthProvider {
pub auto_verify_email: bool,
/// Allows linking an OAUTH account to an existing user account by matching emails
pub account_linking_enabled: bool,
/// switch to enable or disable PKCE
pub use_pkce: bool,
Copy link
Contributor

@privacyguard privacyguard Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use_pkce should also be added to the serialized fields in the PublicOAuthProvider. This will be required by the client to be able to check whether or not code_verifier should be generated for the provider in question at login time.

@privacyguard
Copy link
Contributor

@Nutomic overall the server changes to support PKCE are:
1- The stored provider with all the CRUD methods should have have a boolean: use_pkce
2- The PublicOAuthProvider should contain the field in order to tell the frontend that this provider requires the code_verifier to be generated.
2- The oauth authentication route should accept the code_verifier as a parameter when use_pkce is enabled, validate it, then pass it as a parameter to the issue token request.

We gave our feedback on the functionality aspect. We'll let the rust experts give their feedback on the code.

We also gave feedback on the frontend PR.

@privacyguard
Copy link
Contributor

privacyguard commented Nov 13, 2024

@avdb13 If you check the comments on the SSO PR, there are a couple of comments detailing the steps needed to test locally. If you're using Privacy Portal to test, don't forget to enable PKCE in the OAUTH app settings there too.

#4881 (comment)

@avdb13 avdb13 force-pushed the pkce branch 6 times, most recently from 9ac5b55 to 22f47aa Compare November 21, 2024 11:02
@@ -1162,6 +1162,19 @@ fn build_proxied_image_url(
))
}

pub fn check_code_verifier(code_verifier: &str) -> LemmyResult<&str> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this should probably be renamed to check_pkce_code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only used in a single file, so move it directly there and make it private.

if !response.status().is_success() {
Err(LemmyErrorType::OauthLoginFailed)?;
dbg!(response.bytes().await);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd probably be better to include this in the error message if its vital. Otherwise just remove this line. Ideally after you've tested this and it works, you can remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we've merged other DB changes since then, you'll probably need to rename this migration to the current date, or anything after the last one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never actually had problems with old dates on migrations so its fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One issue is that our check_diesel_migration woodpecker CI task can only check that diesel migration redo works for the newest DB migration.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah because there is already a newer migration on this same branch. Right it needs to be renamed then.

@avdb13 avdb13 changed the title PKCE support for SSO draft: PKCE support for SSO Nov 22, 2024
@avdb13 avdb13 changed the title draft: PKCE support for SSO PKCE support for SSO Nov 22, 2024
@avdb13 avdb13 marked this pull request as draft November 22, 2024 18:00
@avdb13
Copy link
Contributor Author

avdb13 commented Nov 22, 2024

Converting it back to draft for now, I tested it against gitlab but without success as their OIDC implementation seems to be too unreliable. Testing it with django-oauth-toolkit tonight.

@privacyguard
Copy link
Contributor

@avdb13 We tested the PRs locally, there are a couple of issues that need to be fixed:
1- The way code_challenge is being base64url encoded is incorrect. Check out the PKCE section of this tutorial it includes functions you can use to generate the code_verifier and code_challenge. The code_verifier can also be simplified to base64url encoding.
2- The schema.rs file seems to have been edited manually? When we run the migrations use_pkce should be the last property. Changing it back to the last property would require updating some structs.
3- Also, as mentioned by @dessalines, the migration file needs to be manually renamed to update the timestamp to something more recent such as 2024-11-12-090606_oauth_pkce

@avdb13 avdb13 force-pushed the pkce branch 3 times, most recently from 4ed32d7 to cc1a8fc Compare November 24, 2024 00:03
@avdb13
Copy link
Contributor Author

avdb13 commented Nov 24, 2024

Testing it seems to assert correctness yet I keep getting "Invalid login credentials" for some reason. This was with account linking disabled.

Correction: I was incorrectly base64 encoding by not removing all trailing equal signs on the front-end side.

@avdb13 avdb13 marked this pull request as ready for review November 24, 2024 09:00
@avdb13 avdb13 force-pushed the pkce branch 2 times, most recently from 9bfb4c5 to dc58c26 Compare November 24, 2024 10:03
@avdb13
Copy link
Contributor Author

avdb13 commented Nov 24, 2024

CI doesn't pass because of lemmy-js-client. If you generate the types again it should work.
Iforgot to run cargo-fmt but otherwise it's complete.

Thanks for the tutorial @privacyguard , I decided to use the same code with a few small changes.

@avdb13
Copy link
Contributor Author

avdb13 commented Nov 24, 2024

CI doesn't pass because of lemmy-js-client. If you generate the types again it should work.

Thanks for the tutorial @privacyguard , I decided to use the same code with a few small changes.

I have been thinking about opening another PR in order to support the nonce claim for OIDC providers, after this PR hopefully gets merged.

Copy link
Member

@dessalines dessalines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment above to address. Then I can regenerate lemmy-js-client and make a test version you can use to test for lemmy-ui.

@avdb13
Copy link
Contributor Author

avdb13 commented Nov 28, 2024

Not sure why federation tests fail. All issues have been addressed.

@dessalines
Copy link
Member

Its not you, its a different intermittent CI issue we're facing.

BTW in the future, plz use regular commits and merges rather than rebasing. We can't see the specific changes because you rewrote the history and force-pushed it.

@avdb13
Copy link
Contributor Author

avdb13 commented Nov 28, 2024

Its not you, its a different intermittent CI issue we're facing.

Demo to prove this is the case anyway: https://files.catbox.moe/pijbbh.mkv

BTW in the future, plz use regular commits and merges rather than rebasing. We can't see the specific changes because you rewrote the history and force-pushed it.

Apologies, I figured that the changes were limited in scope.

@avdb13
Copy link
Contributor Author

avdb13 commented Nov 29, 2024

Do PRs require approval of all reviewers?

@Nutomic
Copy link
Member

Nutomic commented Dec 2, 2024

Only one approval is required, but I will give others a chance to have a look as well.

@dessalines dessalines merged commit 9505d1d into LemmyNet:main Dec 2, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants