public Task InvokeAsync(HttpContext httpContext)
        {
            if (!httpContext.User.Identity.IsAuthenticated)
            {
                return(_next(httpContext));
            }

            var organisations = httpContext.User.Claims
                                .Where(t => t.Type == InssClaimTypes.Organisation)
                                .Select(c => OrganisationClaimType.FromClaim(c))
                                .ToList();

            if (organisations.Count == 0)
            {
                return(_next(httpContext));
            }

            if (organisations.Count == 1)
            {
                httpContext.SetOrganisationId(Guid.Parse(organisations.First().Id));
                return(_next(httpContext));
            }

            SetCurrentOrganisationFromHeader(httpContext, organisations);
            return(_next(httpContext));
        }
Exemple #2
0
        public async Task GetProfileDataAsync_WithMultipleOrganisationRolesNotMatching_ReturnsClaims()
        {
            // Arrange
            ConfigureRepositoryWith(MultipleOrganisations, MultipleRolesNotMatchingAnyOrganisation);
            var context = new ProfileDataRequestContext();

            context.RequestedClaimTypes = new List <string>()
            {
                InssClaimTypes.Organisation
            };
            context.Subject = GetSubject();


            // Act
            var sut = CreateSut();

            // Assert
            await sut.GetProfileDataAsync(context);

            context.IssuedClaims.Count.Should().Be(MultipleOrganisations.Count);
            foreach (var claim in context.IssuedClaims)
            {
                var orgClaim     = OrganisationClaimType.FromClaim(claim);
                var organisation = MultipleOrganisations.Where(o => o.ExternalId == orgClaim.Id).SingleOrDefault();
                organisation.Should().NotBeNull();

                var roles = MultipleRolesNotMatchingAnyOrganisation.Where(r => r.OrganisationId == organisation.Id).Select(r => r.Role).Distinct();;

                orgClaim.Id.Should().Be(organisation.ExternalId);
                orgClaim.Name.Should().Be(organisation.Name);
                orgClaim.OrganisationTypeName.Should().Be(organisation.Type);
                orgClaim.CurrentUserRoles.Should().BeEquivalentTo(roles);
            }
        }
Exemple #3
0
        public async Task GetProfileDataAsync_WithOneOrganisationDuplicateRoles_ReturnsOneClaim()
        {
            // Arrange
            ConfigureRepositoryWith(OneOrganisation, DuplicateRolesMatchingOneOrganisation);
            var context = new ProfileDataRequestContext();

            context.RequestedClaimTypes = new List <string>()
            {
                InssClaimTypes.Organisation
            };
            context.Subject = GetSubject();


            // Act
            var sut = CreateSut();

            // Assert
            await sut.GetProfileDataAsync(context);

            context.IssuedClaims.Count.Should().Be(1);
            var orgClaim = context.IssuedClaims.Select(c => OrganisationClaimType.FromClaim(c)).ToList()[0];

            orgClaim.Id.Should().Be(OneOrganisation[0].ExternalId);
            orgClaim.Name.Should().Be(OneOrganisation[0].Name);
            orgClaim.OrganisationTypeName.Should().Be(OneOrganisation[0].Type);
            orgClaim.CurrentUserRoles.Should().BeEquivalentTo(DuplicateRolesMatchingOneOrganisation.Select(r => r.Role).Distinct());
        }
Exemple #4
0
 public static OrganisationModel FromClaimType(OrganisationClaimType claimType)
 {
     return(new OrganisationModel
     {
         Id = Guid.Parse(claimType.Id),
         Name = claimType.Name,
         OrganisationTypeName = claimType.OrganisationTypeName
     });
 }
        public static List <OrganisationModel> GetAvailableOrganisations(this ClaimsPrincipal user)
        {
            var organisationClaims = user.Claims.Where(c => c.Type == InssClaimTypes.Organisation).ToList();

            return(organisationClaims
                   .Select(claim => OrganisationClaimType.FromClaim(claim))
                   .Select(claimType => OrganisationModel.FromClaimType(claimType))
                   .ToList());
        }
Exemple #6
0
 public static void SetOrganisation(
     this HttpContext httpContext,
     OrganisationClaimType organisation)
 => httpContext.Items.Add(Constants.OrganisationItemsKey, organisation);
Exemple #7
0
        public static Claim ToClaim(this OrganisationClaimType organisation)
        {
            var value = JsonSerializer.Serialize(organisation);

            return(new Claim(InssClaimTypes.Organisation, value));
        }