Skip to content

Commit

Permalink
follow rules by ITC to obtain the ac_id
Browse files Browse the repository at this point in the history
Resolves #11

Co-authored-by: CptKKKK <[email protected]>
  • Loading branch information
YDX-2147483647 and CptKKKK committed Apr 10, 2024
1 parent 2d5ee29 commit ac08178
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub const SRUN_PORTAL: &str = "http://10.0.0.55";
pub const SRUN_TYPE: &str = "1";
pub const SRUN_N: &str = "200";

/// Arbitrary HTTP site for srun to redirect
pub const ARBITRARY_HTTP_SITE: &str = "http://www.bit.edu.cn";

/// The response from the `/rad_user_info` endpoint
///
/// This response is used to determine if the device is logged in or not, and if it is logged in,
Expand Down Expand Up @@ -128,12 +131,12 @@ pub async fn get_login_state(client: &Client, verbose: bool) -> Result<SrunLogin
Ok(parsed_json)
}

/// Get the ac_id of the current device
async fn get_acid(client: &Client) -> Result<String> {
let resp = client.get(SRUN_PORTAL).send().await.with_context(|| {
/// Get the ac_id of the current device by visiting a URL
async fn get_acid_by_url(client: &Client, url: &str) -> Result<String> {
let resp = client.get(url).send().await.with_context(|| {
format!(
"failed to get ac_id from `{}`",
SRUN_PORTAL.if_supports_color(Stdout, |t| t.underline())
url.if_supports_color(Stdout, |t| t.underline())
)
})?;
let redirect_url = resp.url().to_string();
Expand All @@ -154,6 +157,17 @@ async fn get_acid(client: &Client) -> Result<String> {
Ok(ac_id.1)
}

/// Get the ac_id of the current device
async fn get_acid(client: &Client) -> Result<String> {
// Visit an arbitrary HTTP site, and let it redirect to `SRUN_PORTAL` with ac_id.
// This is due to the double authentication mechanism, officially documented by ITC.
// https://itc.bit.edu.cn/fwzn/zxbl/f2c0c8e939ce4e9cace880d5403fe4b5.htm
get_acid_by_url(client, ARBITRARY_HTTP_SITE)
.await
// Fall back to visit `SRUN_PORTAL` directly if already logged in.
.or(get_acid_by_url(client, SRUN_PORTAL).await)
}

/// SRUN portal response type when calling login/logout
///
/// Note that fields that are not used are omitted
Expand Down

0 comments on commit ac08178

Please sign in to comment.