Repository navigation
Add authentication and personal access tokens to API v3 - #1307
Conversation
atiannicelli
left a comment
There was a problem hiding this comment.
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 | |||
| 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); |
There was a problem hiding this comment.
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.
| if credential.stale { | ||
| // Update the credential's last_used_at time in the background | ||
| let db = state.db.clone(); | ||
| tokio::spawn(async move { |
There was a problem hiding this comment.
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.
| pub const COOKIE: &str = "PLAY_SESSION"; | ||
|
|
||
| // matches v2's maproulette.session.timeout | ||
| const LIFETIME_MS: i64 = 30 * 24 * 60 * 60 * 1000; |
There was a problem hiding this comment.
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.
| // `Patch` has no users until the challenge endpoints land. | ||
| #[cfg_attr(not(test), expect(dead_code))] |
There was a problem hiding this comment.
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.
|
|
||
| [dev-dependencies] | ||
| http-body-util = "0.1.5" | ||
| reqwest = { version = "0.13.5", default-features = false, features = ["json"] } |
There was a problem hiding this comment.
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.
cf9c0a5 to
a3b4267
Compare
|




Builds on top of #1306.
This adds support for session cookies created by API v2 (and adds a
GET /meendpoint 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.