public async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            var user = new InsolvencyUser(context.Subject);

            context.AddRequestedClaims(user.GetInsolvencyClaims());
            await AddOrganisationClaims(context, user);
        }
Example #2
0
        public virtual async Task <ScpAuthenticationResponse> ProcessScpAuthenticationAsync()
        {
            var httpContext = _httpContextAccessor.HttpContext;
            var result      = await httpContext.AuthenticateAsync(IdentityServerConstants.ExternalCookieAuthenticationScheme);

            LogExternalAuthentication(result);
            if (result?.Succeeded != true)
            {
                return(new ScpAuthenticationResponse {
                    IsSuccessful = false
                });
            }

            var user = new InsolvencyUser(result.Principal);

            var scpClaimsValidationResult = new Validation.InsolvencyUserValidator().Validate(user);

            if (!scpClaimsValidationResult.IsValid)
            {
                _logger.LogError("Error during processing SCP Principal. Unable to extract all required SCP claims from the SCP Principal.");
                _logger.LogError(String.Join(", ", scpClaimsValidationResult.Errors.Select(p => p.ErrorMessage)));
                return(new ScpAuthenticationResponse {
                    IsSuccessful = false
                });
            }

            var response = new ScpAuthenticationResponse
            {
                InsolvencyUser = user,
                ReturnUrl      = result.Properties.Items["returnUrl"] ?? "~/",
                IsSuccessful   = true
            };

            var idToken = result.Properties.GetTokenValue("id_token");

            if (idToken != null)
            {
                response.AuthenticationProperties.StoreTokens(new[] { new AuthenticationToken {
                                                                          Name = "id_token", Value = idToken
                                                                      } });
            }

            return(response);
        }
        protected virtual async Task AddOrganisationClaims(ProfileDataRequestContext context, InsolvencyUser user)
        {
            if (string.IsNullOrEmpty(user.ScpGroupId))
            {
                return;
            }
            if (string.IsNullOrEmpty(user.Email))
            {
                return;
            }

            var organisations = await _iIdentityManagementRepository.GetOrganisationByScpGroupIdAsync(user.ScpGroupId);

            if (organisations.Count == 0)
            {
                _logger.LogWarning($"No organisations for user with ScpGroupId: {user.ScpGroupId} could be found! Unable to add requested claims.");
                return;
            }

            context.AddRequestedClaims(CreateOrganisationClaims(organisations, user.Email));
        }