Beispiel #1
0
        private OAuth20Token GetAccessToken(string state, string code)
        {
            var timestamp = DateTime.UtcNow.ToString("yyyy.MM.dd HH:mm:ss +0000");

            var msg = Scopes + timestamp + ClientID + state;
            var encodedSignature = SignMsg(msg);
            var clientSecret     = WebEncoders.Base64UrlEncode(encodedSignature);

            var requestParams = new Dictionary <string, string>
            {
                { "client_id", ClientID },
                { "code", code },
                { "grant_type", "authorization_code" },
                { "client_secret", clientSecret },
                { "state", state },
                { "redirect_uri", RedirectUri },
                { "scope", Scopes },
                { "timestamp", timestamp },
                { "token_type", "Bearer" }
            };
            var requestQuery = string.Join("&", requestParams.Select(pair => pair.Key + "=" + HttpUtility.UrlEncode(pair.Value)));

            var result = RequestHelper.PerformRequest(AccessTokenUrl, "application/x-www-form-urlencoded", "POST", requestQuery);

            return(OAuth20Token.FromJson(result));
        }
Beispiel #2
0
        public static OAuth20Token RefreshToken(string requestUrl, OAuth20Token token)
        {
            if (token == null || !CanRefresh(token))
            {
                throw new ArgumentException("Can not refresh given token", "token");
            }

            var data = string.Format("client_id={0}&client_secret={1}&refresh_token={2}&grant_type=refresh_token",
                                     HttpUtility.UrlEncode(token.ClientID),
                                     HttpUtility.UrlEncode(token.ClientSecret),
                                     HttpUtility.UrlEncode(token.RefreshToken));

            var json = RequestHelper.PerformRequest(requestUrl, "application/x-www-form-urlencoded", "POST", data);

            if (json != null)
            {
                var refreshed = OAuth20Token.FromJson(json);
                refreshed.ClientID     = token.ClientID;
                refreshed.ClientSecret = token.ClientSecret;
                refreshed.RedirectUri  = token.RedirectUri;
                refreshed.RefreshToken ??= token.RefreshToken;
                return(refreshed);
            }

            return(token);
        }
        public OAuth20Token RefreshToken(string refreshToken)
        {
            if (string.IsNullOrEmpty(refreshToken) || string.IsNullOrEmpty(ClientID) || string.IsNullOrEmpty(ClientSecret))
            {
                throw new ArgumentException("Can not refresh given token");
            }

            var data    = string.Format("grant_type=refresh_token&refresh_token={0}", refreshToken);
            var headers = new Dictionary <string, string> {
                { "Authorization", AuthHeader }
            };

            var json = RequestHelper.PerformRequest(AccessTokenUrl, "application/x-www-form-urlencoded", "POST", data, headers);

            if (json == null)
            {
                throw new Exception("Can not get token");
            }

            var refreshed = OAuth20Token.FromJson(json);

            refreshed.ClientID     = ClientID;
            refreshed.ClientSecret = ClientSecret;
            refreshed.RedirectUri  = RedirectUri;
            refreshed.RefreshToken ??= refreshToken;
            return(refreshed);
        }
Beispiel #4
0
        public static OAuth20Token GetAccessToken <T>(ConsumerFactory consumerFactory, string authCode) where T : Consumer, IOAuthProvider, new()
        {
            var loginProvider = consumerFactory.Get <T>();
            var requestUrl    = loginProvider.AccessTokenUrl;
            var clientID      = loginProvider.ClientID;
            var clientSecret  = loginProvider.ClientSecret;
            var redirectUri   = loginProvider.RedirectUri;

            if (string.IsNullOrEmpty(authCode))
            {
                throw new ArgumentNullException("authCode");
            }
            if (string.IsNullOrEmpty(clientID))
            {
                throw new ArgumentNullException("clientID");
            }
            if (string.IsNullOrEmpty(clientSecret))
            {
                throw new ArgumentNullException("clientSecret");
            }

            var data = string.Format("code={0}&client_id={1}&client_secret={2}",
                                     HttpUtility.UrlEncode(authCode),
                                     HttpUtility.UrlEncode(clientID),
                                     HttpUtility.UrlEncode(clientSecret));

            if (!string.IsNullOrEmpty(redirectUri))
            {
                data += "&redirect_uri=" + HttpUtility.UrlEncode(redirectUri);
            }

            data += "&grant_type=authorization_code";

            var json = RequestHelper.PerformRequest(requestUrl, "application/x-www-form-urlencoded", "POST", data);

            if (json != null)
            {
                if (!json.StartsWith("{"))
                {
                    json = "{\"" + json.Replace("=", "\":\"").Replace("&", "\",\"") + "\"}";
                }

                var token = OAuth20Token.FromJson(json);
                if (token == null)
                {
                    return(null);
                }

                token.ClientID     = clientID;
                token.ClientSecret = clientSecret;
                token.RedirectUri  = redirectUri;
                return(token);
            }

            return(null);
        }
