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

View File

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

View File

@ -72,7 +72,7 @@ class NotifyProvider extends ChangeNotifier {
if (auth.client == null) await auth.loadClient();
if (!await auth.isAuthorized()) return null;
await auth.refreshToken();
await auth.client!.refreshToken(auth.client!.currentRefreshToken!);
var ori = getRequestUri('passport', '/api/notifications/listen');
var uri = Uri(
@ -80,7 +80,7 @@ class NotifyProvider extends ChangeNotifier {
host: ori.host,
path: ori.path,
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(
onSelect: (item) async {
final result = await router.pushNamed(
'chat.channel',
pathParameters: {
'channel': item.alias,
},
);
switch (result) {
case 'refresh':
// fetchChannels();
}
},
),
: const ChatIndexScreenWidget(),
);
}
}
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
State<ChatIndexScreenWidget> createState() => _ChatIndexScreenWidgetState();
@ -176,7 +163,23 @@ class _ChatIndexScreenWidgetState extends State<ChatIndexScreenWidget> {
),
title: Text(element.name),
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(),
Container(
decoration: const BoxDecoration(
decoration: BoxDecoration(
border: Border(
top: BorderSide(width: 0.3, color: Color(0xffdedede)),
top: BorderSide(width: 0.3, color: Theme.of(context).dividerColor),
),
),
child: Row(

View File

@ -165,9 +165,9 @@ class _MomentEditorScreenState extends State<MomentEditorScreen> {
),
widget.editing != null ? editingBanner : Container(),
Container(
decoration: const BoxDecoration(
decoration: BoxDecoration(
border: Border(
top: BorderSide(width: 0.3, color: Color(0xffdedede)),
top: BorderSide(width: 0.3, color: Theme.of(context).dividerColor),
),
),
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(
height: 56,
padding: const EdgeInsets.only(top: 4, bottom: 4, right: 8),
decoration: const BoxDecoration(
decoration: BoxDecoration(
border: Border(
top: BorderSide(width: 0.3, color: Color(0xffdedede)),
top: BorderSide(width: 0.3, color: Theme.of(context).dividerColor),
),
),
child: Row(