tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
OAuthProviders.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5using Microsoft.Extensions.Logging;
6using Microsoft.Extensions.Options;
7
12
14{
17 {
21 readonly IReadOnlyCollection<IOAuthValidator> validators;
22
31 IGitHubServiceFactory gitHubServiceFactory,
32 IAbstractHttpClientFactory httpClientFactory,
33 ILoggerFactory loggerFactory,
34 IOptions<SecurityConfiguration> securityConfigurationOptions)
35 {
36 ArgumentNullException.ThrowIfNull(loggerFactory);
37
38 var securityConfiguration = securityConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(securityConfigurationOptions));
39
40 var validatorsBuilder = new List<IOAuthValidator>();
41 validators = validatorsBuilder;
42
43 if (securityConfiguration.OAuth == null)
44 return;
45
46 if (securityConfiguration.OAuth.TryGetValue(OAuthProvider.GitHub, out var gitHubConfig))
47 validatorsBuilder.Add(
49 gitHubServiceFactory,
50 loggerFactory.CreateLogger<GitHubOAuthValidator>(),
51 gitHubConfig));
52
53 if (securityConfiguration.OAuth.TryGetValue(OAuthProvider.Discord, out var discordConfig))
54 validatorsBuilder.Add(
56 httpClientFactory,
57 loggerFactory.CreateLogger<DiscordOAuthValidator>(),
58 discordConfig));
59
60 if (securityConfiguration.OAuth.TryGetValue(OAuthProvider.TGForums, out var tgConfig))
61 validatorsBuilder.Add(
63 httpClientFactory,
64 loggerFactory.CreateLogger<TGForumsOAuthValidator>(),
65 tgConfig));
66
67 if (securityConfiguration.OAuth.TryGetValue(OAuthProvider.Keycloak, out var keyCloakConfig))
68 validatorsBuilder.Add(
70 httpClientFactory,
71 loggerFactory.CreateLogger<KeycloakOAuthValidator>(),
72 keyCloakConfig));
73
74 if (securityConfiguration.OAuth.TryGetValue(OAuthProvider.InvisionCommunity, out var invisionConfig))
75 validatorsBuilder.Add(
77 httpClientFactory,
78 loggerFactory.CreateLogger<InvisionCommunityOAuthValidator>(),
79 invisionConfig));
80 }
81
83 public IOAuthValidator? GetValidator(OAuthProvider oAuthProvider, bool forLogin)
84 => validators.FirstOrDefault(
85 x => x.Provider == oAuthProvider
86 && ((forLogin && x.GatewayStatus != OAuthGatewayStatus.Only) || (!forLogin && x.GatewayStatus != OAuthGatewayStatus.Disabled)));
87
89 public Dictionary<OAuthProvider, OAuthProviderInfo> ProviderInfos()
90 {
91 var providersAndTasks = validators.ToDictionary(
92 x => x.Provider,
93 x => x.GetProviderInfo());
94
95 return providersAndTasks
96 .Where(x => x.Value != null)
97 .ToDictionary(
98 x => x.Key,
99 x => x.Value);
100 }
101 }
102}
IOAuthValidator? GetValidator(OAuthProvider oAuthProvider, bool forLogin)
Gets the IOAuthValidator for a given oAuthProvider .The IOAuthValidator for oAuthProvider .
OAuthProviders(IGitHubServiceFactory gitHubServiceFactory, IAbstractHttpClientFactory httpClientFactory, ILoggerFactory loggerFactory, IOptions< SecurityConfiguration > securityConfigurationOptions)
Initializes a new instance of the OAuthProviders class.
readonly IReadOnlyCollection< IOAuthValidator > validators
The IReadOnlyCollection<T> of IOAuthValidators.
Dictionary< OAuthProvider, OAuthProviderInfo > ProviderInfos()
Gets a Dictionary<TKey, TValue> of the provider client IDs.A new Dictionary<TKey, TValue> of the acti...
Validates OAuth responses for a given Provider.
OAuthProvider
List of OAuth providers supported by TGS.
OAuthGatewayStatus
Status of the OAuth gateway for a provider.