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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue