Repository navigation
feat(rest-api-application): migrate auth_key context to contract-first architecture - #1950
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements SI-3 of the contract-first REST API migration by moving the auth_key context to the protocol → application → runtime-adapter → axum transport layering, and rewiring the Axum handlers to dispatch via a new application use-case service instead of calling tracker-core directly.
Changes:
- Added
auth_keyprotocol DTOs (resources + input forms) underrest-api-protocol, and moved usage away from Axum-local DTO modules. - Introduced
AuthKeyPort(application boundary) andAuthKeyApiService(use-case) inrest-api-application, plusTrackerAuthKeyAdapterinrest-api-runtime-adapter. - Updated
axum-rest-api-serverrouting/handlers/tests to use protocol DTOs and the new application service, removing the oldauth_keylocal DTO module.
Reviewed changes
Copilot reviewed 22 out of 23 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/rest-api-runtime-adapter/src/adapters/mod.rs | Exposes the new auth_key adapter module. |
| packages/rest-api-runtime-adapter/src/adapters/auth_key.rs | Implements AuthKeyPort by wrapping KeysHandler and mapping tracker-core outputs to protocol DTOs. |
| packages/rest-api-protocol/src/v1/context/mod.rs | Registers the new auth_key context and documents forms vs resources. |
| packages/rest-api-protocol/src/v1/context/auth_key/mod.rs | Adds the protocol context module for /api/v1/keys. |
| packages/rest-api-protocol/src/v1/context/auth_key/resources/mod.rs | Exposes AuthKey protocol resource module. |
| packages/rest-api-protocol/src/v1/context/auth_key/resources/auth_key.rs | Introduces AuthKey DTO + AuthKeyError protocol error type. |
| packages/rest-api-protocol/src/v1/context/auth_key/forms/mod.rs | Exposes AddKeyForm input DTO module. |
| packages/rest-api-protocol/src/v1/context/auth_key/forms/add_key_form.rs | Moves AddKeyForm into the protocol layer (requires serde_with). |
| packages/rest-api-protocol/Cargo.toml | Adds serde_with dependency to support the moved form DTO. |
| packages/rest-api-application/src/use_cases/mod.rs | Registers the new auth-key use-case service module. |
| packages/rest-api-application/src/use_cases/auth_key.rs | Adds AuthKeyApiService orchestrating calls to the AuthKeyPort. |
| packages/rest-api-application/src/ports/mod.rs | Registers the new auth-key port module. |
| packages/rest-api-application/src/ports/auth_key.rs | Defines AuthKeyPort trait (application boundary). |
| packages/axum-rest-api-server/tests/server/v1/asserts.rs | Switches AuthKey assertions to use protocol DTO type. |
| packages/axum-rest-api-server/src/v1/routes.rs | Wires TrackerAuthKeyAdapter + AuthKeyApiService into v1 routing. |
| packages/axum-rest-api-server/src/v1/context/auth_key/routes.rs | Changes handler state from KeysHandler to AuthKeyApiService. |
| packages/axum-rest-api-server/src/v1/context/auth_key/handlers.rs | Rewrites handlers to call AuthKeyApiService and map protocol-level errors. |
| packages/axum-rest-api-server/src/v1/context/auth_key/responses.rs | Switches response payload type to protocol AuthKey and adjusts invalid-key response signature. |
| packages/axum-rest-api-server/src/v1/context/auth_key/resources.rs | Removes Axum-local AuthKey DTO module (migrated to protocol). |
| packages/axum-rest-api-server/src/v1/context/auth_key/mod.rs | Removes forms/resources module exports (now provided by protocol). |
| packages/axum-rest-api-server/Cargo.toml | Drops serde_with dependency (moved to protocol crate). |
| docs/issues/open/1941-1938-si-3-migrate-auth-key-context.md | Updates the SI-3 issue spec/progress to match the implemented architecture. |
| Cargo.lock | Moves serde_with dependency from axum-rest-api-server to rest-api-protocol. |
Comments suppressed due to low confidence (1)
packages/axum-rest-api-server/src/v1/context/auth_key/responses.rs:60
invalid_auth_key_duration_responseis triggered fromPOST /keyswhen the providedseconds_validoverflows the clock computation, but the message currently starts with "Invalid URL". This isn't a URL issue; it should describe invalid request input.
#[must_use]
pub fn invalid_auth_key_duration_response(duration: u64) -> Response {
bad_request_response(&format!("Invalid URL: invalid auth key duration: \"{duration}\""))
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…t architecture - Add AuthKey and AuthKeyError to rest-api-protocol (resources/) - Add AddKeyForm to rest-api-protocol (forms/) — protocol now covers both input (forms) and output (resources) DTOs - Define AuthKeyPort trait in rest-api-application - Implement AuthKeyApiService use-case and TrackerAuthKeyAdapter - Rewire Axum handlers to dispatch through AuthKeyApiService - Add serde_with dependency to rest-api-protocol for AddKeyForm - Update issue spec progress
…_handler Use failed_to_add_key_response for the Database error branch in the add handler instead of failed_to_generate_key_response.
- Add absolute rule: if GPG passphrase prompt times out, the agent must stop and notify the user — never bypass signing with --no-gpg-sign - Update Committer agent instructions with the same rule - Update commit-changes skill with detailed GPG timeout procedure
6b0628e to
f7cbdb2
Compare
|
ACK f7cbdb2 |
da2ce7
left a comment
There was a problem hiding this comment.
This is a post-merge review of the change from #1950 as it stands at a94c3ced8 on the default branch.
What still runs: the four auth-key handlers dispatch through AuthKeyApiService to TrackerAuthKeyAdapter, which wraps KeysHandler; the protocol crate owns AuthKey, AuthKeyError and AddKeyForm; the port, use case and adapter now live under v1/. Later commits took the key out of error replies and Debug output and parse the deprecated route's segment in the handler (4b2af7a, b5c9497), and serve the routes only when the private capability is available (5b5e589), so the echo of a malformed key that #1950 carried over from the earlier handlers is no longer live. #1950 also changed the 500 reply of POST /api/v1/keys from failed to generate key to failed to add key (775b4e6), which its description does not mention; the new text matches the operation.
[Minor] A large key lifetime panics the request instead of answering 400, and in one range it panics only after the key has been stored. TrackerAuthKeyAdapter::generate_key (packages/rest-api-runtime-adapter/src/v1/adapters/auth_key.rs:44) and the random-key branch of add_key reach key::generate_key, which unwraps CurrentClock::now_add (packages/tracker-core/src/authentication/key/mod.rs:111); only the uploaded-key branch of KeysHandler::add_peer_key checks for overflow (packages/tracker-core/src/authentication/handler.rs:102). So POST /api/v1/key/18446744073709551615, or POST /api/v1/keys with {"key":null,"seconds_valid":18446744073709551615}, panics the connection's task and the client gets no reply at all. A smaller lifetime that still fits the database's i64 column but lies beyond the range DateTime::from_timestamp accepts (about 8.2 × 10^12 seconds after the epoch, the year 262142), for example "seconds_valid": 10000000000000, passes now_add, is written to the database and to the in-memory repository, and then panics in peer_key_to_auth_key (packages/rest-api-runtime-adapter/src/v1/adapters/auth_key.rs:90) when PeerKey::expiry_time unwraps the conversion in torrust-clock. With an uploaded key, that key now authenticates announces although the client saw the request fail, and a retry answers 500 failed to add key for the duplicate. The protocol already has AuthKeyError::DurationOverflow, but generate_key maps every error to Database (auth_key.rs:49) and generate_auth_key_handler answers every error with 500 (packages/axum-rest-api-server/src/v1/context/auth_key/handlers.rs:80). Smallest fix: before a key is generated or stored, reject a lifetime for which now_add overflows or whose expiry falls outside the convertible range, return it as DurationOverflow on all three add paths, and answer that variant with 400 in generate_auth_key_handler as add_auth_key_handler already does (handlers.rs:42); a contract test with the two lifetimes above would hold it. The unwraps predate #1950, which moved the call and the conversion into the adapter unchanged (2a019a5).
[Minor] The JSON form still quotes a rejected value back, and that is where a key typed into the wrong field lands. add_auth_key_handler takes extract::Json<AddKeyForm> (packages/axum-rest-api-server/src/v1/context/auth_key/handlers.rs:33), so a body the form rejects is answered with the extractor's own 422 text, and that text quotes a rejected string: a body of {"key":null,"seconds_valid":"<the 32-character key>"} is answered with a 422 whose text contains seconds_valid: invalid type: string "<the key>". The secrecy ADR as amended for #2999 (docs/adrs/20260822094338_adopt_secrecy_for_sensitive_values.md) requires that an error from a library that prints the value it rejected be made safe at the boundary, and 4b2af7a applied that to the same mistake on POST /api/v1/key/<seconds>; the contract assert for the form (packages/axum-rest-api-server/tests/server/v1/asserts.rs:119) ignores its _invalid_value argument, so no test notices. Smallest fix: accept Result<Json<AddKeyForm>, JsonRejection> in the handler and answer a rejection with a 422 that names the field and the expected type without the value, then make the assert check that the reply does not hold the value and add a key-shaped value to the test's list, as the deprecated-route test does.
[Nit] The use case's documentation promises work it does not do. The module doc says AuthKeyApiService adds validation, error mapping or caching as needed, and the type doc says it maps domain errors to protocol error types (packages/rest-api-application/src/v1/use_cases/auth_key.rs:3-4 and :12-13), but every method forwards to the port unchanged; the mapping is map_peer_key_error in the adapter. The handler docs also list fewer replies than the handlers give: generate_auth_key_handler and delete_auth_key_handler say they return two types of responses, 200 and 500, but both also answer 400 and 409 (packages/axum-rest-api-server/src/v1/context/auth_key/handlers.rs:53-58 and :90-97), and the docs of the add and reload handlers omit 409 (handlers.rs:20-27 and :119-124). Smallest fix: say that the service forwards to the port today, and list the 400 and 409 replies.
[Nit] AuthKeyErrorDisplay (packages/axum-rest-api-server/src/v1/context/auth_key/handlers.rs:143-159) wraps an AuthKeyError reference so that it can be passed where E: std::error::Error is required, but AuthKeyError already implements Error (packages/rest-api-protocol/src/v1/context/auth_key/resources/auth_key.rs:70) and the standard library implements Error for &T whenever T: Error, so the four response calls accept &err or e as they are. Smallest fix: pass the error itself and delete the wrapper.
Checked and found sound: the composition in packages/axum-rest-api-server/src/v1/routes.rs:25-37, which builds the service only for a private tracker with persistence and otherwise answers 409; the mapping of every PeerKeyError variant, including the arm for variants added later; the key field of AddKeyForm, which accepts any JSON string without quoting it back; the redacted Debug of AuthKey, AddKeyForm and AddKeyRequest; Key parsing and its error text, which name the length or the characters and not the key; the delete path, which parses the key before it touches the store and answers 400 with the reason only; the reload path; and the parsing of the deprecated route's segment, which answers 400 without quoting the segment.
Description
Implements SI-3: Migrate
auth_keycontext to contract-first architecture (sub-issue of #1938).Changes
rest-api-protocol: Addedv1::context::auth_key/with:resources/auth_key.rs—AuthKeyDTO +AuthKeyErrorenum (3 variants matchingPeerKeyError)forms/add_key_form.rs—AddKeyForminput DTO (protocol now covers both input and output types)rest-api-application: DefinedAuthKeyPorttrait andAuthKeyApiServiceuse-caserest-api-runtime-adapter: ImplementedTrackerAuthKeyAdapterwrappingKeysHandler+ conversion functionaxum-rest-api-server: Rewired handlers to dispatch throughAuthKeyApiService, removed local DTOs (resources.rs,forms.rs)Architecture flow
Verification
AuthKeyPorttrait defined inrest-api-applicationAuthKeyApiServiceuse-case implementedTrackerAuthKeyAdapterimplemented