Moved hash function into seperate file.

NOTE: useful for changePassword,
which requires the hash password functionality as well.
This commit is contained in:
Jakob Meier 2023-03-25 14:27:47 +01:00
parent e3ca4fafa6
commit 569dda01fd
No known key found for this signature in database
GPG key ID: 66BDC7E6A01A6152
2 changed files with 118 additions and 121 deletions

8
lib/backend/crypto.dart Normal file
View file

@ -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;
}

View file

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:outbag_app/backend/crypto.dart';
import 'package:outbag_app/backend/request.dart'; import 'package:outbag_app/backend/request.dart';
import 'package:outbag_app/backend/user.dart'; import 'package:outbag_app/backend/user.dart';
import 'package:outbag_app/tools/fetch_wrapper.dart'; import 'package:outbag_app/tools/fetch_wrapper.dart';
@ -69,106 +70,101 @@ class _AuthPageState extends State<AuthPage> {
), ),
), ),
body: Center( body: Center(
child: ConstrainedBox( child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400), constraints: const BoxConstraints(maxWidth: 400),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ 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)
? [
Padding( Padding(
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),
child: TextField( child: TextField(
controller: _ctrPasswordRpt, controller: _ctrServer,
keyboardType: keyboardType: TextInputType.url,
TextInputType.visiblePassword, 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, obscureText: true,
decoration: const InputDecoration( decoration: const InputDecoration(
prefixIcon: Icon(Icons.lock), prefixIcon: Icon(Icons.lock),
labelText: 'Repeat Password', labelText: 'Password',
hintText: 'Type your password again', hintText: 'Your password',
helperText: helperText:
'Make sure to type the correct password', 'Password have to be at least six characters long',
border: OutlineInputBorder(), border: OutlineInputBorder(),
), ),
), ),
) ),
] // ONLY SIGNUP
: []), ...((widget.mode != Mode.signin)
// ONLY SIGNUP OTA ? [
...((widget.mode == Mode.signupOTA) Padding(
? [ padding: const EdgeInsets.all(8),
Padding( child: TextField(
padding: const EdgeInsets.all(8), controller: _ctrPasswordRpt,
child: TextField( keyboardType: TextInputType.visiblePassword,
controller: _ctrOTA, obscureText: true,
keyboardType: decoration: const InputDecoration(
TextInputType.visiblePassword, prefixIcon: Icon(Icons.lock),
decoration: const InputDecoration( labelText: 'Repeat Password',
prefixIcon: Icon(Icons.key), hintText: 'Type your password again',
labelText: 'OTA', helperText:
hintText: 'Make sure to type the correct password',
'One-Time-Authorization token', border: OutlineInputBorder(),
helperText: ),
'This token might be required if the server is rate limited', ),
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( floatingActionButton: FloatingActionButton.extended(
onPressed: () async { onPressed: () async {
setState(() { setState(() {
@ -227,34 +223,30 @@ class _AuthPageState extends State<AuthPage> {
} }
// hash password // hash password
var bytes = utf8.encode(_ctrPassword.text); final password = hashPassword(_ctrPassword.text);
final password = sha256.convert(bytes).toString();
doNetworkRequest( doNetworkRequest(scaffMgr, needUser: false, req: (_) {
scaffMgr, if (widget.mode == Mode.signin) {
needUser: false, return postUnauthorized(
req: (_) {
if (widget.mode == Mode.signin) {
return postUnauthorized(
target: server, target: server,
path: 'signin', path: 'signin',
body: { body: {
'name': _ctrUsername.text, 'name': _ctrUsername.text,
'server': server.tag, 'server': server.tag,
'accountKey': password 'accountKey': password
}); });
} else if (widget.mode == Mode.signup) { } else if (widget.mode == Mode.signup) {
return postUnauthorized( return postUnauthorized(
target: server, target: server,
path: 'signup', path: 'signup',
body: { body: {
'name': _ctrUsername.text, 'name': _ctrUsername.text,
'server': server.tag, 'server': server.tag,
'accountKey': password 'accountKey': password
}); });
} else { } else {
// signup OTA // signup OTA
return postUnauthorized( return postUnauthorized(
target: server, target: server,
path: 'signupOTA', path: 'signupOTA',
body: { body: {
@ -262,23 +254,20 @@ class _AuthPageState extends State<AuthPage> {
'server': server.tag, 'server': server.tag,
'accountKey': password, 'accountKey': password,
'OTA': _ctrOTA.text '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), label: Text(modeName),
icon: const Icon(Icons.check), icon: const Icon(Icons.check),