basic oauth finally but also more dependencies
This commit is contained in:
		
							parent
							
								
									f6ea45d0e2
								
							
						
					
					
						commit
						bb8680e3f6
					
				
					 2 changed files with 38 additions and 9 deletions
				
			
		|  | @ -4,6 +4,8 @@ import 'package:http/http.dart' as http; | ||||||
| import 'package:url_launcher/url_launcher.dart'; | import 'package:url_launcher/url_launcher.dart'; | ||||||
| import '../../global.dart' as global; | import '../../global.dart' as global; | ||||||
| import '../../business_logic/settings.dart' as settings; | import '../../business_logic/settings.dart' as settings; | ||||||
|  | import 'package:shelf/shelf.dart'; | ||||||
|  | import 'package:shelf/shelf_io.dart' as shelf_io; | ||||||
| 
 | 
 | ||||||
| class App { | class App { | ||||||
|   late String clientSecret; |   late String clientSecret; | ||||||
|  | @ -30,6 +32,35 @@ class App { | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | Response readAuthcode(Request request) { | ||||||
|  |   Map<String, String> params = request.url.queryParameters; | ||||||
|  |   if (params.containsKey("code") && params["code"] != null) { | ||||||
|  |     String code = params["code"].toString(); | ||||||
|  |     print(code); | ||||||
|  |   } | ||||||
|  |   return Response(200, | ||||||
|  |       body: | ||||||
|  |           "<html><head><meta http-equiv='Refresh' content='0; URL=https://example.com/'></head></html>"); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // returns status code | ||||||
|  | Future<int> handleFullOauth() async { | ||||||
|  |   try { | ||||||
|  |     http.Response response = await doOauthFlow(); | ||||||
|  |     if (response.statusCode != 200) { | ||||||
|  |       return response.statusCode; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     var handler = const Pipeline().addHandler(readAuthcode); | ||||||
|  |     var server = await shelf_io.serve(handler, 'localhost', 1312); | ||||||
|  |     server.idleTimeout = const Duration(minutes: 5); | ||||||
|  |     server.autoCompress = true; | ||||||
|  |     return 200; | ||||||
|  |   } catch (e) { | ||||||
|  |     return 400; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| Future<http.Response> doOauthFlow() async { | Future<http.Response> doOauthFlow() async { | ||||||
|   String url = await settings.loadInstanceUrl(); |   String url = await settings.loadInstanceUrl(); | ||||||
|   try { |   try { | ||||||
|  | @ -49,7 +80,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': "http://localhost:4040", |         'redirect_uris': "http://localhost:1312", | ||||||
|         'scopes': "read write", |         'scopes': "read write", | ||||||
|         'website': global.website |         'website': global.website | ||||||
|       })); |       })); | ||||||
|  | @ -64,7 +95,7 @@ void openBrowserForAuthCode(String baseurl, App app) { | ||||||
|       query: "client_id=" + |       query: "client_id=" + | ||||||
|           app.clientId + |           app.clientId + | ||||||
|           "&scope=read+write" + |           "&scope=read+write" + | ||||||
|           "&redirect_uri=http://localhost:4040" + |           "&redirect_uri=http://localhost:1312" + | ||||||
|           "&response_type=code"); |           "&response_type=code"); | ||||||
|   launchUrl(url); |   launchUrl(url); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -124,18 +124,16 @@ 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<int>( | ||||||
|                   future: oauth.doOauthFlow(), |                   future: oauth.handleFullOauth(), | ||||||
|                   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 != null) { | ||||||
|                         if (snapshot.data?.statusCode != 200) { |                         if (snapshot.data != 200) { | ||||||
|                           return Row( |                           return Row( | ||||||
|                             children: [ |                             children: [Text("error ${snapshot.data}")], | ||||||
|                               Text("error ${snapshot.data!.statusCode}") |  | ||||||
|                             ], |  | ||||||
|                           ); |                           ); | ||||||
|                         } |                         } | ||||||
|                       } |                       } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue