From a3ccde76945d559c40ac3e43b2b7ef04842a7616 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Fri, 14 Jan 2022 08:17:45 +0800 Subject: [PATCH] add generate-code-only --- src/main.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/main.rs b/src/main.rs index dc6e4bb..50b435c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::env; use std::process::exit; +use std::str::FromStr; use std::time::Duration; use clap::{App, Arg}; @@ -31,6 +32,8 @@ async fn main() -> XResult<()> { .arg(Arg::new("proxy").short('p').long("proxy").takes_value(true).help("Proxy")) .arg(Arg::new("hide-qr").long("hide-qr").help("Hide QR")) .arg(Arg::new("skip-certificate-check").short('k').long("skip-certificate-check").help("Skip certificate check")) + .arg(Arg::new("generate-code-only").short('G').long("generate-code-only").help("Generate code only")) + .arg(Arg::new("generate-code-count").short('C').long("generate-code-count").takes_value(true).help("Generate code count")) .arg(Arg::new("config").short('c').long("config").takes_value(true).help("Config file")) .get_matches(); @@ -83,6 +86,24 @@ async fn main() -> XResult<()> { information!("token_endpoint: {}", &token_endpoint); information!("userinfo_endpoint: {}", &userinfo_endpoint); + let generate_code_only = matches.is_present("generate-code-only"); + if generate_code_only { + let generate_count_count = i32::from_str(matches.value_of("generate-code-count").unwrap_or("2")).unwrap_or(2); + for i in 0..generate_count_count { + debugging!("Get {} #{}", device_authorization_endpoint, i); + let mut params = HashMap::new(); + params.insert("client_id", &openid_client_configuration.client_id); + params.insert("scope", &openid_client_configuration.scope); + let device_authorization_result = opt_result!(client.post(&device_authorization_endpoint) + .form(¶ms).send().await, "Get device authorization: {}, failed: {}", device_authorization_endpoint); + let device_authorization = opt_result!( + device_authorization_result.json::().await, "Parse device authorization failed: {}"); + + success!("[{}] {}", i, device_authorization.get_complete_url()); + } + return Ok(()); + } + // xh POST https://oauth2.googleapis.com/device/code // client_id==364023986785-o5mqsqd1ej2d1vmvcqai108b7m7v7vc9.apps.googleusercontent.com scope=openid //{