pub async fn auth_flow(
    client: &ReqwestClient,
    creds: &Credentials,
    mut parameters: HashMap<String, String>,
) -> Result<HeaderMap> {
    let realm = parameters
        .remove("realm")
        .ok_or_else(|| AuthError::AuthServerUri)?;

    let (user, pass) = match creds {
        Credentials::UserPass(user, pass) => (user, pass),
        _ => return Err(AuthError::InvalidCredentials.into()),
    };

    parameters.insert("grant_type".to_string(), "password".to_string());
    parameters.insert("client_id".to_string(), "containrs".to_string());
    parameters.insert("username".to_string(), user.to_string());
    parameters.insert("password".to_string(), pass.to_string());

    let req = client.post(realm.as_str()).form(&parameters);
    trace!("Oauth2 server req: {:#?}", req);
    let res = req.send().await.context(AuthError::AuthServerNoResponse)?;
    trace!("Oauth2 server res: {:#?}", res);

    if !res.status().is_success() {
        match res.status() {
            StatusCode::UNAUTHORIZED => return Err(AuthError::InvalidCredentials.into()),
            status => {
                debug!("Unexpected response: {:#?}", res);
                debug!("Unexpected response content: {:#?}", res.text().await);
                return Err(AuthError::AuthServerError(status).into());
            }
        }
    }

    let token_reponse: TokenResponse = res
        .json()
        .await
        .context(AuthError::AuthServerInvalidResponse)?;

    // TODO: use those expiration times

    let token = token_reponse
        .access_token
        .ok_or_else(|| AuthError::AuthServerMissingToken)?;

    let mut map = HeaderMap::new();
    map.typed_insert(
        AuthorizationHeader::bearer(&token).context(AuthError::AuthServerInvalidToken)?,
    );
    Ok(map)
}
/// OAuth2 username-password auth flow handler
OAuth2 username-password auth flow handler
OAuth2 username - password auth flow handler
pub async fn auth_flow(
    client: &ReqwestClient,
    creds: &Credentials,
    mut parameters: HashMap<String, String>,
) -> Result<HeaderMap> {
    let realm = parameters
        .remove("realm")
        .ok_or_else(|| AuthError::AuthServerUri)?;
    let (user, pass) = match creds {
        Credentials::UserPass(user, pass) => (user, pass),
        _ => return Err(AuthError::InvalidCredentials.into()),
    };
    parameters.insert("grant_type".to_string(), "password".to_string());
    parameters.insert("client_id".to_string(), "containrs".to_string());
    parameters.insert("username".to_string(), user.to_string());
    parameters.insert("password".to_string(), pass.to_string());
    let req = client.post(realm.as_str()).form(&parameters);
    trace!("Oauth2 server req: {:#?}", req);
    let res = req.send().await.context(AuthError::AuthServerNoResponse)?;
    trace!("Oauth2 server res: {:#?}", res);
    if !res.status().is_success() {
        match res.status() {
            StatusCode::UNAUTHORIZED => return Err(AuthError::InvalidCredentials.into()),
            status => {
                debug!("Unexpected response: {:#?}", res);
                debug!("Unexpected response content: {:#?}", res.text().await);
                return Err(AuthError::AuthServerError(status).into());
            }
        }
    }
    let token_reponse: TokenResponse = res
        .json()
        .await
        .context(AuthError::AuthServerInvalidResponse)?;
    // TODO: use those expiration times
    let token = token_reponse
        .access_token
        .ok_or_else(|| AuthError::AuthServerMissingToken)?;
    let mut map = HeaderMap::new();
    map.typed_insert(
        AuthorizationHeader::bearer(&token).context(AuthError::AuthServerInvalidToken)?,
    );
    Ok(map)
}
pub async fn auth_flow ( client : & ReqwestClient , creds : & Credentials , mut parameters : HashMap < String , String > , ) -> Result < HeaderMap > { let realm = parameters . remove ( " " ) . ok_or_else ( | | AuthError :: AuthServerUri ) ? ; let ( user , pass ) = match creds { Credentials :: UserPass ( user , pass ) => ( user , pass ) , _ => return Err ( AuthError :: InvalidCredentials . into ( ) ) , } ; parameters . insert ( " " . to_string ( ) , " " . to_string ( ) ) ; parameters . insert ( " " . to_string ( ) , " " . to_string ( ) ) ; parameters . insert ( " " . to_string ( ) , user . to_string ( ) ) ; parameters . insert ( " " . to_string ( ) , pass . to_string ( ) ) ; let req = client . post ( realm . as_str ( ) ) . form ( & parameters ) ; trace ! ( " " req ) ; let res = req . send ( ) . await . context ( AuthError :: AuthServerNoResponse ) ? ; trace ! ( " " res ) ; if ! res . status ( ) . is_success ( ) { match res . status ( ) { StatusCode :: UNAUTHORIZED => return Err ( AuthError :: InvalidCredentials . into ( ) ) , status => { debug ! ( " " res ) ; debug ! ( " " res text ( ) await ) ; return Err ( AuthError :: AuthServerError ( status ) . into ( ) ) ; } } } let token_reponse : TokenResponse = res . json ( ) . await . context ( AuthError :: AuthServerInvalidResponse ) ? ; // TODO: use those expiration times let token = token_reponse . access_token . ok_or_else ( | | AuthError :: AuthServerMissingToken ) ? ; let mut map = HeaderMap :: new ( ) ; map . typed_insert ( AuthorizationHeader :: bearer ( & token ) . context ( AuthError :: AuthServerInvalidToken ) ? , ) ; Ok ( map ) }
OAuth2 username-password auth flow handler
OAuth2 username - password auth flow handler
client
&ReqwestClient
creds
&Credentials
parameters
HashMap<String, String>
client
&ReqwestClient
creds
&Credentials
parameters
HashMap<String, String>