-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
216 lines (190 loc) · 7.91 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using JWT.Algorithms;
using JWT.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Web;
namespace MyHome
{
public static class Program
{
private static readonly Logger logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
public static void Main(string[] args)
{
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
try
{
logger.Debug("Init main");
var builder = WebApplication.CreateBuilder(args);
// setup logging
builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
builder.Host.UseNLog();
// setup services
ConfigureServices(builder.Services);
var app = builder.Build();
Configure(app, app.Services.GetService<MyHome>());
app.Run();
}
catch (Exception exception)
{
//NLog: catch setup errors
logger.Error("Stopped program because of exception");
logger.Debug(exception);
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
LogManager.Shutdown();
}
}
private static void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(new MyHome());
services.AddControllers();
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromHours(2);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
services.AddResponseCompression();
services.AddOpenApi();
}
private static void Configure(WebApplication app, MyHome myHome)
{
if (app.Environment.IsDevelopment())
{
logger.Warn("Running in development mode");
app.UseDeveloperExceptionPage();
}
app.UseSession();
app.UseResponseCompression();
app.Use(async (context, next) =>
{
if (Login(context, myHome))
await next();
});
var fileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "UI"));
app.UseDefaultFiles(new DefaultFilesOptions { FileProvider = fileProvider });
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = fileProvider,
OnPrepareResponse = context =>
{
// allow cache for 1 day
context.Context.Response.Headers.CacheControl = "private, max-age=86400, stale-while-revalidate=86400";
}
});
app.UseWebSockets();
app.UseRouting();
app.MapControllers();
app.MapOpenApi();
app.Lifetime.ApplicationStopping.Register(myHome.Stop);
}
private static bool Login(HttpContext context, MyHome myHome)
{
if (context.Request.Path == "/login" && context.Request.Method == "POST")
{
return Authenticate(context, myHome);
}
else if (!ShouldSkipAuthentication(context.Request.Path) && !Authenticated(context, myHome))
{
if (!context.Request.Path.StartsWithSegments("/api")) // pages only
context.Response.Redirect("./login.html");
else
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
context.Session.Clear();
return false;
}
context.Session.SetString("time", DateTime.Now.ToString());
return true;
}
private static bool Authenticate(HttpContext context, MyHome myHome)
{
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(context.Request.Form["password"]));
var hashStr = Convert.ToHexString(hash);
if (myHome.Config.Password == hashStr)
{
if (context.Request.Form.ContainsKey("token")) // generate JWT token
{
var expiration = DateTimeOffset.UtcNow.Add(GetExpirationTime(context, myHome));
var token = JwtBuilder.Create()
.WithAlgorithm(new HMACSHA256Algorithm())
.WithSecret(myHome.Config.Password)
.AddClaim("exp", expiration.ToUnixTimeSeconds())
.Encode();
context.Response.StatusCode = StatusCodes.Status200OK;
context.Response.Body.WriteAsync(Encoding.UTF8.GetBytes(token));
}
else
{
logger.Info("LogIn: Correct password");
context.Session.SetString("password", hashStr);
context.Response.Redirect("./");
}
}
else
{
logger.Warn("LogIn: Incorrect password");
context.Response.Redirect("./login.html?invalid");
}
return false;
}
private static bool ShouldSkipAuthentication(PathString path)
{
var isResource = path.StartsWithSegments("/external") || path.StartsWithSegments("/images") ||
path == "/scripts.js" || path == "/style.css" || path == "/login.html";
return isResource || path.StartsWithSegments("/api/status") || path.StartsWithSegments("/api/songs");
}
private static bool Authenticated(HttpContext context, MyHome myHome)
{
// JWT token authentication
if (!string.IsNullOrEmpty(context.Request.Headers.Authorization))
{
try
{
string token = JwtBuilder.Create()
.WithAlgorithm(new HMACSHA256Algorithm())
.WithSecret(myHome.Config.Password)
.MustVerifySignature()
.Decode(context.Request.Headers.Authorization);
return !string.IsNullOrEmpty(token);
}
catch
{
return false;
}
}
// session authentication
return (context.Session.GetString("password") ?? "") == myHome.Config.Password && !IsSessionExpired(context, myHome);
}
private static bool IsSessionExpired(HttpContext context, MyHome myHome)
{
if (!context.Session.Keys.Contains("time"))
return false;
var time = DateTime.Parse(context.Session.GetString("time"), CultureInfo.CurrentCulture);
var duration = GetExpirationTime(context, myHome);
return DateTime.Now - time > duration;
}
private static TimeSpan GetExpirationTime(HttpContext context, MyHome myHome)
{
var ip = context.Connection.RemoteIpAddress.MapToIPv4();
var knownIp = myHome.SecuritySystem.PresenceDeviceIPs.ContainsValue(ip.ToString());
return knownIp ? TimeSpan.FromMinutes(120) : TimeSpan.FromMinutes(15);// if known device set session duration to 2h
}
}
}