public static OpenIddictAuthorizationModel ToModel(this OpenIddictAuthorization entity) { if (entity == null) { return(null); } var model = new OpenIddictAuthorizationModel { Id = entity.Id, ApplicationId = entity.ApplicationId, CreationDate = entity.CreationDate, Properties = entity.Properties, Scopes = entity.Scopes, Status = entity.Status, Subject = entity.Subject, Type = entity.Type }; foreach (var extraProperty in entity.ExtraProperties) { model.ExtraProperties.Add(extraProperty.Key, extraProperty.Value); } return(model); }
public async Task ValidateToken_ReturnsValidResultForValidAuthorization() { // Arrange var authorization = new OpenIddictAuthorization(); var format = new Mock <ISecureDataFormat <AuthenticationTicket> >(); format.Setup(mock => mock.Unprotect("valid-token")) .Returns(delegate { var identity = new ClaimsIdentity(OpenIddictValidationDefaults.AuthenticationScheme); identity.AddClaim(new Claim(OAuthValidationConstants.Claims.Subject, "Fabrikam")); var ticket = new AuthenticationTicket( new ClaimsPrincipal(identity), OpenIddictValidationDefaults.AuthenticationScheme); ticket.SetProperty(OpenIddictConstants.Properties.InternalAuthorizationId, "5230CBAD-89F9-4C3F-B48C-9253B6FB8620"); return(ticket); }); var manager = CreateAuthorizationManager(instance => { instance.Setup(mock => mock.FindByIdAsync("5230CBAD-89F9-4C3F-B48C-9253B6FB8620", It.IsAny <CancellationToken>())) .ReturnsAsync(authorization); instance.Setup(mock => mock.IsValidAsync(authorization, It.IsAny <CancellationToken>())) .ReturnsAsync(true); }); var server = CreateResourceServer(builder => { builder.Services.AddSingleton(manager); builder.EnableAuthorizationValidation(); builder.Configure(options => { options.AccessTokenFormat = format.Object; options.UseReferenceTokens = false; }); }); var client = server.CreateClient(); var request = new HttpRequestMessage(HttpMethod.Get, "/"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "valid-token"); // Act var response = await client.SendAsync(request); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Mock.Get(manager).Verify(mock => mock.FindByIdAsync("5230CBAD-89F9-4C3F-B48C-9253B6FB8620", It.IsAny <CancellationToken>()), Times.Once()); Mock.Get(manager).Verify(mock => mock.IsValidAsync(authorization, It.IsAny <CancellationToken>()), Times.Once()); }
public static OpenIddictAuthorization ToEntity(this OpenIddictAuthorizationModel model) { Check.NotNull(model, nameof(model)); var entity = new OpenIddictAuthorization(model.Id) { ApplicationId = model.ApplicationId, CreationDate = model.CreationDate, Properties = model.Properties, Scopes = model.Scopes, Status = model.Status, Subject = model.Subject, Type = model.Type }; foreach (var extraProperty in model.ExtraProperties) { entity.ExtraProperties.Add(extraProperty.Key, extraProperty.Value); } return(entity); }
public async Task HandleIntrospectionRequest_RequestIsRejectedWhenAuthorizationIsInvalid() { // Arrange var identity = new ClaimsIdentity(OpenIddictServerDefaults.AuthenticationScheme); identity.AddClaim(OpenIddictConstants.Claims.Subject, "Bob le Bricoleur"); var ticket = new AuthenticationTicket( new ClaimsPrincipal(identity), new AuthenticationProperties(), OpenIddictServerDefaults.AuthenticationScheme); ticket.SetAudiences("Fabrikam"); ticket.SetInternalTokenId("3E228451-1555-46F7-A471-951EFBA23A56"); ticket.SetTokenUsage(OpenIdConnectConstants.TokenUsages.AccessToken); var format = new Mock <ISecureDataFormat <AuthenticationTicket> >(); format.Setup(mock => mock.Unprotect("2YotnFZFEjr1zCsicMWpAA")) .Returns(ticket); var authorization = new OpenIddictAuthorization(); var manager = CreateAuthorizationManager(instance => { instance.Setup(mock => mock.FindByIdAsync("18D15F73-BE2B-6867-DC01-B3C1E8AFDED0", It.IsAny <CancellationToken>())) .ReturnsAsync(authorization); instance.Setup(mock => mock.IsValidAsync(authorization, It.IsAny <CancellationToken>())) .ReturnsAsync(false); }); var server = CreateAuthorizationServer(builder => { builder.Services.AddSingleton(CreateApplicationManager(instance => { var application = new OpenIddictApplication(); instance.Setup(mock => mock.FindByClientIdAsync("Fabrikam", It.IsAny <CancellationToken>())) .ReturnsAsync(application); instance.Setup(mock => mock.GetClientTypeAsync(application, It.IsAny <CancellationToken>())) .Returns(new ValueTask <string>(OpenIddictConstants.ClientTypes.Confidential)); instance.Setup(mock => mock.ValidateClientSecretAsync(application, "7Fjfp0ZBr1KtDRbnfVdmIw", It.IsAny <CancellationToken>())) .ReturnsAsync(true); })); builder.Services.AddSingleton(CreateTokenManager(instance => { var token = new OpenIddictToken(); instance.Setup(mock => mock.FindByReferenceIdAsync("QaTk2f6UPe9trKismGBJr0OIs0KqpvNrqRsJqGuJAAI", It.IsAny <CancellationToken>())) .ReturnsAsync(token); instance.Setup(mock => mock.GetTypeAsync(token, It.IsAny <CancellationToken>())) .Returns(new ValueTask <string>(OpenIdConnectConstants.TokenUsages.AccessToken)); instance.Setup(mock => mock.GetIdAsync(token, It.IsAny <CancellationToken>())) .Returns(new ValueTask <string>("3E228451-1555-46F7-A471-951EFBA23A56")); instance.Setup(mock => mock.GetPayloadAsync(token, It.IsAny <CancellationToken>())) .Returns(new ValueTask <string>("2YotnFZFEjr1zCsicMWpAA")); instance.Setup(mock => mock.FindByIdAsync("3E228451-1555-46F7-A471-951EFBA23A56", It.IsAny <CancellationToken>())) .ReturnsAsync(token); instance.Setup(mock => mock.IsValidAsync(token, It.IsAny <CancellationToken>())) .ReturnsAsync(false); instance.Setup(mock => mock.GetAuthorizationIdAsync(token, It.IsAny <CancellationToken>())) .Returns(new ValueTask <string>("18D15F73-BE2B-6867-DC01-B3C1E8AFDED0")); })); builder.Services.AddSingleton(manager); builder.Configure(options => options.AccessTokenFormat = format.Object); builder.UseReferenceTokens(); }); var client = new OpenIdConnectClient(server.CreateClient()); // Act var response = await client.PostAsync(IntrospectionEndpoint, new OpenIdConnectRequest { ClientId = "Fabrikam", ClientSecret = "7Fjfp0ZBr1KtDRbnfVdmIw", Token = "QaTk2f6UPe9trKismGBJr0OIs0KqpvNrqRsJqGuJAAI" }); // Assert Assert.Single(response.GetParameters()); Assert.False((bool)response[OpenIddictConstants.Claims.Active]); Mock.Get(manager).Verify(mock => mock.FindByIdAsync("18D15F73-BE2B-6867-DC01-B3C1E8AFDED0", It.IsAny <CancellationToken>()), Times.Once()); Mock.Get(manager).Verify(mock => mock.IsValidAsync(authorization, It.IsAny <CancellationToken>()), Times.Once()); }
public static OpenIddictAuthorization ToEntity(this OpenIddictAuthorizationModel model, OpenIddictAuthorization entity) { Check.NotNull(model, nameof(model)); Check.NotNull(entity, nameof(entity)); entity.ApplicationId = model.ApplicationId; entity.CreationDate = model.CreationDate; entity.Properties = model.Properties; entity.Scopes = model.Scopes; entity.Status = model.Status; entity.Subject = model.Subject; entity.Type = model.Type; foreach (var extraProperty in model.ExtraProperties) { entity.ExtraProperties.Remove(extraProperty.Key); entity.ExtraProperties.Add(extraProperty.Key, extraProperty.Value); } return(entity); }