#[derive(Debug, PartialEq)]
pub enum IdentityProvider {
Microsoft {
tenant_id: String,
},
Auth0 {
domain: String,
audience: String,
},
}
impl IdentityProvider {
pub fn auth_url(&self) -> String {
match self {
Self::Microsoft { tenant_id } => format!(
"https://login.microsoftonline.com/{}/oauth2/v2.0/authorize",
tenant_id
),
Self::Auth0 { domain, .. } => format!("https://{}/authorize", domain),
}
}
pub fn token_url(&self) -> String {
match self {
Self::Microsoft { tenant_id } => format!(
"https://login.microsoftonline.com/{}/oauth2/v2.0/token",
tenant_id
),
Self::Auth0 { domain, .. } => format!("https://{}/oauth/token", domain),
}
}
pub fn audience(&self) -> Option<&str> {
match self {
Self::Microsoft { .. } => None,
Self::Auth0 { audience, .. } => Some(audience),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_microsoft_endpoints() {
let provider = IdentityProvider::Microsoft {
tenant_id: "common".to_string(),
};
assert_eq!(
provider.auth_url(),
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
);
assert_eq!(
provider.token_url(),
"https://login.microsoftonline.com/common/oauth2/v2.0/token"
);
assert_eq!(provider.audience(), None);
}
#[test]
fn test_microsoft_endpoints_with_guid() {
let provider = IdentityProvider::Microsoft {
tenant_id: "1234567891011121314".to_string(),
};
assert_eq!(
provider.auth_url(),
"https://login.microsoftonline.com/1234567891011121314/oauth2/v2.0/authorize"
);
assert_eq!(
provider.token_url(),
"https://login.microsoftonline.com/1234567891011121314/oauth2/v2.0/token"
);
}
#[test]
fn test_auth0_endpoints() {
let provider = IdentityProvider::Auth0 {
domain: "my-org.eu.auth0.com".to_string(),
audience: "api://ez-token".to_string(),
};
assert_eq!(provider.auth_url(), "https://my-org.eu.auth0.com/authorize");
assert_eq!(
provider.token_url(),
"https://my-org.eu.auth0.com/oauth/token"
);
assert_eq!(provider.audience(), Some("api://ez-token"));
}
}