Skip to content

Commit

Permalink
feat: upgrade gateway to axum 0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
oddgrd committed Nov 29, 2022
1 parent de24329 commit 0b93fec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions gateway/src/api/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ async fn request_acme_certificate(
}

pub struct ApiBuilder {
router: Router<Body>,
router: Router,
service: Option<Arc<GatewayService>>,
sender: Option<Sender<BoxedTask>>,
bind: Option<SocketAddr>,
Expand Down Expand Up @@ -361,7 +361,7 @@ impl ApiBuilder {
self
}

pub fn into_router(self) -> Router<Body> {
pub fn into_router(self) -> Router {
let service = self.service.expect("a GatewayService is required");
let sender = self.sender.expect("a task Sender is required");
self.router
Expand Down
41 changes: 21 additions & 20 deletions gateway/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::fmt::{Debug, Formatter};
use std::str::FromStr;
use std::sync::Arc;

use axum::extract::{Extension, FromRequest, Path, RequestParts, TypedHeader};
use axum::extract::{Extension, FromRequestParts, Path, TypedHeader};
use axum::headers::authorization::Bearer;
use axum::headers::Authorization;
use axum::http::request::Parts;
use rand::distributions::{Alphanumeric, DistString};
use serde::{Deserialize, Serialize};
use tracing::{trace, Span};
Expand All @@ -24,14 +25,14 @@ impl Key {
}

#[async_trait]
impl<B> FromRequest<B> for Key
impl<S> FromRequestParts<S> for Key
where
B: Send,
S: Send + Sync,
{
type Rejection = Error;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let key = TypedHeader::<Authorization<Bearer>>::from_request(req)
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let key = TypedHeader::<Authorization<Bearer>>::from_request_parts(parts, state)
.await
.map_err(|_| Error::from(ErrorKind::KeyMissing))
.and_then(|TypedHeader(Authorization(bearer))| bearer.token().trim().parse())?;
Expand Down Expand Up @@ -183,15 +184,15 @@ impl Permissions {
}

#[async_trait]
impl<B> FromRequest<B> for User
impl<S> FromRequestParts<S> for User
where
B: Send,
S: Send + Sync,
{
type Rejection = Error;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let key = Key::from_request(req).await?;
let Extension(service) = Extension::<Arc<GatewayService>>::from_request(req)
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let key = Key::from_request_parts(parts, state).await?;
let Extension(service) = Extension::<Arc<GatewayService>>::from_request_parts(parts, state)
.await
.unwrap();
let user = User::retrieve_from_key(&service, key)
Expand Down Expand Up @@ -231,17 +232,17 @@ pub struct ScopedUser {
}

#[async_trait]
impl<B> FromRequest<B> for ScopedUser
impl<S> FromRequestParts<S> for ScopedUser
where
B: Send,
S: Send + Sync,
{
type Rejection = Error;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let user = User::from_request(req).await?;
let scope = match Path::<ProjectName>::from_request(req).await {
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let user = User::from_request_parts(parts, state).await?;
let scope = match Path::<ProjectName>::from_request_parts(parts, state).await {
Ok(Path(p)) => p,
Err(_) => Path::<(ProjectName, String)>::from_request(req)
Err(_) => Path::<(ProjectName, String)>::from_request_parts(parts, state)
.await
.map(|Path((p, _))| p)
.unwrap(),
Expand All @@ -260,14 +261,14 @@ pub struct Admin {
}

#[async_trait]
impl<B> FromRequest<B> for Admin
impl<S> FromRequestParts<S> for Admin
where
B: Send,
S: Send + Sync,
{
type Rejection = Error;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let user = User::from_request(req).await?;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let user = User::from_request_parts(parts, state).await?;
if user.is_super_user() {
Ok(Self { user })
} else {
Expand Down

0 comments on commit 0b93fec

Please sign in to comment.