Skip to content

Add new module easy for helper functions#25

Merged
nakaryo716 merged 2 commits into
mainfrom
easy
Jul 7, 2025
Merged

Add new module easy for helper functions#25
nakaryo716 merged 2 commits into
mainfrom
easy

Conversation

@nakaryo716

Copy link
Copy Markdown
Owner

What new?

A new module easy is added. It include helper functions, such as generate_auth_redirect() and create_id_token_request().

Purpose

Previously, It was difficult to construct CodeRequest and IDTokenRequest directly. These functions allow more convenient usage and reduce boilerplate code.

Detailes

  • Added new module easy
  • Implemented helper functions:
    • generate_auth_redirect()
    • create_id_token_request()
  • These functions allow side effect(generate CSRFToken, Nonce) and make it easier to construct requests

Usage Example

async fn login(
    State(app_state): State<Arc<AppState>>,
    jar: CookieJar,
) -> Result<impl IntoResponse, AppError> {
    let (csrf_token, _nonce, redirect_uri) = generate_auth_redirect(
        &app_state.oidc_cfg,
        AccessType::Online,
        AdditionalScope::Both,
    )
    .map_err(WrapOIDCError)?;

    let csrf_token_store_key = Uuid::new_v4();
    app_state.insert(&csrf_token_store_key.to_string(), csrf_token.value());

    // Create cookie that is holding a key which indicate csrf_token_value
    let mut cookie = Cookie::new(COOKIE_KEY, csrf_token_store_key.to_string());
    cookie.set_http_only(true);
    set_cookie_expires(&mut cookie, Duration::minutes(5));

    // Set-Cookie ant Redirect as a response
    Ok((jar.add(cookie), Redirect::to(&redirect_uri)))
}

async fn callback(
    State(app_state): State<Arc<AppState>>,
    jar: CookieJar,
    req: Request,
) -> Result<impl IntoResponse, AppError> {
    let csrf_token_key = get_csrf_token_key_from_cookie(&jar)?;
    // fetch csrf_token_val by using key that you got from cookie
    let csrf_token_val = app_state
        .get(&csrf_token_key)
        .ok_or(AppError::GenURL)?
        .to_owned();

    let id_token_req = create_id_token_request(&app_state.oidc_cfg, &csrf_token_val, req)
        .map_err(WrapOIDCError)?;

    // Send request to google
    // This is effect
    let id_token_res = send_id_token_req(&id_token_req)
        .await
        .map_err(WrapOIDCError)?;

    // print IDTokenResponse
    // It has some value, such as refresh_token, access_token, etc...
    println!("----IDTokenResponse----");
    println!("{id_token_res:#?}");

    // encode IDToken from raw string
    let id_token = IDToken::from_id_token_raw(id_token_res.id_token()).map_err(WrapOIDCError)?;

    // print id_token
    println!("----IDToken----");
    println!("{id_token:#?}");

    Ok((StatusCode::OK, "login success"))
}

Other

  • No breaking changes
  • No related issues

Previously, the process of obtaining an `IDTokne` was declarative, but
required many steps, making the code hard to write and maintain.
The new `easy` module provides convenient helper functions that combine
the plublic API to reduce boilerplate and simplify usage.
@nakaryo716 nakaryo716 marked this pull request as ready for review July 7, 2025 13:21
@nakaryo716 nakaryo716 merged commit 1c416b0 into main Jul 7, 2025
1 check passed
@nakaryo716 nakaryo716 deleted the easy branch July 7, 2025 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant