Ejemplo n.º 1
0
        /// <summary>
        /// Gets an available access token for a particulare store.  This is probably a method you shouldn't be using in production code,
        /// as it will likely result in using the wrong/inappropriate token to peform operations.
        /// </summary>
        /// <param name="storeType">Type off store</param>
        /// <returns>Access token for the store.</returns>
        public String GetAnyAccessToken(TokenStoreType storeType)
        {
            TokenStore entities = new TokenStore(Utilities.GetEntityConnectionString());

            var tokens = entities.Tokens.Where(t => t.Type == (int)TokenType.AccessToken & t.StoreTypeId == (int)storeType);

            foreach (var tokenItem in tokens)
            {
                return tokenItem.TokenContent;
            }

            return null;
        }
        public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, TokenStoreType storeType, String userId, string accessToken, string accessTokenSecret)
        {
            TokenStore entities = new TokenStore(Utilities.GetEntityConnectionString());

            var tokens = entities.Tokens.Where(t => t.TokenContent == requestToken & t.StoreTypeId == (int)storeType);

            foreach (var tokenItem in tokens)
            {
                entities.DeleteObject(tokenItem);
            }

            tokens = entities.Tokens.Where(t => t.TokenContent == accessToken & t.StoreTypeId == (int)storeType);
            bool doSaveChanges = false;

            foreach (var tokenItem in tokens)
            {
                tokenItem.Secret = accessTokenSecret;
                tokenItem.UserId = userId;

                doSaveChanges = true;
            }

            if (doSaveChanges)
            {
                entities.SaveChanges();
                return;
            }

            // go create a new token.
            Token tas = entities.Tokens.CreateObject();
            tas.Id = Guid.NewGuid();
            tas.UserId = userId;
            tas.TokenContent = accessToken;
            tas.Secret = accessTokenSecret;
            tas.StoreTypeId = (byte)storeType;
            tas.Type = (int)TokenType.AccessToken;

            entities.Tokens.AddObject(tas);
            entities.SaveChanges();

            this.localTokensAndSecrets.Remove(requestToken);
            this.localTokensAndSecrets[accessToken] = accessTokenSecret;
        }
Ejemplo n.º 3
0
        public bool GetAccessTokenAndSecret(TokenStoreType storeType, String userId, out String accessToken, out String accessTokenSecret)
        {
            TokenStore entities = new TokenStore(Utilities.GetEntityConnectionString());

            var tokens = entities.Tokens.Where(t => t.UserId == userId & t.Type == (int)TokenType.AccessToken & t.StoreTypeId == (int)storeType);

            foreach (var tokenItem in tokens)
            {
                accessToken = tokenItem.TokenContent;
                accessTokenSecret = tokenItem.Secret;

                return true;
            }

            accessToken = null;
            accessTokenSecret = null;

            return false;
        }
