public DropBoxStorageProviderSession(DropBoxToken token, DropBoxConfiguration config, OAuthConsumerContext consumerContext, IStorageProviderService service)
 {
     SessionToken = token;
     ServiceConfiguration = config;
     Service = service;
     Context = consumerContext;
 }
        public String GetOAuthAuthorizationHeader(String resourceUrl, OAuthConsumerContext conContext, OAuthToken accessToken, Dictionary<String, String> parameter, String method)
        {
            var oAuth = new OAuthBase();

            String normalizedUrl;
            String normalizedRequestParameters;

            var timestamp = oAuth.GenerateTimeStamp();
            var nonce = oAuth.GenerateNonce();
            var signature = oAuth.GenerateSignature(new Uri(resourceUrl),
                                                    conContext.ConsumerKey, conContext.ConsumerSecret,
                                                    accessToken == null ? string.Empty : accessToken.TokenKey, accessToken == null ? string.Empty : accessToken.TokenSecret,
                                                    method, timestamp, nonce,
                                                    conContext.SignatureMethod,
                                                    out normalizedUrl, out normalizedRequestParameters);
            signature = HttpUtility.UrlEncode(signature);

            var sb = new StringBuilder("OAuth");
            sb.Append(", oauth_version=1.0");
            sb.AppendFormat(", oauth_nonce={0}", nonce);
            sb.AppendFormat(", oauth_timestamp={0}", timestamp);
            sb.AppendFormat(", oauth_consumer_key={0}", conContext.ConsumerKey);
            if (accessToken != null)
            {
                sb.AppendFormat(", , oauth_token={0}", accessToken.TokenKey);
            }
            sb.Append(", oauth_signature_method=\"HMAC-SHA1\"");
            sb.AppendFormat(", oauth_signature={0}", signature);
            
            return sb.ToString();
        }
        static private String GenerateSignedUrl(String baseRequestTokenUrl, String method, OAuthConsumerContext context, OAuthToken token)
        {
            String normalizedUrl;
            String normalizedRequestParameters;

            var oAuth = new OAuthBase();

            string nonce = oAuth.GenerateNonce();
            string timeStamp = oAuth.GenerateTimeStamp();
            string sig = oAuth.GenerateSignature(   new Uri(baseRequestTokenUrl),
                                                    context.ConsumerKey, context.ConsumerSecret,
                                                    token == null ? string.Empty : token.TokenKey, token == null ? string.Empty : token.TokenSecret,
                                                    method.ToString(), timeStamp, nonce,
                                                    context.SignatureMethod,
                                                    out normalizedUrl, out normalizedRequestParameters);

            //
            // The signature as self has to be encoded to be ensure that no not url compatibale characters 
            // will be used. The SHA1 hash can contain a bunch of this characters
            //
            sig = HttpUtility.UrlEncode(sig);

            var sb = new StringBuilder(normalizedUrl);

            sb.AppendFormat("?");
            sb.AppendFormat(normalizedRequestParameters);
            sb.AppendFormat("&oauth_signature={0}", sig);

            return sb.ToString();
        }
 public GoogleDocsStorageProviderSession(IStorageProviderService service, ICloudStorageConfiguration configuration, OAuthConsumerContext context, ICloudStorageAccessToken token)
 {
     Service = service;
     ServiceConfiguration = configuration;
     Context = context;
     SessionToken = token;
 }
        public virtual WebRequest CreateWebRequest(String url, String method, ICredentials credentials, Object context, OAuthConsumerContext conContext, OAuthToken accessToken, Dictionary<String, String> parameter)
        {
            // generate the signed url
            String signedUrl = GetProtectedResourceUrl(url, conContext, accessToken, parameter, method);

            // generate the web request as self
            return CreateWebRequest(signedUrl, method, credentials, false, context);
        }
 public static GoogleDocsRequestToken GetGoogleDocsRequestToken(GoogleDocsConfiguration configuration, String consumerKey, String consumerSecret)
 {
     var consumerContext = new OAuthConsumerContext(consumerKey, consumerSecret);
     var serviceContext = new OAuthServiceContext(GetRequestTokenUrl(configuration),
                                                  configuration.OAuthAuthorizeTokenUrl.ToString(),
                                                  configuration.AuthorizationCallBack.ToString(),
                                                  GetAccessTokenUrl(configuration, null));
     var service = new OAuthService();
     var token = service.GetRequestToken(serviceContext, consumerContext);
     return token != null ? new GoogleDocsRequestToken(token) : null;
 }
 public static ICloudStorageAccessToken ExchangeGoogleDocsRequestTokenIntoAccessToken(GoogleDocsConfiguration configuration, String consumerKey, String consumerSecret, GoogleDocsRequestToken requestToken, String oAuthVerifier)
 {
     var consumerContext = new OAuthConsumerContext(consumerKey, consumerSecret);
     var serviceContext = new OAuthServiceContext(configuration.OAuthGetRequestTokenUrl.ToString(),
                                                  configuration.OAuthAuthorizeTokenUrl.ToString(),
                                                  configuration.AuthorizationCallBack.ToString(),
                                                  GetAccessTokenUrl(configuration, oAuthVerifier));
     var service = new OAuthService();
     var accessToken = service.GetAccessToken(serviceContext, consumerContext, requestToken.RealToken);
     if (accessToken == null) throw new UnauthorizedAccessException();
     return new GoogleDocsToken(accessToken, consumerKey, consumerSecret);
 }
        /// <summary>
        /// This method retrieves a new request token from the dropbox server
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="ConsumerKey"></param>
        /// <param name="ConsumerSecret"></param>
        /// <returns></returns>
        public static DropBoxRequestToken GetDropBoxRequestToken(DropBoxConfiguration configuration, String ConsumerKey, String ConsumerSecret)
        {
            // build the consumer context
            var consumerContext = new OAuthConsumerContext(ConsumerKey, ConsumerSecret);

            // build up the oauth session
            var serviceContext = new OAuthServiceContext(configuration.RequestTokenUrl.ToString(),
                                                         configuration.AuthorizationTokenUrl.ToString(), configuration.AuthorizationCallBack.ToString(),
                                                         configuration.AccessTokenUrl.ToString());

            // get a request token from the provider      
            var svc = new OAuthService();
            var oauthToken = svc.GetRequestToken(serviceContext, consumerContext);
            return oauthToken != null ? new DropBoxRequestToken(oauthToken) : null;
        }
        static public String GenerateSignedUrl(String baseResourceUrl, String method, OAuthConsumerContext context, OAuthToken token, Dictionary<String, String> urlParameter)
        {            
            var sb = new StringBuilder(baseResourceUrl);

            if (urlParameter != null)
            {
                sb.Append('?');

                foreach (KeyValuePair<String, String> kvp in urlParameter)
                {   
                    if ( sb[sb.Length - 1] == '?' )
                        sb.AppendFormat("{0}={1}", kvp.Key, OAuthBase.UrlEncode(kvp.Value));
                    else
                        sb.AppendFormat("&{0}={1}", kvp.Key, OAuthBase.UrlEncode(kvp.Value));
                }
            }                        

            return GenerateSignedUrl(sb.ToString(), method, context, token);
        }
        /// <summary>
        /// This method is able to exchange the request token into an access token which can be used in 
        /// sharpbox. It is necessary that the user validated the request via authorization url otherwise 
        /// this call wil results in an unauthorized exception!
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="ConsumerKey"></param>
        /// <param name="ConsumerSecret"></param>
        /// <param name="DropBoxRequestToken"></param>
        /// <returns></returns>
        public static ICloudStorageAccessToken ExchangeDropBoxRequestTokenIntoAccessToken(DropBoxConfiguration configuration, String ConsumerKey, String ConsumerSecret, DropBoxRequestToken DropBoxRequestToken)
        {
            // build the consumer context
            var consumerContext = new OAuthConsumerContext(ConsumerKey, ConsumerSecret);

            // build up the oauth session
            var serviceContext = new OAuthServiceContext(configuration.RequestTokenUrl.ToString(),
                                                         configuration.AuthorizationTokenUrl.ToString(), configuration.AuthorizationCallBack.ToString(),
                                                         configuration.AccessTokenUrl.ToString());

            // build the access token
            var svc = new OAuthService();
            var accessToken = svc.GetAccessToken(serviceContext, consumerContext, DropBoxRequestToken.RealToken);
            if (accessToken == null)
                throw new UnauthorizedAccessException();

            // create the access token 
            return new DropBoxToken(accessToken,
                                    new DropBoxBaseTokenInformation
                                        {
                                            ConsumerKey = ConsumerKey,
                                            ConsumerSecret = ConsumerSecret
                                        });
        }
        /// <summary>
        /// This method offers the mobile login api of dropbox for users who are migrating from version 0 of the 
        /// dropbox API because version 1 supports token based logins only
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="appkey"></param>
        /// <param name="appsecret"></param>
        /// <returns></returns>
        public static ICloudStorageAccessToken LoginWithMobileAPI(String username, String password, String appkey, String appsecret)
        {
            // get the configuration
            var configuration = DropBoxConfiguration.GetStandardConfiguration();

            // build the consumer context
            var consumerContext = new OAuthConsumerContext(appkey, appsecret);

            // build up the oauth session
            var serviceContext = new OAuthServiceContext(configuration.RequestTokenUrl.ToString(),
                                                         configuration.AuthorizationTokenUrl.ToString(), configuration.AuthorizationCallBack.ToString(),
                                                         configuration.AccessTokenUrl.ToString());

            // get a request token from the provider      
            var svc = new OAuthService();
            var oAuthRequestToken = svc.GetRequestToken(serviceContext, consumerContext);
            if (oAuthRequestToken == null)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidConsumerKeySecret);
            }
            var DropBoxRequestToken = new DropBoxToken(oAuthRequestToken, new DropBoxBaseTokenInformation {ConsumerKey = appkey, ConsumerSecret = appsecret});

            // generate the dropbox service
            var service = new DropBoxStorageProviderService();

            // build up a request Token Session
            var requestSession = new DropBoxStorageProviderSession(DropBoxRequestToken, configuration, consumerContext, service);

            // build up the parameters
            var param = new Dictionary<String, String>
                            {
                                {"email", username},
                                {"password", password}
                            };

            // call the mobile login api 
            var result = "";

            try
            {
                int code;
                result = DropBoxRequestParser.RequestResourceByUrl(DropBoxMobileLogin, param, service, requestSession, out code);
                if (result.Length == 0)
                    throw new UnauthorizedAccessException();
            }

