FreeRDP
Loading...
Searching...
No Matches
qt-aad-helper/redirect_watcher.cpp
1
21#include "redirect_watcher.hpp"
22
23#include <map>
24#include <regex>
25#include <vector>
26
27#include <winpr/string.h>
28
29namespace
30{
31 std::string from_url_encoded_str(const std::string& str)
32 {
33 std::string cxxstr;
34 auto cstr = winpr_str_url_decode(str.c_str(), str.length());
35 if (cstr)
36 {
37 cxxstr = std::string(cstr);
38 free(cstr);
39 }
40 return cxxstr;
41 }
42
43 std::vector<std::string> split(const std::string& input, const std::string& regex)
44 {
45 // passing -1 as the submatch index parameter performs splitting
46 std::regex re(regex);
47 std::sregex_token_iterator first{ input.begin(), input.end(), re, -1 };
48 std::sregex_token_iterator last;
49 return { first, last };
50 }
51
52 std::map<std::string, std::string> urlsplit(const std::string& url)
53 {
54 auto pos = url.find('?');
55 if (pos == std::string::npos)
56 return {};
57
58 pos++; // skip '?'
59 auto surl = url.substr(pos);
60 auto args = split(surl, "&");
61
62 std::map<std::string, std::string> argmap;
63 for (const auto& arg : args)
64 {
65 auto kv = split(arg, "=");
66 if (kv.size() == 2)
67 argmap.insert({ kv[0], kv[1] });
68 }
69
70 return argmap;
71 }
72} // namespace
73
74RedirectWatcher::RedirectWatcher(std::string redirectUri) : _redirect_uri(std::move(redirectUri))
75{
76}
77
78bool RedirectWatcher::matches(const std::string& uri) const
79{
80 if (_redirect_uri.empty())
81 return false;
82
83 std::string duri = from_url_encoded_str(uri);
84 if (duri.length() < _redirect_uri.length())
85 return false;
86
87 return _strnicmp(duri.c_str(), _redirect_uri.c_str(), _redirect_uri.length()) == 0;
88}
89
90bool RedirectWatcher::hasError(const std::string& uri, std::string& error) const
91{
92 auto args = urlsplit(uri);
93 auto err = args.find("error");
94 if (err == args.end())
95 return false;
96
97 error = err->second;
98 auto suberr = args.find("error_subcode");
99 if (suberr != args.end())
100 error += ": " + suberr->second;
101
102 return true;
103}