Skip to main content

steamkit/steam/authentication/
credentials_auth_session.rs

1//! An authentication session started with a username and password.
2
3use std::sync::Arc;
4use std::time::Duration;
5
6use steamkit_protos::steam::{CAuthenticationAllowedConfirmation, EAuthSessionGuardType};
7use tokio_util::sync::CancellationToken;
8
9use crate::error::SteamError;
10use crate::types::steam_id::SteamID;
11
12use super::auth_poll_result::AuthPollResult;
13use super::auth_session::AuthSession;
14use super::i_authenticator::IAuthenticator;
15
16/// An authentication session started with a username and password.
17pub struct CredentialsAuthSession {
18    session: AuthSession,
19}
20
21impl std::fmt::Debug for CredentialsAuthSession {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        f.debug_struct("CredentialsAuthSession")
24            .field("session", &self.session)
25            .finish()
26    }
27}
28
29impl CredentialsAuthSession {
30    /// Creates a credentials session.
31    pub(crate) fn new(session: AuthSession) -> Self {
32        CredentialsAuthSession { session }
33    }
34
35    /// SteamID of the account logging in. Only set if the credentials were
36    /// correct.
37    pub fn steam_id(&self) -> SteamID {
38        self.session.steam_id()
39    }
40
41    /// Sends a Steam Guard code of the given type for this session.
42    pub async fn send_steam_guard_code(
43        &self,
44        code: &str,
45        code_type: EAuthSessionGuardType,
46    ) -> Result<(), SteamError> {
47        self.session.send_steam_guard_code(code, code_type).await?;
48
49        Ok(())
50    }
51
52    /// The authenticator used for 2-factor authentication, if any.
53    pub fn authenticator(&self) -> Option<&Arc<dyn IAuthenticator>> {
54        self.session.authenticator()
55    }
56
57    /// Unique id of the requestor. Also used for routing, and part of the QR code.
58    pub fn client_id(&self) -> u64 {
59        self.session.client_id()
60    }
61
62    /// Request id to present when polling.
63    pub fn request_id(&self) -> &[u8] {
64        self.session.request_id()
65    }
66
67    /// How often to call `poll_auth_session_status`.
68    pub fn polling_interval(&self) -> Duration {
69        self.session.polling_interval()
70    }
71
72    /// The confirmation types that can confirm the request.
73    pub fn allowed_confirmations(&self) -> &[CAuthenticationAllowedConfirmation] {
74        self.session.allowed_confirmations()
75    }
76
77    /// Handles any 2-factor authentication and polls until authentication
78    /// succeeds.
79    pub async fn polling_wait_for_result(
80        &self,
81        cancellation: CancellationToken,
82    ) -> Result<AuthPollResult, SteamError> {
83        self.session.polling_wait_for_result(cancellation).await
84    }
85
86    /// Polls the authentication status once.
87    pub async fn poll_auth_session_status(&self) -> Result<Option<AuthPollResult>, SteamError> {
88        self.session.poll_auth_session_status().await
89    }
90}