basic oauth finally but also more dependencies
This commit is contained in:
		
							parent
							
								
									4baacc751d
								
							
						
					
					
						commit
						f6ea45d0e2
					
				
					 5 changed files with 21 additions and 10 deletions
				
			
		|  | @ -30,10 +30,13 @@ class App { | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Future<http.Response> askKnownServerForAppRegistration() async { | Future<http.Response> doOauthFlow() async { | ||||||
|   String url = await settings.loadInstanceUrl(); |   String url = await settings.loadInstanceUrl(); | ||||||
|   try { |   try { | ||||||
|     return await registerApp(url); |     http.Response response = await registerApp(url); | ||||||
|  |     print(response.body); | ||||||
|  |     openBrowserForAuthCode(url, App.fromJson(jsonDecode(response.body))); | ||||||
|  |     return response; | ||||||
|   } catch (e) { |   } catch (e) { | ||||||
|     return http.Response(jsonEncode({}), 404); |     return http.Response(jsonEncode({}), 404); | ||||||
|   } |   } | ||||||
|  | @ -46,7 +49,7 @@ Future<http.Response> registerApp(String baseurl) async { | ||||||
|       headers: global.defaultHeaders, |       headers: global.defaultHeaders, | ||||||
|       body: jsonEncode({ |       body: jsonEncode({ | ||||||
|         'client_name': global.name, |         'client_name': global.name, | ||||||
|         'redirect_uris': "urn:ietf:wg:oauth:2.0:oob", |         'redirect_uris': "http://localhost:4040", | ||||||
|         'scopes': "read write", |         'scopes': "read write", | ||||||
|         'website': global.website |         'website': global.website | ||||||
|       })); |       })); | ||||||
|  | @ -54,14 +57,14 @@ Future<http.Response> registerApp(String baseurl) async { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void openBrowserForAuthCode(String baseurl, App app) { | void openBrowserForAuthCode(String baseurl, App app) { | ||||||
|   Uri url = Uri.https( |   Uri url = Uri( | ||||||
|       baseurl, |       scheme: "https", | ||||||
|  |       path: "$baseurl/oauth/authorize", | ||||||
|       // ignore: prefer_interpolation_to_compose_strings |       // ignore: prefer_interpolation_to_compose_strings | ||||||
|       "/oauth/authorize" |       query: "client_id=" + | ||||||
|               "?client_id=" + |  | ||||||
|           app.clientId + |           app.clientId + | ||||||
|           "&scope=read+write" + |           "&scope=read+write" + | ||||||
|           "&redirect_uri=urn:ietf:wg:oauth:2.0:oob" + |           "&redirect_uri=http://localhost:4040" + | ||||||
|           "&response_type=code"); |           "&response_type=code"); | ||||||
|   launchUrl(url); |   launchUrl(url); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,4 +1,5 @@ | ||||||
| import 'package:shared_preferences/shared_preferences.dart'; | import 'package:shared_preferences/shared_preferences.dart'; | ||||||
|  | import 'package:slothmu/business_logic/auth/oauth.dart'; | ||||||
| 
 | 
 | ||||||
| enum Settings { | enum Settings { | ||||||
|   instanceUrl, |   instanceUrl, | ||||||
|  |  | ||||||
|  | @ -125,13 +125,12 @@ class _AuthPageState extends State<AuthPage> { | ||||||
|                     icon: const Icon(Icons.arrow_back), |                     icon: const Icon(Icons.arrow_back), | ||||||
|                     label: Text("back-button".i18n())), |                     label: Text("back-button".i18n())), | ||||||
|                 FutureBuilder<http.Response>( |                 FutureBuilder<http.Response>( | ||||||
|                   future: oauth.askKnownServerForAppRegistration(), |                   future: oauth.doOauthFlow(), | ||||||
|                   builder: (context, snapshot) { |                   builder: (context, snapshot) { | ||||||
|                     if (snapshot.hasError) { |                     if (snapshot.hasError) { | ||||||
|                       return Text("login-failed-snackbar-text".i18n()); |                       return Text("login-failed-snackbar-text".i18n()); | ||||||
|                     } else if (snapshot.hasData) { |                     } else if (snapshot.hasData) { | ||||||
|                       if (snapshot.data?.statusCode != null) { |                       if (snapshot.data?.statusCode != null) { | ||||||
|                         print(snapshot.data?.body); |  | ||||||
|                         if (snapshot.data?.statusCode != 200) { |                         if (snapshot.data?.statusCode != 200) { | ||||||
|                           return Row( |                           return Row( | ||||||
|                             children: [ |                             children: [ | ||||||
|  |  | ||||||
|  | @ -273,6 +273,13 @@ packages: | ||||||
|       url: "https://pub.dartlang.org" |       url: "https://pub.dartlang.org" | ||||||
|     source: hosted |     source: hosted | ||||||
|     version: "2.1.1" |     version: "2.1.1" | ||||||
|  |   shelf: | ||||||
|  |     dependency: "direct main" | ||||||
|  |     description: | ||||||
|  |       name: shelf | ||||||
|  |       url: "https://pub.dartlang.org" | ||||||
|  |     source: hosted | ||||||
|  |     version: "1.3.1" | ||||||
|   sky_engine: |   sky_engine: | ||||||
|     dependency: transitive |     dependency: transitive | ||||||
|     description: flutter |     description: flutter | ||||||
|  |  | ||||||
|  | @ -42,6 +42,7 @@ dependencies: | ||||||
|   shared_preferences: ^2.0.15 |   shared_preferences: ^2.0.15 | ||||||
|   intl: ^0.17.0 |   intl: ^0.17.0 | ||||||
|   url_launcher: ^6.1.4 |   url_launcher: ^6.1.4 | ||||||
|  |   shelf: ^1.3.1 | ||||||
| 
 | 
 | ||||||
| dev_dependencies: | dev_dependencies: | ||||||
|   flutter_test: |   flutter_test: | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue