Skip to content

Commit

Permalink
New : DB id by guid option #105
Browse files Browse the repository at this point in the history
New : Support user logout #104
  • Loading branch information
shps951023 committed Mar 30, 2024
1 parent b2526a3 commit 972082b
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 143 deletions.
Empty file added DevNote.md
Empty file.
9 changes: 9 additions & 0 deletions src/Frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ watch(() => route.name, (newVal) => {
routeName.value = newVal?.toString() ?? "";
});
const logout = () => {
localStorage.removeItem('X-MiniAuth-Token')
window.location.href = '/miniauth/login.html'
}
const loadingFlag = ref(false)
emitter.on('showLoading', () => {
loadingFlag.value = true
Expand Down Expand Up @@ -48,6 +54,9 @@ emitter.on('closeLoading', () => {
<router-link class="nav-link " to="/Roles"> Roles </router-link>
</li>
</ul>
<div class="navbar-nav ms-auto">
<div @click="logout" class="btn nav-item nav-link">Logout</div>
</div>
</div>
</div>
</nav>
Expand Down
10 changes: 9 additions & 1 deletion src/Frontend/src/views/EndpointsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@
</select>
</td>
<td >
<button class="btn btn-success" @click="saveEndpoint(item)">Save</button>
<button class="btn" @click="saveEndpoint(item)">
<svg width="20px" height="20px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="System / Save">
<path id="Vector"
d="M17 21.0002L7 21M17 21.0002L17.8031 21C18.921 21 19.48 21 19.9074 20.7822C20.2837 20.5905 20.5905 20.2843 20.7822 19.908C21 19.4806 21 18.921 21 17.8031V9.21955C21 8.77072 21 8.54521 20.9521 8.33105C20.9095 8.14 20.8393 7.95652 20.7432 7.78595C20.6366 7.59674 20.487 7.43055 20.1929 7.10378L17.4377 4.04241C17.0969 3.66374 16.9242 3.47181 16.7168 3.33398C16.5303 3.21 16.3242 3.11858 16.1073 3.06287C15.8625 3 15.5998 3 15.075 3H6.2002C5.08009 3 4.51962 3 4.0918 3.21799C3.71547 3.40973 3.40973 3.71547 3.21799 4.0918C3 4.51962 3 5.08009 3 6.2002V17.8002C3 18.9203 3 19.4796 3.21799 19.9074C3.40973 20.2837 3.71547 20.5905 4.0918 20.7822C4.5192 21 5.07899 21 6.19691 21H7M17 21.0002V17.1969C17 16.079 17 15.5192 16.7822 15.0918C16.5905 14.7155 16.2837 14.4097 15.9074 14.218C15.4796 14 14.9203 14 13.8002 14H10.2002C9.08009 14 8.51962 14 8.0918 14.218C7.71547 14.4097 7.40973 14.7155 7.21799 15.0918C7 15.5196 7 16.0801 7 17.2002V21M15 7H9"
stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
</g>
</svg>
</button>
</td>
</tr>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/src/views/RolesView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<tbody>
<tr v-for="(item, index) in roles" :key="index">
<td>
{{ item.Id }}
{{ index+1 }}
</td>
<td>
<input type="text" v-model="item.Name">
Expand Down
4 changes: 0 additions & 4 deletions src/Frontend/src/views/UsersView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<table class="table">
<thead>
<tr class="table-dark">
<th>ID</th>
<th>User Name</th>
<th>Roles</th>
<th>First Name</th>
Expand All @@ -39,9 +38,6 @@
</thead>
<tbody>
<tr v-for="(item, index) in users" :key="index">
<td>
{{ item.Id }}
</td>
<td>
<input type="text" v-model="item.Username">
</td>
Expand Down
7 changes: 7 additions & 0 deletions src/MiniAuth/Helpers/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

namespace MiniAuth.Helpers
{
internal static class IdHelper
{
public static string NewId()
{
return System.Guid.NewGuid().ToString();
}
}
internal static class JsonHelper
{
public static string ToJson(this object data, int code = 200, string message = null)
Expand Down
3 changes: 1 addition & 2 deletions src/MiniAuth/Managers/JWTManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public JWTManager(string subjectName, string password, string cerPath)

public string GetToken(string sub, string name, int expMins, IEnumerable<string> roles)
{
//TODO:token id
var id = Guid.NewGuid().ToString();
var id = Helpers.IdHelper.NewId();
var payload = new Dictionary<string, object>
{
{ "sub", sub },
Expand Down
12 changes: 5 additions & 7 deletions src/MiniAuth/Managers/RoleEndpointManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private static async Task InsertNewEndpoint(System.Data.Common.DbConnection conn
{
command.CommandText = @"INSERT INTO endpoints (id,type,name,route,methods,enable,RedirectToLoginPage,roles)
VALUES (@id,@type,@name,@route,@methods,@enable,@RedirectToLoginPage,@roles)";
command.AddParameters(new Dictionary<string, object>()
var ps = new Dictionary<string, object>
{
{ "@id", endpoint.Id },
{ "@type", endpoint.Type },
Expand All @@ -91,8 +91,9 @@ private static async Task InsertNewEndpoint(System.Data.Common.DbConnection conn
{ "@methods", string.Join(",", endpoint.Methods??new[]{ ""}) },
{ "@enable", endpoint.Enable ? 1 : 0 },
{ "@RedirectToLoginPage", endpoint.RedirectToLoginPage ? 1 : 0 },
{ "@roles", string.Join(",",endpoint.Roles) },
});
{ "@roles", endpoint.Roles==null?null:string.Join(",",endpoint.Roles) },
};
command.AddParameters(ps);
await command.ExecuteNonQueryAsync();
}
}
Expand Down Expand Up @@ -120,7 +121,6 @@ private static List<RoleEndpointEntity> GetSystemEndpoints(IEnumerable<EndpointD
Route = route,
Methods = methods,
Enable = true,
Roles = new string[] { "1" },
RedirectToLoginPage = !isApi
});
}
Expand All @@ -132,7 +132,6 @@ private static List<RoleEndpointEntity> GetSystemEndpoints(IEnumerable<EndpointD
Route = "/miniauth/api/getallenpoints",
Methods = new string[0],
Enable = true,
Roles = new string[] { "1" },
RedirectToLoginPage = false
});
endpoints.Add(new RoleEndpointEntity
Expand All @@ -143,7 +142,6 @@ private static List<RoleEndpointEntity> GetSystemEndpoints(IEnumerable<EndpointD
Route = "/miniauth/index.html",
Methods = new string[0],
Enable = true,
Roles = new string[] { "1" },
RedirectToLoginPage = false
});
return endpoints;
Expand All @@ -169,7 +167,7 @@ private static async Task<List<RoleEndpointEntity>> GetDbEndpoints(DbConnection
Enable = reader.GetInt32(4) == 1,
RedirectToLoginPage = reader.GetInt32(5) == 1,
Type = reader.GetString(6),
Roles = reader.GetString(7)?.Split(',')
Roles = reader.IsDBNull(7)?null:reader.GetString(7)?.Split(',')
};
dbEndpoints.Add(endpoint);
}
Expand Down
65 changes: 28 additions & 37 deletions src/MiniAuth/Managers/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MiniAuth.Managers
public interface IUserManager
{
void CreateUser(string username, string password, string roles);
List<string> GetUserRoleIds(string username);
Dictionary<string,object> GetUser(string userName);
void UpdatePassword(string username, string newPassword);
bool ValidateUser(string username, string password);
}
Expand All @@ -25,11 +25,12 @@ public void CreateUser(string username, string password, string roles)
{
using (var connection = _db.GetConnection())
{
string sql = "INSERT INTO Users (Username, Password) VALUES (@username, @password);";
string sql = "INSERT INTO Users (id,Username, Password) VALUES (@id,@username, @password);";
var command = connection.CreateCommand();
command.CommandText = sql;
command.AddParameters(new Dictionary<string, object>
{
{ "@id", Helpers.IdHelper.NewId() },
{ "@username", username },
{ "@password", HashGenerator.GetHashPassword(password) },
});
Expand All @@ -38,46 +39,12 @@ public void CreateUser(string username, string password, string roles)
// TODO: Assign roles to user
}
}
public class UserEndpointDto
{
public string RoleName { get; set; }
public int? RoleId { get; set; }
public string? EndpointId { get; set; }
public string Route { get; set; }
public string EndpointName { get; set; }
}
public List<string> GetUserRoleIds(string username)
{
var result = new List<string>(); ;
using (var connection = this._db.GetConnection())
{
using (var command = connection.CreateCommand())
{
command.CommandText = @"
SELECT ur.id
FROM users u
LEFT JOIN users_roles ur ON u.id = ur.user_id
WHERE u.username = @username";

command.AddParameters(new Dictionary<string, object>() { { "@username", username } });

using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
result.Add(reader.GetInt32(0).ToString());
}
}
}
return result;
}
}

public bool ValidateUser(string username, string password)
{
using (var connection = _db.GetConnection())
{
string sql = "SELECT * FROM Users WHERE Username = @username;";
string sql = "SELECT * FROM Users WHERE Username = @username and enable = 1;";
var command = connection.CreateCommand();
command.CommandText = sql;
command.AddParameters(new Dictionary<string, object>
Expand All @@ -104,5 +71,29 @@ public void UpdatePassword(string id, string newPassword)
}
}

public Dictionary<string, object> GetUser(string userName)
{
using (var connection = _db.GetConnection())
{
string sql = "SELECT * FROM Users WHERE Username = @username and enable = 1;";
var command = connection.CreateCommand();
command.CommandText = sql;
command.AddParameters(new Dictionary<string, object>
{
{ "@username", userName }
});
var reader = command.ExecuteReader();
if (reader.Read())
{
return new Dictionary<string, object>
{
{ "id", reader["id"] },
{ "username", reader["username"] },
{ "roles", reader["roles"]?.ToString().Split(',') }
};
}
return null;
}
}
}
}
84 changes: 33 additions & 51 deletions src/MiniAuth/MiniAuthDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ public DbConnection GetConnection()
}
public MiniAuthDB(string connectionString)
{
_GetConnection = () => {
_GetConnection = () =>
{
var cn = new T();
cn.ConnectionString = connectionString;
if(cn.State==System.Data.ConnectionState.Closed )
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
return cn;
};
Expand All @@ -33,68 +34,49 @@ public MiniAuthDB(string connectionString)
{
SQLiteConnection.CreateFile("miniauth.db");
string sql = @"
DROP TABLE IF EXISTS users;
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
extension TEXT NULL
create table users (
id text not null primary key,
username text not null unique,
password text not null,
roles text,
enable integer default 1,
first_name text,
last_name text,
mail text,
emp_no text
);
ALTER table users add column emp_no text default '';
ALTER table users add column first_name text default '';
ALTER table users add column last_name text default '';
ALTER table users add column mail text default '';
ALTER table users add column roles text default '';
ALTER table users add column Enable integer default 0;
DROP TABLE IF EXISTS roles;
CREATE TABLE roles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
enable INTEGER DEFAULT (1) NOT NULL
create table roles (
id text primary key,
name text not null unique,
enable integer default (1) not null
);
DROP TABLE IF EXISTS users_roles;
CREATE TABLE users_roles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
role_id INTEGER,
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(role_id) REFERENCES roles(id)
);
DROP TABLE IF EXISTS endpoints;
CREATE TABLE endpoints (
id string PRIMARY KEY,
create table endpoints (
id text primary key,
type text not null,
name TEXT NOT NULL,
route TEXT NOT NULL,
methods TEXT,
enable INTEGER NOT NULL,
redirectToLoginPage INTEGER NOT NULL,
roles TEXT NOT NULL
);
DROP TABLE IF EXISTS role_endpoints;
CREATE TABLE role_endpoints (
role_id INTEGER NOT NULL,
endpoint_id string NOT NULL,
PRIMARY KEY (role_id, endpoint_id)
name text not null,
route text not null,
methods text,
enable integer default (1) not null,
redirecttologinpage integer not null,
roles text
);
-- Insert users
INSERT INTO users (username,password) VALUES ('miniauth','');
insert into roles (id,name) values ('141f6722-b2d2-4d2b-81a8-a889335e2acd','miniauth-admin');
insert into roles (id,name) values ('3ab21f79-fa49-498b-aa3d-e57188d3b0d2','miniauth-user');
insert into roles (id,name) values ('25347851-cdfe-4456-b525-52dc8cb95f10','miniauth-hr');
insert into roles (id,name) values ('b783d347-30cc-46b5-b2fc-d0b2b41684ad','miniauth-it');
insert into users (id,username,password,roles) values ('d8eb1139-7ee0-4dbd-b8a1-3c979543b982','miniauth','','141f6722-b2d2-4d2b-81a8-a889335e2acd');
insert into users (id,username,password,roles) values ('a564df6f-705f-4361-a346-b578d7a711a8','miniauth-hr','','25347851-cdfe-4456-b525-52dc8cb95f10');
-- Insert roles
INSERT INTO roles (name) VALUES ('admin');
INSERT INTO roles (name) VALUES ('user');
-- Assign roles to users
INSERT INTO users_roles (user_id, role_id) VALUES (1, 1);
";
using (var connection = _GetConnection())
{
connection.ExecuteNonQuery(sql);
new UserManager(this).UpdatePassword("1", "miniauth");
new UserManager(this).UpdatePassword("d8eb1139-7ee0-4dbd-b8a1-3c979543b982", "miniauth");
}
}
}
Expand Down
Loading

0 comments on commit 972082b

Please sign in to comment.