Esempio n. 1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session.IsReadOnly)
            {
                SubmitError("No session is availible.", Source);
                return;
            }

            var config = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox) as DropBoxConfiguration;
            var callbackUri = new UriBuilder(Request.GetUrlRewriter());
            if (!string.IsNullOrEmpty(Request.QueryString[AuthorizationUrlKey]) && Session[RequestTokenSessionKey] != null)
            {
                //Authorization callback
                try
                {
                    var accessToken = DropBoxStorageProviderTools.ExchangeDropBoxRequestTokenIntoAccessToken(config,
                                                                                                             ImportConfiguration.DropboxAppKey,
                                                                                                             ImportConfiguration.DropboxAppSecret,
                                                                                                             Session[RequestTokenSessionKey] as DropBoxRequestToken);

                    Session[RequestTokenSessionKey] = null; //Exchanged
                    var storage = new CloudStorage();
                    var base64token = storage.SerializeSecurityTokenToBase64Ex(accessToken, config.GetType(), new Dictionary<string, string>());
                    storage.Open(config, accessToken); //Try open storage!
                    var root = storage.GetRoot();
                    if (root == null) throw new Exception();

                    SubmitToken(base64token, Source);
                }
                catch
                {
                    SubmitError("Failed to open storage with token", Source);
                }
            }
            else
            {
                callbackUri.Query += string.Format("&{0}=1", AuthorizationUrlKey);
                config.AuthorizationCallBack = callbackUri.Uri;
                // create a request token
                var requestToken = DropBoxStorageProviderTools.GetDropBoxRequestToken(config, ImportConfiguration.DropboxAppKey,
                                                                                      ImportConfiguration.DropboxAppSecret);
                var authorizationUrl = DropBoxStorageProviderTools.GetDropBoxAuthorizationUrl(config, requestToken);
                Session[RequestTokenSessionKey] = requestToken; //Store token into session!!!
                Response.Redirect(authorizationUrl);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session.IsReadOnly)
            {
                SubmitError("No session is availible.", Source);
                return;
            }

            var redirectUri = Request.GetUrlRewriter().GetLeftPart(UriPartial.Path);

            if (!string.IsNullOrEmpty(Request[AuthorizationCodeUrlKey]))
            {
                //we ready to obtain and store token
                var authCode = Request[AuthorizationCodeUrlKey];
                var accessToken = SkyDriveAuthorizationHelper.GetAccessToken(ImportConfiguration.SkyDriveAppKey,
                                                                             ImportConfiguration.SkyDriveAppSecret,
                                                                             redirectUri,
                                                                             authCode);
                
                //serialize token
                var config = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.SkyDrive);
                var storage = new CloudStorage();
                var base64AccessToken = storage.SerializeSecurityTokenToBase64Ex(accessToken, config.GetType(), new Dictionary<string, string>());

                //check and submit
                storage.Open(config, accessToken);
                var root = storage.GetRoot();
                if (root != null)
                {
                    SubmitToken(base64AccessToken, Source);
                }
                else
                {
                    SubmitError("Failed to open storage with token", Source);
                }
            }
            else
            {
                var authCodeUri = SkyDriveAuthorizationHelper.BuildAuthCodeUrl(ImportConfiguration.SkyDriveAppKey, null, redirectUri);
                Response.Redirect(authCodeUri);
            }
        }
        private static AuthData GetEncodedAccesToken(AuthData authData, string providerName)
        {
            var prName = (nSupportedCloudConfigurations) Enum.Parse(typeof (nSupportedCloudConfigurations), providerName, true);
            if (prName != nSupportedCloudConfigurations.Google)
                return authData;

            var tokenSecret = ImportConfiguration.GoogleTokenManager.GetTokenSecret(authData.Token);
            var consumerKey = ImportConfiguration.GoogleTokenManager.ConsumerKey;
            var consumerSecret = ImportConfiguration.GoogleTokenManager.ConsumerSecret;

            var accessToken = GoogleDocsAuthorizationHelper.BuildToken(authData.Token, tokenSecret, consumerKey, consumerSecret);
            var storage = new CloudStorage();

            authData.Token = storage.SerializeSecurityTokenToBase64Ex(accessToken, typeof (GoogleDocsConfiguration), null);
            return authData;
        }
        private static AuthData GetEncodedAccesToken(AuthData authData, ProviderTypes provider)
        {
            switch (provider)
            {
                case ProviderTypes.GoogleDrive:

                    var code = authData.Token;

                    var token = OAuth20TokenHelper.GetAccessToken(GoogleLoginProvider.GoogleOauthTokenUrl,
                                                                  GoogleLoginProvider.GoogleOAuth20ClientId,
                                                                  GoogleLoginProvider.GoogleOAuth20ClientSecret,
                                                                  GoogleLoginProvider.GoogleOAuth20RedirectUrl,
                                                                  code);

                    if (token == null) throw new UnauthorizedAccessException(string.Format(FilesCommonResource.ErrorMassage_SecurityException_Auth, provider));

                    authData.Token = EncryptPassword(token.ToJson());

                    break;
                case ProviderTypes.SkyDrive:

                    code = authData.Token;

                    token = OAuth20TokenHelper.GetAccessToken(OneDriveLoginProvider.OneDriveOauthTokenUrl,
                                                              OneDriveLoginProvider.OneDriveOAuth20ClientId,
                                                              OneDriveLoginProvider.OneDriveOAuth20ClientSecret,
                                                              OneDriveLoginProvider.OneDriveOAuth20RedirectUrl,
                                                              code);

                    if (token == null) throw new UnauthorizedAccessException(string.Format(FilesCommonResource.ErrorMassage_SecurityException_Auth, provider));

                    var accessToken = AppLimit.CloudComputing.SharpBox.Common.Net.oAuth20.OAuth20Token.FromJson(token.ToJson());

                    var config = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.SkyDrive);
                    var storage = new CloudStorage();
                    var base64AccessToken = storage.SerializeSecurityTokenToBase64Ex(accessToken, config.GetType(), new Dictionary<string, string>());

                    authData.Token = base64AccessToken;

                    break;
                case ProviderTypes.SharePoint:
                case ProviderTypes.WebDav:
                    break;
                default:
                    authData.Url = null;
                    break;
            }

            return authData;
        }
        private static AuthData GetEncodedAccesToken(AuthData authData, ProviderTypes provider)
        {
            switch (provider)
            {
                case ProviderTypes.Google:
                case ProviderTypes.GoogleDrive:

                    var tokenSecret = ImportConfiguration.GoogleTokenManager.GetTokenSecret(authData.Token);
                    var consumerKey = ImportConfiguration.GoogleTokenManager.ConsumerKey;
                    var consumerSecret = ImportConfiguration.GoogleTokenManager.ConsumerSecret;

                    var accessToken = GoogleDocsAuthorizationHelper.BuildToken(authData.Token, tokenSecret, consumerKey, consumerSecret);
                    var storage = new CloudStorage();

                    authData.Token = storage.SerializeSecurityTokenToBase64Ex(accessToken, typeof(GoogleDocsConfiguration), null);

                    break;
            }

            return authData;
        }