#if MONOTOUCH || WINDOWS_PHONE || MONODROID
            catch (Exception ex)
            {
                if (ex is UnauthorizedAccessException)
                    throw ex;
                else
                    throw new SharpBoxException(SharpBoxErrorCodes.ErrorCouldNotContactStorageService, ex);
            }
#else
            catch (System.Web.HttpException netex)
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorCouldNotContactStorageService, netex);
            }
#endif

            // exchange a request token for an access token
            var accessToken = new DropBoxToken(result);

            // adjust the token 
            if (accessToken.BaseTokenInformation == null)
            {
                accessToken.BaseTokenInformation = new DropBoxBaseTokenInformation
                                                       {
                                                           ConsumerKey = appkey,
                                                           ConsumerSecret = appsecret
                                                       };
            }


            // go ahead
            return accessToken;
        }
 static public String GenerateAccessTokenUrl(String baseAccessTokenUrl, OAuthConsumerContext context, OAuthToken requestToken)
 {
     return GenerateSignedUrl(baseAccessTokenUrl, WebRequestMethodsEx.Http.Get, context, requestToken);            
 }
 static public String GenerateRequestTokenUrl(String baseRequestTokenUrl, OAuthConsumerContext context)
 {
     return GenerateSignedUrl(baseRequestTokenUrl, WebRequestMethodsEx.Http.Get, context, null);            
 }
        public DropBoxStorageProviderSession BuildSessionFromAccessToken(DropBoxToken token, DropBoxConfiguration configuration)
        {
            // build the consumer context
            var consumerContext = new OAuthConsumerContext(token.BaseTokenInformation.ConsumerKey, token.BaseTokenInformation.ConsumerSecret);

            // build the session
            var session = new DropBoxStorageProviderSession(token, configuration, consumerContext, this);

            // go aahead
            return session;
        }
 public String GetSignedUrl(String resourceUrl, OAuthConsumerContext conContext, OAuthToken accessToken, Dictionary<String, String> parameter)
 {
     return OAuthUrlGenerator.GenerateSignedUrl(resourceUrl, WebRequestMethodsEx.Http.Post, conContext, accessToken, parameter);
 }
 public String GetProtectedResourceUrl(String resourceUrl, OAuthConsumerContext conContext, OAuthToken accessToken, Dictionary<String, String> parameter, String webMethod)
 {
     // build url
     return OAuthUrlGenerator.GenerateSignedUrl(resourceUrl, webMethod, conContext, accessToken, parameter);            
 }
        public OAuthToken GetAccessToken(OAuthServiceContext svcContext, OAuthConsumerContext conContext, OAuthToken requestToken)
        {
            String url = OAuthUrlGenerator.GenerateAccessTokenUrl(svcContext.AccessTokenUrl, conContext, requestToken);

            return GetToken(url);
        }
        public OAuthToken GetRequestToken(OAuthServiceContext svcContext, OAuthConsumerContext conContext)
        {
            String url = OAuthUrlGenerator.GenerateRequestTokenUrl(svcContext.RequestTokenUrl, conContext);

            return GetToken(url);
        }