Rewrite http client

This commit is contained in:
LittleSheep 2024-05-02 12:16:01 +08:00
parent d968169e42
commit 3089e1f8d2
8 changed files with 121 additions and 64 deletions

View File

@ -3,13 +3,13 @@ import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:oauth2/oauth2.dart' as oauth2; import 'package:oauth2/oauth2.dart' as oauth2;
import 'package:solian/utils/http.dart';
import 'package:solian/utils/service_url.dart'; import 'package:solian/utils/service_url.dart';
class AuthProvider extends ChangeNotifier { class AuthProvider extends ChangeNotifier {
AuthProvider(); AuthProvider();
final deviceEndpoint = final deviceEndpoint = getRequestUri('passport', '/api/notifications/subscribe');
getRequestUri('passport', '/api/notifications/subscribe');
final tokenEndpoint = getRequestUri('passport', '/api/auth/token'); final tokenEndpoint = getRequestUri('passport', '/api/auth/token');
final userinfoEndpoint = getRequestUri('passport', '/api/users/me'); final userinfoEndpoint = getRequestUri('passport', '/api/users/me');
final redirectUrl = Uri.parse('solian://auth'); final redirectUrl = Uri.parse('solian://auth');
@ -21,19 +21,17 @@ class AuthProvider extends ChangeNotifier {
static const storageKey = 'identity'; static const storageKey = 'identity';
static const profileKey = 'profiles'; static const profileKey = 'profiles';
/// Before use this variable to make request HttpClient? client;
/// **MAKE SURE YOU HAVE CALL THE isAuthorized() METHOD**
oauth2.Client? client;
DateTime? lastRefreshedAt;
Future<bool> loadClient() async { Future<bool> loadClient() async {
if (await storage.containsKey(key: storageKey)) { if (await storage.containsKey(key: storageKey)) {
try { try {
final credentials = final credentials = oauth2.Credentials.fromJson((await storage.read(key: storageKey))!);
oauth2.Credentials.fromJson((await storage.read(key: storageKey))!); client = HttpClient(
client = oauth2.Client(credentials, defaultToken: credentials.accessToken,
identifier: clientId, secret: clientSecret); defaultRefreshToken: credentials.refreshToken,
onTokenRefreshed: setToken,
);
await fetchProfiles(); await fetchProfiles();
return true; return true;
} catch (e) { } catch (e) {
@ -45,13 +43,12 @@ class AuthProvider extends ChangeNotifier {
} }
} }
Future<oauth2.Client> createClient( Future<HttpClient> createClient(BuildContext context, String username, String password) async {
BuildContext context, String username, String password) async {
if (await loadClient()) { if (await loadClient()) {
return client!; return client!;
} }
return await oauth2.resourceOwnerPasswordGrant( final credentials = (await oauth2.resourceOwnerPasswordGrant(
tokenEndpoint, tokenEndpoint,
username, username,
password, password,
@ -59,6 +56,15 @@ class AuthProvider extends ChangeNotifier {
secret: clientSecret, secret: clientSecret,
scopes: ['openid'], scopes: ['openid'],
basicAuth: false, basicAuth: false,
))
.credentials;
setToken(credentials.accessToken, credentials.refreshToken!);
return HttpClient(
defaultToken: credentials.accessToken,
defaultRefreshToken: credentials.refreshToken,
onTokenRefreshed: setToken,
); );
} }
@ -70,21 +76,16 @@ class AuthProvider extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
Future<void> refreshToken() async { Future<void> setToken(String atk, String rtk) async {
if (client != null) { if (client != null) {
final credentials = await client!.credentials.refresh( final credentials = oauth2.Credentials(atk, refreshToken: rtk, idToken: atk, scopes: ['openid']);
identifier: clientId, secret: clientSecret, basicAuth: false);
client = oauth2.Client(credentials,
identifier: clientId, secret: clientSecret);
storage.write(key: storageKey, value: credentials.toJson()); storage.write(key: storageKey, value: credentials.toJson());
} }
notifyListeners(); notifyListeners();
} }
Future<void> signin( Future<void> signin(BuildContext context, String username, String password) async {
BuildContext context, String username, String password) async {
client = await createClient(context, username, password); client = await createClient(context, username, password);
storage.write(key: storageKey, value: client!.credentials.toJson());
await fetchProfiles(); await fetchProfiles();
} }
@ -96,21 +97,7 @@ class AuthProvider extends ChangeNotifier {
Future<bool> isAuthorized() async { Future<bool> isAuthorized() async {
const storage = FlutterSecureStorage(); const storage = FlutterSecureStorage();
if (await storage.containsKey(key: storageKey)) { return await storage.containsKey(key: storageKey);
if (client == null) {
await loadClient();
}
if (lastRefreshedAt == null ||
DateTime.now()
.subtract(const Duration(minutes: 3))
.isAfter(lastRefreshedAt!)) {
await refreshToken();
lastRefreshedAt = DateTime.now();
}
return true;
} else {
return false;
}
} }
Future<dynamic> getProfiles() async { Future<dynamic> getProfiles() async {

View File

@ -28,14 +28,14 @@ class ChatProvider extends ChangeNotifier {
if (auth.client == null) await auth.loadClient(); if (auth.client == null) await auth.loadClient();
if (!await auth.isAuthorized()) return null; if (!await auth.isAuthorized()) return null;
await auth.refreshToken(); await auth.client!.refreshToken(auth.client!.currentRefreshToken!);
var ori = getRequestUri('messaging', '/api/ws'); var ori = getRequestUri('messaging', '/api/ws');
var uri = Uri( var uri = Uri(
scheme: ori.scheme.replaceFirst('http', 'ws'), scheme: ori.scheme.replaceFirst('http', 'ws'),
host: ori.host, host: ori.host,
path: ori.path, path: ori.path,
queryParameters: {'tk': Uri.encodeComponent(auth.client!.credentials.accessToken)}, queryParameters: {'tk': Uri.encodeComponent(auth.client!.currentToken!)},
); );
final channel = WebSocketChannel.connect(uri); final channel = WebSocketChannel.connect(uri);

View File

@ -72,7 +72,7 @@ class NotifyProvider extends ChangeNotifier {
if (auth.client == null) await auth.loadClient(); if (auth.client == null) await auth.loadClient();
if (!await auth.isAuthorized()) return null; if (!await auth.isAuthorized()) return null;
await auth.refreshToken(); await auth.client!.refreshToken(auth.client!.currentRefreshToken!);
var ori = getRequestUri('passport', '/api/notifications/listen'); var ori = getRequestUri('passport', '/api/notifications/listen');
var uri = Uri( var uri = Uri(
@ -80,7 +80,7 @@ class NotifyProvider extends ChangeNotifier {
host: ori.host, host: ori.host,
path: ori.path, path: ori.path,
queryParameters: { queryParameters: {
'tk': Uri.encodeComponent(auth.client!.credentials.accessToken) 'tk': Uri.encodeComponent(auth.client!.currentToken!)
}, },
); );

View File

@ -73,28 +73,15 @@ class _ChatIndexScreenState extends State<ChatIndexScreen> {
), ),
], ],
) )
: ChatIndexScreenWidget( : const ChatIndexScreenWidget(),
onSelect: (item) async {
final result = await router.pushNamed(
'chat.channel',
pathParameters: {
'channel': item.alias,
},
);
switch (result) {
case 'refresh':
// fetchChannels();
}
},
),
); );
} }
} }
class ChatIndexScreenWidget extends StatefulWidget { class ChatIndexScreenWidget extends StatefulWidget {
final Function(Channel item) onSelect; final Function(Channel item)? onSelect;
const ChatIndexScreenWidget({super.key, required this.onSelect}); const ChatIndexScreenWidget({super.key, this.onSelect});
@override @override
State<ChatIndexScreenWidget> createState() => _ChatIndexScreenWidgetState(); State<ChatIndexScreenWidget> createState() => _ChatIndexScreenWidgetState();
@ -176,7 +163,23 @@ class _ChatIndexScreenWidgetState extends State<ChatIndexScreenWidget> {
), ),
title: Text(element.name), title: Text(element.name),
subtitle: Text(element.description), subtitle: Text(element.description),
onTap: () => widget.onSelect(element), onTap: () async {
if (widget.onSelect != null) {
widget.onSelect!(element);
return;
}
final result = await router.pushNamed(
'chat.channel',
pathParameters: {
'channel': element.alias,
},
);
switch (result) {
case 'refresh':
fetchChannels();
}
},
); );
}, },
), ),

