From 325890374596543b1e93ba94523e6143418d0c07 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 25 Jun 2019 00:39:20 +1000 Subject: [PATCH] Add API class - Secure token based on username/password --- lib/api.dart | 72 +++++++++++++++++++++++++++++++++++++++++++++++ lib/main.dart | 12 ++++++-- lib/settings.dart | 36 ++++++++++++++++++++++++ pubspec.lock | 14 +++++++++ pubspec.yaml | 1 + 5 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 lib/api.dart create mode 100644 lib/settings.dart diff --git a/lib/api.dart b/lib/api.dart new file mode 100644 index 00000000..76237405 --- /dev/null +++ b/lib/api.dart @@ -0,0 +1,72 @@ +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:path/path.dart' as path; +import 'package:http/http.dart' as http; + + +class InventreeAPI { + + InventreeAPI(this._username, this._password) { + _secureToken(); + } + + // Base URL for InvenTree API + String _base_url = "http://10.0.0.7:8000/api/"; + + String _username = ""; + String _password = ""; + + // Authentication token (initially empty, must be requested) + String _token = ""; + + // Construct an API URL + String _makeUrl(String url) { + return path.join(_base_url, url); + } + + bool _hasToken() { + return _token.isNotEmpty; + } + + // Request an API token from the server. + // A valid username/password combination must be provided + void _secureToken() async { + _token = ""; + + var _url = _makeUrl("user/token/"); + final response = await http.post(_url, + body: { + "username": _username, + "password": _password + }); + + if (response.statusCode != 200) { + print("Invalid status code:" + String.fromCharCode(response.statusCode)); + } else { + var _json = json.decode(response.body); + + if (_json["token"] != null) { + _token = _json["token"]; + print("Received token: " + _token); + } + } + } + + Future get(String url) async { + var _url = _makeUrl(url); + final response = await http.get(_url, + headers: { + HttpHeaders.authorizationHeader: "Token: " + _token + } + ); + + print("Making request to " + _url); + print(response.statusCode); + print(response.body); + + return response; + } + +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 9ebe5a66..a266a119 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,8 @@ import 'package:flutter/material.dart'; +import 'settings.dart'; +import 'api.dart'; + void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @@ -57,6 +60,10 @@ class _MyHomePageState extends State { }); } + void _login() { + Navigator.push(context, MaterialPageRoute(builder: (context) => InvenTreeSettingsWidget())); + } + @override Widget build(BuildContext context) { // This method is rerun every time setState is called, for instance as done @@ -89,8 +96,9 @@ class _MyHomePageState extends State { ), new Divider(), new ListTile( - title: new Text("Log In"), - leading: new Icon(Icons.security), + title: new Text("Settings"), + leading: new Icon(Icons.settings), + onTap: _login, ), ], ) diff --git a/lib/settings.dart b/lib/settings.dart new file mode 100644 index 00000000..38def43c --- /dev/null +++ b/lib/settings.dart @@ -0,0 +1,36 @@ +import 'package:flutter/material.dart'; + +class InvenTreeSettingsWidget extends StatefulWidget { + // InvenTree settings view + + @override + _InvenTreeSettingsState createState() => _InvenTreeSettingsState(); + +} + + +class _InvenTreeSettingsState extends State { + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text("InvenTree Settings"), + ), + body: Center( + child: ListView( + children: [ + ListTile( + title: Text("Server Settings"), + subtitle: Text("Configure server and login settings"), + onTap: _editServerSettings, + ), + ], + ) + ) + ); + } + + void _editServerSettings() { + + } +} \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 87aef9c7..593bf706 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -81,6 +81,20 @@ packages: description: flutter source: sdk version: "0.0.0" + http: + dependency: "direct main" + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.0+2" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.3" image: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index c15fabbe..d0791b30 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -23,6 +23,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.2 + http: ^0.12.0+2 dev_dependencies: flutter_test: