chore(deps): update wiki digest to dc8e632
[ArchiSteamFarm.git] / ArchiSteamFarm.OfficialPlugins.MobileAuthenticator / MobileAuthenticatorHandler.cs
blobaa7dfdf97987ac9c7cbd2db0ddabbe5121ea0572
1 // ----------------------------------------------------------------------------------------------
2 // _ _ _ ____ _ _____
3 // / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
4 // / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
5 // / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
6 // /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
7 // ----------------------------------------------------------------------------------------------
8 // |
9 // Copyright 2015-2024 Ɓukasz "JustArchi" Domeradzki
10 // Contact: JustArchi@JustArchi.net
11 // |
12 // Licensed under the Apache License, Version 2.0 (the "License");
13 // you may not use this file except in compliance with the License.
14 // You may obtain a copy of the License at
15 // |
16 // http://www.apache.org/licenses/LICENSE-2.0
17 // |
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the License is distributed on an "AS IS" BASIS,
20 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 // See the License for the specific language governing permissions and
22 // limitations under the License.
24 using System;
25 using System.Threading.Tasks;
26 using ArchiSteamFarm.Core;
27 using ArchiSteamFarm.NLog;
28 using SteamKit2;
29 using SteamKit2.Internal;
31 namespace ArchiSteamFarm.OfficialPlugins.MobileAuthenticator;
33 internal sealed class MobileAuthenticatorHandler : ClientMsgHandler {
34 private readonly ArchiLogger ArchiLogger;
35 private readonly TwoFactor UnifiedTwoFactorService;
37 internal MobileAuthenticatorHandler(ArchiLogger archiLogger, SteamUnifiedMessages steamUnifiedMessages) {
38 ArgumentNullException.ThrowIfNull(archiLogger);
39 ArgumentNullException.ThrowIfNull(steamUnifiedMessages);
41 ArchiLogger = archiLogger;
42 UnifiedTwoFactorService = steamUnifiedMessages.CreateService<TwoFactor>();
45 public override void HandleMsg(IPacketMsg packetMsg) => ArgumentNullException.ThrowIfNull(packetMsg);
47 internal async Task<CTwoFactor_AddAuthenticator_Response?> AddAuthenticator(ulong steamID, string deviceID) {
48 if ((steamID == 0) || !new SteamID(steamID).IsIndividualAccount) {
49 throw new ArgumentOutOfRangeException(nameof(steamID));
52 ArgumentException.ThrowIfNullOrEmpty(deviceID);
54 if (Client == null) {
55 throw new InvalidOperationException(nameof(Client));
58 if (!Client.IsConnected) {
59 return null;
62 CTwoFactor_AddAuthenticator_Request request = new() {
63 authenticator_type = 1,
64 authenticator_time = Utilities.GetUnixTime(),
65 device_identifier = deviceID,
66 steamid = steamID
69 SteamUnifiedMessages.ServiceMethodResponse<CTwoFactor_AddAuthenticator_Response> response;
71 try {
72 response = await UnifiedTwoFactorService.AddAuthenticator(request).ToLongRunningTask().ConfigureAwait(false);
73 } catch (Exception e) {
74 ArchiLogger.LogGenericWarningException(e);
76 return null;
79 return response.Result == EResult.OK ? response.Body : null;
82 internal async Task<CTwoFactor_FinalizeAddAuthenticator_Response?> FinalizeAuthenticator(ulong steamID, string activationCode, string authenticatorCode, ulong authenticatorTime) {
83 if ((steamID == 0) || !new SteamID(steamID).IsIndividualAccount) {
84 throw new ArgumentOutOfRangeException(nameof(steamID));
87 ArgumentException.ThrowIfNullOrEmpty(activationCode);
88 ArgumentException.ThrowIfNullOrEmpty(authenticatorCode);
89 ArgumentOutOfRangeException.ThrowIfNegativeOrZero(authenticatorTime);
91 if (Client == null) {
92 throw new InvalidOperationException(nameof(Client));
95 if (!Client.IsConnected) {
96 return null;
99 CTwoFactor_FinalizeAddAuthenticator_Request request = new() {
100 activation_code = activationCode,
101 authenticator_code = authenticatorCode,
102 authenticator_time = authenticatorTime,
103 steamid = steamID
106 SteamUnifiedMessages.ServiceMethodResponse<CTwoFactor_FinalizeAddAuthenticator_Response> response;
108 try {
109 response = await UnifiedTwoFactorService.FinalizeAddAuthenticator(request).ToLongRunningTask().ConfigureAwait(false);
110 } catch (Exception e) {
111 ArchiLogger.LogGenericWarningException(e);
113 return null;
116 return response.Result == EResult.OK ? response.Body : null;