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.redirectUri});
|
||||
factory App.fromJson(Map<String, dynamic> json) {
|
||||
return App(
|
||||
final app = App(
|
||||
id: json["id"].toString(),
|
||||
name: json["name"].toString(),
|
||||
website: json["website"].toString(),
|
||||
redirectUri: json["redirect_uri"].toString(),
|
||||
clientId: json["client_id"].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);
|
||||
await pollCode();
|
||||
server.close();
|
||||
await refreshToken();
|
||||
return 200;
|
||||
} catch (e) {
|
||||
return 400;
|
||||
|
@ -86,14 +89,16 @@ Future<http.Response> doOauthFlow() async {
|
|||
Future<http.Response> registerApp(String baseurl) async {
|
||||
//String url = baseurl Uri."api/v1/apps";
|
||||
Uri url = Uri.https(baseurl, "/api/v1/apps");
|
||||
final response = await http.post(url,
|
||||
headers: global.defaultHeaders,
|
||||
body: jsonEncode({
|
||||
'client_name': global.name,
|
||||
'redirect_uris': "http://localhost:1312",
|
||||
'scopes': "read write",
|
||||
'website': global.website
|
||||
}));
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: global.defaultHeaders,
|
||||
body: jsonEncode({
|
||||
'client_name': global.name,
|
||||
'redirect_uris': "http://localhost:1312",
|
||||
'scopes': "read write follow push",
|
||||
'website': global.website
|
||||
}),
|
||||
);
|
||||
return response;
|
||||
}
|
||||
|
||||
|
@ -109,8 +114,35 @@ void openBrowserForAuthCode(String baseurl, App app) {
|
|||
// ignore: prefer_interpolation_to_compose_strings
|
||||
query: "client_id=" +
|
||||
app.clientId +
|
||||
"&scope=read+write" +
|
||||
"&scope=read+write+follow+push" +
|
||||
"&redirect_uri=http://localhost:1312" +
|
||||
"&response_type=code");
|
||||
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:shared_preferences/shared_preferences.dart';
|
||||
import '../global.dart' as global;
|
||||
import '../business_logic/auth/oauth.dart' as oauth;
|
||||
|
||||
enum Settings {
|
||||
instanceUrl,
|
||||
|
@ -58,3 +59,44 @@ Future<Locale> loadLocale() async {
|
|||
}
|
||||
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 '../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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue