Skip to content

Commit

Permalink
Bridge all verbs (get/put/patch/post/delete) + Authorization is set i…
Browse files Browse the repository at this point in the history
…n Gateway-Authorization header for the bridge
  • Loading branch information
fdubois1 committed Aug 3, 2021
1 parent cfdc353 commit 2b5077a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
14 changes: 4 additions & 10 deletions devolutions-gateway/src/http/controllers/http_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use saphir::macros::controller;
use saphir::request::Request;
use saphir::response::Builder;

pub const REQUEST_AUTHORIZATION_TOKEN_HDR_NAME: &str = "Request-Authorization-Token";

pub struct HttpBridgeController {
client: reqwest::Client,
}
Expand All @@ -20,7 +18,11 @@ impl HttpBridgeController {

#[controller(name = "bridge")]
impl HttpBridgeController {
#[get("/message")]
#[post("/message")]
#[put("/message")]
#[patch("/message")]
#[delete("/message")]
#[guard(AccessGuard, init_expr = r#"JetTokenType::Bridge"#)]
async fn message(&self, req: Request) -> Result<Builder, HttpErrorStatus> {
use core::convert::TryFrom;
Expand All @@ -37,15 +39,7 @@ impl HttpBridgeController {
let req: saphir::request::Request<reqwest::Body> = req.map(reqwest::Body::from);
let mut req: http::Request<reqwest::Body> = http::Request::from(req);

// === Replace Authorization header (used to be authorized on the gateway) with the request authorization token === //

let mut rsp = {
let headers = req.headers_mut();
headers.remove(http::header::AUTHORIZATION);
if let Some(auth_token) = headers.remove(REQUEST_AUTHORIZATION_TOKEN_HDR_NAME) {
headers.insert(http::header::AUTHORIZATION, auth_token);
}

// Update request destination
let uri = http::Uri::try_from(claims.target.as_str()).map_err(HttpErrorStatus::bad_request)?;
*req.uri_mut() = uri;
Expand Down
7 changes: 6 additions & 1 deletion devolutions-gateway/src/http/middlewares/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use saphir::response::Builder as ResponseBuilder;
use slog_scope::error;
use std::sync::Arc;

const GATEWAY_AUTHORIZATION_HDR_NAME: &str = "Gateway-Authorization";

pub struct AuthMiddleware {
config: Arc<Config>,
}
Expand Down Expand Up @@ -38,7 +40,10 @@ async fn auth_middleware(
) -> Result<HttpContext, SaphirError> {
let request = ctx.state.request_unchecked_mut();

let auth_header = request.headers().get(http::header::AUTHORIZATION);
let gateway_auth_header = request.headers_mut().remove(GATEWAY_AUTHORIZATION_HDR_NAME);
let auth_header = gateway_auth_header
.as_ref()
.or_else(|| request.headers().get(http::header::AUTHORIZATION));

let auth_header = match auth_header {
Some(header) => header.clone(),
Expand Down
9 changes: 8 additions & 1 deletion powershell/DevolutionsGateway/Public/DGateway.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ function New-DGatewayToken {
param(
[string] $ConfigPath,

[ValidateSet('association', 'scope')]
[ValidateSet('association', 'scope', 'bridge')]
[Parameter(Mandatory = $true)]
[string] $Type, #type

Expand All @@ -640,6 +640,9 @@ function New-DGatewayToken {
#private scope claims
[string] $Scope, # scope

#private bridge claims
[string] $Target, # target

# signature parameters
[string] $PrivateKeyFile
)
Expand Down Expand Up @@ -722,6 +725,10 @@ function New-DGatewayToken {
$Payload | Add-Member -MemberType NoteProperty -Name 'scope' -Value $Scope
}

if (($Type -eq 'bridge') -and ($Target)) {
$Payload | Add-Member -MemberType NoteProperty -Name 'target' -Value $Target
}

New-JwtRs256 -Header $Header -Payload $Payload -PrivateKey $PrivateKey
}

Expand Down

0 comments on commit 2b5077a

Please sign in to comment.