use crate::social::core::{OAuth2Client, SocialAuthError, StandardClaims};
pub struct UserInfoClient {
client: OAuth2Client,
}
impl UserInfoClient {
pub fn new(client: OAuth2Client) -> Self {
Self { client }
}
pub async fn get_user_info(
&self,
userinfo_endpoint: &str,
access_token: &str,
) -> Result<StandardClaims, SocialAuthError> {
let response = self
.client
.client()
.get(userinfo_endpoint)
.header("User-Agent", "reinhardt-auth")
.bearer_auth(access_token)
.send()
.await
.map_err(|e| SocialAuthError::Network(e.to_string()))?;
if !response.status().is_success() {
let status = response.status();
let error_body = response
.text()
.await
.unwrap_or_else(|_| "Unknown error".to_string());
return Err(SocialAuthError::UserInfoError(format!(
"UserInfo request failed ({}): {}",
status, error_body
)));
}
let claims: StandardClaims = response
.json()
.await
.map_err(|e| SocialAuthError::UserInfoError(e.to_string()))?;
Ok(claims)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_userinfo_client_creation() {
let client = OAuth2Client::new();
let userinfo_client = UserInfoClient::new(client);
assert!(std::mem::size_of_val(&userinfo_client) > 0);
}
}