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

Code before:

    for db_auth_method in auth_methods {
        let is_type_same = db_auth_method.auth_type == (&req.auth_method).foreign_into();
        let is_extra_identifier_same = match &req.auth_method {
            user_api::AuthConfig::OpenIdConnect { public_config, .. } => {
                let db_auth_name = db_auth_method
                    .public_config
                    .map(|config| {
                        utils::user::parse_value::<user_api::OpenIdConnectPublicConfig>(
                            config,
                            "OpenIdConnectPublicConfig",
                        )
                    })
                    .transpose()?
                    .map(|config| config.name);
                db_auth_name.is_some_and(|name| name == public_config.name)
            }
            user_api::AuthConfig::Password | user_api::AuthConfig::MagicLink => true,
        };
        if is_type_same && is_extra_identifier_same {
            return Err(report!(UserErrors::UserAuthMethodAlreadyExists));
        }
    }

    let now = common_utils::date_time::now();
    let insert_user_authentication_method_response = state
        .store
        .insert_user_authentication_method(UserAuthenticationMethodNew {
            id,
            auth_id,
            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: insert_user_authentication_method_response.id,
            auth_id: insert_user_authentication_method_response.auth_id,
            owner_id: insert_user_authentication_method_response.owner_id,
            owner_type: insert_user_authentication_method_response.owner_type,
            auth_type: insert_user_authentication_method_response.auth_type,
            email_domain: Some(insert_user_authentication_method_response.email_domain),

Review comment (security):
The prefix `insert_` is technically correct since it holds the DB insert result, but it still feels a bit verbose in usage. Consider renaming it to something simpler like `user_auth_method` or `inserted_auth_method` for clarity.


Code after:

    for db_auth_method in auth_methods {
        let is_type_same = db_auth_method.auth_type == (&req.auth_method).foreign_into();
        let is_extra_identifier_same = match &req.auth_method {
            user_api::AuthConfig::OpenIdConnect { public_config, .. } => {
                let db_auth_name = db_auth_method
                    .public_config
                    .map(|config| {
                        utils::user::parse_value::<user_api::OpenIdConnectPublicConfig>(
                            config,
                            "OpenIdConnectPublicConfig",
                        )
                    })
                    .transpose()?
                    .map(|config| config.name);
                db_auth_name.is_some_and(|name| name == public_config.name)
            }
            user_api::AuthConfig::Password | user_api::AuthConfig::MagicLink => true,
        };
        if is_type_same && is_extra_identifier_same {
            return Err(report!(UserErrors::UserAuthMethodAlreadyExists));
        }
    }

    let now = common_utils::date_time::now();
    let inserted_auth_method = state
        .store
        .insert_user_authentication_method(UserAuthenticationMethodNew {
            id,
            auth_id,
            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),