Skip to content
This repository was archived by the owner on Oct 6, 2020. It is now read-only.

Commit ffc4f79

Browse files
author
Francesco Cogno
authored
Rename AAD functions to match the Microsoft documentation (#299)
* migration done * fixed README
1 parent c8b5230 commit ffc4f79

File tree

8 files changed

+92
-26
lines changed

8 files changed

+92
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
[![Build Status](https://travis-ci.org/MindFlavor/AzureSDKForRust.svg?branch=master)](https://travis-ci.org/MindFlavor/AzureSDKForRust) [![Coverage Status](https://coveralls.io/repos/MindFlavor/AzureSDKForRust/badge.svg?branch=master&service=github)](https://coveralls.io/github/MindFlavor/AzureSDKForRust?branch=master) ![stability-unstable](https://img.shields.io/badge/stability-unstable-yellow.svg)
77

8-
[![tag](https://img.shields.io/github/tag/mindflavor/AzureSDKForRust.svg)](https://github.com/MindFlavor/AzureSDKForRust/tree/cosmos_0.100.2) [![release](https://img.shields.io/github/release/mindflavor/AzureSDKForRust.svg)](https://github.com/MindFlavor/AzureSDKForRust/releases/tag/cosmos_0.100.2) [![commitssince](https://img.shields.io/github/commits-since/mindflavor/AzureSDKForRust/cosmos_0.100.2)](https://github.com/MindFlavor/AzureSDKForRust/commits/master)
8+
[![tag](https://img.shields.io/github/tag/mindflavor/AzureSDKForRust.svg)](https://github.com/MindFlavor/AzureSDKForRust/tree/aad_0.45.0) [![release](https://img.shields.io/github/release/mindflavor/AzureSDKForRust.svg)](https://github.com/MindFlavor/AzureSDKForRust/releases/tag/aad_0.45.0) [![commitssince](https://img.shields.io/github/commits-since/mindflavor/AzureSDKForRust/aad_0.45.0)](https://github.com/MindFlavor/AzureSDKForRust/commits/master)
99

1010
[![GitHub contributors](https://img.shields.io/github/contributors/MindFlavor/AzureSDKForRust.svg)](https://github.com/MindFlavor/AzureSDKForRust/graphs/contributors)
1111

azure_sdk_auth_aad/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "azure_sdk_auth_aad"
3-
version = "0.44.0"
3+
version = "0.45.0"
44
description = "Rust wrappers around Microsoft Azure REST APIs - Azure OAuth2 helper crate"
55
readme = "README.md"
66
authors = ["Francesco Cogno <francesco.cogno@outlook.com>"]

azure_sdk_auth_aad/examples/non_interactive.rs renamed to azure_sdk_auth_aad/examples/client_credentials_flow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
1919
// This Future will give you the final token to
2020
// use in authorization.
2121
let client = Arc::new(reqwest::Client::new());
22-
let token = authorize_non_interactive(
22+
let token = authorize_client_credentials_flow(
2323
client.clone(),
2424
&client_id,
2525
&client_secret,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use azure_sdk_auth_aad::*;
2+
use oauth2::{ClientId, ClientSecret};
3+
use std::env;
4+
use std::error::Error;
5+
use std::sync::Arc;
6+
7+
#[tokio::main]
8+
async fn main() -> Result<(), Box<dyn Error>> {
9+
let client_id =
10+
ClientId::new(env::var("CLIENT_ID").expect("Missing CLIENT_ID environment variable."));
11+
let client_secret = ClientSecret::new(
12+
env::var("CLIENT_SECRET").expect("Missing CLIENT_SECRET environment variable."),
13+
);
14+
let tenant_id = env::var("TENANT_ID").expect("Missing TENANT_ID environment variable.");
15+
16+
let storage_account_name = std::env::args()
17+
.nth(1)
18+
.expect("please specify the storage account name as first command line parameter");
19+
let container_name = std::env::args()
20+
.nth(2)
21+
.expect("please specify the container name as second command line parameter");
22+
23+
let client = Arc::new(reqwest::Client::new());
24+
25+
let token = authorize_client_credentials_flow(
26+
client,
27+
&client_id,
28+
&client_secret,
29+
&format!(
30+
"https://{}.blob.core.windows.net/.default",
31+
storage_account_name
32+
),
33+
&tenant_id,
34+
)
35+
.await?;
36+
37+
println!("token received: {:?}", token);
38+
println!("token secret: {}", token.access_token().secret());
39+
40+
let dt = chrono::Utc::now();
41+
let time = format!("{}", dt.format("%a, %d %h %Y %T GMT"));
42+
println!("x-ms-date ==> {}", time);
43+
44+
let resp = reqwest::Client::new()
45+
.get(&format!(
46+
"https://{}.blob.core.windows.net/{}?restype=container&comp=list",
47+
storage_account_name, container_name
48+
))
49+
.header(
50+
"Authorization",
51+
format!("Bearer {}", token.access_token().secret()),
52+
)
53+
.header("x-ms-version", "2019-07-07")
54+
.header("x-ms-date", time)
55+
.send()
56+
.await?
57+
.text()
58+
.await?;
59+
60+
println!("\n\nresp {:?}", resp);
61+
62+
Ok(())
63+
}

azure_sdk_auth_aad/examples/interactive.rs renamed to azure_sdk_auth_aad/examples/code_flow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
1616
env::var("SUBSCRIPTION_ID").expect("Missing SUBSCRIPTION_ID environment variable.");
1717

1818
// Create URL to browse for initial authorization
19-
let c = authorize_delegate(
19+
let c = authorize_code_flow(
2020
client_id,
2121
Some(client_secret),
2222
&tenant_id,

azure_sdk_auth_aad/examples/interactive_blob.rs renamed to azure_sdk_auth_aad/examples/code_flow_blob.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
2121
.expect("please specify the container name as second command line parameter");
2222

2323
// Create URL to browse for initial authorization
24-
let c = authorize_delegate(
24+
let c = authorize_code_flow(
2525
client_id,
2626
Some(client_secret),
2727
&tenant_id,

azure_sdk_auth_aad/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct AuthObj {
3232
pub pkce_code_verifier: PkceCodeVerifier,
3333
}
3434

35-
pub fn authorize_delegate(
35+
pub fn authorize_code_flow(
3636
client_id: ClientId,
3737
client_secret: Option<ClientSecret>,
3838
tenant_id: &str,
@@ -103,23 +103,23 @@ pub async fn exchange(
103103
Ok(token)
104104
}
105105

106-
pub async fn authorize_non_interactive(
106+
pub async fn authorize_client_credentials_flow(
107107
client: Arc<reqwest::Client>,
108108
// grant_type: &str, fixed on "client_credentials",
109109
client_id: &oauth2::ClientId,
110110
client_secret: &oauth2::ClientSecret,
111-
resource: &str,
111+
scope: &str,
112112
tenant_id: &str,
113113
) -> Result<LoginResponse, AzureError> {
114114
let encoded: String = form_urlencoded::Serializer::new(String::new())
115-
.append_pair("grant_type", "client_credentials")
116115
.append_pair("client_id", client_id.as_str())
116+
.append_pair("scope", scope)
117117
.append_pair("client_secret", client_secret.secret())
118-
.append_pair("resource", resource)
118+
.append_pair("grant_type", "client_credentials")
119119
.finish();
120120

121121
let url = url::Url::parse(&format!(
122-
"https://login.microsoftonline.com/{}/oauth2/token",
122+
"https://login.microsoftonline.com/{}/oauth2/v2.0/token",
123123
tenant_id
124124
))
125125
.map_err(|error| AzureError::GenericErrorWithText(error.to_string()))?;

azure_sdk_auth_aad/src/login_response.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use std::str::FromStr;
77
#[derive(Debug, Clone, Deserialize)]
88
struct _LoginResponse {
99
token_type: String,
10-
expires_in: String,
11-
ext_expires_in: String,
12-
expires_on: String,
13-
not_before: String,
14-
resource: String,
10+
expires_in: u64,
11+
ext_expires_in: u64,
12+
expires_on: Option<String>,
13+
not_before: Option<String>,
14+
resource: Option<String>,
1515
access_token: String,
1616
}
1717

@@ -20,9 +20,9 @@ pub struct LoginResponse {
2020
pub token_type: String,
2121
pub expires_in: u64,
2222
pub ext_expires_in: u64,
23-
pub expires_on: DateTime<Utc>,
24-
pub not_before: DateTime<Utc>,
25-
pub resource: String,
23+
pub expires_on: Option<DateTime<Utc>>,
24+
pub not_before: Option<DateTime<Utc>>,
25+
pub resource: Option<String>,
2626
pub access_token: AccessToken,
2727
}
2828

@@ -51,16 +51,19 @@ impl LoginResponse {
5151
}
5252

5353
fn from_base_response(r: _LoginResponse) -> Result<LoginResponse, AzureError> {
54-
let expires_on: i64 = r.expires_on.parse()?;
55-
let expires_on: DateTime<Utc> = Utc.timestamp(expires_on, 0);
56-
57-
let not_before: i64 = r.not_before.parse()?;
58-
let not_before: DateTime<Utc> = Utc.timestamp(not_before, 0);
54+
let expires_on: Option<DateTime<Utc>> = match r.expires_on {
55+
Some(d) => Some(Utc.timestamp(d.parse()?, 0)),
56+
None => None,
57+
};
58+
let not_before: Option<DateTime<Utc>> = match r.not_before {
59+
Some(d) => Some(Utc.timestamp(d.parse()?, 0)),
60+
None => None,
61+
};
5962

6063
Ok(LoginResponse {
6164
token_type: r.token_type,
62-
expires_in: r.expires_in.parse()?,
63-
ext_expires_in: r.ext_expires_in.parse()?,
65+
expires_in: r.expires_in,
66+
ext_expires_in: r.ext_expires_in,
6467
expires_on,
6568
not_before,
6669
resource: r.resource,

0 commit comments

Comments
 (0)