123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using iiie.Authentication.Business.JWT;
-
- namespace iiie.Authentication.DBO
- {
- /// <summary>
- /// All editable user fields
- /// </summary>
- public class UserEdit
- {
- /// <summary>
- /// When getting / setting from / to database: salt and password hash
- /// When getting from controller: user plain text password
- /// When returning from controller: empty or user token
- /// </summary>
- public object Private { get; private set; }
-
- /// <summary>
- /// Application specific user role(s)
- /// </summary>
- public int Role { get; set; }
-
- /// <summary>
- /// Optionnal user first name
- /// </summary>
- public string Firstname { get; set; }
-
- /// <summary>
- /// Optionnal user last name
- /// </summary>
- public string Lastname { get; set; }
-
- /// <summary>
- /// Get the user password salt from Private
- /// </summary>
- /// <returns>The salt</returns>
- public string GetSalt()
- {
- return ((string[]) Private)[0];
- }
-
- /// <summary>
- /// Set a value in the Private stirng array
- /// </summary>
- /// <param name="text">The value to be set</param>
- /// <param name="pos">The position in the array</param>
- private void SetArrayText(string text, int pos)
- {
- if (Private == null || Private.GetType() != typeof(string[]))
- Private = new string[2];
- ((string[]) Private)[pos] = text;
- }
-
- /// <summary>
- /// Set the user password salt to Private
- /// </summary>
- /// <param name="salt">The salt</param>
- public void SetSalt(string salt)
- {
- SetArrayText(salt, 0);
- }
-
- /// <summary>
- /// Get the password hash from Private
- /// </summary>
- /// <returns>The hash</returns>
- public string GetHash()
- {
- return ((string[])Private)[1];
- }
-
- /// <summary>
- /// Compute and set the password hash to Private
- /// </summary>
- /// <param name="password">The plain text password</param>
- public void SetPlainTextHash(string password)
- {
- SetArrayText(PasswordHash.CreateHash(password), 1);
- }
-
- /// <summary>
- /// Set the password hash to Private
- /// </summary>
- /// <param name="hash">The password hash</param>
- public void SetHash(string hash)
- {
- SetArrayText(hash, 1);
- }
-
- /// <summary>
- /// Get the Private as string
- /// </summary>
- /// <returns>The string data</returns>
- public string GetPlainTextPrivate()
- {
- return (string) Private;
- }
-
- /// <summary>
- /// Compute and set the token to Private
- /// </summary>
- /// <param name="username">The user username</param>
- /// <param name="salt">The use rpassword salt</param>
- public void SetPlainTextToken(string username, string salt)
- {
- Private = TokenManager.GetToken(username, salt);
- }
- }
- }
|