| 21 | |
| 22 | |
| 23 | string FindAuthenticityToken(string const & action, string body) |
| 24 | { |
| 25 | static boost::regex const kActionAndTokenRE(R"~(action="(.+?)".*?name="authenticity_token" value="(.+?)")~"); |
| 26 | |
| 27 | // Regex doesn't support multiline matches. Need to remove all line endings from the body. |
| 28 | std::erase_if(body, [](char c) { return c == '\n' || c == '\r'; }); |
| 29 | |
| 30 | auto const begin = boost::sregex_iterator{body.begin(), body.end(), kActionAndTokenRE}; |
| 31 | auto const end = boost::sregex_iterator{}; |
| 32 | |
| 33 | for (auto it = begin; it != end; ++it) |
| 34 | { |
| 35 | auto const match = *it; |
| 36 | if (match.size() == 3 && match[1] == action) |
| 37 | return match[2]; |
| 38 | } |
| 39 | MYTHROW(OsmOAuth::AuthenticityTokenNotFound, ("authenticity_token for action", action, "was not found in", body)); |
| 40 | } |
| 41 | |
| 42 | // Parse URL in format "{OSM_OAUTH2_REDIRECT_URI}?code=XXXX". Extract code value |
| 43 | string FindOauthCode(string const & redirectUri) |