Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
9.8 KiB

  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:http/http.dart' as http;
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import '/views/main/conversation_list.dart';
  6. import '/utils/encryption/rsa_key_helper.dart';
  7. import '/utils/encryption/aes_helper.dart';
  8. import '/utils/storage/encryption_keys.dart';
  9. class LoginResponse {
  10. final String status;
  11. final String message;
  12. final String asymmetricPublicKey;
  13. final String asymmetricPrivateKey;
  14. const LoginResponse({
  15. required this.status,
  16. required this.message,
  17. required this.asymmetricPublicKey,
  18. required this.asymmetricPrivateKey,
  19. });
  20. factory LoginResponse.fromJson(Map<String, dynamic> json) {
  21. return LoginResponse(
  22. status: json['status'],
  23. message: json['message'],
  24. asymmetricPublicKey: json['asymmetric_public_key'],
  25. asymmetricPrivateKey: json['asymmetric_private_key'],
  26. );
  27. }
  28. }
  29. Future<LoginResponse> login(context, String username, String password) async {
  30. final resp = await http.post(
  31. Uri.parse('http://192.168.1.5:8080/api/v1/login'),
  32. headers: <String, String>{
  33. 'Content-Type': 'application/json; charset=UTF-8',
  34. },
  35. body: jsonEncode(<String, String>{
  36. 'username': username,
  37. 'password': password,
  38. }),
  39. );
  40. LoginResponse response = LoginResponse.fromJson(jsonDecode(resp.body));
  41. if (resp.statusCode != 200) {
  42. throw Exception(response.message);
  43. }
  44. var rsaPrivPem = AesHelper.aesDecrypt(password, base64.decode(response.asymmetricPrivateKey));
  45. var rsaPriv = RsaKeyHelper.parsePrivateKeyFromPem(rsaPrivPem);
  46. setPrivateKey(rsaPriv);
  47. final preferences = await SharedPreferences.getInstance();
  48. preferences.setBool('islogin', true);
  49. return response;
  50. }
  51. class Login extends StatelessWidget {
  52. const Login({Key? key}) : super(key: key);
  53. static const String _title = 'Envelope';
  54. @override
  55. Widget build(BuildContext context) {
  56. return Scaffold(
  57. backgroundColor: Colors.cyan,
  58. appBar: AppBar(
  59. title: null,
  60. automaticallyImplyLeading: true,
  61. //`true` if you want Flutter to automatically add Back Button when needed,
  62. //or `false` if you want to force your own back button every where
  63. leading: IconButton(icon: const Icon(Icons.arrow_back),
  64. //onPressed:() => Navigator.pop(context, false),
  65. onPressed:() => {
  66. Navigator.pop(context)
  67. }
  68. )
  69. ),
  70. body: const SafeArea(
  71. child: LoginWidget(),
  72. ),
  73. );
  74. }
  75. }
  76. class LoginWidget extends StatefulWidget {
  77. const LoginWidget({Key? key}) : super(key: key);
  78. @override
  79. State<LoginWidget> createState() => _LoginWidgetState();
  80. }
  81. class _LoginWidgetState extends State<LoginWidget> {
  82. final _formKey = GlobalKey<FormState>();
  83. TextEditingController usernameController = TextEditingController();
  84. TextEditingController passwordController = TextEditingController();
  85. @override
  86. Widget build(BuildContext context) {
  87. const TextStyle _inputTextStyle = TextStyle(fontSize: 18, color: Colors.black);
  88. final ButtonStyle _buttonStyle = ElevatedButton.styleFrom(
  89. primary: Colors.white,
  90. onPrimary: Colors.cyan,
  91. minimumSize: const Size.fromHeight(50),
  92. padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
  93. textStyle: const TextStyle(
  94. fontSize: 20,
  95. fontWeight: FontWeight.bold,
  96. color: Colors.red,
  97. ),
  98. );
  99. return Center(
  100. child: Form(
  101. key: _formKey,
  102. child: Center(
  103. child: Padding(
  104. padding: const EdgeInsets.all(15),
  105. child: Column(
  106. mainAxisAlignment: MainAxisAlignment.center,
  107. crossAxisAlignment: CrossAxisAlignment.center,
  108. children: [
  109. const Text('Login', style: TextStyle(fontSize: 35, color: Colors.white),),
  110. const SizedBox(height: 30),
  111. TextFormField(
  112. controller: usernameController,
  113. decoration: const InputDecoration(
  114. hintText: 'Username',
  115. ),
  116. style: _inputTextStyle,
  117. // The validator receives the text that the user has entered.
  118. validator: (value) {
  119. if (value == null || value.isEmpty) {
  120. return 'Create a username';
  121. }
  122. return null;
  123. },
  124. ),
  125. const SizedBox(height: 5),
  126. TextFormField(
  127. controller: passwordController,
  128. obscureText: true,
  129. enableSuggestions: false,
  130. autocorrect: false,
  131. decoration: const InputDecoration(
  132. hintText: 'Password',
  133. ),
  134. style: _inputTextStyle,
  135. // The validator receives the text that the user has entered.
  136. validator: (value) {
  137. if (value == null || value.isEmpty) {
  138. return 'Enter a password';
  139. }
  140. return null;
  141. },
  142. ),
  143. const SizedBox(height: 5),
  144. ElevatedButton(
  145. style: _buttonStyle,
  146. onPressed: () {
  147. if (_formKey.currentState!.validate()) {
  148. ScaffoldMessenger.of(context).showSnackBar(
  149. const SnackBar(content: Text('Processing Data')),
  150. );
  151. login(
  152. context,
  153. usernameController.text,
  154. passwordController.text,
  155. ).then((value) {
  156. /*
  157. Navigator.of(context).popUntil((route) {
  158. print(route.isFirst);
  159. return route.isFirst;
  160. });
  161. */
  162. Navigator.pushNamedAndRemoveUntil(context, '/home', ModalRoute.withName('/home'));
  163. }).catchError((error) {
  164. print(error); // TODO: Show error on interface
  165. });
  166. }
  167. },
  168. child: const Text('Submit'),
  169. ),
  170. ],
  171. )
  172. )
  173. )
  174. )
  175. );
  176. }
  177. }