Ejemplo n.º 1
0
        public Task SetTwoFactorAuthDataAsync(TUser user, TwoFactorAuthData data)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            user.HashedTwoFactorAuthCode     = data.HashedCode;
            user.DateTwoFactorAuthCodeIssued = data.DateIssued;

            return(Task.FromResult(0));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Login to Unity Publisher Portal.
        /// </summary>
        /// <param name="username">Email or username</param>
        /// <param name="password">Password</param>
        /// <param name="tfaResumeData">Two-factor auth data previously obtained</param>
        /// <param name="tfaCode">Two-factor auth code</param>
        /// <returns>Returns LoginResult with current status of login and Two-factor data if Unity Publisher Portal need this.</returns>
        public async Task <LoginResult> LoginAsync(string username, string password, TwoFactorAuthData tfaResumeData = null, string tfaCode = null)
        {
            if (IsLoggedIn)
            {
                return(new LoginResult()
                {
                    AccessToken = _accessToken,
                    Status = LoginStatus.Success,
                });
            }

            var result = await GetLoginTokenAsync(username, password, tfaResumeData, tfaCode);

            if (result.Status == LoginStatus.Success)
            {
                LoginWithTokenAsync(result.AccessToken);
            }

            return(result);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Login to Unity Publisher Portal with TFA data specified.
 /// </summary>
 /// <param name="tfaResumeData">Two-factor auth data previously obtained</param>
 /// <param name="tfaCode">Two-factor auth code</param>
 /// <returns>Returns LoginResult with current status of login.</returns>
 public async Task <LoginResult> LoginAsync(TwoFactorAuthData tfaResumeData, string tfaCode)
 {
     return(await LoginAsync(null, null, tfaResumeData, tfaCode));
 }
Ejemplo n.º 4
0
        private async Task <LoginResult> GetLoginTokenAsync(string username, string password, TwoFactorAuthData tfaResumeData, string tfaCode)
        {
            var cookieContainer = new CookieContainer();

            using (var httpClient = CreateHttpClient(null, cookieContainer))
            {
                string pageData;

                if (tfaResumeData == null)
                {
                    var response = await httpClient.GetAsync(ClientConsts.SALES_URL);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new UnityPublisherApiException($"Login failed, error code {response.StatusCode}", response.StatusCode);
                    }

                    var redirectUrl  = response.RequestMessage.RequestUri;
                    var genesisToken = GetCookie(cookieContainer, redirectUrl, ClientConsts.GENESIS_COOKIE_NAME);

                    if (string.IsNullOrWhiteSpace(genesisToken))
                    {
                        throw new UnityPublisherApiException($"{ClientConsts.GENESIS_COOKIE_NAME} cookie not found", response.StatusCode);
                    }

                    pageData = await response.Content.ReadAsStringAsync();

                    var authTokenRegex   = new Regex("<input type=\"hidden\" name=\"authenticity_token\" value=\"(.+)\" />");
                    var authTokenMatches = authTokenRegex.Matches(pageData);
                    var authToken        = authTokenMatches.Count > 0 ? authTokenMatches[0].Groups[1].Value : null;

                    if (string.IsNullOrWhiteSpace(authToken))
                    {
                        throw new UnityPublisherApiException($"Page authenticity token not found");
                    }

                    response = await httpClient.PostAsync(redirectUrl, new FormUrlEncodedContent(new Dictionary <string, string>()
                    {
                        { "utf8", "✓" },
                        { "_method", "put" },
                        { "authenticity_token", authToken },
                        { "conversations_create_session_form[email]", username },
                        { "conversations_create_session_form[password]", password },
                        { "conversations_create_session_form[remember_me]", true.ToString() },
                        { "commit", "Log in" },
                    }));

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new UnityPublisherApiException($"Conversation failed, error code {response.StatusCode}", response.StatusCode);
                    }

                    pageData = await response.Content.ReadAsStringAsync();

                    if (pageData.Contains("conversations_tfa_required_form[verify_code]"))
                    {
                        var tfaActionRegex   = new Regex("id=\"new_conversations_tfa_required_form\" action=\"(.+?)\"");
                        var tfaActionMatches = tfaActionRegex.Matches(pageData);
                        var tfaActionUrl     = tfaActionMatches.Count > 0 ? tfaActionMatches[0].Groups[1].Value : null;

                        return(new LoginResult()
                        {
                            TfaResumeData = new TwoFactorAuthData()
                            {
                                ActionUrl = ClientConsts.ID_UNITY_URL + tfaActionUrl,
                                GenesisToken = genesisToken,
                                AuthenticityToken = authToken
                            },
                            Status = LoginStatus.TFA
                        });
                    }
                }
                else
                {
                    SetCookie(cookieContainer, new Uri(ClientConsts.ID_UNITY_URL), ClientConsts.GENESIS_COOKIE_NAME, tfaResumeData.GenesisToken);

                    var response = await httpClient.PostAsync(tfaResumeData.ActionUrl, new FormUrlEncodedContent(new Dictionary <string, string>()
                    {
                        { "utf8", "✓" },
                        { "_method", "put" },
                        { "authenticity_token", tfaResumeData.AuthenticityToken },
                        { "conversations_tfa_required_form[verify_code]", tfaCode },
                        { "conversations_tfa_required_form[submit_verify_code]", "Verify" },
                    }));

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new UnityPublisherApiException($"TFA conversation failed, error code {response.StatusCode}", response.StatusCode);
                    }

                    pageData = await response.Content.ReadAsStringAsync();
                }

                var bounceRegex   = new Regex("window\\.location\\.href \\= \"(.+)\"");
                var bounceMatches = bounceRegex.Matches(pageData);
                var bounceUrl     = bounceMatches.Count > 0 ? bounceMatches[0].Groups[1].Value : null;

                var bounceResponse = await httpClient.GetAsync(bounceUrl);

                if (!bounceResponse.IsSuccessStatusCode || string.IsNullOrWhiteSpace(bounceUrl))
                {
                    throw new UnityPublisherApiException($"Bounce failed, error code {bounceResponse.StatusCode}", bounceResponse.StatusCode);
                }

                var bounceUri = new Uri(bounceUrl);

                var kharmaToken = GetCookie(cookieContainer, bounceUri, ClientConsts.TOKEN_COOKIE_NAME);

                if (string.IsNullOrWhiteSpace(kharmaToken))
                {
                    throw new UnityPublisherApiException($"{ClientConsts.TOKEN_COOKIE_NAME} cookie not found");
                }

                var kharmaSession = GetCookie(cookieContainer, bounceUri, ClientConsts.SESSION_COOKIE_NAME);

                if (string.IsNullOrWhiteSpace(kharmaSession))
                {
                    throw new UnityPublisherApiException($"{ClientConsts.SESSION_COOKIE_NAME} cookie not found");
                }

                return(new LoginResult()
                {
                    AccessToken = $"{kharmaToken}.{kharmaSession}",
                    Status = LoginStatus.Success
                });
            }
        }