Skip to main content

auth_framework/api/
oauth.rs

1//! OAuth 2.0 API Endpoints
2//!
3//! **These endpoints are not implemented on this release line.** Earlier releases answered them with
4//! placeholder data (an introspection said `active: true` for any string, the token endpoint issued
5//! made-up tokens without checking the client, the authorization endpoint redirected with a made-up
6//! code). Every handler here now answers `501 Not Implemented` whatever the request says, so
7//! nothing that depends on them can be told that something is valid when it was never checked.
8//! The working OAuth 2.0 server is `server::oauth` (library API), and the 0.5 and 0.6 lines
9//! implement these endpoints in `api::oauth2`.
10
11use crate::api::{ApiResponse, ApiState};
12use axum::{
13    Json,
14    extract::{Query, State},
15    http::{HeaderMap, StatusCode},
16    response::{IntoResponse, Response},
17};
18use serde::{Deserialize, Serialize};
19
20/// OAuth authorization request parameters
21#[derive(Debug, Deserialize)]
22pub struct AuthorizeRequest {
23    pub response_type: String,
24    pub client_id: String,
25    pub redirect_uri: String,
26    pub scope: Option<String>,
27    pub state: Option<String>,
28    pub code_challenge: Option<String>,
29    pub code_challenge_method: Option<String>,
30}
31
32/// OAuth token request
33#[derive(Deserialize)]
34pub struct TokenRequest {
35    pub grant_type: String,
36    pub code: Option<String>,
37    pub client_id: String,
38    pub client_secret: Option<String>,
39    pub redirect_uri: Option<String>,
40    pub refresh_token: Option<String>,
41    pub code_verifier: Option<String>,
42}
43
44impl std::fmt::Debug for TokenRequest {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        let mut d = f.debug_struct("TokenRequest");
47        d.field("grant_type", &self.grant_type);
48        d.field("code", &crate::utils::redact::redacted(&self.code));
49        d.field("client_id", &self.client_id);
50        d.field(
51            "client_secret",
52            &crate::utils::redact::redacted(&self.client_secret),
53        );
54        d.field("redirect_uri", &self.redirect_uri);
55        d.field(
56            "refresh_token",
57            &crate::utils::redact::redacted(&self.refresh_token),
58        );
59        d.field(
60            "code_verifier",
61            &crate::utils::redact::redacted(&self.code_verifier),
62        );
63        d.finish()
64    }
65}
66
67/// OAuth token response
68#[derive(Serialize)]
69pub struct TokenResponse {
70    pub access_token: String,
71    pub token_type: String,
72    pub expires_in: u64,
73    #[serde(skip_serializing_if = "Option::is_none")]
74    pub refresh_token: Option<String>,
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub scope: Option<String>,
77    #[serde(skip_serializing_if = "Option::is_none")]
78    pub id_token: Option<String>,
79}
80
81impl std::fmt::Debug for TokenResponse {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        let mut d = f.debug_struct("TokenResponse");
84        d.field(
85            "access_token",
86            &crate::utils::redact::redacted(&self.access_token),
87        );
88        d.field("token_type", &self.token_type);
89        d.field("expires_in", &self.expires_in);
90        d.field(
91            "refresh_token",
92            &crate::utils::redact::redacted(&self.refresh_token),
93        );
94        d.field("scope", &self.scope);
95        d.field("id_token", &crate::utils::redact::redacted(&self.id_token));
96        d.finish()
97    }
98}
99
100/// OAuth error response
101#[derive(Debug, Serialize)]
102pub struct OAuthError {
103    pub error: String,
104    #[serde(skip_serializing_if = "Option::is_none")]
105    pub error_description: Option<String>,
106    #[serde(skip_serializing_if = "Option::is_none")]
107    pub error_uri: Option<String>,
108    #[serde(skip_serializing_if = "Option::is_none")]
109    pub state: Option<String>,
110}
111
112/// Client information
113#[derive(Debug, Serialize)]
114pub struct ClientInfo {
115    pub client_id: String,
116    pub name: String,
117    pub description: String,
118    pub redirect_uris: Vec<String>,
119    pub scopes: Vec<String>,
120}
121
122const NOT_IMPLEMENTED_DESCRIPTION: &str =
123    "this OAuth endpoint is not implemented in this release line";
124
125fn not_implemented_response() -> Response {
126    let error = OAuthError {
127        error: "not_implemented".to_string(),
128        error_description: Some(NOT_IMPLEMENTED_DESCRIPTION.to_string()),
129        error_uri: None,
130        state: None,
131    };
132    (StatusCode::NOT_IMPLEMENTED, Json(error)).into_response()
133}
134
135fn not_implemented<T: Serialize>() -> ApiResponse<T> {
136    ApiResponse::<T>::error_typed("NOT_IMPLEMENTED", NOT_IMPLEMENTED_DESCRIPTION)
137}
138
139/// GET /oauth/authorize
140/// Not implemented: answers `501` and never redirects.
141pub async fn authorize(
142    State(_state): State<ApiState>,
143    Query(_params): Query<AuthorizeRequest>,
144) -> impl IntoResponse {
145    not_implemented_response()
146}
147
148/// POST /oauth/token
149/// Not implemented: answers `501` for every grant type and issues nothing.
150pub async fn token(
151    State(_state): State<ApiState>,
152    _headers: HeaderMap,
153    Json(_req): Json<TokenRequest>,
154) -> ApiResponse<TokenResponse> {
155    not_implemented()
156}
157
158/// POST /oauth/revoke
159/// Token revocation endpoint
160#[derive(Deserialize)]
161pub struct RevokeRequest {
162    pub token: String,
163    pub token_type_hint: Option<String>,
164}
165
166impl std::fmt::Debug for RevokeRequest {
167    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
168        let mut d = f.debug_struct("RevokeRequest");
169        d.field("token", &crate::utils::redact::redacted(&self.token));
170        d.field("token_type_hint", &self.token_type_hint);
171        d.finish()
172    }
173}
174
175/// Not implemented: answers `501`; nothing is revoked.
176pub async fn revoke_token(
177    State(_state): State<ApiState>,
178    Json(_req): Json<RevokeRequest>,
179) -> ApiResponse<()> {
180    not_implemented()
181}
182
183/// POST /oauth/introspect
184/// Token introspection endpoint (RFC 7662)
185#[derive(Deserialize)]
186pub struct IntrospectRequest {
187    pub token: String,
188    pub token_type_hint: Option<String>,
189}
190
191impl std::fmt::Debug for IntrospectRequest {
192    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
193        let mut d = f.debug_struct("IntrospectRequest");
194        d.field("token", &crate::utils::redact::redacted(&self.token));
195        d.field("token_type_hint", &self.token_type_hint);
196        d.finish()
197    }
198}
199
200#[derive(Debug, Serialize)]
201pub struct IntrospectResponse {
202    pub active: bool,
203    #[serde(skip_serializing_if = "Option::is_none")]
204    pub scope: Option<String>,
205    #[serde(skip_serializing_if = "Option::is_none")]
206    pub client_id: Option<String>,
207    #[serde(skip_serializing_if = "Option::is_none")]
208    pub username: Option<String>,
209    #[serde(skip_serializing_if = "Option::is_none")]
210    pub token_type: Option<String>,
211    #[serde(skip_serializing_if = "Option::is_none")]
212    pub exp: Option<u64>,
213    #[serde(skip_serializing_if = "Option::is_none")]
214    pub iat: Option<u64>,
215    #[serde(skip_serializing_if = "Option::is_none")]
216    pub sub: Option<String>,
217}
218
219/// Not implemented: answers `501`; never reports a token as active.
220pub async fn introspect_token(
221    State(_state): State<ApiState>,
222    Json(_req): Json<IntrospectRequest>,
223) -> ApiResponse<IntrospectResponse> {
224    not_implemented()
225}
226
227/// GET /oauth/clients/{client_id}
228/// Not implemented: answers `501`; no client information is made up.
229pub async fn get_client_info(
230    State(_state): State<ApiState>,
231    axum::extract::Path(_client_id): axum::extract::Path<String>,
232) -> ApiResponse<ClientInfo> {
233    not_implemented()
234}