Ejemplo n.º 4
0
        private ITokenStore GetTokenStore(TokenStoreType tokenStoreType)
        {
            switch (tokenStoreType)
            {
            case TokenStoreType.Session:
                return(new SessionTokenStore());

            case TokenStoreType.Cookie:
                return(new CookieTokenStore());

            case TokenStoreType.SingleApplication:
                return(new GlobalApplicationStore());

            case TokenStoreType.PerClient:
                return(new DefaultTokenStore());

            default:
                throw new ArgumentOutOfRangeException("tokenStoreType");
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Retrieves a token secret given an original token.
        /// </summary>
        /// <param name="storeType">Type of store to connect to.</param>
        /// <param name="token">Content of token to match against.</param>
        /// <returns></returns>
        public string GetTokenSecret(TokenStoreType storeType, string token) 
        {
            if (this.localTokensAndSecrets.ContainsKey(token))
            {
                return this.localTokensAndSecrets[token];
            }

            TokenStore ts = new TokenStore(Utilities.GetEntityConnectionString());

            var tokens = ts.Tokens.Where(t => t.TokenContent == token & t.StoreTypeId == (int)storeType);

            // should probably do better enforcement here that there is only one response.
            foreach (var tokenItem in tokens)
            {
                this.localTokensAndSecrets[token] = tokenItem.Secret;

                return tokenItem.Secret;
            } 
            
            return null;
		}
Ejemplo n.º 6
0
        public void StoreNewToken(TokenType tokenType, TokenStoreType storeType, String userId, String token, String contextContent, String tokenSecret)
        {
			this.localTokensAndSecrets[token] = tokenSecret;

            // does the token already exist?
            TokenStore tokenStore = new TokenStore(Utilities.GetEntityConnectionString());

            var tokens = tokenStore.Tokens.Where(t => t.TokenContent == token & t.StoreTypeId == (byte)storeType & t.UserId == userId & t.Type == (int)TokenType.AccessToken);
            bool doSaveChanges = false;

            foreach (var tokenItem in tokens)
            {
                tokenItem.Secret = tokenSecret;
                tokenItem.UserId = userId;
                tokenItem.StoreTypeId = (byte)storeType;
                tokenItem.ContextContent = contextContent;
                doSaveChanges = true; 
            }

            if (doSaveChanges)
            {
                tokenStore.SaveChanges();
                return;
            }

            // go create a new token.
            Token tas = tokenStore.Tokens.CreateObject();
            tas.Id = Guid.NewGuid();
            tas.UserId =  userId;
            tas.TokenContent = token;
            tas.Secret = tokenSecret;
            tas.StoreTypeId = (byte)storeType;
            tas.ContextContent = contextContent;
            tas.Type = (int)tokenType;

            tokenStore.Tokens.AddObject(tas);
            tokenStore.SaveChanges();
        }
        /// <summary>
        /// Retrieves a token secret given an original token.
        /// </summary>
        /// <param name="storeType">Type of store to connect to.</param>
        /// <param name="token">Content of token to match against.</param>
        /// <returns></returns>
        public string GetTokenSecret(TokenStoreType storeType, string token)
        {
            if (this.localTokensAndSecrets.ContainsKey(token))
            {
                return this.localTokensAndSecrets[token];
            }

            TokenStore ts = new TokenStore(Utilities.GetEntityConnectionString());

            var tokens = ts.Tokens.Where(t => t.TokenContent == token & t.StoreTypeId == (int)storeType);

            // should probably do better enforcement here that there is only one response.
            foreach (var tokenItem in tokens)
            {
                this.localTokensAndSecrets[token] = tokenItem.Secret;

                return tokenItem.Secret;
            }

            return null;
        }
Ejemplo n.º 8
0
        protected ServiceClient(string serviceUri, string applicationId, string applicationSecret, TokenStoreType tokenStoreType)
        {
            ServiceUri = serviceUri;

            _applicationId     = applicationId;
            _applicationSecret = applicationSecret;

            _tokenStore = GetTokenStore(tokenStoreType);
        }
Ejemplo n.º 9
0
 protected ServiceClient(string applicationId, string applicationSecret, TokenStoreType tokenStoreType)
     : this(DEFAULT_API_URI, applicationId, applicationSecret, tokenStoreType)
 {
 }
Ejemplo n.º 10
0
 public ApiClient(string serviceUri, string applicationId, string applicationSecret, TokenStoreType tokenStoreType)
     : base(serviceUri, applicationId, applicationSecret, tokenStoreType)
 {
 }
Ejemplo n.º 11
0
 public static TService GetClient <TService>(string serviceUri, string applicationId, string applicationSecret, TokenStoreType tokenStore)
 {
     return(GetClient <TService>(serviceUri, applicationId, applicationSecret, TokenStoreFactory.GetTokenStore(tokenStore)));
 }
        public void StoreNewToken(TokenType tokenType, TokenStoreType storeType, String userId, String token, String contextContent, String tokenSecret)
        {
            this.localTokensAndSecrets[token] = tokenSecret;

            // does the token already exist?
            TokenStore tokenStore = new TokenStore(Utilities.GetEntityConnectionString());

            var tokens = tokenStore.Tokens.Where(t => t.TokenContent == token & t.StoreTypeId == (byte)storeType & t.UserId == userId & t.Type == (int)TokenType.AccessToken);
            bool doSaveChanges = false;

            foreach (var tokenItem in tokens)
            {
                tokenItem.Secret = tokenSecret;
                tokenItem.UserId = userId;
                tokenItem.StoreTypeId = (byte)storeType;
                tokenItem.ContextContent = contextContent;
                doSaveChanges = true;
            }

            if (doSaveChanges)
            {
                tokenStore.SaveChanges();
                return;
            }

            // go create a new token.
            Token tas = tokenStore.Tokens.CreateObject();
            tas.Id = Guid.NewGuid();
            tas.UserId =  userId;
            tas.TokenContent = token;
            tas.Secret = tokenSecret;
            tas.StoreTypeId = (byte)storeType;
            tas.ContextContent = contextContent;
            tas.Type = (int)tokenType;

            tokenStore.Tokens.AddObject(tas);
            tokenStore.SaveChanges();
        }
 public void StoreNewToken(TokenType tokenType, TokenStoreType storeType, String userId, String token, String contextContent)
 {
     this.StoreNewToken(tokenType, storeType, userId, token, contextContent, null);
 }
Ejemplo n.º 14
0
 public void StoreNewToken(TokenType tokenType, TokenStoreType storeType, String userId, String token, String contextContent)
 {
     this.StoreNewToken(tokenType, storeType, userId, token, contextContent, null);
 }
        /// <summary>
        /// Gets an available access token for a particulare store.  This is probably a method you shouldn't be using in production code,
        /// as it will likely result in using the wrong/inappropriate token to peform operations.
        /// </summary>
        /// <param name="storeType">Type off store</param>
        /// <returns>Access token for the store.</returns>
        public String GetAnyAccessToken(TokenStoreType storeType)
        {
            TokenStore entities = new TokenStore(Utilities.GetEntityConnectionString());

            var tokens = entities.Tokens.Where(t => t.Type == (int)TokenType.AccessToken & t.StoreTypeId == (int)storeType);

            foreach (var tokenItem in tokens)
            {
                return tokenItem.TokenContent;
            }

            return null;
        }
        public bool GetAccessTokenAndSecret(TokenStoreType storeType, String userId, out String accessToken, out String accessTokenSecret)
        {
            TokenStore entities = new TokenStore(Utilities.GetEntityConnectionString());

            var tokens = entities.Tokens.Where(t => t.UserId == userId & t.Type == (int)TokenType.AccessToken & t.StoreTypeId == (int)storeType);

            foreach (var tokenItem in tokens)
            {
                accessToken = tokenItem.TokenContent;
                accessTokenSecret = tokenItem.Secret;

                return true;
            }

            accessToken = null;
            accessTokenSecret = null;

            return false;
        }
Ejemplo n.º 17
0
 public OAuthAuthorizationClient(string serviceUri, string applicationId, string applicationSecret, TokenStoreType type)
     : this(serviceUri, applicationId, applicationSecret, TokenStoreFactory.GetTokenStore(type))
 {
 }
Ejemplo n.º 18
0
        public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, TokenStoreType storeType, String userId, string accessToken, string accessTokenSecret) 
        {
            TokenStore entities = new TokenStore(Utilities.GetEntityConnectionString());

            var tokens = entities.Tokens.Where(t => t.TokenContent == requestToken & t.StoreTypeId == (int)storeType);

            foreach (var tokenItem in tokens)
            {
                entities.DeleteObject(tokenItem);
            }

            tokens = entities.Tokens.Where(t => t.TokenContent == accessToken & t.StoreTypeId == (int)storeType);
            bool doSaveChanges = false;

            foreach (var tokenItem in tokens)
            {
                tokenItem.Secret = accessTokenSecret;
                tokenItem.UserId = userId;

                doSaveChanges = true;
            }

            if (doSaveChanges)
            {
                entities.SaveChanges();
                return;
            }

            // go create a new token.
            Token tas = entities.Tokens.CreateObject();
            tas.Id = Guid.NewGuid();
            tas.UserId = userId;
            tas.TokenContent = accessToken;
            tas.Secret = accessTokenSecret;
            tas.StoreTypeId = (byte)storeType;
            tas.Type = (int)TokenType.AccessToken;

            entities.Tokens.AddObject(tas);
            entities.SaveChanges();

            this.localTokensAndSecrets.Remove(requestToken);
            this.localTokensAndSecrets[accessToken] = accessTokenSecret;
		}