View File

@ -175,9 +175,9 @@ class _CommentEditorScreenState extends State<CommentEditorScreen> {
), ),
widget.editing != null ? editingBanner : Container(), widget.editing != null ? editingBanner : Container(),
Container( Container(
decoration: const BoxDecoration( decoration: BoxDecoration(
border: Border( border: Border(
top: BorderSide(width: 0.3, color: Color(0xffdedede)), top: BorderSide(width: 0.3, color: Theme.of(context).dividerColor),
), ),
), ),
child: Row( child: Row(

View File

@ -165,9 +165,9 @@ class _MomentEditorScreenState extends State<MomentEditorScreen> {
), ),
widget.editing != null ? editingBanner : Container(), widget.editing != null ? editingBanner : Container(),
Container( Container(
decoration: const BoxDecoration( decoration: BoxDecoration(
border: Border( border: Border(
top: BorderSide(width: 0.3, color: Color(0xffdedede)), top: BorderSide(width: 0.3, color: Theme.of(context).dividerColor),
), ),
), ),
child: Row( child: Row(

67
lib/utils/http.dart Normal file
View File

@ -0,0 +1,67 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:solian/utils/service_url.dart';
class HttpClient extends http.BaseClient {
final bool isUnauthorizedRetry;
final Future<String> Function()? onUnauthorizedRetry;
final Function(String atk, String rtk)? onTokenRefreshed;
final _client = http.Client();
HttpClient({
this.isUnauthorizedRetry = true,
this.onUnauthorizedRetry,
this.onTokenRefreshed,
String? defaultToken,
String? defaultRefreshToken,
}) {
currentToken = defaultToken;
currentRefreshToken = defaultRefreshToken;
}
String? currentToken;
String? currentRefreshToken;
Future<String> refreshToken(String token) async {
final res = await _client.post(
getRequestUri('passport', '/api/auth/token'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'refresh_token': token, 'grant_type': 'refresh_token'}),
);
if (res.statusCode != 200) {
var message = utf8.decode(res.bodyBytes);
throw Exception('An error occurred when trying refresh token: $message');
}
final result = jsonDecode(utf8.decode(res.bodyBytes));
currentToken = result['access_token'];
currentRefreshToken = result['refresh_token'];
if (onTokenRefreshed != null) {
onTokenRefreshed!(currentToken!, currentRefreshToken!);
}
return currentToken!;
}
@override
Future<http.StreamedResponse> send(http.BaseRequest request) async {
request.headers['Authorization'] = 'Bearer $currentToken';
final res = await _client.send(request);
if (res.statusCode == 401 && isUnauthorizedRetry) {
if (onUnauthorizedRetry != null) {
currentToken = await onUnauthorizedRetry!();
} else if (currentRefreshToken != null) {
currentToken = await refreshToken(currentRefreshToken!);
} else {
final result = await http.Response.fromStream(res);
throw Exception(utf8.decode(result.bodyBytes));
}
request.headers['Authorization'] = 'Bearer $currentToken';
return await _client.send(request);
}
return res;
}
}

View File

@ -140,9 +140,9 @@ class _ChatMessageEditorState extends State<ChatMessageEditor> {
Container( Container(
height: 56, height: 56,
padding: const EdgeInsets.only(top: 4, bottom: 4, right: 8), padding: const EdgeInsets.only(top: 4, bottom: 4, right: 8),
decoration: const BoxDecoration( decoration: BoxDecoration(
border: Border( border: Border(
top: BorderSide(width: 0.3, color: Color(0xffdedede)), top: BorderSide(width: 0.3, color: Theme.of(context).dividerColor),
), ),
), ),
child: Row( child: Row(