Repository navigation
feat: Add OAuth 2.0 support with PKCE flow - #6940
Kota-Jagadeesh wants to merge 32 commits into
Conversation
|
I'm getting this error page on this branch: screen-20260810-020943.mp4 |
|
ahh, @RitikaPahwa4444 that is expected. as the OAuth 2.0 consumer keys aren't committed here(git) , build.gradle.kts falls back to empty strings if they aren't present in |
I'll mail you and @nicolas-raoul my developer client keys in some time, so you can use them in |
chrisdebian
left a comment
There was a problem hiding this comment.
Thanks for building this out — the PKCE implementation itself is correct: SecureRandom with 32 bytes for the verifier, SHA-256 + base64url for the challenge, S256 as the method, all matching RFC 7636. Found three real issues around it though, all in how the tokens it protects get handled afterward.
1. Shipping a client_secret alongside PKCE defeats the reason to use PKCE at all. grantOAuthToken() sends client_secret = BuildConfig.OAUTH_CLIENT_SECRET, compiled in from local.properties. PKCE exists specifically so a public client — anything that can't keep a secret, which by definition includes a distributed Android APK — doesn't need one (RFC 8252, "OAuth 2.0 for Native Apps," is explicit that native apps must be treated as public clients and must not use client secrets). Whatever value ends up in OAUTH_CLIENT_SECRET is trivially recoverable from any built APK via jadx/dex2jar, so it provides no actual protection — it's PKCE doing the real work here regardless of whether the secret is present. Suggest dropping client_secret entirely and registering the app's OAuth consumer as a public client, matching what PKCE is for.
2. refresh_token is stored more weakly than access_token, and more weakly than this codebase's own existing convention. SessionManager.updateOAuthAccount() puts the access token in accountManager.setAuthToken() (the auth-token slot, correctly access-controlled) but the refresh token in accountManager.setUserData() — the general-purpose metadata bag, not designed for secrets. The refresh token is the longer-lived, more valuable of the two credentials (usable to mint fresh access tokens until revoked), so if anything it deserves more protection, not less. Worth noting the existing password-based login path already treats its one secret correctly, via accountManager.setPassword()/getPassword() — the strongest single-value slot AccountManager offers. The new OAuth path doesn't extend that same care to the refresh token.
3. The bearer token gets attached to every outgoing request on the shared OkHttpClient, not just requests to the host that issued it. The new interceptor in NetworkingModule.provideOkHttpClient() adds Authorization: Bearer <accessToken> unconditionally, with no check on chain.request().url.host. That OkHttpClient singleton is the same instance wired into provideOkHttpJsonApiClient() a few lines below, which serves tools_forge (Toolforge), WIKIDATA_SPARQL_QUERY_URL, and BuildConfig.WIKIMEDIA_CAMPAIGNS_URL — three genuinely different hosts, none of which are the OAuth resource server. As written, a logged-in user's access token would be sent as a header to all of them. Suggest scoping the interceptor to the specific host(s) the token is actually valid for before this merges.
Smaller note, not blocking: the authorization request in startOAuthLogin() has no state parameter. PKCE already covers the main authorization-code-injection threat this would traditionally guard against, so it's a defence-in-depth gap rather than an open hole, but worth adding for completeness per the usual OAuth 2.0 guidance (RFC 6749 §10.12).
Happy to go into more detail on any of these.
|
@nicolas-raoul @RitikaPahwa4444 Any good news on OAuth? Do we have plans to implement this for next release? |
|
Yes, login needs to be fixed on priority: #6906. I couldn't get a chance to test it again but I'll try again over this weekend. |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Credential exposure, incomplete API authentication, and OAuth lifecycle and recovery defects block safe use.
Review effort: Balanced
Findings: 3
Open (9)
Android APK exposes embedded OAuth client secret · New Retrofit requests lack scoped OAuth authentication · New Bearer token leaks to unrelated external services · New OAuth callback uses lost verifier and wrong restored login flow · New Profile errors incorrectly mark fabricated OAuth user as logged in · New OAuth production and beta endpoints are mismatched · New Cached OAuth tokens never refresh or expire safely · New Pending profile callback can crash destroyed LoginActivity · New OAuth button missing from landscape and extra-large layouts · New
What changed in this PR
Adds browser-based OAuth 2.0 login with PKCE alongside the existing Commons login flow, addressing #6885.
Changes:
- Adds OAuth login controls, browser authorization, and callback handling.
- Introduces token exchange, profile fetching, and account token storage.
- Adds bearer-token attachment and OAuth build configuration.
| File | Description |
|---|---|
| app/src/main/res/values/strings.xml | Adds OAuth login label. |
| app/src/main/res/layout/activity_login.xml | Adds OAuth login button. |
| app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.kt | Adds bearer-token interceptor. |
| app/src/main/java/fr/free/nrw/commons/auth/SessionManager.kt | Stores OAuth account credentials. |
| app/src/main/java/fr/free/nrw/commons/auth/LoginActivity.kt | Implements browser authorization and callbacks. |
| app/src/main/java/fr/free/nrw/commons/auth/login/OAuthTokenResponse.kt | Models token responses. |
| app/src/main/java/fr/free/nrw/commons/auth/login/OAuthProfileResponse.kt | Models profile responses. |
| app/src/main/java/fr/free/nrw/commons/auth/login/LoginInterface.kt | Defines OAuth endpoints. |
| app/src/main/java/fr/free/nrw/commons/auth/login/LoginClient.kt | Adds token exchange and profile requests. |
| app/src/main/AndroidManifest.xml | Registers OAuth callback deep link. |
| app/build.gradle.kts | Loads OAuth client configuration. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
with the issue #6906 being ressolved, i will look into the copilot review comments and get the possible fix for the relavant ones |
|
I've fixed the important issues and am now waiting for the client key to be approved by the steward. Once it's approved, I'll test the changes and push them |
|
Tested the flow and everything works fine :) Willl update the PR shortly |
|
Would you mind checking the "Android CI / Run tests and generate APK" failures? Thanks! |
|
Specifically: |
…or in NetworkingModule
@nicolas-raoul i ran the unit tests locally and they pass in my machinee, looking at the above stack trace ig roboelectric hit a transient network hiccup while fetching the sdk jar from maveen. could you re run the failed ci job? |
|
@nicolas-raoul This is ready to test :) |
|
Thanks @Kota-Jagadeesh, CI is green now!
Thanks! |
…on below buttonFrame
|
@nicolas-raoul thanks for the feedback! all four issues are addressed, landscape layout now keeps login_button alongside oauth_login_button below buttonFrame, unused refresh token storage is removed, OAuth error strings are moved to strings.xml + values-qq, and OAuthTokenInterceptor is deduplicated with unit tests added :) |
nicolas-raoul
left a comment
There was a problem hiding this comment.
Thanks for the quick update @Kota-Jagadeesh! Three points from reading the updated diff:
-
Duplicate
OAuthTokenInterceptorstill present inOkHttpConnectionFactory.kt:
app/src/main/java/fr/free/nrw/commons/auth/OAuthTokenInterceptor.ktwas added, but a secondclass OAuthTokenInterceptor : Interceptorwas also left insideOkHttpConnectionFactory.kt(package fr.free.nrw.commons, lines 69–136), andOkHttpConnectionFactoryinstantiates that local copy instead offr.free.nrw.commons.auth.OAuthTokenInterceptor. Please delete the duplicate class inOkHttpConnectionFactory.ktand importfr.free.nrw.commons.auth.OAuthTokenInterceptor. -
oauth_login_buttonis visible even whenOAUTH_CLIENT_IDis not configured:
getOAuthClientId()defaults to"YOUR_OAUTH_CLIENT_ID"whenlocal.propertieshas noOAUTH_CLIENT_ID(for example in local contributor builds, CI APKs, or F-Droid builds). Tapping Log in with OAuth in that state opens Meta-Wiki withclient_id=YOUR_OAUTH_CLIENT_ID, which fails immediately. CouldLoginActivityhideoauthLoginButtonwheneverBuildConfig.OAUTH_CLIENT_ID.isBlank() || BuildConfig.OAUTH_CLIENT_ID == "YOUR_OAUTH_CLIENT_ID"? -
Access token expiry (4 hours on Wikimedia OAuth 2.0):
Unless an OAuth 2.0 client is owner-only, Wikimedia OAuth 2.0 access tokens issued via the authorization-code/PKCE flow expire after 4 hours. Without refreshing viarefresh_token, won't uploads and edits start failing with HTTP 401 four hours after logging in? How did this behave in your testing?
in my testing, login and upload, edit worked fine within the 4 hour window, so i hadn't hit the 401 yet. and without so, would you prefer that i add the |
|
✅ Generated APK variants! |
|
same pr is best, thanks! :-) |


Description (required)
Fixes #6885
fr.free.nrw.commons://oauth-callback) inAndroidManifest.xmlto intercept post-auth redirects./w/rest.php/oauth2/access_token) and profile fetching (/w/rest.php/oauth2/resource/profile) inLoginInterface, securely persistingaccess_tokenandrefresh_tokeninSessionManagervia Android theaccountmanager.NetworkingModuleto automatically attachAuthorization: Bearer <access_token>headers to the outgoing API requests.activity_login.xmlTests performed (required)
Tested
ProdDebugonRedmi Note 13 PROwith API level36. Verified web authentication flow, deep-link callback execution, token persistence, and successful account contributions fetching.Screenshots (for UI changes only)
Screenrecorder-2026-08-09-21-31-53-10.mp4