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(Debug, 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
44/// OAuth token response
45#[derive(Debug, Serialize)]
46pub struct TokenResponse {
47    pub access_token: String,
48    pub token_type: String,
49    pub expires_in: u64,
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub refresh_token: Option<String>,
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub scope: Option<String>,
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub id_token: Option<String>,
56}
57
58/// OAuth error response
59#[derive(Debug, Serialize)]
60pub struct OAuthError {
61    pub error: String,
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub error_description: Option<String>,
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub error_uri: Option<String>,
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub state: Option<String>,
68}
69
70/// Client information
71#[derive(Debug, Serialize)]
72pub struct ClientInfo {
73    pub client_id: String,
74    pub name: String,
75    pub description: String,
76    pub redirect_uris: Vec<String>,
77    pub scopes: Vec<String>,
78}
79
80const NOT_IMPLEMENTED_DESCRIPTION: &str =
81    "this OAuth endpoint is not implemented in this release line";
82
83fn not_implemented_response() -> Response {
84    let error = OAuthError {
85        error: "not_implemented".to_string(),
86        error_description: Some(NOT_IMPLEMENTED_DESCRIPTION.to_string()),
87        error_uri: None,
88        state: None,
89    };
90    (StatusCode::NOT_IMPLEMENTED, Json(error)).into_response()
91}
92
93fn not_implemented<T: Serialize>() -> ApiResponse<T> {
94    ApiResponse::<T>::error_typed("NOT_IMPLEMENTED", NOT_IMPLEMENTED_DESCRIPTION)
95}
96
97/// GET /oauth/authorize
98/// Not implemented: answers `501` and never redirects.
99pub async fn authorize(
100    State(_state): State<ApiState>,
101    Query(_params): Query<AuthorizeRequest>,
102) -> impl IntoResponse {
103    not_implemented_response()
104}
105
106/// POST /oauth/token
107/// Not implemented: answers `501` for every grant type and issues nothing.
108pub async fn token(
109    State(_state): State<ApiState>,
110    _headers: HeaderMap,
111    Json(_req): Json<TokenRequest>,
112) -> ApiResponse<TokenResponse> {
113    not_implemented()
114}
115
116/// POST /oauth/revoke
117/// Token revocation endpoint
118#[derive(Debug, Deserialize)]
119pub struct RevokeRequest {
120    pub token: String,
121    pub token_type_hint: Option<String>,
122}
123
124/// Not implemented: answers `501`; nothing is revoked.
125pub async fn revoke_token(
126    State(_state): State<ApiState>,
127    Json(_req): Json<RevokeRequest>,
128) -> ApiResponse<()> {
129    not_implemented()
130}
131
132/// POST /oauth/introspect
133/// Token introspection endpoint (RFC 7662)
134#[derive(Debug, Deserialize)]
135pub struct IntrospectRequest {
136    pub token: String,
137    pub token_type_hint: Option<String>,
138}
139
140#[derive(Debug, Serialize)]
141pub struct IntrospectResponse {
142    pub active: bool,
143    #[serde(skip_serializing_if = "Option::is_none")]
144    pub scope: Option<String>,
145    #[serde(skip_serializing_if = "Option::is_none")]
146    pub client_id: Option<String>,
147    #[serde(skip_serializing_if = "Option::is_none")]
148    pub username: Option<String>,
149    #[serde(skip_serializing_if = "Option::is_none")]
150    pub token_type: Option<String>,
151    #[serde(skip_serializing_if = "Option::is_none")]
152    pub exp: Option<u64>,
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub iat: Option<u64>,
155    #[serde(skip_serializing_if = "Option::is_none")]
156    pub sub: Option<String>,
157}
158
159/// Not implemented: answers `501`; never reports a token as active.
160pub async fn introspect_token(
161    State(_state): State<ApiState>,
162    Json(_req): Json<IntrospectRequest>,
163) -> ApiResponse<IntrospectResponse> {
164    not_implemented()
165}
166
167/// GET /oauth/clients/{client_id}
168/// Not implemented: answers `501`; no client information is made up.
169pub async fn get_client_info(
170    State(_state): State<ApiState>,
171    axum::extract::Path(_client_id): axum::extract::Path<String>,
172) -> ApiResponse<ClientInfo> {
173    not_implemented()
174}