Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Passkey_OAuth2-Osaki-rs-20250325.pdf

ktaka-ccmp
March 25, 2025
22

 Passkey_OAuth2-Osaki-rs-20250325.pdf

ktaka-ccmp

March 25, 2025
Tweet

Transcript

  1. OAuth2、Passkeyのおさらい Google OAuth2 • Google OAuth2認証 • ユーザーセッション作成 • クッキーセット🍪

    Passkey • パスキー認証 • ユーザーセッション作成 • クッキーセット🍪 GitHub
  2. Passkey サインイン JavaScriptがフローを制御 • 認証Start • 認証オプション作成 • チャレンジに秘密鍵でサインしてもらう •

    サーバ上の公開鍵で検証 • user lookup→セッション作成 • クッキー🍪セット 認証器 Google Password Manager、Yubikey
  3. 使い方: Axumのルーターにネスト use auth2_passkey_axum::{ O2P_ROUTE_PREFIX, //ネスト先のPathプリフィックス oauth2_passkey_router, //ルータ is_authenticated, //ログイン判別ミドルウェア

    }; let app = Router::new() .route("/", get(index)) .route("/protected", get(handler1)) .route("/protected2", get(handler2).route_layer(from_fn(is_authenticated))) .nest(O2P_ROUTE_PREFIX.as_str(), oauth2_passkey_router()); nestすれば使えるようになる
  4. ページ保護:handlerの引数で(2) let app = Router::new() .route("/protected", get(handler1)) //userの有りなしで動作を変える。 async fn

    handler1(user: Option<User>) -> impl IntoResponse { match user { Some(u) => Html(format!("Hey {}!", u.account)), None => Html("Anonymous user!".to_string()), } } GitHub
  5. middlewareでページ保護 let app = Router::new() .route("/protected2", get(handler2).route_layer(from_fn(is_authenticated))) //handerの引数のuserは不要 async fn

    handler2() -> ... pub async fn is_authenticated(req: Request, next: Next) -> impl IntoResponse { match check_session(req.headers()).await { Ok(true) => next.run(req).await, Ok(false) | Err(_) => (StatusCode::UNAUTHORIZED, "Unauthorized").into_response(), } } 有効なセッションがあれば next、無け れば401 Unauthorized Redirectにしても良い Redirect::temporary(url).into_response()