diff --git a/bin/server.dart b/bin/server.dart index 9d68a6e..bd96f4e 100755 --- a/bin/server.dart +++ b/bin/server.dart @@ -1,5 +1,6 @@ import 'dart:io'; import 'package:passworld_api/api/api.dart'; +import 'package:passworld_api/database/accounts_to_postgres.dart'; import 'package:shelf/shelf.dart'; import 'package:shelf/shelf_io.dart'; import 'package:shelf_router/shelf_router.dart'; @@ -8,6 +9,7 @@ import 'package:shelf_router/shelf_router.dart'; final _router = Router() // GET ..get('/', API.rootHandler) + ..get('/admin/get-all-users', API.getAllUsers) // POST (EN VRAI C'EST DES GET AVEC UN BODY) ..post('/user/password-file', API.downloadPasswordDb) ..post('/auth', API.authenticator) @@ -40,6 +42,7 @@ Future fileExist(String path) { void main(List args) async { // Use any available host or container IP (usually `0.0.0.0`). + await AccountsToPostgres.createAccountTable(); final ip = InternetAddress.anyIPv4; // Configure a pipeline that logs requests. diff --git a/lib/api/api.dart b/lib/api/api.dart index d9ca7d7..fd9686e 100644 --- a/lib/api/api.dart +++ b/lib/api/api.dart @@ -1,3 +1,5 @@ +import 'package:passworld_api/db_to_api.dart'; +import 'package:postgres/postgres.dart'; import 'package:shelf/shelf.dart'; import 'package:shelf_router/shelf_router.dart'; import 'dart:convert'; @@ -57,9 +59,8 @@ class API { final Map body = json.decode(tmp); if (await checkRequiredFields(required, body)) { - AccountsToPostgres db = AccountsToPostgres(); // List twofa = body[required[3]]; - db.create( + await AccountsToPostgres.create( body[required[0]], body[required[1]], body[required[2]] /*, twofa*/); return Response.ok('true'); } else { @@ -115,4 +116,14 @@ class API { } return true; } + + // + // ADMIN + // + + static Future getAllUsers(Request req) async { + PostgreSQLResult res = await AccountsToPostgres.getAllUsers(); + String json = DB2API.map2Json(res); + return Response.ok(json); + } } diff --git a/lib/database/accounts_to_postgres.dart b/lib/database/accounts_to_postgres.dart index ad07766..9126f11 100644 --- a/lib/database/accounts_to_postgres.dart +++ b/lib/database/accounts_to_postgres.dart @@ -1,54 +1,75 @@ import 'dart:convert'; +import 'dart:ffi'; import 'dart:io'; import 'package:postgres/postgres.dart'; class AccountsToPostgres { - /* Dev - final connection = PostgreSQLConnection("localhost", 5432, 'passworld', - username: 'pass', password: '1p2a3s4s5'); - */ - - // Production - final connection = PostgreSQLConnection(Platform.environment["DB_SERVER"]!, - 5432, Platform.environment["DB_DATABASE"]!, + /* Dev Coco */ + // static final connection = PostgreSQLConnection("localhost", 5432, 'passworld', + // username: 'pass', password: '1p2a3s4s5'); + + /* Dev RemRem */ + // static final connection = PostgreSQLConnection("localhost", 5432, 'passworld', + // username: 'hel', password: ''); + + /* Production */ + static final connection = PostgreSQLConnection( + Platform.environment["DB_SERVER"]!, + 5432, + Platform.environment["DB_DATABASE"]!, username: Platform.environment["DB_USER"], password: Platform.environment["DB_PASSWORD"]); AccountsToPostgres() { - initConnection(); + //initConnection(); } - void initConnection() async { + static Future openConnection() async { await connection.open().then((value) { - print("PostgreSQL connection opened"); + print("🟢 PassWorld DB connection opened"); + }); + } + + static void closeConnection() async { + connection.close().then((value) { + print("🔴 PassWorld DB connection closed"); }); } - @override - void create(String email, String hash, + static Future createAccountTable() async { + await openConnection(); + await connection + .query( + "CREATE TABLE IF NOT EXISTS \"Account\"(id TEXT PRIMARY KEY,hash TEXT NOT NULL,salt TEXT NOT NULL,twofa VARCHAR(50)[],passwords INTEGER[])") + .then((value) { + print("🟦 Account Table Created"); + }); + } + + // Add support for twoFa if needed + static Future create(String email, String hash, String salt /*, List twoFaStr*/) async { - await connection.query( - "INSERT INTO \"Account\" VALUES(@id,@hash,@salt,@twofa,@passwords)", + await connection.query("INSERT INTO \"Account\" VALUES(@id,@hash,@salt)", substitutionValues: { "id": email, "hash": hash, "salt": salt /*, "twofa": twoFaStr*/ }); - print("Account succesfully created"); + print("✅ Account succesfully created"); } - @override - Future selectHashById(String id) async { + static Future selectHashById(String id) async { List> results = await connection.query( "SELECT hash FROM \"Account\" WHERE id=@identifiant", substitutionValues: {"identifiant": id}); + closeConnection(); return results[0][0]; } - @override - void updatePass(String identifiant, String hash, String salt) async { + static Future updatePass( + String identifiant, String hash, String salt) async { if (selectHashById(identifiant) == null) { return; } else { @@ -62,8 +83,8 @@ class AccountsToPostgres { } } - @override - void updateFilePass(String identifiant, File passwordFile) async { + static Future updateFilePass( + String identifiant, File passwordFile) async { List passwordBlob = utf8.encode(await passwordFile.readAsString(encoding: utf8)); @@ -76,8 +97,7 @@ class AccountsToPostgres { } } - @override - void updateTwoFa(String identifiant, List tfa) async { + static Future updateTwoFa(String identifiant, List tfa) async { List twoFaStr = List.empty(growable: true); if (selectHashById(identifiant) == null) { @@ -89,9 +109,18 @@ class AccountsToPostgres { } } - @override - void DeleteById(String id) async { + static Future deleteById(String id) async { await connection.query("DELETE FROM \"Account\" WHERE id=@identifiant", substitutionValues: {"identifiant": id}); } + + // + // ADMIN + // + + static Future getAllUsers() async { + PostgreSQLResult res = + await connection.query("SELECT id, hash, salt from \"Account\""); + return res; + } } diff --git a/lib/db_to_api.dart b/lib/db_to_api.dart new file mode 100644 index 0000000..ef53cf4 --- /dev/null +++ b/lib/db_to_api.dart @@ -0,0 +1,8 @@ +import 'dart:convert'; +import 'package:postgres/postgres.dart'; + +class DB2API { + static String map2Json(PostgreSQLResult data) { + return jsonEncode(data); + } +} diff --git a/test/db_remrem_test.dart b/test/db_remrem_test.dart new file mode 100644 index 0000000..55622ef --- /dev/null +++ b/test/db_remrem_test.dart @@ -0,0 +1,20 @@ +import 'dart:io'; + +import 'package:passworld_api/database/accounts_to_postgres.dart'; +import 'package:passworld_api/db_to_api.dart'; + +void main() async { + await AccountsToPostgres.createAccountTable(); + await AccountsToPostgres.create("remremc@gmail.com", "hehehe", "tameare"); + var res = await AccountsToPostgres.getAllUsers(); + for (final row in res) { + stdout.write(row[0]); + stdout.write(row[1]); + print(row[2]); + } + //print(res.runtimeType); + String json = DB2API.map2Json(res); + print(json); + AccountsToPostgres.closeConnection(); + return; +}