/// <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; }
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; }
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"); } }
/// <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; }
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(); }
protected ServiceClient(string serviceUri, string applicationId, string applicationSecret, TokenStoreType tokenStoreType) { ServiceUri = serviceUri; _applicationId = applicationId; _applicationSecret = applicationSecret; _tokenStore = GetTokenStore(tokenStoreType); }
protected ServiceClient(string applicationId, string applicationSecret, TokenStoreType tokenStoreType) : this(DEFAULT_API_URI, applicationId, applicationSecret, tokenStoreType) { }
public ApiClient(string serviceUri, string applicationId, string applicationSecret, TokenStoreType tokenStoreType) : base(serviceUri, applicationId, applicationSecret, tokenStoreType) { }
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) { this.StoreNewToken(tokenType, storeType, userId, token, contextContent, null); }
public OAuthAuthorizationClient(string serviceUri, string applicationId, string applicationSecret, TokenStoreType type) : this(serviceUri, applicationId, applicationSecret, TokenStoreFactory.GetTokenStore(type)) { }