public ClientAccessToken AddClientAccessToken(int apiClientId, string tokenRequestScope = null) { using (var context = _contextFactory.CreateContext()) { var client = context.Clients .FirstOrDefault(c => c.ApiClientId == apiClientId); if (client == null) { throw new InvalidOperationException("Cannot add client access token when the client does not exist."); } // Get the config value, defaulting to 1 hour if (!int.TryParse(_configValueProvider.GetValue("BearerTokenTimeoutMinutes"), out int duration)) { duration = DefaultDuration; } var token = new ClientAccessToken(TimeSpan.FromMinutes(duration)) { Scope = string.IsNullOrEmpty(tokenRequestScope) ? null : tokenRequestScope.Trim() }; client.ClientAccessTokens.Add(token); context.SaveChanges(); return(token); } }
private static void MockInstanceRegistrationSetup(List <OdsInstanceRegistration> odsInstanceRegistrations) { var odsInstances = odsInstanceRegistrations.Select(x => new OdsInstance { OdsInstanceId = x.Id, Name = x.Name, InstanceType = "Ods", IsExtended = false, Status = "OK", Version = "1.0.0" }).ToList(); var applications = odsInstances.Select(x => new Application { ApplicationName = x.Name.GetAdminApplicationName(), OperationalContextUri = OperationalContext.DefaultOperationalContextUri, OdsInstance = x }).ToList(); foreach (var application in applications) { var client = new ApiClient { Name = application.ApplicationName, Key = "test key", Secret = "test secret", ActivationCode = "test activation code" }; var clientAccessToken = new ClientAccessToken { ApiClient = client, Expiration = DateTime.Now.AddDays(1) }; var appEduOrganization = new ApplicationEducationOrganization { Application = application, EducationOrganizationId = application.OdsInstance.Name.ExtractNumericInstanceSuffix() }; client.ClientAccessTokens.Add(clientAccessToken); application.ApiClients.Add(client); application.ApplicationEducationOrganizations.Add(appEduOrganization); } Scoped <IUsersContext>(database => { foreach (var odsInstance in odsInstances) { database.OdsInstances.Add(odsInstance); } foreach (var application in applications) { database.Applications.Add(application); } database.SaveChanges(); }); }
protected override void Arrange() { base.Arrange(); var vendor = LoadAVendor(); var application = LoadAnApplication(vendor, "whatever"); var apiClient = LoadAnApiClient(application); _accessToken = LoadAnAccessToken(apiClient, DateTime.UtcNow.AddSeconds(-10)); }
public void ShouldDeleteApplicationWithClient() { var application = new Application { ApplicationName = "test application", OperationalContextUri = OperationalContext.DefaultOperationalContextUri }; var client = new ApiClient { Name = "test client", Key = "n/a", Secret = "n/a", ActivationCode = "fake activation code" }; var clientAccessToken = new ClientAccessToken { ApiClient = client, Expiration = DateTime.Now.AddDays(1) }; client.ClientAccessTokens.Add(clientAccessToken); application.ApiClients.Add(client); Save(application); var applicationId = application.ApplicationId; applicationId.ShouldBeGreaterThan(0); var clientId = client.ApiClientId; clientId.ShouldBeGreaterThan(0); var tokenId = clientAccessToken.Id; tokenId.ShouldNotBe(Guid.Empty); Scoped <IUsersContext>(usersContext => { var deleteApplicationCommand = new DeleteApplicationCommand(usersContext); deleteApplicationCommand.Execute(applicationId); }); Transaction(usersContext => usersContext.Applications.Where(a => a.ApplicationId == applicationId).ToArray()).ShouldBeEmpty(); Transaction(usersContext => usersContext.Clients.Where(c => c.ApiClientId == clientId).ToArray()).ShouldBeEmpty(); }
public ClientAccessToken AddClientAccessToken(int apiClientId, string tokenRequestScope = null) { using (var context = _contextFactory.CreateContext()) { var client = context.Clients.FirstOrDefault(c => c.ApiClientId == apiClientId); if (client == null) { throw new InvalidOperationException("Cannot add client access token when the client does not exist."); } var token = new ClientAccessToken(TimeSpan.FromMinutes(_duration.Value)) { Scope = string.IsNullOrEmpty(tokenRequestScope) ? null : tokenRequestScope.Trim() }; client.ClientAccessTokens.Add(token); context.SaveChanges(); return(token); } }
public async Task <Response <string> > ConnectTokenAsync() { Response <string> response = null; ClientAccessToken currentToken = await clientAccessTokenCache.GetAsync("WebClientToken"); if (currentToken is not null) { response = Response <string> .Success(currentToken.AccessToken, StatusCodes.Status200OK); logger.LogResponse(response); return(response); } TokenResponse res = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest() { Address = "connect/token", ClientId = clientSettings.WebClient.ClientId, ClientSecret = clientSettings.WebClient.ClientSecret, Scope = "IdentityServerApi" }); if (res.IsError) { response = await res.GetResponseAsync <string>(true, "TokenService/ConnectTokenAsync", "Token alınırken beklenmedik bir hata ile karşılaşıldı"); logger.LogResponse(response); return(response); } await clientAccessTokenCache.SetAsync("WebClientToken", res.AccessToken, res.ExpiresIn); response = Response <string> .Success(res.AccessToken, (int)res.HttpStatusCode); logger.LogResponse(response); return(response); }
protected override Task ArrangeAsync() { _suppliedClient = new ApiClient { ApiClientId = 1 }; _suppliedAccessToken = Guid.NewGuid(); _suppliedTTL = TimeSpan.FromMinutes(30); _accessTokenClientRepo = Stub <IAccessTokenClientRepo>(); _apiClientAuthenticator = A.Fake <IApiClientAuthenticator>(); var accessToken = new ClientAccessToken(_suppliedTTL) { ApiClient = _suppliedClient, Id = _suppliedAccessToken }; A.CallTo(() => _accessTokenClientRepo.AddClientAccessTokenAsync(A <int> ._, A <string> ._)) .Returns(accessToken); A.CallTo(() => _apiClientAuthenticator.TryAuthenticateAsync(A <string> ._, A <string> ._)) .Returns( Task.FromResult( new ApiClientAuthenticator.AuthenticationResult { IsAuthenticated = true, ApiClientIdentity = new ApiClientIdentity { Key = "clientId", ApiClientId = _suppliedClient.ApiClientId }, })); _controller = ControllerHelper.CreateTokenController(_apiClientAuthenticator, _accessTokenClientRepo); return(Task.CompletedTask); }
public void SetToken(ClientAccessToken token) { Access_token = token.Id.ToString("N"); Expires_in = (int) token.Duration.TotalSeconds; Scope = token.Scope; }