Skip to content

Commit 240d9e9

Browse files
committed
Make discord OAuth2 options optional
1 parent 55247b0 commit 240d9e9

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

API/Program.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,38 @@
3030
builder.Services
3131
.AddOpenShockMemDB(redisOptions)
3232
.AddOpenShockDB(databaseOptions)
33-
.AddOpenShockServices(auth => auth
34-
.AddCookie(OAuthConstants.FlowScheme, o =>
35-
{
33+
.AddOpenShockServices(auth =>
34+
{
35+
auth.AddCookie(OAuthConstants.FlowScheme, o => {
3636
o.Cookie.Name = OAuthConstants.FlowCookieName;
3737
o.ExpireTimeSpan = TimeSpan.FromMinutes(10);
3838
o.SlidingExpiration = false;
39-
})
40-
.AddDiscord(OAuthConstants.DiscordScheme, o =>
39+
});
40+
41+
var options = builder.Configuration.GetSection(DiscordOAuthOptions.SectionName).Get<DiscordOAuthOptions>();
42+
if (options is not null)
4143
{
42-
o.SignInScheme = OAuthConstants.FlowScheme;
44+
auth.AddDiscord(OAuthConstants.DiscordScheme, o => {
45+
o.SignInScheme = OAuthConstants.FlowScheme;
4346

44-
var options = builder.Configuration.GetRequiredSection(DiscordOAuthOptions.SectionName).Get<DiscordOAuthOptions>()!;
47+
4548

46-
o.ClientId = options.ClientId;
47-
o.ClientSecret = options.ClientSecret;
48-
o.CallbackPath = options.CallbackPath;
49-
o.AccessDeniedPath = options.AccessDeniedPath;
50-
foreach (var scope in options.Scopes) o.Scope.Add(scope);
49+
o.ClientId = options.ClientId;
50+
o.ClientSecret = options.ClientSecret;
51+
o.CallbackPath = options.CallbackPath;
52+
o.AccessDeniedPath = options.AccessDeniedPath;
53+
foreach (var scope in options.Scopes) o.Scope.Add(scope);
5154

52-
o.Prompt = "none";
53-
o.SaveTokens = false;
55+
o.Prompt = "none";
56+
o.SaveTokens = false;
5457

55-
o.ClaimActions.MapJsonKey(OAuthConstants.ClaimEmailVerified, "verified");
56-
o.ClaimActions.MapJsonKey(OAuthConstants.ClaimDisplayName, "global_name");
58+
o.ClaimActions.MapJsonKey(OAuthConstants.ClaimEmailVerified, "verified");
59+
o.ClaimActions.MapJsonKey(OAuthConstants.ClaimDisplayName, "global_name");
5760

58-
o.Validate();
59-
})
60-
)
61+
o.Validate();
62+
});
63+
}
64+
})
6165
.AddOpenShockSignalR(redisOptions);
6266

6367
builder.Services.AddScoped<IDeviceService, DeviceService>();

0 commit comments

Comments
 (0)