From 569dda01fde994b58df510fc97188f673a73cb01 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Sat, 25 Mar 2023 14:27:47 +0100 Subject: [PATCH] Moved hash function into seperate file. NOTE: useful for changePassword, which requires the hash password functionality as well. --- lib/backend/crypto.dart | 8 ++ lib/screens/auth.dart | 231 +++++++++++++++++++--------------------- 2 files changed, 118 insertions(+), 121 deletions(-) create mode 100644 lib/backend/crypto.dart diff --git a/lib/backend/crypto.dart b/lib/backend/crypto.dart new file mode 100644 index 0000000..998095c --- /dev/null +++ b/lib/backend/crypto.dart @@ -0,0 +1,8 @@ +import 'dart:convert'; +import 'package:crypto/crypto.dart'; + +String hashPassword(String pw) { + var bytes = utf8.encode(pw); + final password = sha256.convert(bytes).toString(); + return password; +} diff --git a/lib/screens/auth.dart b/lib/screens/auth.dart index 7870cc1..19f31a6 100644 --- a/lib/screens/auth.dart +++ b/lib/screens/auth.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:outbag_app/backend/crypto.dart'; import 'package:outbag_app/backend/request.dart'; import 'package:outbag_app/backend/user.dart'; import 'package:outbag_app/tools/fetch_wrapper.dart'; @@ -69,106 +70,101 @@ class _AuthPageState extends State { ), ), body: Center( - child: ConstrainedBox( - constraints: const BoxConstraints(maxWidth: 400), - child: Column( - crossAxisAlignment: CrossAxisAlignment.center, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Padding( - padding: const EdgeInsets.all(8), - child: TextField( - controller: _ctrServer, - keyboardType: TextInputType.url, - decoration: const InputDecoration( - prefixIcon: Icon(Icons.dns), - labelText: 'Server', - hintText: 'Your homeserver url', - helperText: - 'Your data will be stored on your homeserver', - border: OutlineInputBorder(), - ), - ), - ), - Padding( - padding: const EdgeInsets.all(8), - child: TextField( - controller: _ctrUsername, - keyboardType: TextInputType.emailAddress, - decoration: const InputDecoration( - prefixIcon: Icon(Icons.person), - labelText: 'Username', - hintText: 'Your username', - helperText: - 'your username and server tag allow others to identify you', - border: OutlineInputBorder(), - ), - ), - ), - Padding( - padding: const EdgeInsets.all(8), - child: TextField( - controller: _ctrPassword, - keyboardType: TextInputType.visiblePassword, - obscureText: true, - decoration: const InputDecoration( - prefixIcon: Icon(Icons.lock), - labelText: 'Password', - hintText: 'Your password', - helperText: - 'Password have to be at least six characters long', - border: OutlineInputBorder(), - ), - ), - ), - // ONLY SIGNUP - ...((widget.mode != Mode.signin) - ? [ + child: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 400), + child: Column( + crossAxisAlignment: CrossAxisAlignment.center, + mainAxisAlignment: MainAxisAlignment.center, + children: [ Padding( padding: const EdgeInsets.all(8), child: TextField( - controller: _ctrPasswordRpt, - keyboardType: - TextInputType.visiblePassword, + controller: _ctrServer, + keyboardType: TextInputType.url, + decoration: const InputDecoration( + prefixIcon: Icon(Icons.dns), + labelText: 'Server', + hintText: 'Your homeserver url', + helperText: + 'Your data will be stored on your homeserver', + border: OutlineInputBorder(), + ), + ), + ), + Padding( + padding: const EdgeInsets.all(8), + child: TextField( + controller: _ctrUsername, + keyboardType: TextInputType.emailAddress, + decoration: const InputDecoration( + prefixIcon: Icon(Icons.person), + labelText: 'Username', + hintText: 'Your username', + helperText: + 'your username and server tag allow others to identify you', + border: OutlineInputBorder(), + ), + ), + ), + Padding( + padding: const EdgeInsets.all(8), + child: TextField( + controller: _ctrPassword, + keyboardType: TextInputType.visiblePassword, obscureText: true, decoration: const InputDecoration( prefixIcon: Icon(Icons.lock), - labelText: 'Repeat Password', - hintText: 'Type your password again', + labelText: 'Password', + hintText: 'Your password', helperText: - 'Make sure to type the correct password', + 'Password have to be at least six characters long', border: OutlineInputBorder(), ), ), - ) - ] - : []), - // ONLY SIGNUP OTA - ...((widget.mode == Mode.signupOTA) - ? [ - Padding( - padding: const EdgeInsets.all(8), - child: TextField( - controller: _ctrOTA, - keyboardType: - TextInputType.visiblePassword, - decoration: const InputDecoration( - prefixIcon: Icon(Icons.key), - labelText: 'OTA', - hintText: - 'One-Time-Authorization token', - helperText: - 'This token might be required if the server is rate limited', - border: OutlineInputBorder(), - ), - ), - ) - ] - : []), - ], - ) - ) - ), + ), + // ONLY SIGNUP + ...((widget.mode != Mode.signin) + ? [ + Padding( + padding: const EdgeInsets.all(8), + child: TextField( + controller: _ctrPasswordRpt, + keyboardType: TextInputType.visiblePassword, + obscureText: true, + decoration: const InputDecoration( + prefixIcon: Icon(Icons.lock), + labelText: 'Repeat Password', + hintText: 'Type your password again', + helperText: + 'Make sure to type the correct password', + border: OutlineInputBorder(), + ), + ), + ) + ] + : []), + // ONLY SIGNUP OTA + ...((widget.mode == Mode.signupOTA) + ? [ + Padding( + padding: const EdgeInsets.all(8), + child: TextField( + controller: _ctrOTA, + keyboardType: TextInputType.visiblePassword, + decoration: const InputDecoration( + prefixIcon: Icon(Icons.key), + labelText: 'OTA', + hintText: 'One-Time-Authorization token', + helperText: + 'This token might be required if the server is rate limited', + border: OutlineInputBorder(), + ), + ), + ) + ] + : []), + ], + ))), floatingActionButton: FloatingActionButton.extended( onPressed: () async { setState(() { @@ -227,34 +223,30 @@ class _AuthPageState extends State { } // hash password - var bytes = utf8.encode(_ctrPassword.text); - final password = sha256.convert(bytes).toString(); + final password = hashPassword(_ctrPassword.text); - doNetworkRequest( - scaffMgr, - needUser: false, - req: (_) { - if (widget.mode == Mode.signin) { - return postUnauthorized( + doNetworkRequest(scaffMgr, needUser: false, req: (_) { + if (widget.mode == Mode.signin) { + return postUnauthorized( target: server, path: 'signin', body: { 'name': _ctrUsername.text, 'server': server.tag, 'accountKey': password - }); - } else if (widget.mode == Mode.signup) { - return postUnauthorized( + }); + } else if (widget.mode == Mode.signup) { + return postUnauthorized( target: server, path: 'signup', body: { 'name': _ctrUsername.text, 'server': server.tag, 'accountKey': password - }); - } else { - // signup OTA - return postUnauthorized( + }); + } else { + // signup OTA + return postUnauthorized( target: server, path: 'signupOTA', body: { @@ -262,23 +254,20 @@ class _AuthPageState extends State { 'server': server.tag, 'accountKey': password, 'OTA': _ctrOTA.text - }); - } - }, - onOK: (body) async { - // authorize user - await User( - username: _ctrUsername.text, - password: password, - server: server) - .toDisk(); - }, - after: () { - setState(() { - showSpinner = false; - }); + }); } - ); + }, onOK: (body) async { + // authorize user + await User( + username: _ctrUsername.text, + password: password, + server: server) + .toDisk(); + }, after: () { + setState(() { + showSpinner = false; + }); + }); }, label: Text(modeName), icon: const Icon(Icons.check),