Beispiel #1
0
        public Response SetQueriesOnContext(List <ClaimToThing> claimsToThings, HttpContext context)
        {
            var queryDictionary = ConvertQueryStringToDictionary(context);

            foreach (var config in claimsToThings)
            {
                var value = _claimsParser.GetValue(context.User.Claims, config.NewKey, config.Delimiter, config.Index);

                if (value.IsError)
                {
                    return(new ErrorResponse(value.Errors));
                }

                var exists = queryDictionary.FirstOrDefault(x => x.Key == config.ExistingKey);

                if (!string.IsNullOrEmpty(exists.Key))
                {
                    queryDictionary[exists.Key] = value.Data;
                }
                else
                {
                    queryDictionary.Add(config.ExistingKey, value.Data);
                }
            }

            context.Request.QueryString = ConvertDictionaryToQueryString(queryDictionary);

            return(new OkResponse());
        }
        public Response ChangeDownstreamPath(List <ClaimToThing> claimsToThings, IEnumerable <Claim> claims,
                                             DownstreamPathTemplate downstreamPathTemplate, List <PlaceholderNameAndValue> placeholders)
        {
            foreach (var config in claimsToThings)
            {
                var value = _claimsParser.GetValue(claims, config.NewKey, config.Delimiter, config.Index);

                if (value.IsError)
                {
                    return(new ErrorResponse(value.Errors));
                }

                var placeholderName = $"{{{config.ExistingKey}}}";

                if (!downstreamPathTemplate.Value.Contains(placeholderName))
                {
                    return(new ErrorResponse(new CouldNotFindPlaceholderError(placeholderName)));
                }

                if (placeholders.Any(ph => ph.Name == placeholderName))
                {
                    placeholders.RemoveAll(ph => ph.Name == placeholderName);
                }

                placeholders.Add(new PlaceholderNameAndValue(placeholderName, value.Data));
            }

            return(new OkResponse());
        }
Beispiel #3
0
        public Response SetClaimsOnContext(List <ClaimToThing> claimsToThings, HttpContext context)
        {
            foreach (var config in claimsToThings)
            {
                var value = _claimsParser.GetValue(context.User.Claims, config.NewKey, config.Delimiter, config.Index);

                if (value.IsError)
                {
                    return(new ErrorResponse(value.Errors));
                }

                var exists = context.User.Claims.FirstOrDefault(x => x.Type == config.ExistingKey);

                var identity = context.User.Identity as ClaimsIdentity;

                if (exists != null)
                {
                    identity?.RemoveClaim(exists);
                }

                identity?.AddClaim(new System.Security.Claims.Claim(config.ExistingKey, value.Data));
            }

            return(new OkResponse());
        }
Beispiel #4
0
        public Response <bool> Authorise(ClaimsPrincipal claimsPrincipal, Dictionary <string, string> routeClaimsRequirement)
        {
            foreach (var required in routeClaimsRequirement)
            {
                var value = _claimsParser.GetValue(claimsPrincipal.Claims, required.Key, string.Empty, 0);

                if (value.IsError)
                {
                    return(new ErrorResponse <bool>(value.Errors));
                }

                if (value.Data != null)
                {
                    var authorised = value.Data == required.Value;
                    if (!authorised)
                    {
                        return(new ErrorResponse <bool>(new List <Error>
                        {
                            new ClaimValueNotAuthorisedError(
                                $"claim value: {value.Data} is not the same as required value: {required.Value} for type: {required.Key}")
                        }));
                    }
                }
                else
                {
                    return(new ErrorResponse <bool>(new List <Error>
                    {
                        new UserDoesNotHaveClaimError($"user does not have claim {required.Key}")
                    }));
                }
            }
            return(new OkResponse <bool>(true));
        }
        public Response SetQueriesOnDownstreamRequest(List <ClaimToThing> claimsToThings, IEnumerable <Claim> claims, HttpRequestMessage downstreamRequest)
        {
            var queryDictionary = ConvertQueryStringToDictionary(downstreamRequest.RequestUri.Query);

            foreach (var config in claimsToThings)
            {
                var value = _claimsParser.GetValue(claims, config.NewKey, config.Delimiter, config.Index);

                if (value.IsError)
                {
                    return(new ErrorResponse(value.Errors));
                }

                var exists = queryDictionary.FirstOrDefault(x => x.Key == config.ExistingKey);

                if (!string.IsNullOrEmpty(exists.Key))
                {
                    queryDictionary[exists.Key] = value.Data;
                }
                else
                {
                    queryDictionary.Add(config.ExistingKey, value.Data);
                }
            }

            var uriBuilder = new UriBuilder(downstreamRequest.RequestUri);

            uriBuilder.Query = ConvertDictionaryToQueryString(queryDictionary);

            downstreamRequest.RequestUri = uriBuilder.Uri;

            return(new OkResponse());
        }
Beispiel #6
0
        public Response SetHeadersOnDownstreamRequest(List <ClaimToThing> claimsToThings, IEnumerable <System.Security.Claims.Claim> claims, DownstreamRequest downstreamRequest)
        {
            foreach (var config in claimsToThings)
            {
                var value = _claimsParser.GetValue(claims, config.NewKey, config.Delimiter, config.Index);

                if (value.IsError)
                {
                    return(new ErrorResponse(value.Errors));
                }

                var exists = downstreamRequest.Headers.FirstOrDefault(x => x.Key == config.ExistingKey);

                if (!string.IsNullOrEmpty(exists.Key))
                {
                    downstreamRequest.Headers.Remove(exists.Key);
                }

                downstreamRequest.Headers.Add(config.ExistingKey, value.Data);
            }

            return(new OkResponse());
        }
Beispiel #7
0
        public Response SetHeadersOnContext(List <ClaimToThing> claimsToThings, HttpContext context)
        {
            foreach (var config in claimsToThings)
            {
                var value = _claimsParser.GetValue(context.User.Claims, config.NewKey, config.Delimiter, config.Index);

                if (value.IsError)
                {
                    return(new ErrorResponse(value.Errors));
                }

                var exists = context.Request.Headers.FirstOrDefault(x => x.Key == config.ExistingKey);

                if (!string.IsNullOrEmpty(exists.Key))
                {
                    context.Request.Headers.Remove(exists);
                }

                context.Request.Headers.Add(config.ExistingKey, new StringValues(value.Data));
            }

            return(new OkResponse());
        }
 private void WhenICallTheParser()
 {
     _result = _claimsParser.GetValue(_claims, _key, _delimiter, _index);
 }