Esempio n. 1
0
 public static string Match(this IRegex c, string pattern, string groupName)
 {
     return(c.Match(pattern, groupName, RegexOptions.None));
 }
Esempio n. 2
0
 public static string Match(this IRegex c, string pattern, int index)
 {
     return(c.Match(pattern, index, RegexOptions.None));
 }
Esempio n. 3
0
 public static string Match(this IRegex c, string pattern)
 {
     return(c.Match(pattern, 0));
 }
Esempio n. 4
0
        public static MatchedTextRange?SearchInText(
            this SearchState state,
            StringSlice text,
            int?startTextPosition)
        {
            IRegex re = state.re;

            // matched string position
            int  matchBegin       = 0;    // index of the first matched char
            int  matchEnd         = 0;    // index of following after the last matched one
            bool wholeTextMatched = false;

            if (!string.IsNullOrEmpty(state.options.Template))             // empty/null template means that text matching isn't required, i.e. match any input
            {
                int textPos;

                if (startTextPosition.HasValue)
                {
                    textPos = startTextPosition.Value;
                }
                else if (state.options.ReverseSearch)
                {
                    textPos = text.Length;
                }
                else
                {
                    textPos = 0;
                }

                for (; ;)
                {
                    if (re != null)
                    {
                        if (!re.Match(text, textPos, ref state.searchMatch))
                        {
                            return(null);
                        }
                        matchBegin = state.searchMatch.Index;
                        matchEnd   = matchBegin + state.searchMatch.Length;
                    }
                    else
                    {
                        StringComparison cmp = state.options.MatchCase ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
                        int i;
                        // todo: use running hash
                        if (state.options.ReverseSearch)
                        {
                            i = text.LastIndexOf(state.options.Template, textPos, cmp);
                        }
                        else
                        {
                            i = text.IndexOf(state.options.Template, textPos, cmp);
                        }
                        if (i < 0)
                        {
                            return(null);
                        }
                        matchBegin = i;
                        matchEnd   = matchBegin + state.options.Template.Length;
                    }

                    if (state.options.WholeWord && !IsWordBoundary(text, matchBegin, matchEnd))
                    {
                        textPos = state.options.ReverseSearch ? matchBegin : matchEnd;
                        continue;
                    }

                    break;
                }
            }
            else
            {
                matchBegin       = 0;
                matchEnd         = text.Length;
                wholeTextMatched = true;
            }

            return(new MatchedTextRange(text, matchBegin, matchEnd, wholeTextMatched));
        }
Esempio n. 5
0
        public static string ParseExpression(IRegex regexEngine, string matchedPattern, string expression)
        {
            FlexMatch match = regexEngine.Match(matchedPattern, expression);

            return(match?.Success ?? false?ParseValue(match.Value) : null);
        }
Esempio n. 6
0
        protected override string IsValidDynamicHelper(ref string fingerprintText,
                                                       ref string message)
        {
            var fingerprint = new Fingerprint(fingerprintText);

            string id  = fingerprint.Id;
            string key = fingerprint.Key;

            try
            {
                var iamClient = new AmazonIdentityManagementServiceClient(id, key);

                GetAccountAuthorizationDetailsRequest  request;
                GetAccountAuthorizationDetailsResponse response;

                request  = new GetAccountAuthorizationDetailsRequest();
                response = iamClient.GetAccountAuthorizationDetailsAsync(request).GetAwaiter().GetResult();

                message = BuildAuthorizedMessage(id, response);
            }
            catch (AmazonIdentityManagementServiceException e)
            {
                switch (e.ErrorCode)
                {
                case "AccessDenied":
                {
                    FlexMatch match = RegexEngine.Match(e.Message, AwsUserExpression);

                    // May return a message containing user id details such as:
                    // User: arn:aws:iam::123456123456:user/example.com@@dead1234dead1234dead1234 is not
                    // authorized to perform: iam:GetAccountAuthorizationDetails on resource: *

                    if (match.Success)
                    {
                        int    trimmedChars = "User: "******"is not authorized ".Length;
                        string iamUser      = match.Value.String.Substring("User: "******"the compromised AWS identity is '{iamUser}";
                    }

                    return(nameof(ValidationState.Authorized));
                }

                case "InvalidClientTokenId":
                case "SignatureDoesNotMatch":
                {
                    return(nameof(ValidationState.NoMatch));
                }
                }

                message = $"An unexpected exception was caught attempting to authenticate AWS id '{id}': {e.Message}";
                return(nameof(ValidationState.Unknown));
            }
            catch (Exception e)
            {
                message = $"An unexpected exception was caught attempting to authentic AWS id '{id}': {e.Message}";
                return(nameof(ValidationState.Unknown));
            }

            /*          var client = new HttpClient();
             *
             *          try
             *          {
             *              string uri = "https://iam.amazonaws.com/?Action=GetAccountAuthorizationDetails" +
             *                           "?X-Amz-Algorithm=AWS4-HMAC-SHA256" +
             *                          $"&X-Amz-Credential={id}";
             *
             *              HttpResponseMessage response = client.GetAsync(uri).GetAwaiter().GetResult();
             *
             *              switch (response.StatusCode)
             *              {
             *                  case HttpStatusCode.Forbidden:
             *                  {
             *                      message = $"for AWS credential id '{id}'.";
             *                      return nameof(ValidationState.Unauthorized);
             *                  }
             *              }
             *          }
             *          catch (Exception e)
             *          {
             *              message = $"An unexpected exception was caught attempting to authentic AWS id '{id}': {e.Message}";
             *              return nameof(ValidationState.Unknown);
             *          }
             */
            return(nameof(ValidationState.Authorized));
        }