Moved hash function into seperate file.
NOTE: useful for changePassword, which requires the hash password functionality as well.
This commit is contained in:
parent
e3ca4fafa6
commit
569dda01fd
2 changed files with 118 additions and 121 deletions
8
lib/backend/crypto.dart
Normal file
8
lib/backend/crypto.dart
Normal 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;
|
||||
}
|
|
@ -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<AuthPage> {
|
|||
),
|
||||
),
|
||||
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<AuthPage> {
|
|||
}
|
||||
|
||||
// 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<AuthPage> {
|
|||
'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),
|
||||
|
|
Loading…
Reference in a new issue