hanskorg / google-authenticator-rust

GoogleAuthenticator Rust google两步验证生成器
MIT License
47 stars 13 forks source link

Replace deprecated Google Chart API for QR code generation #16

Open ivanshyu opened 3 weeks ago

ivanshyu commented 3 weeks ago

Description

The qr_code_url method in GoogleAuthenticator struct is currently using the Google Chart API (https://chart.googleapis.com/chart) to generate QR codes. However, this API has been officially deprecated by Google and is no longer supported or maintained.

As per Google's announcement:

"The Image Charts API has been officially deprecated as of March 14, 2019 and turned down on March 14, 2023."

This means that the QR code generation functionality is now broken and needs to be updated to use an alternative method.

Current Implementation

authenticator.rs (lines 249-271)

    pub fn qr_code_url(
        &self,
        secret: &str,
        name: &str,
        title: &str,
        width: u32,
        height: u32,
        level: ErrorCorrectionLevel,
    ) -> String {
        let width = if width == 0 { 200 } else { width };
        let height = if height == 0 { 200 } else { height };

        // Scheme will be a url to the totp we will use. Since it is a query parameter of the final
        // url, it must be percent encoded. In turn, this means that `name` and `title` must be
        // percent encoded twice for the final url to work if they contain characters that are not
        // allowed in urls.
        let scheme = Self::create_scheme(name, secret, title);
        let scheme = utf8_percent_encode(&scheme, NON_ALPHANUMERIC);
        format!(
            "https://chart.googleapis.com/chart?chs={}x{}&chld={}|0&cht=qr&chl={}",
            width, height, level, scheme
        )
    }

Questions

camsjams commented 6 days ago

Also it looks like https://quickchart.io/chart is mostly a drop in replacement for https://chart.googleapis.com/chart.

I did a replace on https://chart.googleapis.com/chart=>https://quickchart.io/chart in the generated URL and it works, mostly the same aside from slightly different formatting style.