WWWAWWAWAWAWA2
continuous-integration/drone/push Build is passing Details

adminDb
remrem 3 years ago
parent c8c3e1250b
commit b5c7a186c7

@ -1,5 +1,6 @@
import 'dart:io'; import 'dart:io';
import 'package:passworld_api/api/api.dart'; 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.dart';
import 'package:shelf/shelf_io.dart'; import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart'; import 'package:shelf_router/shelf_router.dart';
@ -8,6 +9,7 @@ import 'package:shelf_router/shelf_router.dart';
final _router = Router() final _router = Router()
// GET // GET
..get('/', API.rootHandler) ..get('/', API.rootHandler)
..get('/admin/get-all-users', API.getAllUsers)
// POST (EN VRAI C'EST DES GET AVEC UN BODY) // POST (EN VRAI C'EST DES GET AVEC UN BODY)
..post('/user/password-file', API.downloadPasswordDb) ..post('/user/password-file', API.downloadPasswordDb)
..post('/auth', API.authenticator) ..post('/auth', API.authenticator)
@ -40,6 +42,7 @@ Future<bool> fileExist(String path) {
void main(List<String> args) async { void main(List<String> args) async {
// Use any available host or container IP (usually `0.0.0.0`). // Use any available host or container IP (usually `0.0.0.0`).
await AccountsToPostgres.createAccountTable();
final ip = InternetAddress.anyIPv4; final ip = InternetAddress.anyIPv4;
// Configure a pipeline that logs requests. // Configure a pipeline that logs requests.

@ -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/shelf.dart';
import 'package:shelf_router/shelf_router.dart'; import 'package:shelf_router/shelf_router.dart';
import 'dart:convert'; import 'dart:convert';
@ -57,9 +59,8 @@ class API {
final Map<String, dynamic> body = json.decode(tmp); final Map<String, dynamic> body = json.decode(tmp);
if (await checkRequiredFields(required, body)) { if (await checkRequiredFields(required, body)) {
AccountsToPostgres db = AccountsToPostgres();
// List<String> twofa = body[required[3]]; // List<String> twofa = body[required[3]];
db.create( await AccountsToPostgres.create(
body[required[0]], body[required[1]], body[required[2]] /*, twofa*/); body[required[0]], body[required[1]], body[required[2]] /*, twofa*/);
return Response.ok('true'); return Response.ok('true');
} else { } else {
@ -115,4 +116,14 @@ class API {
} }
return true; return true;
} }
//
// ADMIN
//
static Future<Response> getAllUsers(Request req) async {
PostgreSQLResult res = await AccountsToPostgres.getAllUsers();
String json = DB2API.map2Json(res);
return Response.ok(json);
}
} }

@ -1,54 +1,75 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:ffi';
import 'dart:io'; import 'dart:io';
import 'package:postgres/postgres.dart'; import 'package:postgres/postgres.dart';
class AccountsToPostgres { class AccountsToPostgres {
/* Dev /* Dev Coco */
final connection = PostgreSQLConnection("localhost", 5432, 'passworld', // static final connection = PostgreSQLConnection("localhost", 5432, 'passworld',
username: 'pass', password: '1p2a3s4s5'); // username: 'pass', password: '1p2a3s4s5');
*/
/* Dev RemRem */
// Production // static final connection = PostgreSQLConnection("localhost", 5432, 'passworld',
final connection = PostgreSQLConnection(Platform.environment["DB_SERVER"]!, // username: 'hel', password: '');
5432, Platform.environment["DB_DATABASE"]!,
/* Production */
static final connection = PostgreSQLConnection(
Platform.environment["DB_SERVER"]!,
5432,
Platform.environment["DB_DATABASE"]!,
username: Platform.environment["DB_USER"], username: Platform.environment["DB_USER"],
password: Platform.environment["DB_PASSWORD"]); password: Platform.environment["DB_PASSWORD"]);
AccountsToPostgres() { AccountsToPostgres() {
initConnection(); //initConnection();
} }
void initConnection() async { static Future<void> openConnection() async {
await connection.open().then((value) { 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");
});
}
static Future<void> 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");
}); });
} }
@override // Add support for twoFa if needed
void create(String email, String hash, static Future<void> create(String email, String hash,
String salt /*, List<String> twoFaStr*/) async { String salt /*, List<String> twoFaStr*/) async {
await connection.query( await connection.query("INSERT INTO \"Account\" VALUES(@id,@hash,@salt)",
"INSERT INTO \"Account\" VALUES(@id,@hash,@salt,@twofa,@passwords)",
substitutionValues: { substitutionValues: {
"id": email, "id": email,
"hash": hash, "hash": hash,
"salt": salt /*, "salt": salt /*,
"twofa": twoFaStr*/ "twofa": twoFaStr*/
}); });
print("Account succesfully created"); print("Account succesfully created");
} }
@override static Future<String> selectHashById(String id) async {
Future<String> selectHashById(String id) async {
List<List<dynamic>> results = await connection.query( List<List<dynamic>> results = await connection.query(
"SELECT hash FROM \"Account\" WHERE id=@identifiant", "SELECT hash FROM \"Account\" WHERE id=@identifiant",
substitutionValues: {"identifiant": id}); substitutionValues: {"identifiant": id});
closeConnection();
return results[0][0]; return results[0][0];
} }
@override static Future<void> updatePass(
void updatePass(String identifiant, String hash, String salt) async { String identifiant, String hash, String salt) async {
if (selectHashById(identifiant) == null) { if (selectHashById(identifiant) == null) {
return; return;
} else { } else {
@ -62,8 +83,8 @@ class AccountsToPostgres {
} }
} }
@override static Future<void> updateFilePass(
void updateFilePass(String identifiant, File passwordFile) async { String identifiant, File passwordFile) async {
List<int> passwordBlob = List<int> passwordBlob =
utf8.encode(await passwordFile.readAsString(encoding: utf8)); utf8.encode(await passwordFile.readAsString(encoding: utf8));
@ -76,8 +97,7 @@ class AccountsToPostgres {
} }
} }
@override static Future<void> updateTwoFa(String identifiant, List<String> tfa) async {
void updateTwoFa(String identifiant, List<String> tfa) async {
List<String> twoFaStr = List.empty(growable: true); List<String> twoFaStr = List.empty(growable: true);
if (selectHashById(identifiant) == null) { if (selectHashById(identifiant) == null) {
@ -89,9 +109,18 @@ class AccountsToPostgres {
} }
} }
@override static Future<void> deleteById(String id) async {
void DeleteById(String id) async {
await connection.query("DELETE FROM \"Account\" WHERE id=@identifiant", await connection.query("DELETE FROM \"Account\" WHERE id=@identifiant",
substitutionValues: {"identifiant": id}); substitutionValues: {"identifiant": id});
} }
//
// ADMIN
//
static Future<PostgreSQLResult> getAllUsers() async {
PostgreSQLResult res =
await connection.query("SELECT id, hash, salt from \"Account\"");
return res;
}
} }

@ -0,0 +1,8 @@
import 'dart:convert';
import 'package:postgres/postgres.dart';
class DB2API {
static String map2Json(PostgreSQLResult data) {
return jsonEncode(data);
}
}

@ -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;
}
Loading…
Cancel
Save