use std::sync::Arc;
use crate::errors::Result;
use crate::provider::{Check, OAuthProvider, ProfileFn, Provider, ProviderType, UrlValues};
use crate::providers::common;
use crate::types::{Profile, TokenSet};
use crate::user::OAuthUser;
const AUTH_URL_TEMPLATE: &str = "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize";
const TOKEN_URL_TEMPLATE: &str = "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token";
const USER_INFO_URL: &str = "https://graph.microsoft.com/v1.0/me";
pub const PROVIDER_ID: &str = "azuread-v2";
pub fn default_scopes() -> Vec<String> {
vec![
"openid".into(),
"profile".into(),
"email".into(),
"User.Read".into(),
]
}
#[derive(Default)]
pub struct Options {
pub client_id: String,
pub client_secret: String,
pub tenant_id: String,
pub scopes: Vec<String>,
pub authorization_params: UrlValues,
pub profile: Option<ProfileFn>,
}
pub fn new(o: Options) -> Arc<dyn Provider> {
let tenant = if o.tenant_id.is_empty() {
"common".to_string()
} else {
o.tenant_id
};
let scopes = if o.scopes.is_empty() {
default_scopes()
} else {
o.scopes
};
let profile = o
.profile
.unwrap_or_else(|| Arc::new(default_profile) as ProfileFn);
Arc::new(OAuthProvider {
provider_id: PROVIDER_ID.to_string(),
display_name: "Azure Active Directory".to_string(),
kind: Some(ProviderType::Oidc),
issuer: format!("https://login.microsoftonline.com/{tenant}/v2.0"),
client_id: o.client_id,
client_secret: o.client_secret,
authorization_url: AUTH_URL_TEMPLATE.replace("{tenant}", &tenant),
token_url: TOKEN_URL_TEMPLATE.replace("{tenant}", &tenant),
user_info_url: USER_INFO_URL.to_string(),
scopes,
authorization_params: o.authorization_params,
checks: vec![Check::Pkce, Check::State, Check::Nonce],
profile: Some(profile),
..OAuthProvider::default()
})
}
pub fn default_profile(p: &Profile, _tokens: &TokenSet) -> Result<OAuthUser> {
Ok(OAuthUser {
id: common::first_non_empty([
common::string(p.get("sub")),
common::string(p.get("id")),
common::string(p.get("oid")),
]),
name: common::first_non_empty([
common::string(p.get("name")),
common::string(p.get("displayName")),
]),
email: common::first_non_empty([
common::string(p.get("email")),
common::string(p.get("mail")),
common::string(p.get("userPrincipalName")),
]),
first_name: common::string(p.get("givenName")),
last_name: common::string(p.get("surname")),
image: common::string(p.get("picture")),
provider: PROVIDER_ID.to_string(),
..OAuthUser::default()
})
}