Skip to content

Commit f87ebcf

Browse files
committed
allow authorization token in request URL for media requests
1 parent df7225e commit f87ebcf

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

server/src/api/extract.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use super::jsonapi::Error;
21
use axum::{
32
async_trait,
43
body::{Bytes, HttpBody},
54
extract::{
65
rejection::TypedHeaderRejection, FromRequest, FromRequestParts, Json as AxumJson,
7-
Path as AxumPath, TypedHeader as AxumTypedHeader,
6+
Path as AxumPath, Query, TypedHeader as AxumTypedHeader,
87
},
98
headers::{
109
authorization::{Authorization, Bearer},
@@ -21,6 +20,7 @@ use jsonwebtoken::{
2120
use serde::{de::DeserializeOwned, Deserialize, Serialize};
2221
use thiserror::Error;
2322

23+
use super::jsonapi::Error as JsonAPIError;
2424
use base::setting::{get_settings, SettingsError};
2525

2626
static HEADER_VALUE: &str = "application/vnd.api+json";
@@ -47,7 +47,7 @@ impl IntoResponse for JsonError {
4747
JsonError::Io | JsonError::BodyRead(_) => StatusCode::INTERNAL_SERVER_ERROR,
4848
JsonError::Mime | JsonError::Syntax(_) | JsonError::Data(_) => StatusCode::BAD_REQUEST,
4949
};
50-
let err = Error {
50+
let err = JsonAPIError {
5151
status,
5252
title: "Could not parse JSON request body".to_string(),
5353
detail: Some(self.into()),
@@ -140,7 +140,7 @@ pub enum TypedHeaderError {
140140

141141
impl IntoResponse for TypedHeaderError {
142142
fn into_response(self) -> Response {
143-
Error {
143+
JsonAPIError {
144144
status: StatusCode::BAD_REQUEST,
145145
title: self.to_string(),
146146
detail: match self {
@@ -173,12 +173,12 @@ where
173173
T: DeserializeOwned + Send,
174174
S: Send + Sync,
175175
{
176-
type Rejection = Error;
176+
type Rejection = JsonAPIError;
177177

178178
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
179179
let AxumPath(t) = AxumPath::<T>::from_request_parts(parts, state)
180180
.await
181-
.map_err(|e| Error {
181+
.map_err(|e| Self::Rejection {
182182
status: StatusCode::NOT_FOUND,
183183
title: "Invalid URL path".to_string(),
184184
detail: Some(e.into()),
@@ -206,7 +206,7 @@ pub enum ClaimsError {
206206
Settings(#[from] SettingsError),
207207

208208
#[error("Missing Authorization header")]
209-
TypedHeader(#[from] TypedHeaderError),
209+
Missing,
210210

211211
#[error("Invalid authentication token")]
212212
Unauthorized(#[from] JwtError),
@@ -218,19 +218,26 @@ impl IntoResponse for ClaimsError {
218218
ClaimsError::Settings(_) => StatusCode::INTERNAL_SERVER_ERROR,
219219
_ => StatusCode::UNAUTHORIZED,
220220
};
221-
Error {
221+
JsonAPIError {
222222
status,
223223
title: self.to_string(),
224224
detail: match self {
225225
ClaimsError::Settings(e) => Some(Box::new(e)),
226-
ClaimsError::TypedHeader(e) => Some(Box::new(e)),
226+
ClaimsError::Missing => {
227+
Some("No Authorization header or query parameter found".into())
228+
}
227229
ClaimsError::Unauthorized(e) => Some(Box::new(e)),
228230
},
229231
}
230232
.into_response()
231233
}
232234
}
233235

236+
#[derive(Debug, Serialize, Deserialize)]
237+
struct ClaimsQuery {
238+
pub authorization: String,
239+
}
240+
234241
#[async_trait]
235242
impl<S> FromRequestParts<S> for Claims
236243
where
@@ -239,9 +246,21 @@ where
239246
type Rejection = ClaimsError;
240247

241248
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
242-
let TypedHeader(header) =
243-
TypedHeader::<Authorization<Bearer>>::from_request_parts(parts, state).await?;
244-
check_token(header.token()).map(|td| td.claims)
249+
match TypedHeader::<Authorization<Bearer>>::from_request_parts(parts, state)
250+
.await
251+
.ok()
252+
{
253+
Some(TypedHeader(header)) => check_token(header.token()).map(|td| td.claims),
254+
None => match Query::<ClaimsQuery>::from_request_parts(parts, state)
255+
.await
256+
.ok()
257+
{
258+
Some(Query(ClaimsQuery { authorization })) => {
259+
check_token(&authorization).map(|td| td.claims)
260+
}
261+
None => Err(Self::Rejection::Missing),
262+
},
263+
}
245264
}
246265
}
247266

0 commit comments

Comments
 (0)