-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
157 lines (144 loc) · 6.21 KB
/
Program.cs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Copyright (C) 2022, Manuel Meitinger
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using Microsoft.Web.WebView2.Core;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Web;
using System.Windows.Forms;
namespace OlatAccessibilityApp
{
internal static class Program
{
public static T GetResource<T>(string name, Func<Stream, T> reader)
{
using var stream = typeof(Program).Assembly.GetManifestResourceStream(typeof(Program), name);
return reader(stream);
}
private static string? GetSetting([CallerMemberName] string name = "") => ConfigurationManager.AppSettings.Get(name);
public static Uri BaseUri => new(GetSetting() ?? throw new ConfigurationErrorsException());
public static string Caption => GetSetting() ?? "OpenOlat - infinite learning";
public static string UserDataPath => GetSetting() switch
{
null or "" => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), typeof(Program).Assembly.GetName().Name),
string path => Environment.ExpandEnvironmentVariables(path),
};
public static string? WebView2Path => GetSetting() switch
{
null or "" => null,
string path => Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), Environment.ExpandEnvironmentVariables(path)),
};
[STAThread]
public static void Main()
{
Credential? credential = null;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm mainForm = new();
SplashForm splashForm = new();
ApplicationContext context = new(splashForm);
mainForm.WebView.CoreWebView2InitializationCompleted += OnCoreWebView2InitializationCompleted;
mainForm.WebView.NavigationCompleted += OnNavigationComplete;
mainForm.WebView.NavigationStarting += OnNavitationStarting;
mainForm.WebView.EnsureCoreWebView2Async();
Application.Run(context);
void Login(bool alwaysQueryForCredential = false)
{
if (credential is null || alwaysQueryForCredential)
{
credential = Credential.Query(splashForm.Handle, credential);
}
if (credential is not null)
{
// do _not_ use using for content or ReadAsStreamAsync
var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{ "o_fiooolat_login_name", credential.UserName },
{ "o_fiooolat_login_pass", credential.Password },
});
var request = mainForm.WebView.CoreWebView2.Environment.CreateWebResourceRequest
(
new Uri(BaseUri, "/dmz/1%3A1%3Aoolat_login%3A1%3A0%3Aofo_%3Afid/").AbsoluteUri,
"POST",
content.ReadAsStreamAsync().Result,
content.Headers.ToString()
);
mainForm.WebView.CoreWebView2.NavigateWithWebResourceRequest(request);
}
else
{
splashForm.Close();
}
}
void OnCoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (e.IsSuccess)
{
credential = Credential.Read();
Login();
}
else
{
ReportError(e.InitializationException.Message);
splashForm.Close();
}
}
void OnNavigationComplete(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
if (!e.IsSuccess)
{
if (e.WebErrorStatus is not (CoreWebView2WebErrorStatus.OperationCanceled or CoreWebView2WebErrorStatus.ConnectionAborted))
{
ReportError(e.WebErrorStatus.ToString());
context.MainForm.Close();
}
}
else if (new Uri(BaseUri, "/auth/").IsBaseOf(mainForm.WebView.Source))
{
if (context.MainForm == splashForm)
{
context.MainForm = mainForm;
mainForm.Show();
splashForm.Close();
}
}
else
{
if (context.MainForm == splashForm)
{
Login(alwaysQueryForCredential: true);
}
else
{
context.MainForm.Close();
}
}
}
void OnNavitationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
{
if (!BaseUri.IsBaseOf(new Uri(e.Uri)))
{
e.Cancel = true;
mainForm.WebView.ExecuteScriptAsync($"window.open({HttpUtility.JavaScriptStringEncode(e.Uri, addDoubleQuotes: true)});");
}
}
void ReportError(string message) => MessageBox.Show(context.MainForm, message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}