Skip to content

Commit

Permalink
Add authentication support (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-j-green authored Nov 25, 2023
1 parent 2ade60c commit 3d2f946
Show file tree
Hide file tree
Showing 88 changed files with 6,190 additions and 127 deletions.
Binary file modified gaseous-server/.DS_Store
Binary file not shown.
Binary file modified gaseous-server/Assets/.DS_Store
Binary file not shown.
Binary file modified gaseous-server/Assets/Ratings/.DS_Store
Binary file not shown.
16 changes: 16 additions & 0 deletions gaseous-server/Classes/Auth/Classes/IdentityRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Data;
using gaseous_server.Classes;
using Microsoft.AspNetCore.Identity;

namespace Authentication
{
/// <summary>
/// Class that implements the ASP.NET Identity
/// IRole interface
/// </summary>
public class ApplicationRole : IdentityRole
{
}
}
15 changes: 15 additions & 0 deletions gaseous-server/Classes/Auth/Classes/IdentityUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using gaseous_server.Classes;
using Microsoft.AspNetCore.Identity;
using System;

namespace Authentication
{
/// <summary>
/// Class that implements the ASP.NET Identity
/// IUser interface
/// </summary>
public class ApplicationUser : IdentityUser
{
public SecurityProfileViewModel SecurityProfile { get; set; }
}
}
171 changes: 171 additions & 0 deletions gaseous-server/Classes/Auth/Classes/RoleStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using gaseous_server.Classes;
using Microsoft.AspNetCore.Identity;
using MySqlConnector;

namespace Authentication
{
/// <summary>
/// Class that implements the key ASP.NET Identity role store iterfaces
/// </summary>
public class RoleStore : IQueryableRoleStore<ApplicationRole>
{
private RoleTable roleTable;
public Database Database { get; private set; }

public IQueryable<ApplicationRole> Roles
{
get
{
List<ApplicationRole> roles = roleTable.GetRoles();
return roles.AsQueryable();
}
}

public RoleStore()
{
Database = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
roleTable = new RoleTable(Database);
}

/// <summary>
/// Constructor that takes a MySQLDatabase as argument
/// </summary>
/// <param name="database"></param>
public RoleStore(Database database)
{
Database = database;
roleTable = new RoleTable(database);
}

public Task<IdentityResult> CreateAsync(ApplicationRole role, CancellationToken cancellationToken)
{
if (role == null)
{
throw new ArgumentNullException("role");
}

roleTable.Insert(role);

return Task.FromResult<IdentityResult>(IdentityResult.Success);
}

public Task<IdentityResult> DeleteAsync(ApplicationRole role, CancellationToken cancellationToken)
{
if (role == null)
{
throw new ArgumentNullException("user");
}

roleTable.Delete(role.Id);

return Task.FromResult<IdentityResult>(IdentityResult.Success);
}

public Task<ApplicationRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)

Check warning on line 67 in gaseous-server/Classes/Auth/Classes/RoleStore.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in return type of 'Task<ApplicationRole> RoleStore.FindByIdAsync(string roleId, CancellationToken cancellationToken)' doesn't match implicitly implemented member 'Task<ApplicationRole?> IRoleStore<ApplicationRole>.FindByIdAsync(string roleId, CancellationToken cancellationToken)'.
{
ApplicationRole result = roleTable.GetRoleById(roleId) as ApplicationRole;

return Task.FromResult<ApplicationRole>(result);
}

public Task<bool> RoleExistsAsync(string roleId, CancellationToken cancellationToken)
{
ApplicationRole? result = roleTable.GetRoleById(roleId) as ApplicationRole;

if (result == null)
{
return Task.FromResult<bool>(false);
}
else
{
return Task.FromResult<bool>(true);
}
}

public Task<ApplicationRole?> FindByNameAsync(string roleName, CancellationToken cancellationToken)
{
ApplicationRole? result = roleTable.GetRoleByName(roleName) as ApplicationRole;

return Task.FromResult<ApplicationRole?>(result);
}

public Task<IdentityResult> UpdateAsync(ApplicationRole role, CancellationToken cancellationToken)
{
if (role == null)
{
throw new ArgumentNullException("user");
}

roleTable.Update(role);

return Task.FromResult<IdentityResult>(IdentityResult.Success);
}

public void Dispose()
{
if (Database != null)
{
Database = null;
}
}

public Task<string> GetRoleIdAsync(ApplicationRole role, CancellationToken cancellationToken)
{
if (role != null)
{
return Task.FromResult<string>(roleTable.GetRoleId(role.Name));
}

return Task.FromResult<string>(null);
}

public Task<string?> GetRoleNameAsync(ApplicationRole role, CancellationToken cancellationToken)
{
if (role != null)
{
return Task.FromResult<string?>(roleTable.GetRoleName(role.Id));
}

return Task.FromResult<string?>(null);
}

public Task SetRoleNameAsync(ApplicationRole role, string? roleName, CancellationToken cancellationToken)
{
if (role == null)
{
throw new ArgumentNullException("role");
}

role.Name = roleName;
roleTable.Update(role);

return Task.FromResult<IdentityResult>(IdentityResult.Success);
}

public Task<string?> GetNormalizedRoleNameAsync(ApplicationRole role, CancellationToken cancellationToken)
{
if (role != null)
{
return Task.FromResult<string?>(roleTable.GetRoleName(role.Id));
}

return Task.FromResult<string?>(null);
}

public Task SetNormalizedRoleNameAsync(ApplicationRole role, string? normalizedName, CancellationToken cancellationToken)
{
if (role == null)
{
throw new ArgumentNullException("role");
}

role.Name = normalizedName;
roleTable.Update(role);

return Task.FromResult<IdentityResult>(IdentityResult.Success);
}
}
}
168 changes: 168 additions & 0 deletions gaseous-server/Classes/Auth/Classes/RoleTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System;
using System.Collections.Generic;
using System.Data;
using gaseous_server.Classes;
using Microsoft.AspNetCore.Identity;

