tgstation-server 5.12.7
The /tg/station 13 server suite
Loading...
Searching...
No Matches
AuthenticationContextFactory.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3using System.Threading;
4using System.Threading.Tasks;
5
6using Microsoft.EntityFrameworkCore;
7using Microsoft.Extensions.Logging;
8using Microsoft.Extensions.Options;
9
13
15{
18 {
21
26
31
35 readonly ILogger<AuthenticationContextFactory> logger;
36
41
52 IOptions<SwarmConfiguration> swarmConfigurationOptions,
53 ILogger<AuthenticationContextFactory> logger)
54 {
55 this.databaseContext = databaseContext ?? throw new ArgumentNullException(nameof(databaseContext));
56 this.identityCache = identityCache ?? throw new ArgumentNullException(nameof(identityCache));
57 swarmConfiguration = swarmConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(swarmConfigurationOptions));
58 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
59 }
60
62 public void Dispose() => CurrentAuthenticationContext?.Dispose();
63
65 public async Task CreateAuthenticationContext(long userId, long? instanceId, DateTimeOffset validAfter, CancellationToken cancellationToken)
66 {
68 throw new InvalidOperationException("Authentication context has already been loaded");
69
70 var user = await databaseContext
71 .Users
72 .AsQueryable()
73 .Where(x => x.Id == userId)
74 .Include(x => x.CreatedBy)
75 .Include(x => x.PermissionSet)
76 .Include(x => x.Group)
77 .ThenInclude(x => x.PermissionSet)
78 .Include(x => x.OAuthConnections)
79 .FirstOrDefaultAsync(cancellationToken);
80 if (user == default)
81 {
82 logger.LogWarning("Unable to find user with ID {0}!", userId);
84 return;
85 }
86
87 ISystemIdentity systemIdentity;
88 if (user.SystemIdentifier != null)
89 systemIdentity = identityCache.LoadCachedIdentity(user);
90 else
91 {
92 if (user.LastPasswordUpdate.HasValue && user.LastPasswordUpdate > validAfter)
93 {
94 logger.LogDebug("Rejecting token for user {0} created before last password update: {1}", userId, user.LastPasswordUpdate.Value);
96 return;
97 }
98
99 systemIdentity = null;
100 }
101
102 var userPermissionSet = user.PermissionSet ?? user.Group.PermissionSet;
103 try
104 {
105 InstancePermissionSet instancePermissionSet = null;
106 if (instanceId.HasValue)
107 {
108 instancePermissionSet = await databaseContext.InstancePermissionSets
109 .AsQueryable()
110 .Where(x => x.PermissionSetId == userPermissionSet.Id && x.InstanceId == instanceId && x.Instance.SwarmIdentifer == swarmConfiguration.Identifier)
111 .Include(x => x.Instance)
112 .FirstOrDefaultAsync(cancellationToken);
113
114 if (instancePermissionSet == null)
115 logger.LogDebug("User {0} does not have permissions on instance {1}!", userId, instanceId.Value);
116 }
117
119 systemIdentity,
120 user,
121 instancePermissionSet);
122 }
123 catch
124 {
125 systemIdentity?.Dispose();
126 throw;
127 }
128 }
129 }
130}
string? Identifier
The server's identifier.
Definition: SwarmServer.cs:21
Configuration for the server swarm system.
async Task CreateAuthenticationContext(long userId, long? instanceId, DateTimeOffset validAfter, CancellationToken cancellationToken)
Create an IAuthenticationContext to populate CurrentAuthenticationContext. A Task representing the ru...
IAuthenticationContext CurrentAuthenticationContext
The IAuthenticationContext the IAuthenticationContextFactory created.
readonly IDatabaseContext databaseContext
The IDatabaseContext for the AuthenticationContextFactory.
AuthenticationContextFactory(IDatabaseContext databaseContext, IIdentityCache identityCache, IOptions< SwarmConfiguration > swarmConfigurationOptions, ILogger< AuthenticationContextFactory > logger)
Initializes a new instance of the AuthenticationContextFactory class.
readonly ILogger< AuthenticationContextFactory > logger
The ILogger for the AuthenticationContextFactory.
readonly SwarmConfiguration swarmConfiguration
The SwarmConfiguration for the AuthenticationContextFactory.
readonly IIdentityCache identityCache
The IIdentityCache for the AuthenticationContextFactory.
IDatabaseCollection< User > Users
The Users in the IDatabaseContext.
Represents the currently authenticated Models.User.
ISystemIdentity LoadCachedIdentity(User user)
Attempt to load a cached ISystemIdentity.
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform.