Repository: juspay/hyperswitch
File: crates/router/src/core/user.rs (Rust)

Code before:
    }

    let now = common_utils::date_time::now();
    state
        .store
        .insert_user_authentication_method(UserAuthenticationMethodNew {
            id: id_for_response.clone(),
            auth_id: auth_id.clone(),
            owner_id: req.owner_id.clone(),
            owner_type: req.owner_type,
            auth_type: (&req.auth_method).foreign_into(),
            private_config,
            public_config,
            allow_signup: req.allow_signup,
            created_at: now,
            last_modified_at: now,
            email_domain: email_domain.clone(),
        })
        .await
        .to_duplicate_response(UserErrors::UserAuthMethodAlreadyExists)?;

    let auth_type = match &req.auth_method {
        user_api::AuthConfig::OpenIdConnect { .. } => "openid_connect".to_string(),
        user_api::AuthConfig::MagicLink => "magic_link".to_string(),
        user_api::AuthConfig::Password => "password".to_string(),
    };

    let response: Vec<user_api::CreateUserAuthenticationMethodResponse> =
        vec![user_api::CreateUserAuthenticationMethodResponse {
            id: id_for_response,
            auth_id: auth_id_for_response,
            owner_id: req.owner_id,
            owner_type: req.owner_type,
            auth_type,
            email_domain: Some(email_domain_for_response),
            allow_signup: req.allow_signup,
        }];

    Ok(ApplicationResponse::Json(response))
}

pub async fn update_user_authentication_method(
    state: SessionState,
    req: user_api::UpdateUserAuthenticationMethodRequest,
) -> UserResponse<()> {
    let user_auth_encryption_key = hex::decode(
        state
            .conf
            .user_auth_methods
            .get_inner()
            .encryption_key

Review comment (security):
we can use `auth type = (&req.auth_method).foreign_into(),` before insert_user_authentication_method, and use the same variable in insert and response as well. Please check if we can leverage that.

Code after:
            owner_id: req.owner_id,
            owner_type: req.owner_type,
            auth_type: (&req.auth_method).foreign_into(),
            private_config,
            public_config,
            allow_signup: req.allow_signup,
            created_at: now,
            last_modified_at: now,
            email_domain,
        })
        .await
        .to_duplicate_response(UserErrors::UserAuthMethodAlreadyExists)?;

    Ok(ApplicationResponse::Json(
        user_api::CreateUserAuthenticationMethodResponse {
            id: inserted_auth_method.id,
            auth_id: inserted_auth_method.auth_id,
            owner_id: inserted_auth_method.owner_id,
            owner_type: inserted_auth_method.owner_type,
            auth_type: inserted_auth_method.auth_type,
            email_domain: Some(inserted_auth_method.email_domain),
            allow_signup: inserted_auth_method.allow_signup,
        },
    ))
}

pub async fn update_user_authentication_method(
    state: SessionState,
    req: user_api::UpdateUserAuthenticationMethodRequest,
) -> UserResponse<()> {
    let user_auth_encryption_key = hex::decode(
        state
            .conf
            .user_auth_methods
            .get_inner()
            .encryption_key
            .clone()
            .expose(),
    )
    .change_context(UserErrors::InternalServerError)
    .attach_printable("Failed to decode DEK")?;

    match req {
        user_api::UpdateUserAuthenticationMethodRequest::AuthMethod {
            id,
            auth_config: auth_method,
        } => {
            let (private_config, public_config) =
                utils::user::construct_public_and_private_db_configs(
                    &state,
                    &auth_method,