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