1 // ----------------------------------------------------------------------------------------------
3 // / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
4 // / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
5 // / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
6 // /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
7 // ----------------------------------------------------------------------------------------------
9 // Copyright 2015-2024 Ćukasz "JustArchi" Domeradzki
10 // Contact: JustArchi@JustArchi.net
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
16 // http://www.apache.org/licenses/LICENSE-2.0
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.
25 using System
.Threading
.Tasks
;
26 using ArchiSteamFarm
.Core
;
27 using ArchiSteamFarm
.NLog
;
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
);
55 throw new InvalidOperationException(nameof(Client
));
58 if (!Client
.IsConnected
) {
62 CTwoFactor_AddAuthenticator_Request request
= new() {
63 authenticator_type
= 1,
64 authenticator_time
= Utilities
.GetUnixTime(),
65 device_identifier
= deviceID
,
69 SteamUnifiedMessages
.ServiceMethodResponse
<CTwoFactor_AddAuthenticator_Response
> response
;
72 response
= await UnifiedTwoFactorService
.AddAuthenticator(request
).ToLongRunningTask().ConfigureAwait(false);
73 } catch (Exception e
) {
74 ArchiLogger
.LogGenericWarningException(e
);
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
);
92 throw new InvalidOperationException(nameof(Client
));
95 if (!Client
.IsConnected
) {
99 CTwoFactor_FinalizeAddAuthenticator_Request request
= new() {
100 activation_code
= activationCode
,
101 authenticator_code
= authenticatorCode
,
102 authenticator_time
= authenticatorTime
,
106 SteamUnifiedMessages
.ServiceMethodResponse
<CTwoFactor_FinalizeAddAuthenticator_Response
> response
;
109 response
= await UnifiedTwoFactorService
.FinalizeAddAuthenticator(request
).ToLongRunningTask().ConfigureAwait(false);
110 } catch (Exception e
) {
111 ArchiLogger
.LogGenericWarningException(e
);
116 return response
.Result
== EResult
.OK
? response
.Body
: null;