Beispiel #5
0
        public override ICloudStorageAccessToken LoadToken(Dictionary <String, String> tokendata)
        {
            var type = tokendata[CloudStorage.TokenCredentialType];

            if (type.Equals(typeof(OAuth20Token).ToString()))
            {
                var json = tokendata[SkyDriveConstants.SerializedDataKey];
                return(OAuth20Token.FromJson(json));
            }
            return(null);
        }
Beispiel #6
0
        public static OAuth20Token GetAccessToken(string requestUrl, string clientID, string clientSecret, string redirectUri, string authCode)
        {
            if (String.IsNullOrEmpty(authCode))
            {
                throw new ArgumentNullException("authCode");
            }
            if (String.IsNullOrEmpty(clientID))
            {
                throw new ArgumentNullException("clientID");
            }
            if (String.IsNullOrEmpty(clientSecret))
            {
                throw new ArgumentNullException("clientSecret");
            }

            var data = string.Format("code={0}&client_id={1}&client_secret={2}", authCode, clientID, clientSecret);

            if (!String.IsNullOrEmpty(redirectUri))
            {
                data += "&redirect_uri=" + redirectUri;
            }

            data += "&grant_type=authorization_code";

            var json = RequestHelper.PerformRequest(requestUrl, "application/x-www-form-urlencoded", "POST", data);

            if (json != null)
            {
                if (!json.StartsWith("{"))
                {
                    json = "{\"" + json.Replace("=", "\":\"").Replace("&", "\",\"") + "\"}";
                }

                var token = OAuth20Token.FromJson(json);
                if (token == null)
                {
                    return(null);
                }

                token.ClientID     = clientID;
                token.ClientSecret = clientSecret;
                token.RedirectUri  = redirectUri;
                return(token);
            }

            return(null);
        }
        public OAuth20Token GetAccessToken(string authCode)
        {
            if (string.IsNullOrEmpty(authCode))
            {
                throw new ArgumentNullException("authCode");
            }
            if (string.IsNullOrEmpty(ClientID))
            {
                throw new ArgumentException("clientID");
            }
            if (string.IsNullOrEmpty(ClientSecret))
            {
                throw new ArgumentException("clientSecret");
            }

            var data    = string.Format("grant_type=authorization_code&code={0}", authCode);
            var headers = new Dictionary <string, string> {
                { "Authorization", AuthHeader }
            };

            var json = RequestHelper.PerformRequest(AccessTokenUrl, "application/x-www-form-urlencoded", "POST", data, headers);

            if (json == null)
            {
                throw new Exception("Can not get token");
            }

            if (!json.StartsWith("{"))
            {
                json = "{\"" + json.Replace("=", "\":\"").Replace("&", "\",\"") + "\"}";
            }

            var token = OAuth20Token.FromJson(json);

            if (token == null)
            {
                return(null);
            }

            token.ClientID     = ClientID;
            token.ClientSecret = ClientSecret;
            token.RedirectUri  = RedirectUri;
            return(token);
        }
Beispiel #8
0
        public GoogleDriveProviderInfo(int id, string providerKey, string customerTitle, string token, Guid owner, FolderType rootFolderType, DateTime createOn)
        {
            if (string.IsNullOrEmpty(providerKey))
            {
                throw new ArgumentNullException("providerKey");
            }
            if (string.IsNullOrEmpty(token))
            {
                throw new ArgumentException("Token can't be null");
            }

            ID            = id;
            CustomerTitle = customerTitle;
            Owner         = owner == Guid.Empty ? SecurityContext.CurrentAccount.ID : owner;

            ProviderKey     = providerKey;
            _token          = OAuth20Token.FromJson(token);
            _rootFolderType = rootFolderType;
            _createOn       = createOn;
        }
        public static OAuth20Token GetAccessToken(string requestUrl, string clientID, string clientSecret, string redirectUri, string authCode)
        {
            if (String.IsNullOrEmpty(clientID))
            {
                throw new ArgumentNullException("clientID");
            }
            if (String.IsNullOrEmpty(clientSecret))
            {
                throw new ArgumentNullException("clientSecret");
            }
            if (String.IsNullOrEmpty(redirectUri))
            {
                throw new ArgumentNullException("redirectUri");
            }
            if (String.IsNullOrEmpty(authCode))
            {
                throw new ArgumentNullException("authCode");
            }

            var data = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code",
                                     authCode, clientID, clientSecret, redirectUri);

            var json = RequestHelper.PerformRequest(requestUrl, "application/x-www-form-urlencoded", "POST", data);

            if (json != null)
            {
                var token = OAuth20Token.FromJson(json);
                if (token == null)
                {
                    return(null);
                }

                token.ClientID     = clientID;
                token.ClientSecret = clientSecret;
                token.RedirectUri  = redirectUri;
                return(token);
            }

            return(null);
        }
Beispiel #10
0
 private OAuth20Token DecryptToken(string token)
 {
     return(string.IsNullOrEmpty(token) ? null : OAuth20Token.FromJson(InstanceCrypto.Decrypt(token)));
 }