Skip to content

Commit c0a7e16

Browse files
committed
Got JWT token working! All is ready!
1 parent d87215b commit c0a7e16

3 files changed

Lines changed: 40 additions & 13 deletions

File tree

Program.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
using Microsoft.AspNetCore.Authentication.JwtBearer;
12
using Microsoft.AspNetCore.Http.HttpResults;
23
using Microsoft.IdentityModel.Tokens;
34
using skipper_paste;
45
using System.Text;
56
using System.Text.Json;
67

78
var builder = WebApplication.CreateBuilder(args);
8-
9+
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
10+
#if DEBUG
11+
builder.Logging.ClearProviders();
12+
builder.Logging.AddSimpleConsole();
13+
builder.Logging.SetMinimumLevel(LogLevel.Trace);
14+
#endif
915
// Add services to the container.
1016
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
1117
builder.Services.AddOpenApi();
1218
builder.Services.AddCors();
19+
builder.Services.AddAuthentication();
1320
builder.Services.AddAuthorizationBuilder()
1421
.AddPolicy("PasteScope", policy =>
1522
{
@@ -42,7 +49,7 @@
4249
issuer: $"https://{domain}",
4350
audience: "paste",
4451
claims: claims,
45-
expires: DateTime.UtcNow.AddHours(1),
52+
expires: DateTime.UtcNow.AddYears(2),
4653
signingCredentials: creds
4754
);
4855

@@ -54,31 +61,36 @@
5461
}
5562

5663
//add jwt token validation with secret configured from startup
57-
builder.Services.AddAuthentication("Bearer")
58-
.AddJwtBearer("Bearer", options =>
64+
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
65+
.AddJwtBearer(options =>
5966
{
6067
options.Authority = $"https://{domain}";
6168
options.Audience = "paste";
6269
options.RequireHttpsMetadata = false; // For development purposes only,
6370
options.TokenValidationParameters = new TokenValidationParameters
6471
{
6572
ValidateIssuer = true,
66-
ValidateAudience = false,
73+
ValidateAudience = true,
6774
ValidateLifetime = true,
6875
ValidateIssuerSigningKey = true,
76+
NameClaimType = System.Security.Claims.ClaimTypes.Name,
6977
ValidIssuer = $"https://{domain}",
70-
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),
71-
LogValidationExceptions = true
78+
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret))
7279
};
7380
});
7481

7582
var app = builder.Build();
7683

7784
app.UseCors();
85+
app.UseAuthentication();
86+
app.UseAuthorization();
7887

88+
app.Logger.LogInformation("Checking paste directory at {directory}", pasteDirectory);
7989

8090
if (!Directory.Exists(pasteDirectory))
8191
{
92+
app.Logger.LogInformation("Paste directory at {directory} doesn't exist!", pasteDirectory);
93+
8294
Directory.CreateDirectory(pasteDirectory);
8395
}
8496

@@ -92,13 +104,13 @@
92104
app.UseAuthentication();
93105
app.UseAuthentication();
94106

95-
app.MapPost("/paste", (PasteData data) =>
107+
app.MapPost("/paste", (PasteData data, HttpRequest request) =>
96108
{
97109
var pasteId = RandomNameGenerator.GenerateRandomName(5);
98110

99111
File.WriteAllText(Path.Combine(pasteDirectory, pasteId + ".json"), JsonSerializer.Serialize(data));
100112

101-
return Results.Ok(new PasteLink(pasteId, $"{domain}/get/{pasteId}"));
113+
return Results.Ok(new PasteLink(pasteId, $"{(request.IsHttps ? "https" : "http")}://{domain}/get/{pasteId}"));
102114
})
103115
.WithName("PasteJson")
104116
.RequireAuthorization();
@@ -118,6 +130,10 @@
118130
return Results.NotFound("Paste not found");
119131
}
120132
});
133+
//.RequireRateLimiting();
134+
135+
app.Logger.LogInformation("Starting main http work...");
136+
121137

122138
app.Run();
123139

appsettings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
"LogLevel": {
44
"Default": "Trace",
55
"Microsoft.AspNetCore": "Trace"
6+
},
7+
"Console": {
8+
"IncludeScopes": true,
9+
"LogLevel": {
10+
"Microsoft.AspNetCore.Mvc.Razor.Internal": "Warning",
11+
"Microsoft.AspNetCore.Mvc.Razor.Razor": "Debug",
12+
"Microsoft.AspNetCore.Mvc.Razor": "Error",
13+
"Default": "Trace"
14+
}
615
}
716
},
817
"AllowedHosts": "*",

skipper-paste.http

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@skipper_paste_HostAddress = http://localhost:5051
22

3-
GET {{skipper_paste_HostAddress}}/get/test
3+
GET {{skipper_paste_HostAddress}}/get/WT6cD
44
Accept: application/json
55

66
###
@@ -9,8 +9,10 @@ Accept: application/json
99

1010
POST {{skipper_paste_HostAddress}}/paste
1111
Content-Type: application/json
12-
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6InBhc3RlIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6InBhc3RlLXVzZXIiLCJleHAiOjE3NTM2MDA4NDYsImlzcyI6Imh0dHBzOi8vbG9jYWxob3N0OjcwODEiLCJhdWQiOiJwYXN0ZSJ9.2F50Z1gO6ERvznvGPFho7ieTj3lFkWKU0ZuIpMbwASM
12+
x-test: aaaa
13+
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6InBhc3RlIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6InBhc3RlLXVzZXIiLCJleHAiOjE4MTY2NzY4NDIsImlzcyI6Imh0dHBzOi8vbG9jYWxob3N0OjcwODEiLCJhdWQiOiJwYXN0ZSJ9.0YqiOmDpnZuE3j8AWYswd8KslPq9H-8Rrg3YK2G040E
14+
1315
{
14-
"key": "aaaa",
15-
"value": "bbbb"
16+
"Content": "aaaa",
17+
"Note": "bbbb"
1618
}

0 commit comments

Comments
 (0)