-
Notifications
You must be signed in to change notification settings - Fork 19
/
pleroma_api_oauth_service_impl.dart
135 lines (115 loc) · 3.99 KB
/
pleroma_api_oauth_service_impl.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import 'dart:async';
import 'package:fedi/pleroma/api/oauth/pleroma_api_oauth_model.dart';
import 'package:fedi/pleroma/api/oauth/pleroma_api_oauth_service.dart';
import 'package:fedi/pleroma/api/pleroma_api_service.dart';
import 'package:fedi/pleroma/api/rest/pleroma_api_rest_model.dart';
import 'package:fedi/pleroma/api/rest/pleroma_api_rest_service.dart';
import 'package:fedi/rest/rest_request_model.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart';
import 'package:uni_links/uni_links.dart';
import 'package:url_launcher/url_launcher.dart';
var _logger = Logger('pleroma_api_oauth_service_impl.dart');
class PleromaApiOAuthService extends BasePleromaApiService
implements IPleromaApiOAuthService {
final oauthRelativeUrlPath = '/oauth/';
PleromaApiOAuthService({
required IPleromaApiRestService restService,
}) : super(
restService: restService,
);
@override
Future<String?> launchAuthorizeFormAndExtractAuthorizationCode({
required PleromaApiOAuthAuthorizeRequest authorizeRequest,
}) async {
_logger.finest(() => 'launchAuthorizeFormAndExtractAuthorizationCode');
var host = restService.baseUri;
var baseUrl = join(oauthRelativeUrlPath, 'authorize');
var keyValueMap = authorizeRequest.toJson();
var queryArgs = keyValueMap.entries
.map((entry) => '${entry.key}=${entry.value}')
.join('&');
queryArgs = queryArgs.replaceAll(' ', '%20');
var url = '$host/$baseUrl?$queryArgs';
var isCanLaunch = await canLaunch(url);
_logger.finest(() => 'launchAuthorizeFormAndExtractAuthorizationCode \n'
'\t url = $url\n'
'\t canLaunch=$isCanLaunch');
var completer = Completer<String>();
if (isCanLaunch) {
// ignore: avoid-late-keyword
late StreamSubscription<Uri?> subscription;
subscription = uriLinkStream.listen(
(Uri? uri) {
subscription.cancel();
closeWebView();
var code = extractAuthCodeFromUri(uri!);
completer.complete(code);
},
onError: (e) {
subscription.cancel();
closeWebView();
completer.completeError(e);
},
);
_logger.finest(() => 'launch url=$url');
await launch(url);
} else {
completer.completeError(PleromaApiOAuthCantLaunchException());
}
return completer.future;
}
String extractAuthCodeFromUri(Uri uri) =>
uri.queryParameters['code'].toString();
@override
Future<PleromaApiOAuthToken> retrieveAccountAccessToken({
required PleromaApiOAuthAccountTokenRequest tokenRequest,
}) async {
var queryArgs = tokenRequest
.toJson()
.entries
.map(
(entry) => RestRequestQueryArg(
key: entry.key,
value: entry.value,
),
)
.toList();
var request = RestRequest.post(
relativePath: join(oauthRelativeUrlPath, 'token'), queryArgs: queryArgs,
// bodyJson: tokenRequest.toJson()
);
var httpResponse = await restService.sendHttpRequest(request);
return restService.processJsonSingleResponse(
httpResponse,
PleromaApiOAuthToken.fromJson,
);
}
@override
Future<PleromaApiOAuthToken> retrieveAppAccessToken({
required PleromaApiOAuthAppTokenRequest tokenRequest,
}) async {
var request = RestRequest.post(
relativePath: join(oauthRelativeUrlPath, 'token'),
bodyJson: tokenRequest.toJson(),
);
var httpResponse = await restService.sendHttpRequest(request);
return restService.processJsonSingleResponse(
httpResponse,
PleromaApiOAuthToken.fromJson,
);
}
@override
Future revokeToken({
required PleromaApiOAuthAppTokenRevokeRequest revokeRequest,
}) async {
var request = RestRequest.post(
relativePath: join(oauthRelativeUrlPath, 'revoke'),
bodyJson: revokeRequest.toJson(),
);
var httpResponse = await restService.sendHttpRequest(request);
return restService.processEmptyResponse(
httpResponse,
);
}
}