tgstation-server
6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
src
Tgstation.Server.Host
Security
AuthenticationContextClaimsTransformation.cs
Go to the documentation of this file.
1
using
System;
2
using
System.Collections.Generic
;
3
using
System.Security.Claims
;
4
using
System.Threading.Tasks
;
5
6
using
Microsoft.AspNetCore.Authentication
;
7
8
using
Tgstation.Server.Api.Rights
;
9
using
Tgstation.Server.Host.Models
;
10
11
namespace
Tgstation.Server.Host.Security
12
{
16
sealed
class
AuthenticationContextClaimsTransformation
:
IClaimsTransformation
17
{
21
readonly
IAuthenticationContext
authenticationContext
;
22
27
public
AuthenticationContextClaimsTransformation
(
IAuthenticationContext
authenticationContext
)
28
{
29
this.authenticationContext
=
authenticationContext
??
throw
new
ArgumentNullException
(
nameof
(
authenticationContext
));
30
}
31
33
public
Task<ClaimsPrincipal>
TransformAsync
(
ClaimsPrincipal
principal
)
34
{
35
ArgumentNullException
.ThrowIfNull(
principal
);
36
37
if
(!
authenticationContext
.
Valid
)
38
throw
new
InvalidOperationException
(
"Expected a valid authentication context here!"
);
39
40
var
enumerator
=
Enum
.GetValues(
typeof
(
RightsType
));
41
var
claims
=
new
List<Claim>
();
42
if
(
authenticationContext
.
User
.Require(
x
=>
x
.Enabled))
43
claims
.Add(
44
new
Claim
(
45
ClaimTypes
.Role,
46
TgsAuthorizeAttribute
.
UserEnabledRole
));
47
48
foreach
(
RightsType
rightType
in
enumerator
)
49
{
50
// if there's a bad condition, do a weird thing and add all the roles
51
// we need it so we can get to TgsAuthorizeAttribute where we can properly decide between BadRequest and Forbid
52
var
rightAsULong
= (
RightsHelper
.
IsInstanceRight
(
rightType
) &&
authenticationContext.InstancePermissionSet
==
null
)
53
? ~0
UL
54
:
authenticationContext
.
GetRight
(
rightType
);
55
var
rightEnum
=
RightsHelper
.
RightToType
(
rightType
);
56
var
right
= (
Enum
)
Enum
.ToObject(
rightEnum
,
rightAsULong
);
57
foreach
(
Enum
enumeratedRight
in
Enum
.GetValues(
rightEnum
))
58
if
(
right
.HasFlag(
enumeratedRight
))
59
claims
.Add(
60
new
Claim
(
61
ClaimTypes
.Role,
62
RightsHelper
.
RoleName
(
rightType
,
enumeratedRight
)));
63
}
64
65
principal
.AddIdentity(
new
ClaimsIdentity
(
claims
));
66
67
return
Task.FromResult(
principal
);
68
}
69
}
70
}
IClaimsTransformation
Tgstation.Server.Api.Rights.RightsHelper
Helper for RightsTypes.
Definition
RightsHelper.cs:12
Tgstation.Server.Api.Rights.RightsHelper.IsInstanceRight
static bool IsInstanceRight(RightsType rightsType)
Check if a given rightsType is meant for an Models.Instance.
Tgstation.Server.Api.Rights.RightsHelper.RightToType
static Type RightToType(RightsType rightsType)
Map a given rightsType to its respective Enum Type.
Tgstation.Server.Api.Rights.RightsHelper.RoleName
static string RoleName(RightsType rightsType, Enum right)
Gets the role claim name used for a given rightsType and right .
Definition
RightsHelper.cs:65
Tgstation.Server.Host.Security.AuthenticationContextClaimsTransformation
A IClaimsTransformation that maps Claims using an IAuthenticationContext.
Definition
AuthenticationContextClaimsTransformation.cs:17
Tgstation.Server.Host.Security.AuthenticationContextClaimsTransformation.AuthenticationContextClaimsTransformation
AuthenticationContextClaimsTransformation(IAuthenticationContext authenticationContext)
Initializes a new instance of the AuthenticationContextClaimsTransformation class.
Definition
AuthenticationContextClaimsTransformation.cs:27
Tgstation.Server.Host.Security.AuthenticationContextClaimsTransformation.TransformAsync
Task< ClaimsPrincipal > TransformAsync(ClaimsPrincipal principal)
Definition
AuthenticationContextClaimsTransformation.cs:33
Tgstation.Server.Host.Security.AuthenticationContextClaimsTransformation.authenticationContext
readonly IAuthenticationContext authenticationContext
The IAuthenticationContext for the AuthenticationContextClaimsTransformation.
Definition
AuthenticationContextClaimsTransformation.cs:21
Tgstation.Server.Host.Security.TgsAuthorizeAttribute
Helper for using the AuthorizeAttribute with the Api.Rights system.
Definition
TgsAuthorizeAttribute.cs:17
Tgstation.Server.Host.Security.TgsAuthorizeAttribute.UserEnabledRole
const string UserEnabledRole
Role used to indicate access to the server is allowed.
Definition
TgsAuthorizeAttribute.cs:21
Tgstation.Server.Host.Security.IAuthenticationContext
For creating and accessing authentication contexts.
Definition
IAuthenticationContext.cs:12
Tgstation.Server.Host.Security.IAuthenticationContext.Valid
bool Valid
If the IAuthenticationContext is for a valid login.
Definition
IAuthenticationContext.cs:16
Tgstation.Server.Host.Security.IAuthenticationContext.User
User User
The authenticated user.
Definition
IAuthenticationContext.cs:31
Tgstation.Server.Host.Security.IAuthenticationContext.GetRight
ulong GetRight(RightsType rightsType)
Get the value of a given rightsType .
Tgstation.Server.Api.Rights
Definition
AdministrationRights.cs:4
Tgstation.Server.Api.Rights.ConfigurationRights.List
@ List
User may list files if the Models.Instance allows it.
Tgstation.Server.Api.Rights.RightsType
RightsType
The type of rights a model uses.
Definition
RightsType.cs:7
Tgstation.Server.Host.Models
Definition
ChatBot.cs:9
Tgstation.Server.Host.Security
Definition
AuthenticationContext.cs:8
Generated by
1.9.8