Beispiel #1
0
        /// <summary>
        ///     Returns a new instance of Authenticator class based on the description provided by the passed serialized string in
        ///     Json format
        /// </summary>
        /// <param name="serialized">The serialized representation of an Authenticator instance as string in Json format.</param>
        /// <returns>An instance of Authenticator class</returns>
        public static Authenticator DeSerialize(string serialized)
        {
            var authenticator = JsonConvert.DeserializeObject <Authenticator>(serialized);

            // Tries to fill authenticator data by searching for properties in the root of Json object
            if (authenticator.AuthenticatorData == null || !authenticator.AuthenticatorData.HasEnoughInfo())
            {
                var authenticatorData = JsonConvert.DeserializeObject <AddAuthenticatorResponse>(serialized);
                authenticator = new Authenticator(
                    authenticatorData,
                    authenticator.Session,
                    authenticator.DeviceId
                    );
            }

            // Tries to fill session data by searching for properties in the root of Json object
            if (authenticator.Session == null || !authenticator.Session.HasEnoughInfo())
            {
                var sessionData = JsonConvert.DeserializeObject <MobileSession>(serialized);
                authenticator = new Authenticator(
                    authenticator.AuthenticatorData,
                    sessionData,
                    authenticator.DeviceId
                    );
            }

            // Tries to extract steam guard machine authentication tokens
            if (authenticator.Session != null && !(authenticator.Session.SteamMachineAuthenticationTokens?.Count > 0))
            {
                var steamIdProperties = JsonConvert.DeserializeAnonymousType(
                    serialized,
                    new
                {
                    steamid  = (ulong?)null,
                    steam_id = (ulong?)null,
                    session  = new
                    {
                        steamid  = (ulong?)null,
                        steam_id = (ulong?)null
                    }
                }
                    );
                var steamId = steamIdProperties.steam_id ??
                              steamIdProperties.steamid ??
                              steamIdProperties.session.steam_id ??
                              steamIdProperties.session.steamid ?? authenticator.Session.SteamId;

                var webCookieProperties = JsonConvert.DeserializeAnonymousType(
                    serialized,
                    new
                {
                    webcookie  = (string)null,
                    web_cookie = (string)null,
                    session    = new
                    {
                        webcookie  = (string)null,
                        web_cookie = (string)null
                    }
                }
                    );
                var webCookie = webCookieProperties.web_cookie ??
                                webCookieProperties.webcookie ??
                                webCookieProperties.session.web_cookie ?? webCookieProperties.session.webcookie;

                if (steamId != null && !string.IsNullOrWhiteSpace(webCookie))
                {
                    var newSession = new MobileSession(authenticator.Session.OAuthToken, steamId,
                                                       authenticator.Session.SteamLogin, authenticator.Session.SteamLoginSecure,
                                                       authenticator.Session.SessionId, authenticator.Session.RememberLoginToken,
                                                       new Dictionary <ulong, string>
                    {
                        { steamId.Value, webCookie }
                    });

                    authenticator = new Authenticator(
                        authenticator.AuthenticatorData,
                        newSession,
                        authenticator.DeviceId
                        );
                }
            }

            // Tries to fill device identification string by searching for properties in the root of Json object
            if (string.IsNullOrWhiteSpace(authenticator.DeviceId))
            {
                var deviceIdProperties = JsonConvert.DeserializeAnonymousType(
                    serialized,
                    new
                {
                    deviceId  = (string)null,
                    device_id = (string)null,
                    device    = (string)null
                }
                    );
                authenticator = new Authenticator(
                    authenticator.AuthenticatorData,
                    authenticator.Session,
                    deviceIdProperties.device_id ?? deviceIdProperties.deviceId ?? deviceIdProperties.device
                    );
            }

            // Do we have a enough information to call this a valid instance?
            if (!authenticator.HasEnoughInfo())
            {
                return(null);
            }

            return(authenticator);
        }