@ -1,3 +1,4 @@
import ' dart:io ' ;
import ' package:passworld_api/db_to_api.dart ' ;
import ' package:postgres/postgres.dart ' ;
import ' package:shelf/shelf.dart ' ;
@ -7,57 +8,43 @@ import 'package:passworld_api/database/accounts_to_postgres.dart';
/ / Class for all static function that handles api routes
class API {
/ * - - - - - - - - - - - - - - - |
| - - - - - - - GET - - - - - - |
| - - - - - - - - - - - - - - - * /
/ / Default response for /
static Response rootHandler ( Request req ) {
return Response . ok ( ' Greetings from PassWorld! \n ' ) ;
}
/ / Check for authentication
static Future < Response > authenticator ( Request req ) async {
final List < String > required = [ " email " , " password " ] ;
static Future < Response > getSalt ( Request req ) async {
final List < String > required = [ " email " ] ;
final body = await bodyToJson ( req ) ;
if ( await checkRequiredFields ( required , body ) ) {
try {
await AccountsToPostgres . selectHashById ( body [ required [ 0 ] ] ) ;
String salt =
await AccountsToPostgres . selectSaltByMail ( body [ required [ 0 ] ] ) ;
return Response ( 200 , body: salt ) ;
} catch ( e ) {
return Response ( 404 ,
body: ' Not Found ' ) ; / / no hash found - > 404 ( Not Found )
return Response ( 204 , body: ' Account already existing ' ) ; / / No content
}
return Response . ok ( ' Succesfully Authenticated ' ) ; / / 200 ( Ok )
} else {
return Response . badRequest (
body: ' Bad password or email ! ' ) ; / / 400 ( Bad Request )
return Response . badRequest ( body: ' bad body ' ) ;
}
}
/ / Download sqlite password file
static Response downloadPasswordDb ( Request req ) {
final mail = req . params [ ' mail ' ] ;
final password = req . params [ ' cyphered_password_hash ' ] ;
/ / Database query - > return file ( List < int > )
/ / Create stream from List < int >
/ / Rename file - > db_password_ < mail > _ < date >
/ / Send file
return Response . ok ( " " ) ;
/ *
Stream < List < int > > fileStream = file . openRead ( ) ;
return Response . ok ( fileStream , headers: {
' Content-Type ' : ' application/octet-stream ' ,
' Content-Disposition ' : ' attachment, filename=" $ reqFile " '
} ) ;
* /
/ / Check for authentication
static Future < Response > authenticator ( Request req ) async {
final List < String > required = [ " email " , " password " ] ;
final body = await bodyToJson ( req ) ;
if ( await checkRequiredFields ( required , body ) ) {
if ( await checkAuthentication ( body [ required [ 0 ] ] , body [ required [ 1 ] ] ) ) {
return Response . ok ( ' Succesfully Authenticated ' ) ;
} else {
return Response . unauthorized ( ' Bad password or email ! ' ) ; / / 401
}
} else {
return Response . badRequest ( body: ' bad body ' ) ; / / 401
}
}
/ * - - - - - - - - - - - - - - - |
| - - - - - - POST - - - - - - |
| - - - - - - - - - - - - - - - * /
/ / Create account
static Future < Response > createAccount ( Request req ) async {
@ -65,14 +52,14 @@ class API {
final body = await bodyToJson ( req ) ;
if ( await checkRequiredFields ( required , body ) ) {
/ / List < String > twofa = body [ required [ 3 ] ] ;
try {
await AccountsToPostgres . create ( body [ required [ 0 ] ] , body [ required [ 1 ] ] ,
body [ required [ 2] ] /*, twofa*/ ) ;
await AccountsToPostgres . create Account(
body [ required [ 0] ] , body [ required [ 1 ] ] , body [ required [ 2 ] ] ) ;
} catch ( e ) {
return Response ( 409 ,
body: ' Account already existing ' ) ; / / 409 ( Conflict )
}
print ( " ✅ Account succesfully created " ) ;
return Response ( 201 ,
body: ' Account successfully created ' ) ; / / 201 ( Created )
} else {
@ -80,38 +67,130 @@ class API {
}
}
/ * - - - - - - - - - - - - - - - |
| - - - - - - - PUT - - - - - - |
| - - - - - - - - - - - - - - - * /
/ / Delete Account
static Future < Response > deleteAccount ( Request req ) async {
final List < String > required = [ " email " , " password " ] ;
final body = await bodyToJson ( req ) ;
if ( await checkRequiredFields ( required , body ) ) {
try {
if ( await checkAuthentication ( body [ required [ 0 ] ] , body [ required [ 1 ] ] ) ) {
await AccountsToPostgres . deleteAccount ( body [ required [ 0 ] ] ) ;
} else {
return Response ( 403 ,
body:
' You haven \' t provided the good password or mail ' ) ; / / 403 ( Forbidden )
}
} catch ( e , s ) {
print ( " Exception $ e " ) ;
print ( " Stacktrace $ s " ) ;
return Response ( 409 ,
body: ' There was a problem with deletion ' ) ; / / 409 ( Conflict )
}
print ( " ✅ Account succesfully deleted " ) ;
return Response ( 200 , body: ' Account successfully deleted ' ) ; / / 200 ( OK )
} else {
return Response . badRequest ( body: ' Bad request ' ) ; / / 400 ( Bad Request )
}
}
/ / Update master password
static Response changeMasterPassword ( Request req ) {
return Response . ok ( " master password chnaged " ) ;
static Future < Response > changeMasterPassword ( Request req ) async {
final List < String > required = [ " email " , " newPassword " , " newSalt " ] ;
final body = await bodyToJson ( req ) ;
if ( await checkRequiredFields ( required , body ) ) {
try {
await AccountsToPostgres . updatePassword (
body [ required [ 0 ] ] , body [ required [ 1 ] ] , body [ required [ 2 ] ] ) ;
} catch ( e ) {
return Response ( 403 ,
body: ' This is not the good password ' ) ; / / 403 ( Forbidden )
}
return Response ( 201 ,
body: ' user \' s password succesfully changed ' ) ; / / 201 ( Created )
} else {
return Response . badRequest ( body: ' Bad request ' ) ; / / 400 ( Bad Request )
}
}
/ / Update mail
static Response changeMail ( Request req ) {
return Response . ok ( " master password chnaged " ) ;
static Future < Response > changeMail ( Request req ) async {
final List < String > required = [ " email " , " newMail " ] ;
final body = await bodyToJson ( req ) ;
if ( await checkRequiredFields ( required , body ) ) {
try {
await AccountsToPostgres . updateMail (
body [ required [ 0 ] ] , body [ required [ 1 ] ] ) ;
} catch ( e ) {
return Response ( 403 ,
body: ' This is not the good password ' ) ; / / 403 ( Forbidden )
}
return Response ( 201 ,
body: ' user \' s mail succesfully changed ' ) ; / / 201 ( Created )
} else {
return Response . badRequest ( body: ' Bad request ' ) ; / / 400 ( Bad Request )
}
}
/ / Upload sqlite password file
static Response uploadPasswordDb ( Request req ) {
return Response . ok ( " " ) ;
static Future < Response > uploadPasswordDb ( Request req ) async {
final List < String > required = [ " email " , " password " , " file " ] ;
final body = await bodyToJson ( req ) ;
if ( await checkRequiredFields ( required , body ) ) {
try {
if ( await checkAuthentication ( body [ required [ 0 ] ] , body [ required [ 1 ] ] ) ) {
String fileAsBytes = body [ required [ 2 ] ] ;
var arrayBytes = fileAsBytes . split ( ' , ' ) ;
arrayBytes . removeLast ( ) ;
List < int > arrayBytes2 = arrayBytes . map ( int . parse ) . toList ( ) ;
await AccountsToPostgres . updatePasswordFile (
body [ required [ 0 ] ] , arrayBytes2 ) ;
} else {
return Response ( 403 ) ; / / 403 ( Forbidden )
}
} catch ( e , s ) {
print ( " Exception $ e " ) ;
print ( " Stacktrace $ s " ) ;
return Response ( 409 ,
body: ' There was a problem with upload ' ) ; / / 409 ( Conflict )
}
print ( " ✅ PassWord file succesfully uploaded " ) ;
return Response ( 201 ,
body: ' PassWord file succesfully uploaded ' ) ; / / 20 ( OK )
} else {
return Response . badRequest ( body: ' Bad request ' ) ; / / 400 ( Bad Request )
}
}
/ * - - - - - - - - - - - - - - - |
| - - - - - DELETE - - - - - |
| - - - - - - - - - - - - - - - * /
/ / Download sqlite password file
static Future < Response > downloadPasswordDb ( Request req ) async {
final List < String > required = [ " email " , " password " ] ;
final body = await bodyToJson ( req ) ;
/ / Delete account
static Response deleteAccount ( Request req ) {
return Response . ok ( " " ) ;
if ( await checkRequiredFields ( required , body ) ) {
try {
if ( await checkAuthentication ( body [ required [ 0 ] ] , body [ required [ 1 ] ] ) ) {
List < int > file =
await AccountsToPostgres . getPasswordFile ( body [ required [ 0 ] ] ) ;
print ( " ✅ PassWord file succesfully downloaded " ) ;
return Response ( 200 , body: file . toString ( ) ) ;
} else {
return Response ( 403 ) ; / / 403 ( Forbidden )
}
} catch ( e , s ) {
print ( " Exception $ e " ) ;
print ( " Stacktrace $ s " ) ;
return Response ( 409 ,
body: ' There was a problem with upload ' ) ; / / 409 ( Conflict )
} / / 200 ( OK )
} else {
return Response . badRequest ( body: ' Bad request ' ) ; / / 400 ( Bad Request )
}
}
/ * - - - - - - - - - - - - - - - |
| - - - - - - - MISC - - - - - |
| - - - - - - - - - - - - - - - * /
/ / Check if required fields are in req body
static Future < bool > checkRequiredFields (
List < String > fields , Map < String , dynamic > body ) async {
@ -133,6 +212,44 @@ class API {
return json . decode ( tmp ) ;
}
static Future < bool > checkAuthentication (
String givedMail , String givedPassword ) async {
try {
if ( ! await checkMail ( givedMail ) ) return false ;
} catch ( e ) {
/ / catch if there is nothing in result of checkMail
return false ;
}
if ( ! await checkPassword ( givedMail , givedPassword ) ) return false ;
print ( " authentication successed !!! " ) ;
return true ;
}
static Future < bool > checkPassword (
String givedMail , String givedPassword ) async {
print ( " check hash... " ) ;
var hash = await AccountsToPostgres . selectHashByMail ( givedMail ) ;
if ( hash = = givedPassword ) {
print ( " hash is good " ) ;
return true ;
}
print ( " hash is bad " ) ;
return false ;
}
static Future < bool > checkMail ( String givedMail ) async {
print ( " check mail... " ) ;
var mail = await AccountsToPostgres . selectMailByMail ( givedMail ) ;
if ( mail = = givedMail ) {
print ( " mail is good " ) ;
return true ;
}
print ( " mail is bad " ) ;
return false ;
}
/ /
/ / ADMIN
/ /