summaryrefslogtreecommitdiffstats
path: root/client/SDL/aad/wrapper/webview_impl.cpp
blob: 5f4d3d5972a14e17589f3ad2957812cde7fbbe28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * Popup browser for AAD authentication
 *
 * Copyright 2023 Isaac Klein <fifthdegree@protonmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *		 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "webview.h"

#include <assert.h>
#include <string>
#include <vector>
#include <map>
#include <regex>
#include <sstream>
#include "../webview_impl.hpp"

static std::vector<std::string> split(const std::string& input, const std::string& regex)
{
	// passing -1 as the submatch index parameter performs splitting
	std::regex re(regex);
	std::sregex_token_iterator first{ input.begin(), input.end(), re, -1 };
	std::sregex_token_iterator last;
	return { first, last };
}

static std::map<std::string, std::string> urlsplit(const std::string& url)
{
	auto pos = url.find("?");
	if (pos == std::string::npos)
		return {};
	auto surl = url.substr(pos);
	auto args = split(surl, "&");

	std::map<std::string, std::string> argmap;
	for (const auto& arg : args)
	{
		auto kv = split(arg, "=");
		if (kv.size() == 2)
			argmap.insert({ kv[0], kv[1] });
	}
	return argmap;
}

static void fkt(const std::string& url, void* arg)
{
	auto args = urlsplit(url);
	auto val = args.find("code");
	if (val == args.end())
		return;

	assert(arg);
	auto rcode = static_cast<std::string*>(arg);
	*rcode = val->second;
}

bool webview_impl_run(const std::string& title, const std::string& url, std::string& code)
{
	webview::webview w(false, nullptr);

	w.set_title(title);
	w.set_size(640, 480, WEBVIEW_HINT_NONE);

	std::string scheme;
	w.add_scheme_handler("ms-appx-web", fkt, &scheme);
	w.add_navigate_listener(fkt, &code);
	w.navigate(url);
	w.run();
	return !code.empty();
}