namespace Authentication
{
/// <summary>
/// Class that represents the Role table in the MySQL Database
/// </summary>
public class RoleTable
{
private Database _database;

/// <summary>
/// Constructor that takes a MySQLDatabase instance
/// </summary>
/// <param name="database"></param>
public RoleTable(Database database)
{
_database = database;
}

/// <summary>
/// Deltes a role from the Roles table
/// </summary>
/// <param name="roleId">The role Id</param>
/// <returns></returns>
public int Delete(string roleId)
{
string commandText = "Delete from Roles where Id = @id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@id", roleId);

return (int)_database.ExecuteNonQuery(commandText, parameters);
}

/// <summary>
/// Inserts a new Role in the Roles table
/// </summary>
/// <param name="roleName">The role's name</param>
/// <returns></returns>
public int Insert(ApplicationRole role)
{
string commandText = "Insert into Roles (Id, Name) values (@id, @name)";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@name", role.Name);
parameters.Add("@id", role.Id);

return (int)_database.ExecuteNonQuery(commandText, parameters);
}

/// <summary>
/// Returns a role name given the roleId
/// </summary>
/// <param name="roleId">The role Id</param>
/// <returns>Role name</returns>
public string? GetRoleName(string roleId)
{
string commandText = "Select Name from Roles where Id = @id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@id", roleId);

DataTable table = _database.ExecuteCMD(commandText, parameters);

if (table.Rows.Count == 0)
{
return null;
}
else
{
return (string)table.Rows[0][0];
}
}

/// <summary>
/// Returns the role Id given a role name
/// </summary>
/// <param name="roleName">Role's name</param>
/// <returns>Role's Id</returns>
public string? GetRoleId(string roleName)
{
string? roleId = null;
string commandText = "Select Id from Roles where Name = @name";
Dictionary<string, object> parameters = new Dictionary<string, object>() { { "@name", roleName } };

DataTable result = _database.ExecuteCMD(commandText, parameters);
if (result.Rows.Count > 0)
{
return Convert.ToString(result.Rows[0][0]);
}

return roleId;
}

/// <summary>
/// Gets the ApplicationRole given the role Id
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public ApplicationRole? GetRoleById(string roleId)
{
var roleName = GetRoleName(roleId);
ApplicationRole? role = null;

if(roleName != null)
{
role = new ApplicationRole();
role.Id = roleId;
role.Name = roleName;
role.NormalizedName = roleName.ToUpper();
}

return role;

}

/// <summary>
/// Gets the ApplicationRole given the role name
/// </summary>
/// <param name="roleName"></param>
/// <returns></returns>
public ApplicationRole? GetRoleByName(string roleName)
{
var roleId = GetRoleId(roleName);
ApplicationRole role = null;

if (roleId != null)
{
role = new ApplicationRole();
role.Id = roleId;
role.Name = roleName;
role.NormalizedName = roleName.ToUpper();
}

return role;
}

public int Update(ApplicationRole role)
{
string commandText = "Update Roles set Name = @name where Id = @id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@id", role.Id);

return (int)_database.ExecuteNonQuery(commandText, parameters);
}

public List<ApplicationRole> GetRoles()
{
List<ApplicationRole> roles = new List<ApplicationRole>();

string commandText = "Select Name from Roles";

var rows = _database.ExecuteCMDDict(commandText);
foreach(Dictionary<string, object> row in rows)
{
ApplicationRole role = (ApplicationRole)Activator.CreateInstance(typeof(ApplicationRole));
role.Id = (string)row["Id"];
role.Name = (string)row["Name"];
role.NormalizedName = ((string)row["Name"]).ToUpper();
roles.Add(role);
}

return roles;
}
}
}
Loading

0 comments on commit 3d2f946

Please sign in to comment.