Skip to content

Commit

Permalink
Generate AAD token scope from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbatty committed Aug 23, 2023
1 parent 37db397 commit e26458e
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions sdk/data_cosmos/src/authorization_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::resources::permission::AuthorizationToken;
use crate::resources::ResourceType;
use azure_core::base64;
use azure_core::headers::{HeaderValue, AUTHORIZATION, MS_DATE, VERSION};
use azure_core::{date, Context, Policy, PolicyResult, Request};
use azure_core::{date, Context, Policy, PolicyResult, Request, Url};
use hmac::{Hmac, Mac};
use sha2::Sha256;
use std::borrow::Cow;
Expand Down Expand Up @@ -62,6 +62,7 @@ impl Policy for AuthorizationPolicy {
generate_authorization(
&self.authorization_token,
request.method(),
request.url(),
ctx.get()
.expect("ResourceType must be in the Context at this point"),
&resource_link,
Expand Down Expand Up @@ -150,6 +151,7 @@ fn generate_resource_link(request: &Request) -> String {
async fn generate_authorization(
auth_token: &AuthorizationToken,
http_method: &azure_core::Method,
url: &Url,
resource_type: &ResourceType,
resource_link: &str,
time_nonce: OffsetDateTime,
Expand All @@ -168,7 +170,7 @@ async fn generate_authorization(
"aad",
Cow::Owned(
token_credential
.get_token(resource_link)
.get_token(&scope_from_url(url))
.await?
.token
.secret()
Expand All @@ -186,6 +188,14 @@ async fn generate_authorization(
Ok(form_urlencoded::byte_serialize(str_unencoded.as_bytes()).collect::<String>())
}

/// This function generates the scope string from the passed url. The scope string is used to
/// request the AAD token.
fn scope_from_url(url: &Url) -> String {
let scheme = url.scheme();
let hostname = url.host_str().unwrap();
return format!("{scheme}://{hostname}/.default");
}

/// This function generates a valid authorization string, according to the documentation.
/// In case of authorization problems we can compare the `string_to_sign` generated by Azure against
/// our own.
Expand Down Expand Up @@ -282,9 +292,12 @@ mon, 01 jan 1900 01:00:00 gmt
)
.unwrap();

let url = azure_core::Url::parse("https://.documents.azure.com/dbs/ToDoList").unwrap();

let ret = generate_authorization(
&auth_token,
&azure_core::Method::Get,
&url,
&ResourceType::Databases,
"dbs/MyDatabase/colls/MyCollection",
time,
Expand All @@ -307,9 +320,12 @@ mon, 01 jan 1900 01:00:00 gmt
)
.unwrap();

let url = azure_core::Url::parse("https://.documents.azure.com/dbs/ToDoList").unwrap();

let ret = generate_authorization(
&auth_token,
&azure_core::Method::Get,
&url,
&ResourceType::Databases,
"dbs/ToDoList",
time,
Expand Down Expand Up @@ -363,4 +379,12 @@ mon, 01 jan 1900 01:00:00 gmt
);
assert_eq!(&generate_resource_link(&request), "dbs/test_db");
}

#[test]
fn scope_from_url_01() {
let scope = scope_from_url(
&azure_core::Url::parse("https://.documents.azure.com/dbs/test_db/colls").unwrap(),
);
assert_eq!(scope, "https://.documents.azure.com/.default");
}
}

0 comments on commit e26458e

Please sign in to comment.