private async Task WriteCacheBytesAsync(string cacheKey, byte[] token) { var tokenRecord = await _db.CachedTokens.FirstOrDefaultAsync(x => x.AccountId == cacheKey); if (tokenRecord == null) { tokenRecord = new CachedToken { AccountId = cacheKey, Timestamp = DateTime.UtcNow }; _db.CachedTokens.Add(tokenRecord); } tokenRecord.Token = token; try { await _db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { //token is already set, no need to set it again } catch (DbUpdateException) { //todo need to check whether it's PK constraint violation } }
private async Task <string> getAuthToken(string clientId, string clientSecret, string tokenUrl) { if (_tokenDict.TryGetValue(clientId, out var token)) { if (token.IsValid()) { return(token.GetAccessToken()); } else { this.removeAuthToken(clientId); } } using (var client = _clientFactory.CreateClient()) { var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest { Address = tokenUrl, ClientId = clientId, ClientSecret = clientSecret }); if (response.IsError) { return(null); } var cachedToken = new CachedToken(response); _tokenDict.TryAdd(clientId, cachedToken); return(cachedToken.GetAccessToken()); } }
public static SuperIdToken GetSystemUserToken(string contextIdentifier) { lock (_cachedTokens) { CachedToken cachedToken; if (_cachedTokens.TryGetValue(contextIdentifier, out cachedToken)) { if (cachedToken.CacheTime.AddMinutes(45) > DateTime.Now) { return(cachedToken.Token); } } using (var db = new AppDB()) { var sysUserToken = db.Customers .Where(c => c.ContextIdentifier == contextIdentifier) .Select(c => c.SystemUserToken) .FirstOrDefault(); var token = GetSystemUserToken(sysUserToken, contextIdentifier); _cachedTokens[contextIdentifier] = new CachedToken { CacheTime = DateTime.Now, Token = token, }; return(token); } } }
public async Task <string> GetAccessTokenAsync(string authority, string resource, string scope) { var cachedTokenId = string.Format( "{0}:{1}:{2}", Convert.ToBase64String(_textEncoding.GetBytes(authority ?? "")), Convert.ToBase64String(_textEncoding.GetBytes(resource ?? "")), Convert.ToBase64String(_textEncoding.GetBytes(scope ?? "")) ); if (_cachedTokens.TryGetValue(cachedTokenId, out CachedToken cachedToken) && DateTimeOffset.Now < cachedToken.ExpiresOn - _tokenValidityLimit) { return(cachedToken.Token); } var result = await requestAccessTokenAsync(authority, resource, scope); if (result == null) { throw new InvalidOperationException("Failed to obtain the JWT token"); } cachedToken = new CachedToken() { Token = result.AccessToken, ExpiresOn = result.ExpiresOn, }; _cachedTokens.AddOrUpdate(cachedTokenId, cachedToken, (k, v) => cachedToken); return(result.AccessToken); }
public static GetTokenResponse CreateFromResponse(string response) { GetTokenResponse token = new GetTokenResponse(response); CachedToken cachedToken = new CachedToken(response, token.expires); File.WriteAllText(CacheFile, JsonConvert.SerializeObject(cachedToken)); return(token); }
/// <summary> /// Method that request an Application User Token for Spei Operations /// </summary> /// <param name="request">Object that includes all information for the token</param> /// <returns>return the token data </returns> public string GetApplicationUserToken(TokenRequest request) { string keySearch = request.UserKey + ScopeType.SpeiAdmin.ScopeName; CachedToken cachedToken = GetCachedToken(keySearch); string bearer; if (cachedToken != null) { bearer = FormatToHeader(cachedToken.GetToken().AccessToken); } else { TokenResponse tokenResponse = GetToken(request.ClientKey, request.ClientSecret, string.Format( USER_TOKEN_TEMPLATE, GrantType.Password.GrantTypeName, ScopeType.SpeiAdmin.ScopeName, request.UserKey, request.UserSecret)); this.tokensCached.Add(keySearch, new CachedToken(request.UserKey, request.UserSecret, tokenResponse)); bearer = FormatToHeader(tokenResponse.AccessToken); } return(bearer); }
/// <summary> /// Method that request an Application Token /// </summary> /// <param name="request">Object that includes all information for the token</param> /// <returns>return the token data </returns> public string GetApplicationToken(TokenRequest request) { string keySearch = request.ClientKey + ScopeType.General.ScopeName; CachedToken cachedToken = GetCachedToken(keySearch); string bearer; if (cachedToken != null) { bearer = FormatToHeader(cachedToken.GetToken().AccessToken); } else { TokenResponse tokenResponse = GetToken(request.ClientKey, request.ClientSecret, string.Format( APPLICATION_TOKEN_TEMPLATE, GrantType.ClientCredentials.GrantTypeName, ScopeType.General.ScopeName)); this.tokensCached.Add(keySearch, new CachedToken(null, null, tokenResponse)); bearer = FormatToHeader(tokenResponse.AccessToken); } return(bearer); }
public static GetTokenResponse CreateFromCache() { if (File.Exists(CacheFile)) { string response = File.ReadAllText(CacheFile); try { CachedToken cachedToken = JsonConvert.DeserializeObject <CachedToken>(response); if (cachedToken != null && cachedToken.Expires >= DateTime.Now) { GetTokenResponse token = new GetTokenResponse(cachedToken.Response); token.expires = cachedToken.Expires; return(token); } } catch { } File.Delete(CacheFile); } return(null); }