fix oauth
This commit is contained in:
		
							parent
							
								
									30de8189bc
								
							
						
					
					
						commit
						532788e9e6
					
				
					 6 changed files with 110 additions and 17 deletions
				
			
		|  | @ -22,13 +22,15 @@ class App { | ||||||
|       required this.website, |       required this.website, | ||||||
|       required this.redirectUri}); |       required this.redirectUri}); | ||||||
|   factory App.fromJson(Map<String, dynamic> json) { |   factory App.fromJson(Map<String, dynamic> json) { | ||||||
|     return App( |     final app = App( | ||||||
|         id: json["id"].toString(), |         id: json["id"].toString(), | ||||||
|         name: json["name"].toString(), |         name: json["name"].toString(), | ||||||
|         website: json["website"].toString(), |         website: json["website"].toString(), | ||||||
|         redirectUri: json["redirect_uri"].toString(), |         redirectUri: json["redirect_uri"].toString(), | ||||||
|         clientId: json["client_id"].toString(), |         clientId: json["client_id"].toString(), | ||||||
|         clientSecret: json["client_secret"].toString()); |         clientSecret: json["client_secret"].toString()); | ||||||
|  |     settings.saveApp(app); | ||||||
|  |     return app; | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -57,6 +59,7 @@ Future<int> handleFullOauth() async { | ||||||
|     var server = await shelf_io.serve(handler, 'localhost', 1312); |     var server = await shelf_io.serve(handler, 'localhost', 1312); | ||||||
|     await pollCode(); |     await pollCode(); | ||||||
|     server.close(); |     server.close(); | ||||||
|  |     await refreshToken(); | ||||||
|     return 200; |     return 200; | ||||||
|   } catch (e) { |   } catch (e) { | ||||||
|     return 400; |     return 400; | ||||||
|  | @ -86,14 +89,16 @@ Future<http.Response> doOauthFlow() async { | ||||||
| Future<http.Response> registerApp(String baseurl) async { | Future<http.Response> registerApp(String baseurl) async { | ||||||
|   //String url = baseurl Uri."api/v1/apps"; |   //String url = baseurl Uri."api/v1/apps"; | ||||||
|   Uri url = Uri.https(baseurl, "/api/v1/apps"); |   Uri url = Uri.https(baseurl, "/api/v1/apps"); | ||||||
|   final response = await http.post(url, |   final response = await http.post( | ||||||
|       headers: global.defaultHeaders, |     url, | ||||||
|       body: jsonEncode({ |     headers: global.defaultHeaders, | ||||||
|         'client_name': global.name, |     body: jsonEncode({ | ||||||
|         'redirect_uris': "http://localhost:1312", |       'client_name': global.name, | ||||||
|         'scopes': "read write", |       'redirect_uris': "http://localhost:1312", | ||||||
|         'website': global.website |       'scopes': "read write follow push", | ||||||
|       })); |       'website': global.website | ||||||
|  |     }), | ||||||
|  |   ); | ||||||
|   return response; |   return response; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -109,8 +114,35 @@ void openBrowserForAuthCode(String baseurl, App app) { | ||||||
|       // ignore: prefer_interpolation_to_compose_strings |       // ignore: prefer_interpolation_to_compose_strings | ||||||
|       query: "client_id=" + |       query: "client_id=" + | ||||||
|           app.clientId + |           app.clientId + | ||||||
|           "&scope=read+write" + |           "&scope=read+write+follow+push" + | ||||||
|           "&redirect_uri=http://localhost:1312" + |           "&redirect_uri=http://localhost:1312" + | ||||||
|           "&response_type=code"); |           "&response_type=code"); | ||||||
|   launchUrl(url); |   launchUrl(url); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | Future<int> refreshToken() async { | ||||||
|  |   final authCode = await settings.loadAuthCode(); | ||||||
|  |   final appId = await settings.loadClientId(); | ||||||
|  |   final clientSecret = await settings.loadClientSecret(); | ||||||
|  |   final baseurl = await settings.loadInstanceUrl(); | ||||||
|  | 
 | ||||||
|  |   Uri url = Uri.https(baseurl, "/oauth/token"); | ||||||
|  |   final response = await http.post( | ||||||
|  |     url, | ||||||
|  |     headers: global.defaultHeaders, | ||||||
|  |     body: jsonEncode({ | ||||||
|  |       'grant_type': "authorization_code", | ||||||
|  |       'client_id': appId, | ||||||
|  |       'client_secret': clientSecret, | ||||||
|  |       'redirect_uri': "http://localhost:1312", | ||||||
|  |       'scope': "read write follow push", | ||||||
|  |       'code': authCode, | ||||||
|  |     }), | ||||||
|  |   ); | ||||||
|  |   if (response.statusCode == 200) { | ||||||
|  |     final dec = jsonDecode(response.body); | ||||||
|  |     final accessToken = dec["access_token"]!; | ||||||
|  |     await settings.saveToken(accessToken); | ||||||
|  |   } | ||||||
|  |   return response.statusCode; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -2,6 +2,7 @@ import 'package:flutter/painting.dart'; | ||||||
| import 'package:intl/intl.dart'; | import 'package:intl/intl.dart'; | ||||||
| import 'package:shared_preferences/shared_preferences.dart'; | import 'package:shared_preferences/shared_preferences.dart'; | ||||||
| import '../global.dart' as global; | import '../global.dart' as global; | ||||||
|  | import '../business_logic/auth/oauth.dart' as oauth; | ||||||
| 
 | 
 | ||||||
| enum Settings { | enum Settings { | ||||||
|   instanceUrl, |   instanceUrl, | ||||||
|  | @ -58,3 +59,44 @@ Future<Locale> loadLocale() async { | ||||||
|   } |   } | ||||||
|   return Locale(locale); |   return Locale(locale); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | Future<void> saveApp(oauth.App app) async { | ||||||
|  |   final prefs = await SharedPreferences.getInstance(); | ||||||
|  |   prefs.setString("client-secret", app.clientSecret); | ||||||
|  |   prefs.setString("client-id", app.clientId); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | Future<String> loadClientSecret() async { | ||||||
|  |   final prefs = await SharedPreferences.getInstance(); | ||||||
|  |   final secret = prefs.getString("client-secret"); | ||||||
|  |   if (secret == null) { | ||||||
|  |     return ""; | ||||||
|  |   } else { | ||||||
|  |     return secret; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | Future<String> loadClientId() async { | ||||||
|  |   final prefs = await SharedPreferences.getInstance(); | ||||||
|  |   final id = prefs.getString("client-id"); | ||||||
|  |   if (id == null) { | ||||||
|  |     return ""; | ||||||
|  |   } else { | ||||||
|  |     return id; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | Future<bool> saveToken(String token) async { | ||||||
|  |   final prefs = await SharedPreferences.getInstance(); | ||||||
|  |   return await prefs.setString("access-token", token); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | Future<String> loadToken() async { | ||||||
|  |   final prefs = await SharedPreferences.getInstance(); | ||||||
|  |   final id = prefs.getString("access-token"); | ||||||
|  |   if (id == null) { | ||||||
|  |     return ""; | ||||||
|  |   } else { | ||||||
|  |     return id; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -1,7 +1,20 @@ | ||||||
| import 'package:http/http.dart' as http; | import 'package:http/http.dart' as http; | ||||||
|  | import '../settings.dart' as settings; | ||||||
|  | import '../../global.dart' as global; | ||||||
| 
 | 
 | ||||||
| class Thread {} | Future<http.Response> getTimelineFromServer() async { | ||||||
|  |   final token = await settings.loadToken(); | ||||||
|  |   final baseUrl = await settings.loadInstanceUrl(); | ||||||
|  |   final url = Uri( | ||||||
|  |     scheme: "https", | ||||||
|  |     host: baseUrl, | ||||||
|  |     path: "/api/v1/timelines/home", | ||||||
|  |   ); | ||||||
| 
 | 
 | ||||||
| class Post {} |   Map<String, String> headers = {"Authorization": "Bearer $token"}; | ||||||
|  |   headers.addAll(global.defaultHeaders); | ||||||
| 
 | 
 | ||||||
| class Timeline {} |   final response = await http.get(url, headers: headers); | ||||||
|  | 
 | ||||||
|  |   return response; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| import 'package:flutter/painting.dart'; | import 'package:flutter/painting.dart'; | ||||||
| 
 | 
 | ||||||
| const String name = "loris"; | const String name = "loris"; | ||||||
| const String version = "v0.1 'not even alpha'"; | const String version = "v0.1 'is this thing on'"; | ||||||
| const String useragent = "$name/$version"; | const String useragent = "$name/$version"; | ||||||
| const String website = "https://git.kittycat.homes/zoe/slothmu"; | const String website = "https://git.kittycat.homes/zoe/slothmu"; | ||||||
| const Map<String, String> defaultHeaders = { | const Map<String, String> defaultHeaders = { | ||||||
|  |  | ||||||
|  | @ -7,19 +7,23 @@ import 'business_logic/settings.dart' as settings; | ||||||
| import 'package:flutter_localizations/flutter_localizations.dart'; | import 'package:flutter_localizations/flutter_localizations.dart'; | ||||||
| import 'themes/themes.dart' as themes; | import 'themes/themes.dart' as themes; | ||||||
| import 'global.dart' as global; | import 'global.dart' as global; | ||||||
|  | import 'business_logic/auth/oauth.dart' as oauth; | ||||||
| 
 | 
 | ||||||
| String _initRoute = "/"; | String _initRoute = "/"; | ||||||
| ThemeData theme = themes.getTheme(themes.available[0]); | ThemeData theme = themes.getTheme(themes.available[0]); | ||||||
| Locale activeLocale = const Locale("en"); | Locale activeLocale = const Locale("en"); | ||||||
| 
 | 
 | ||||||
| void main() async { | void main() async { | ||||||
|  |   settings.saveAuthCode(""); | ||||||
|   Intl.defaultLocale = "en"; |   Intl.defaultLocale = "en"; | ||||||
|   await settings.saveLocale("en"); |   await settings.saveLocale("en"); | ||||||
|   activeLocale = await settings.loadLocale(); |   activeLocale = await settings.loadLocale(); | ||||||
| 
 | 
 | ||||||
|   // check if all information is available |   // check if all information is available | ||||||
|   if (await settings.loadAuthCode() == "") { |   if (await settings.loadAuthCode() == "") { | ||||||
|     _initRoute = "/"; |     _initRoute = "/login"; | ||||||
|  |   } else { | ||||||
|  |     await oauth.refreshToken(); | ||||||
|   } |   } | ||||||
|   runApp(const Slothmu()); |   runApp(const Slothmu()); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||||
| import 'package:localization/localization.dart'; | import 'package:localization/localization.dart'; | ||||||
| import '../../business_logic/settings.dart' as settings; |  | ||||||
| import 'package:slothmu/partials/thread.dart'; | import 'package:slothmu/partials/thread.dart'; | ||||||
|  | import '../../business_logic/timeline/timeline.dart' as tl; | ||||||
| 
 | 
 | ||||||
| class Timeline extends StatefulWidget { | class Timeline extends StatefulWidget { | ||||||
|   const Timeline({Key? key}) : super(key: key); |   const Timeline({Key? key}) : super(key: key); | ||||||
|  | @ -29,7 +29,9 @@ class _TimelineState extends State<Timeline> { | ||||||
| 
 | 
 | ||||||
|   Future fetchMore() async { |   Future fetchMore() async { | ||||||
|     loading = true; |     loading = true; | ||||||
|     final token = await settings.loadAuthCode(); |     final response = await tl.getTimelineFromServer(); | ||||||
|  |     print(response.body); | ||||||
|  |     print(response.statusCode); | ||||||
|     setState(() { |     setState(() { | ||||||
|       if (children.isNotEmpty) { |       if (children.isNotEmpty) { | ||||||
|         children.removeAt(children.length - 1); |         children.removeAt(children.length - 1); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue