Skip to content

Add authentication and personal access tokens to API v3 - #1307

Merged
jake-low merged 5 commits into
jlow/api-v3-scaffoldfrom
jlow/api-v3-auth
Nov 19, 2026
Merged

jake-low merged 5 commits into
jlow/api-v3-scaffoldfrom
jlow/api-v3-auth

Conversation

@jake-low

@jake-low jake-low commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Builds on top of #1306.

This adds support for session cookies created by API v2 (and adds a GET /me endpoint which returns info about the user, which can be fetched using a said cookie).

It also adds CRUD endpoints for managing personal access tokens (PATs), and authentication code (not yet used) for checking token scopes.

@atiannicelli atiannicelli left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The auth logic checks out against v2:

Session cookies are verified the way SessionManager does it (HS256 over APPLICATION_SECRET, constant-time tokenHash comparison).
A cookie-authenticated request that changes something without an allowed Origin gets a 403 with a clear message (confirmed locally).
A PAT can't create or manage tokens (confirmed locally).
challenge_access follows v2's Permission.effectiveRole: superuser grant, direct challenge grants, teams, then the project.
The token format (prefix, CSPRNG body, CRC checksum) and storing only its SHA-256 look good.

Should fix

  • Commit .sqlx offline data. Without it the query! macros compile only with DATABASE_URL pointing at a live database that has v2's schema, which makes builds hard to reproduce and blocks CI. Run cargo sqlx prepare and commit api-v3/.sqlx/.
  • SonarCloud gives this PR a D reliability rating; worth checking what it flagged.

The rest are line comments.

@@ -0,0 +1,34 @@
# --- MapRoulette Scheme

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should fix: the evolution number needs coordinating. Open #1292 adds its own 130.sql, which already collides with main's 130.sql (from #1300). Whichever merges second will need renumbering, so it's worth agreeing an order with #1292 before either lands.

Comment thread api-v3/src/auth/mod.rs
req.extensions_mut().insert(Auth(principal));
let mut res = next.run(req).await;
if let Some(x_auth) = x_auth {
res.headers_mut().insert("x-auth", x_auth);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider: this tells every client how the request was authenticated, including the credential id (e.g. pat:3). It's already recorded on the tracing span (line 90), so the response header could go, or be limited to debug builds.

Comment thread api-v3/src/auth/mod.rs
if credential.stale {
// Update the credential's last_used_at time in the background
let db = state.db.clone();
tokio::spawn(async move {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor: this spawns a task on every request while the credential is stale, so a burst of requests on one token spawns one touch each until the first lands. An in-memory debounce or a small bounded worker would cap it.

Comment thread api-v3/src/auth/play.rs
pub const COOKIE: &str = "PLAY_SESSION";

// matches v2's maproulette.session.timeout
const LIFETIME_MS: i64 = 30 * 24 * 60 * 60 * 1000;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider: v2 reads this from config (maproulette.session.timeout, application.conf). 30 days is only the default, and -1 disables expiry. If an instance changes it, v2 and v3 will disagree about which sessions are valid. Reading it from an env var here too would keep them in step.

Comment thread api-v3/src/routes/mod.rs
Comment on lines +4 to +5
// `Patch` has no users until the challenge endpoints land.
#[cfg_attr(not(test), expect(dead_code))]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider: Patch has no users in #1310 either, so it's dead code across the whole stack. I'd drop it until an endpoint needs it.

Comment thread api-v3/Cargo.toml

[dev-dependencies]
http-body-util = "0.1.5"
reqwest = { version = "0.13.5", default-features = false, features = ["json"] }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider: reqwest isn't used by any test in the stack (#1310 also adds flate2 and reqwest's stream feature, both unused). Drop them until there are integration tests that need them.

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
D Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@jake-low
jake-low merged commit 049ffd7 into main Nov 19, 2026
4 of 5 checks passed
@jake-low
jake-low deleted the jlow/api-v3-auth branch November 19, 2026 06:22
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.

2 participants