-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
180 lines (143 loc) · 6.26 KB
/
Copy pathProgram.cs
File metadata and controls
180 lines (143 loc) · 6.26 KB
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
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting.Internal;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
namespace LinkyShrinky
{
public class Program
{
static string? adminPage = Environment.GetEnvironmentVariable("ADMIN_PAGE");
public static void Main(string[] args)
{
if (adminPage == null)
adminPage = "admin";
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication("Cookies").AddCookie("Cookies", options =>
{
//Path to redirect user if they try to hit a page that requires auth without being authed
options.LoginPath = "/" + adminPage;
});
// Add services to the container.
builder.Services.AddAuthorization();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseForwardedHeaders();
if (!Directory.Exists("config"))
Directory.CreateDirectory("config");
//Load links from urls.json
LinkStore linkStore = new LinkStore("config/urls.json");//app
//Create authenticator
Authenticator authenticator = new Authenticator("config/user.json");
//Serve wwwroot/admin to path defined by adminPage variable
//This allows wwwroot/admin to hold the dashboard files, even if the adminPage variable is something else
//such as "administration"
app.UseDefaultFiles(new DefaultFilesOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.WebRootPath, "admin")),
RequestPath = "/" + adminPage
});
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.WebRootPath, "admin")),
RequestPath = "/" + adminPage
});
//Login attempt
app.MapPost("/" + adminPage + "/login", async (HttpContext context) =>
{
var form = await context.Request.ReadFormAsync();
string? username = form["username"];
string? password = form["password"];
if (username == null || password == null)
return Results.BadRequest();
var results = authenticator.Verify(username, password);
if (results)
{
//Issue cookie
var claims = new[]
{
new Claim(ClaimTypes.Name, username),
new Claim(ClaimTypes.Role, "Admin")
};
var identity = new ClaimsIdentity(claims, "Cookies");
var principal = new ClaimsPrincipal(identity);
await context.SignInAsync("Cookies", principal);
return Results.Redirect("/" + adminPage + "/dashboard");
}
else
{
return Results.Redirect("/" + adminPage + "?error=1");
}
});
app.MapPost("/" + adminPage + "/logout", async (HttpContext context) =>
{
await context.SignOutAsync("Cookies");
return Results.Redirect("/" + adminPage);
}).RequireAuthorization(policy => policy.RequireRole("Admin"));
//TODO: Probably not regex this. Find another way to reserve admin path
app.MapGet("/{slug:regex(^(?!" + adminPage + "$).+)}", (string slug, HttpContext context) =>
{
string clientIP = context.Connection.RemoteIpAddress?.ToString() ?? "unknown";
string? redirect = linkStore.ResolveRedirect(slug);
if (redirect != null)
{
Console.WriteLine("Resolved slug \"{0}\" to {1} for {2}", slug, redirect, clientIP);
return Results.Redirect(redirect);
}
else
{
Console.WriteLine("Unable to resolve slug \"{0}\" for {1}", slug, clientIP);
return Results.Redirect("/");
}
});
app.MapGet("api/links", () =>
{
return linkStore.shortenedLinks;
}).RequireAuthorization(policy => policy.RequireRole("Admin"));
app.MapPost("api/links", (AddLinkRequest addLinkRequest) =>
{
AddLinkResult result = linkStore.Add(addLinkRequest.Redirect, addLinkRequest.Slug ?? "");
if (result.Success)
{
Console.WriteLine("Successfully added {0} to {1}", result.Slug, result.Redirect);
return Results.Ok(result);
}
else
{
Console.WriteLine("Failed to add!");
return Results.Conflict(result.Error);
}
}).RequireAuthorization(policy => policy.RequireRole("Admin"));
app.MapDelete("api/links/{slug}", (string slug) =>
{
bool result = linkStore.Delete(slug);
if (result)
return Results.NoContent();
else
return Results.Conflict();
}).RequireAuthorization(policy => policy.RequireRole("Admin"));
PeriodicTimer saveTimer = new PeriodicTimer(TimeSpan.FromSeconds(15));
Task.Run(async () =>
{
while (await saveTimer.WaitForNextTickAsync())
linkStore.Flush();
});
app.Lifetime.ApplicationStopping.Register(() =>
{
linkStore.Save();
});
app.Run();
}
}
public class AddLinkRequest
{
[Required]
[Url]
public string Redirect { get; init; } = "";
public string? Slug { get; init; }
}
}