diff --git a/lib/database/accounts_to_postgres.dart b/lib/database/accounts_to_postgres.dart new file mode 100644 index 0000000..0a43c8c --- /dev/null +++ b/lib/database/accounts_to_postgres.dart @@ -0,0 +1,94 @@ + +import 'dart:convert'; +import 'dart:io'; +import 'package:postgres/postgres.dart'; + +class AccountsToPostgres{ + final connection = PostgreSQLConnection("localhost", 5432, 'passworld',username: 'pass',password: '1p2a3s4s5'); + + AccountsToPostgres(){ + initConnection(); + } + + void initConnection()async{ + await connection.open().then((value){ + print("PostgreSQL connection opened"); + + }); + } + + @override + void create(String id,String hash,String salt,List twoFaStr, File passwordFile ) async { + List passwordBlob = utf8.encode( await passwordFile.readAsString(encoding: utf8)); + + + connection.query("INSERT INTO \"Account\" VALUES(@id,@hash,@salt,@twofa,@passwords)",substitutionValues: { + "id" : id, + "hash" : hash, + "salt" : salt, + "twofa" : twoFaStr, + "passwords" : passwordBlob + }); + } + + @override + Future selectHashById(String id) async { + List> results = await connection.query("SELECT hash FROM \"Account\" WHERE id=@identifiant",substitutionValues: { + "identifiant" : id + }); + + connection.close(); + return results[0][0]; + } + + @override + void updatePass(String identifiant,String hash,String salt) async { + if(selectHashById(identifiant)==null){ + return; + }else{ + await connection.query("UPDATE \"Account\" SET hash=@h, salt=@s WHERE id=@identifiant",substitutionValues: { + "identifiant" : identifiant, + "h" : hash, + "s" : salt + }); + } + } + + @override + void updateFilePass(String identifiant, File passwordFile) async{ + List passwordBlob = utf8.encode( await passwordFile.readAsString(encoding: utf8)); + + if(selectHashById(identifiant)==null){ + return; + }else{ + await connection.query("UPDATE \"Account\" SET passwords=@p WHERE id=@identifiant",substitutionValues: { + "identifiant" : identifiant, + "p" : passwordBlob + }); + } + } + + @override + void updateTwoFa(String identifiant,List tfa) async { + List twoFaStr = List.empty(growable: true); + + if(selectHashById(identifiant)==null){ + return; + }else{ + await connection.query("UPDATE \"Account\" SET twofa=@tfa WHERE id=@identifiant",substitutionValues: { + "identifiant" : identifiant, + "tfa" : tfa + }); + } + } + + + + + @override + void DeleteById(String id) async{ + await connection.query("DELETE FROM \"Account\" WHERE id=@identifiant",substitutionValues: { + "identifiant" : id + }); + } +} \ No newline at end of file diff --git a/lib/database/blank.inutile b/lib/database/blank.inutile deleted file mode 100644 index e69de29..0000000 diff --git a/pubspec.lock b/pubspec.lock index fc673b5..b687f02 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -36,6 +36,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.1" + buffer: + dependency: transitive + description: + name: buffer + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.1" collection: dependency: transitive description: @@ -183,6 +190,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.2" + pedantic: + dependency: transitive + description: + name: pedantic + url: "https://pub.dartlang.org" + source: hosted + version: "1.11.1" pool: dependency: transitive description: @@ -190,6 +204,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.5.1" + postgres: + dependency: "direct main" + description: + name: postgres + url: "https://pub.dartlang.org" + source: hosted + version: "2.5.2" pub_semver: dependency: transitive description: @@ -197,6 +218,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.3" + sasl_scram: + dependency: transitive + description: + name: sasl_scram + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.1" + saslprep: + dependency: transitive + description: + name: saslprep + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" shelf: dependency: "direct main" description: @@ -309,6 +344,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.3.1" + unorm_dart: + dependency: transitive + description: + name: unorm_dart + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.0" vm_service: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 8ff70c4..a7cafa0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,6 +9,7 @@ environment: dependencies: args: ^2.0.0 path: ^1.8.2 + postgres: ^2.5.2 shelf: ^1.1.0 shelf_router: ^1.0.0