/// <summary> /// Method to get App details for IdentityServer4 Client table in ConsumerApp object /// </summary> /// <param name="clientId">clientId of App</param> /// <returns>App details as object of ConsumerApp</returns> private async Task <ConsumerApps> GetConsumerByClientIdOfIdentityServerClient(string clientId) { ConsumerApps app = new ConsumerApps(); var client = await _clientDataRepository.FirstOrDefaultAsync(x => x.ClientId == clientId); if (client != null) { var scopesAllowed = await _scopes.FetchAsync(x => x.Client.ClientId == clientId); app.Scopes = new List <AllowedScope>(); AllowedScope scope; foreach (var scopes in scopesAllowed) { var result = Enum.TryParse <AllowedScope>(scopes.Scope, out scope); if (result) { app.Scopes.Add(scope); } } app.Id = client.Id; app.AuthId = client.ClientId; var secret = await _secret.FirstOrDefaultAsync(x => x.Client.ClientId == clientId); app.AuthSecret = secret.Value; var callBackUri = await _redirectUri.FirstOrDefaultAsync(x => x.Client.ClientId == clientId); app.CallbackUrl = callBackUri.RedirectUri; app.Name = client.ClientName; var logoutUri = await _logoutRedirectUri.FirstOrDefaultAsync(x => x.Client.ClientId == clientId); app.LogoutUrl = logoutUri.PostLogoutRedirectUri; } return(app); }
public async Task <IActionResult> UpdateConsumerAppAsync([FromBody] ConsumerApps consumerApp) { _logger.LogDebug("consumerApp request to update"); await _consumerAppRepository.UpdateConsumerAppsAsync(consumerApp); _logger.LogDebug("consumerApp successfully updated"); return(Ok()); }
public async Task AddConsumerAppsAsync() { ConsumerApps consumerApp = GetConsumerApp(); var app = await _consumerAppRespository.AddConsumerAppsAsync(consumerApp); var clientApp = await _clientContext.FirstOrDefaultAsync(x => x.ClientId == app.ClientId); Assert.Equal(clientApp.AllowAccessTokensViaBrowser, true); }
public async Task GetListOfConsumerAppsAsync() { ConsumerApps consumerApp = GetConsumerApp(); var app = await _consumerAppRespository.AddConsumerAppsAsync(consumerApp); var result = await _consumerAppRespository.GetListOfConsumerAppsAsync(); Assert.Equal(result.Count, 1); }
public async Task GetAppDetailsByClientIdAsync() { ConsumerApps consumerApp = GetConsumerApp(); var app = await _consumerAppRespository.AddConsumerAppsAsync(consumerApp); var consumerApps = await _consumerAppRespository.GetAppDetailsByClientIdAsync(_stringConstant.RandomClientId); Assert.Equal(consumerApps.Name, _stringConstant.Name); }
public async Task GetAppDetailsByClientIdForExceptionAsync() { ConsumerApps consumerApp = GetConsumerApp(); var app = await _consumerAppRespository.AddConsumerAppsAsync(consumerApp); var result = await Assert.ThrowsAsync <ConsumerAppNotFound>(() => _consumerAppRespository.GetAppDetailsByClientIdAsync(_stringConstant.RandomClientSecret)); Assert.Equal(result.Message, _stringConstant.ExceptionMessageConsumerAppNotFound); }
public async Task <IActionResult> AddConsumerAppAsync([FromBody] ConsumerApps consumerApps) { try { return(Ok(await _consumerAppRepository.AddConsumerAppsAsync(consumerApps))); } catch (ConsumerAppNameIsAlreadyExists) { return(BadRequest()); } }
public async Task AddConsumerAppsExceptionAsync() { ConsumerApps consumerApp = GetConsumerApp(); var app = await _consumerAppRespository.AddConsumerAppsAsync(consumerApp); consumerApp.Id = 0; var result = await Assert.ThrowsAsync <ConsumerAppNameIsAlreadyExists>(() => _consumerAppRespository.AddConsumerAppsAsync(consumerApp)); Assert.Equal(_stringConstant.ExceptionMessageConsumerAppNameIsAlreadyExists, result.Message); }
public async Task UpdateConsumerAllDetailsAsync() { ConsumerApps consumerApp = GetConsumerApp(); var app = await _consumerAppRespository.AddConsumerAppsAsync(consumerApp); var previousAppDetails = await _consumerAppRespository.GetAppDetailsByClientIdAsync(app.ClientId); consumerApp.AuthSecret = _stringConstant.AccessToken; consumerApp.CallbackUrl = _stringConstant.CallbackUrl; consumerApp.LogoutUrl = _stringConstant.CallbackUrl; consumerApp.Scopes = new List <AllowedScope>() { AllowedScope.email, AllowedScope.openid, AllowedScope.profile, AllowedScope.project_read, AllowedScope.user_read }; consumerApp.AuthId = _stringConstant.SlackUserId; consumerApp.Name = _stringConstant.ProjectName; consumerApp.Id = previousAppDetails.Id; await _consumerAppRespository.UpdateConsumerAppsAsync(consumerApp); var updateApp = await _consumerAppRespository.GetAppDetailsByClientIdAsync(_stringConstant.SlackUserId); #region AuthId Assert Assert.Equal(previousAppDetails.AuthId, _stringConstant.RandomClientId); Assert.Equal(updateApp.AuthId, _stringConstant.SlackUserId); Assert.NotEqual(updateApp.AuthId, previousAppDetails.AuthId); #endregion #region AuthSecret Assert var encodedSecret = _stringConstant.RandomClientSecret.Sha256(); Assert.Equal(previousAppDetails.AuthSecret, encodedSecret); encodedSecret = _stringConstant.AccessToken.Sha256(); Assert.Equal(updateApp.AuthSecret, encodedSecret); Assert.NotEqual(updateApp.AuthSecret, previousAppDetails.AuthSecret); #endregion #region CallbackUrl Assert Assert.Equal(previousAppDetails.CallbackUrl, _stringConstant.PromactErpUrlForTest); Assert.Equal(updateApp.CallbackUrl, _stringConstant.CallbackUrl); Assert.NotEqual(updateApp.CallbackUrl, previousAppDetails.CallbackUrl); #endregion #region LogoutUrl Assert Assert.Equal(previousAppDetails.LogoutUrl, _stringConstant.PromactErpUrlForTest); Assert.Equal(updateApp.LogoutUrl, _stringConstant.CallbackUrl); Assert.NotEqual(updateApp.LogoutUrl, previousAppDetails.LogoutUrl); #endregion #region Scope Assert Assert.Equal(previousAppDetails.Scopes.Count, 3); Assert.Equal(updateApp.Scopes.Count, 5); Assert.NotEqual(updateApp.Scopes.Count, previousAppDetails.Scopes.Count); #endregion #region Name Assert Assert.Equal(previousAppDetails.Name, _stringConstant.Name); Assert.Equal(updateApp.Name, _stringConstant.ProjectName); Assert.NotEqual(updateApp.Name, previousAppDetails.Name); #endregion }
/// <summary> /// This method used for added consumer app and return consumerApps Id. -An /// </summary> /// <param name="consumerApp">App details as object</param> public async Task AddConsumerAppsAsync(ConsumerApps consumerApp) { if (await _clientDataRepository.FirstOrDefaultAsync(x => x.ClientId == consumerApp.AuthId) == null) { var clientApp = ReturnIdentityServerClientFromConsumerApp(consumerApp, ReturnListOfScopesInStringFromEnumAllowedScope(consumerApp.Scopes)); _clientDataRepository.Add(clientApp.ToEntity()); await _clientDataRepository.SaveChangesAsync(); } else { throw new ConsumerAppNameIsAlreadyExists(); } }
/// <summary> /// This method used for update consumer app and return primary key. -An /// </summary> /// <param name="consumerApps">App details as object</param> /// <returns>updated app details</returns> public async Task UpdateConsumerAppsAsync(ConsumerApps consumerApps) { var client = await _clientDataRepository.FirstOrDefaultAsync(x => x.Id == consumerApps.Id); client.ClientName = consumerApps.Name; client.ClientId = consumerApps.AuthId; _clientDataRepository.UpdateAsync(client); await _clientDataRepository.SaveChangesAsync(); await UpdateClientSecret(client.Id, consumerApps.AuthSecret); await UpdateClientScope(client.Id, ReturnListOfScopesInStringFromEnumAllowedScope(consumerApps.Scopes)); await UpdateClientRedirectUri(client.Id, consumerApps.CallbackUrl); await UpdateClientLogoutRedirectUri(client.Id, consumerApps.LogoutUrl); }
/// <summary> /// This method used for get valid object with data. /// </summary> /// <returns></returns> private ConsumerApps GetConsumerApp() { ConsumerApps comnsumerApp = new ConsumerApps(); comnsumerApp.CallbackUrl = _stringConstant.PromactErpUrlForTest; comnsumerApp.AuthId = _stringConstant.RandomClientId; comnsumerApp.AuthSecret = _stringConstant.RandomClientSecret; comnsumerApp.LogoutUrl = _stringConstant.PromactErpUrlForTest; comnsumerApp.Name = _stringConstant.Name; comnsumerApp.Scopes = new List <AllowedScope>() { AllowedScope.email, AllowedScope.openid, AllowedScope.profile, }; return(comnsumerApp); }
/// <summary> /// Method to convert ConsumerApp object to IdentityServer4 Client /// </summary> /// <param name="consumerApp">App details as object of ConsumerApp</param> /// <param name="allowedScopes">list of allowed scopes</param> /// <returns></returns> private IdentityServer4.Models.Client ReturnIdentityServerClientFromConsumerApp(ConsumerApps consumerApp, List <string> allowedScopes) { return(new IdentityServer4.Models.Client { ClientId = consumerApp.AuthId, ClientName = consumerApp.Name, AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, AllowAccessTokensViaBrowser = true, // where to redirect to after login RedirectUris = { consumerApp.CallbackUrl }, // where to redirect to after logout PostLogoutRedirectUris = { consumerApp.LogoutUrl }, AllowedScopes = allowedScopes, ClientSecrets = new List <IdentityServer4.Models.Secret>() { new IdentityServer4.Models.Secret(consumerApp.AuthSecret.Sha256()) }, AllowOfflineAccess = true, RefreshTokenUsage = TokenUsage.ReUse, AccessTokenLifetime = 86400, AuthorizationCodeLifetime = 86400, IdentityTokenLifetime = 86400, AbsoluteRefreshTokenLifetime = 5184000, SlidingRefreshTokenLifetime = 5184000 }); }
public async Task <IActionResult> UpdateConsumerAppAsync([FromBody] ConsumerApps consumerApp) { return(Ok(await _consumerAppRepository.UpdateConsumerAppsAsync(consumerApp))); }