tgstation-server 6.17.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | Static Private Attributes | List of all members
Tgstation.Server.Host.Security.CryptographySuite Class Referencesealed

More...

Inheritance diagram for Tgstation.Server.Host.Security.CryptographySuite:
Inheritance graph
[legend]
Collaboration diagram for Tgstation.Server.Host.Security.CryptographySuite:
Collaboration graph
[legend]

Public Member Functions

 CryptographySuite (IPasswordHasher< User > passwordHasher)
 Initializes a new instance of the CryptographySuite class.
 
byte[] GetSecureBytes (uint amount)
 Generates a secure set of bytes.
Parameters
amountThe amount of bytes to generate.
Returns
A secure set of bytes.

 
void SetUserPassword (User user, string newPassword, bool newUser)
 Sets a User.PasswordHash for a given user .
Parameters
userThe User whos User.PasswordHash is to be set.
newPasswordThe new password for the User.
newUserIf the user is just being created.

 
bool CheckUserPassword (User user, string password)
 Checks a given password matches a given user 's User.PasswordHash. This may result in User.PasswordHash being modified and this should be persisted.
Parameters
userThe User to check.
passwordThe password to check.
Returns
true if password matches the hash, false otherwise.

 
string GetSecureString ()
 Generates a 40-length secure ascii string.
Returns
A 40-length secure ascii string.

 

Private Attributes

readonly IPasswordHasher< UserpasswordHasher
 The IPasswordHasher<TUser> for the CryptographySuite.
 

Static Private Attributes

const uint SecureStringLength = 30
 Length in bytes of generated base64 secure string.
 

Detailed Description

Definition at line 12 of file CryptographySuite.cs.

Constructor & Destructor Documentation

◆ CryptographySuite()

Tgstation.Server.Host.Security.CryptographySuite.CryptographySuite ( IPasswordHasher< User passwordHasher)

Initializes a new instance of the CryptographySuite class.

Parameters
passwordHasherThe value of passwordHasher.

Definition at line 28 of file CryptographySuite.cs.

29 {
30 this.passwordHasher = passwordHasher ?? throw new ArgumentNullException(nameof(passwordHasher));
31 }
readonly IPasswordHasher< User > passwordHasher
The IPasswordHasher<TUser> for the CryptographySuite.

References Tgstation.Server.Host.Security.CryptographySuite.passwordHasher.

Member Function Documentation

◆ CheckUserPassword()

bool Tgstation.Server.Host.Security.CryptographySuite.CheckUserPassword ( User  user,
string  password 
)

Checks a given password matches a given user 's User.PasswordHash. This may result in User.PasswordHash being modified and this should be persisted.

Parameters
userThe User to check.
passwordThe password to check.
Returns
true if password matches the hash, false otherwise.

Implements Tgstation.Server.Host.Security.ICryptographySuite.

Definition at line 53 of file CryptographySuite.cs.

54 {
55 ArgumentNullException.ThrowIfNull(user);
56 ArgumentNullException.ThrowIfNull(password);
57
58 if (user.PasswordHash == null)
59 throw new ArgumentException("user must have PasswordHash!", nameof(user));
60
61 var result = passwordHasher.VerifyHashedPassword(user, user.PasswordHash, password);
62 switch (result)
63 {
64 case PasswordVerificationResult.Failed:
65 return false;
66 case PasswordVerificationResult.SuccessRehashNeeded:
67 SetUserPassword(user, password, false);
68 break;
69 case PasswordVerificationResult.Success:
70 break;
71 default:
72 throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Password hasher return unknown PasswordVerificationResult: {0}", result));
73 }
74
75 return true;
76 }
string? PasswordHash
The hash of the user's password.
Definition User.cs:26
void SetUserPassword(User user, string newPassword, bool newUser)
Sets a User.PasswordHash for a given user .

References Tgstation.Server.Host.Models.User.PasswordHash, Tgstation.Server.Host.Security.CryptographySuite.passwordHasher, and Tgstation.Server.Host.Security.CryptographySuite.SetUserPassword().

Here is the call graph for this function:

◆ GetSecureBytes()

byte[] Tgstation.Server.Host.Security.CryptographySuite.GetSecureBytes ( uint  amount)

Generates a secure set of bytes.

Parameters
amountThe amount of bytes to generate.
Returns
A secure set of bytes.

Implements Tgstation.Server.Host.Security.ICryptographySuite.

Definition at line 34 of file CryptographySuite.cs.

35 {
36 using var rng = RandomNumberGenerator.Create(); // uses RNGCryptoServiceProvider under the hood https://khalidabuhakmeh.com/creating-random-numbers-with-dotnet-core
37 var byt = new byte[amount];
38 rng.GetBytes(byt);
39 return byt;
40 }

◆ GetSecureString()

string Tgstation.Server.Host.Security.CryptographySuite.GetSecureString ( )

Generates a 40-length secure ascii string.

Returns
A 40-length secure ascii string.

Implements Tgstation.Server.Host.Security.ICryptographySuite.

◆ SetUserPassword()

void Tgstation.Server.Host.Security.CryptographySuite.SetUserPassword ( User  user,
string  newPassword,
bool  newUser 
)

Sets a User.PasswordHash for a given user .

Parameters
userThe User whos User.PasswordHash is to be set.
newPasswordThe new password for the User.
newUserIf the user is just being created.

Implements Tgstation.Server.Host.Security.ICryptographySuite.

Definition at line 43 of file CryptographySuite.cs.

44 {
45 ArgumentNullException.ThrowIfNull(user);
46 ArgumentNullException.ThrowIfNull(newPassword);
47 user.PasswordHash = passwordHasher.HashPassword(user, newPassword);
48 if (!newUser)
49 user.LastPasswordUpdate = DateTimeOffset.UtcNow;
50 }

References Tgstation.Server.Host.Security.CryptographySuite.passwordHasher.

Referenced by Tgstation.Server.Host.Security.CryptographySuite.CheckUserPassword().

Here is the caller graph for this function:

Member Data Documentation

◆ passwordHasher

readonly IPasswordHasher<User> Tgstation.Server.Host.Security.CryptographySuite.passwordHasher
private

◆ SecureStringLength

const uint Tgstation.Server.Host.Security.CryptographySuite.SecureStringLength = 30
staticprivate

Length in bytes of generated base64 secure string.

Definition at line 17 of file CryptographySuite.cs.


The documentation for this class was generated from the following file: