Exemplo n.º 1
0
        /// <summary>
        /// Deletes the given key. It deletes the <see cref="GenerateStoredKey"/> named file in <see cref="FolderPath"/>.
        /// </summary>
        /// <param name="key">The key to delete from the data store</param>
        public Task DeleteAsync <T>(string key)
        {
            var companyGuid     = ExtractCompanyGuid(key);
            var externalApiAuth = _externalApiAuthManager.GetByCompanyGuid(companyGuid, ExternalApi.GoogleAnalytics.Id);

            if (externalApiAuth != null)
            {
                if (key.StartsWith("oauth_"))
                {
                    externalApiAuth.Key = string.Empty;
                }
                else
                {
                    externalApiAuth.Token = string.Empty;
                }

                if (string.IsNullOrWhiteSpace(externalApiAuth.Key) && string.IsNullOrWhiteSpace(externalApiAuth.Token))
                {
                    _externalApiAuthManager.Delete(externalApiAuth.Id);
                }
                else
                {
                    _externalApiAuthManager.Update(externalApiAuth);
                }
            }

            return(Task.Delay(0));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Connect(Guid uniqueId, int externalApiId)
        {
            var externalApi     = ExternalApi.ChartMogul.GetById(externalApiId);
            var externalApiAuth = new ExternalApiAuth {
                ExternalApiId = externalApiId
            };

            // Replace with requested object
            if (uniqueId != Guid.Empty)
            {
                externalApiAuth = _externalApiAuthManager.GetExternalApiAuth(CompanyId, uniqueId);
            }

            switch (externalApi.ApiAuthorizationType)
            {
            case ExternalApiAuthorizationType.OAuth20:
                // Hard coded to Google for now
                var company = _companyManager.Get(CompanyId);
                Session["CompanyGuid"] = company.UniqueId.ToString();
                var authResult = await _googleAuthorizer.Authorize(company.UniqueId, externalApiAuth, Url.Action("SetGoogleAnalyticsSiteId"), CancellationToken.None);

                if (authResult.Credential == null)
                {
                    return(View("_startOAuth", new OAuthStartAuthorisation {
                        ExternalApi = externalApi, StartUrl = authResult.RedirectUri
                    }));
                }

                try
                {
                    var accountSummaries = await _googleAuthorizer.GetAccountSummaries(authResult);

                    if (string.IsNullOrWhiteSpace(externalApiAuth.ConfigData))
                    {
                        // Default to first profile
                        externalApiAuth.ConfigData = accountSummaries.items[0].webProperties[0].profiles[0].id;
                        _externalApiAuthManager.Update(externalApiAuth);
                    }
                    return(View("_manageOAuth", new GoogleOAuthConfigView {
                        ExternalApi = externalApi, ExternalApiAuth = externalApiAuth, Accounts = accountSummaries
                    }));
                }
                catch
                {
                    _externalApiAuthManager.Delete(externalApiAuth.Id);
                    return(Content("We couldn't retrieve your Google Analytics site list.\nTry refreshing the page and re-connecting to Google Analytics"));
                }

            default:
                return(View("_connect", externalApiAuth));
            }
        }