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

adminDb
remrem 3 years ago
parent 018d905b6c
commit aeca340c8d

@ -1,6 +1,7 @@
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';
import 'package:passworld_api/database/accounts_to_postgres.dart';
// Class for all static function that handles api routes // Class for all static function that handles api routes
class API { class API {
@ -15,13 +16,14 @@ class API {
// Check for authentication // Check for authentication
static Future<Response> authenticator(Request req) async { static Future<Response> authenticator(Request req) async {
final List<String> required = ["mail", "password"]; // final List<String> required = ["email", "password"];
if (await checkRequiredFields(required, req)) { // if (await checkRequiredFields(required, req)) {
return Response.ok('true'); // return Response.ok('true');
} // } else {
// return Response.badRequest();
return Response.badRequest(); // }
return Response(404);
} }
// Download sqlite password file // Download sqlite password file
@ -49,8 +51,19 @@ class API {
|---------------*/ |---------------*/
// Create account // Create account
static Response createAccount(Request req) { static Future<Response> createAccount(Request req) async {
return Response.ok(""); final List<String> required = ["email", "password", "salt", "twofa"];
var tmp = await req.readAsString();
final Map<String, dynamic> body = json.decode(tmp);
if (await checkRequiredFields(required, body)) {
AccountsToPostgres db = AccountsToPostgres();
db.create(body[required[0]], body[required[1]], body[required[2]],
body[required[3]]);
return Response.ok('true');
} else {
return Response.badRequest();
}
} }
/*---------------| /*---------------|
@ -87,16 +100,18 @@ class API {
// Check if required fields are in req body // Check if required fields are in req body
static Future<bool> checkRequiredFields( static Future<bool> checkRequiredFields(
List<String> fields, Request req) async { List<String> fields, Map<String, dynamic> body) async {
// json object read -> check dic keys // json object read -> check dic keys
var tmp = await req.readAsString(); for (String itFields in fields) {
final Map<String, dynamic> body = json.decode(tmp); if (!body.containsKey(itFields)) {
bool check = false; print(itFields);
for (String s in fields) { return false;
if (body['$s'] == "") { }
if (body[itFields] == "") {
print(itFields);
return false; return false;
} }
} }
return false; return true;
} }
} }

@ -1,94 +1,99 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:postgres/postgres.dart'; import 'package:postgres/postgres.dart';
class AccountsToPostgres{ class AccountsToPostgres {
final connection = PostgreSQLConnection("localhost", 5432, 'passworld',username: 'pass',password: '1p2a3s4s5'); /* Dev
final connection = PostgreSQLConnection("localhost", 5432, 'passworld',
AccountsToPostgres(){ username: 'pass', password: '1p2a3s4s5');
*/
// Production
final connection = PostgreSQLConnection(
Platform.environment["ENV_DB_SERVER"]!,
5432,
Platform.environment["ENV_DB_DATABASE"]!,
username: Platform.environment["ENV_DB_USER"],
password: Platform.environment["ENV_DB_PASSWORD"]);
AccountsToPostgres() {
initConnection(); initConnection();
} }
void initConnection()async{ void initConnection() async {
await connection.open().then((value){ await connection.open().then((value) {
print("PostgreSQL connection opened"); print("PostgreSQL connection opened");
}); });
} }
@override @override
void create(String id,String hash,String salt,List<String> twoFaStr, File passwordFile ) async { void create(
List<int> passwordBlob = utf8.encode( await passwordFile.readAsString(encoding: utf8)); String email, String hash, String salt, List<String> twoFaStr) async {
connection.query(
"INSERT INTO \"Account\" VALUES(@id,@hash,@salt,@twofa,@passwords)",
connection.query("INSERT INTO \"Account\" VALUES(@id,@hash,@salt,@twofa,@passwords)",substitutionValues: { substitutionValues: {
"id" : id, "id": email,
"hash" : hash, "hash": hash,
"salt" : salt, "salt": salt,
"twofa" : twoFaStr, "twofa": twoFaStr
"passwords" : passwordBlob });
}); print("Account succesfully created");
} }
@override @override
Future<String> selectHashById(String id) async { Future<String> selectHashById(String id) async {
List<List<dynamic>> results = await connection.query("SELECT hash FROM \"Account\" WHERE id=@identifiant",substitutionValues: { List<List<dynamic>> results = await connection.query(
"identifiant" : id "SELECT hash FROM \"Account\" WHERE id=@identifiant",
}); substitutionValues: {"identifiant": id});
connection.close();
return results[0][0]; return results[0][0];
} }
@override @override
void updatePass(String identifiant,String hash,String salt) async { void updatePass(String identifiant, String hash, String salt) async {
if(selectHashById(identifiant)==null){ if (selectHashById(identifiant) == null) {
return; return;
}else{ } else {
await connection.query("UPDATE \"Account\" SET hash=@h, salt=@s WHERE id=@identifiant",substitutionValues: { await connection.query(
"identifiant" : identifiant, "UPDATE \"Account\" SET hash=@h, salt=@s WHERE id=@identifiant",
"h" : hash, substitutionValues: {
"s" : salt "identifiant": identifiant,
}); "h": hash,
"s": salt
});
} }
} }
@override @override
void updateFilePass(String identifiant, File passwordFile) async{ void updateFilePass(String identifiant, File passwordFile) async {
List<int> passwordBlob = utf8.encode( await passwordFile.readAsString(encoding: utf8)); List<int> passwordBlob =
utf8.encode(await passwordFile.readAsString(encoding: utf8));
if(selectHashById(identifiant)==null){ if (selectHashById(identifiant) == null) {
return; return;
}else{ } else {
await connection.query("UPDATE \"Account\" SET passwords=@p WHERE id=@identifiant",substitutionValues: { await connection.query(
"identifiant" : identifiant, "UPDATE \"Account\" SET passwords=@p WHERE id=@identifiant",
"p" : passwordBlob substitutionValues: {"identifiant": identifiant, "p": passwordBlob});
});
} }
} }
@override @override
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) {
return; return;
}else{ } else {
await connection.query("UPDATE \"Account\" SET twofa=@tfa WHERE id=@identifiant",substitutionValues: { await connection.query(
"identifiant" : identifiant, "UPDATE \"Account\" SET twofa=@tfa WHERE id=@identifiant",
"tfa" : tfa substitutionValues: {"identifiant": identifiant, "tfa": tfa});
}); }
}
} }
@override @override
void DeleteById(String id) async{ void DeleteById(String id) async {
await connection.query("DELETE FROM \"Account\" WHERE id=@identifiant",substitutionValues: { await connection.query("DELETE FROM \"Account\" WHERE id=@identifiant",
"identifiant" : id substitutionValues: {"identifiant": id});
});
} }
} }

@ -0,0 +1,20 @@
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() async {
String base1 =
'https://codefirst.iut.uca.fr/containers/passworld-api-remiarnal';
String base2 = 'localhost:8080';
Uri baseURL = Uri.parse("$base2/auth");
String body = """
{
"mail" : "haha",
"password" : "haha"
}
""";
var res = await http.post(baseURL, body: body);
print(res.body);
}
Loading…
Cancel
Save