mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 18:57:05 +08:00
feat: enhance oauth flow with json accept header support
This commit is contained in:
@@ -10,6 +10,23 @@ http.Client createHttpClientWithNoSSL() {
|
|||||||
return IOClient(ioClient);
|
return IOClient(ioClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class _JsonAcceptClient extends http.BaseClient {
|
||||||
|
final http.Client _inner;
|
||||||
|
|
||||||
|
_JsonAcceptClient(this._inner);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<http.StreamedResponse> send(http.BaseRequest request) {
|
||||||
|
request.headers['Accept'] = 'application/json';
|
||||||
|
return _inner.send(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void close() {
|
||||||
|
_inner.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class HttpClientManager {
|
class HttpClientManager {
|
||||||
static final HttpClientManager _instance = HttpClientManager._internal();
|
static final HttpClientManager _instance = HttpClientManager._internal();
|
||||||
static const int _maxCancelledRequests = 100;
|
static const int _maxCancelledRequests = 100;
|
||||||
@@ -60,4 +77,17 @@ class HttpClientManager {
|
|||||||
bool hasActiveClient(String requestId) {
|
bool hasActiveClient(String requestId) {
|
||||||
return _clients.containsKey(requestId);
|
return _clients.containsKey(requestId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
http.Client createClientWithJsonAccept(
|
||||||
|
String requestId, {
|
||||||
|
bool noSSL = false,
|
||||||
|
}) {
|
||||||
|
final baseClient = (noSSL && !kIsWeb)
|
||||||
|
? createHttpClientWithNoSSL()
|
||||||
|
: http.Client();
|
||||||
|
|
||||||
|
final client = _JsonAcceptClient(baseClient);
|
||||||
|
_clients[requestId] = client;
|
||||||
|
return client;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';
|
|||||||
import 'package:oauth2/oauth2.dart' as oauth2;
|
import 'package:oauth2/oauth2.dart' as oauth2;
|
||||||
|
|
||||||
import '../../models/models.dart';
|
import '../../models/models.dart';
|
||||||
|
import '../../services/http_client_manager.dart';
|
||||||
|
|
||||||
Future<oauth2.Client> oAuth2AuthorizationCodeGrantHandler({
|
Future<oauth2.Client> oAuth2AuthorizationCodeGrantHandler({
|
||||||
required String identifier,
|
required String identifier,
|
||||||
@@ -36,11 +37,17 @@ Future<oauth2.Client> oAuth2AuthorizationCodeGrantHandler({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a unique request ID for this OAuth flow
|
||||||
|
final requestId = 'oauth2-${DateTime.now().millisecondsSinceEpoch}';
|
||||||
|
final httpClientManager = HttpClientManager();
|
||||||
|
final baseClient = httpClientManager.createClientWithJsonAccept(requestId);
|
||||||
|
|
||||||
final grant = oauth2.AuthorizationCodeGrant(
|
final grant = oauth2.AuthorizationCodeGrant(
|
||||||
identifier,
|
identifier,
|
||||||
authorizationEndpoint,
|
authorizationEndpoint,
|
||||||
tokenEndpoint,
|
tokenEndpoint,
|
||||||
secret: secret,
|
secret: secret,
|
||||||
|
httpClient: baseClient,
|
||||||
);
|
);
|
||||||
|
|
||||||
final authorizationUrl = grant.getAuthorizationUrl(
|
final authorizationUrl = grant.getAuthorizationUrl(
|
||||||
@@ -58,21 +65,26 @@ Future<oauth2.Client> oAuth2AuthorizationCodeGrantHandler({
|
|||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Use standard oauth2 package for other providers
|
||||||
final client = await grant.handleAuthorizationResponse(
|
final client = await grant.handleAuthorizationResponse(
|
||||||
Uri.parse(uri).queryParameters,
|
Uri.parse(uri).queryParameters,
|
||||||
);
|
);
|
||||||
|
|
||||||
log('OAuth2 authorization successful, saving credentials');
|
log('OAuth2 authorization successful, saving credentials');
|
||||||
|
|
||||||
await credentialsFile.writeAsString(client.credentials.toJson());
|
await credentialsFile.writeAsString(client.credentials.toJson());
|
||||||
log(client.credentials.toJson());
|
log(client.credentials.toJson());
|
||||||
|
|
||||||
|
// Clean up the HTTP client
|
||||||
|
httpClientManager.closeClient(requestId);
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log('Error handling authorization response: $e');
|
log('Error handling authorization response: $e');
|
||||||
|
|
||||||
log('URI query parameters: ${Uri.parse(uri).queryParameters}');
|
log('URI query parameters: ${Uri.parse(uri).queryParameters}');
|
||||||
|
|
||||||
|
// Clean up the HTTP client on error
|
||||||
|
httpClientManager.closeClient(requestId);
|
||||||
|
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,6 +114,12 @@ Future<oauth2.Client> oAuth2ClientCredentialsGrantHandler({
|
|||||||
log("Creating Client with id: ${oauth2Model.clientId}");
|
log("Creating Client with id: ${oauth2Model.clientId}");
|
||||||
log("Creating Client with sec: ${oauth2Model.clientSecret}");
|
log("Creating Client with sec: ${oauth2Model.clientSecret}");
|
||||||
|
|
||||||
|
// Create a unique request ID for this OAuth flow
|
||||||
|
final requestId = 'oauth2-client-${DateTime.now().millisecondsSinceEpoch}';
|
||||||
|
final httpClientManager = HttpClientManager();
|
||||||
|
final baseClient = httpClientManager.createClientWithJsonAccept(requestId);
|
||||||
|
|
||||||
|
try {
|
||||||
// Otherwise, perform the client credentials grant
|
// Otherwise, perform the client credentials grant
|
||||||
final client = await oauth2.clientCredentialsGrant(
|
final client = await oauth2.clientCredentialsGrant(
|
||||||
Uri.parse(oauth2Model.accessTokenUrl),
|
Uri.parse(oauth2Model.accessTokenUrl),
|
||||||
@@ -109,6 +127,7 @@ Future<oauth2.Client> oAuth2ClientCredentialsGrantHandler({
|
|||||||
oauth2Model.clientSecret,
|
oauth2Model.clientSecret,
|
||||||
scopes: oauth2Model.scope != null ? [oauth2Model.scope!] : null,
|
scopes: oauth2Model.scope != null ? [oauth2Model.scope!] : null,
|
||||||
basicAuth: false,
|
basicAuth: false,
|
||||||
|
httpClient: baseClient,
|
||||||
);
|
);
|
||||||
log("Created Client with id: ${client.identifier}");
|
log("Created Client with id: ${client.identifier}");
|
||||||
log("Created Client with sec: ${client.secret}");
|
log("Created Client with sec: ${client.secret}");
|
||||||
@@ -123,7 +142,15 @@ Future<oauth2.Client> oAuth2ClientCredentialsGrantHandler({
|
|||||||
log('Failed to save credentials: $e');
|
log('Failed to save credentials: $e');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean up the HTTP client
|
||||||
|
httpClientManager.closeClient(requestId);
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
|
} catch (e) {
|
||||||
|
// Clean up the HTTP client on error
|
||||||
|
httpClientManager.closeClient(requestId);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<oauth2.Client> oAuth2ResourceOwnerPasswordGrantHandler({
|
Future<oauth2.Client> oAuth2ResourceOwnerPasswordGrantHandler({
|
||||||
@@ -155,6 +182,12 @@ Future<oauth2.Client> oAuth2ResourceOwnerPasswordGrantHandler({
|
|||||||
log("Creating Client with id: ${oauth2Model.clientId}");
|
log("Creating Client with id: ${oauth2Model.clientId}");
|
||||||
log("Creating Client with sec: ${oauth2Model.clientSecret}");
|
log("Creating Client with sec: ${oauth2Model.clientSecret}");
|
||||||
|
|
||||||
|
// Create a unique request ID for this OAuth flow
|
||||||
|
final requestId = 'oauth2-password-${DateTime.now().millisecondsSinceEpoch}';
|
||||||
|
final httpClientManager = HttpClientManager();
|
||||||
|
final baseClient = httpClientManager.createClientWithJsonAccept(requestId);
|
||||||
|
|
||||||
|
try {
|
||||||
// Otherwise, perform the owner password grant
|
// Otherwise, perform the owner password grant
|
||||||
final client = await oauth2.resourceOwnerPasswordGrant(
|
final client = await oauth2.resourceOwnerPasswordGrant(
|
||||||
Uri.parse(oauth2Model.accessTokenUrl),
|
Uri.parse(oauth2Model.accessTokenUrl),
|
||||||
@@ -164,6 +197,7 @@ Future<oauth2.Client> oAuth2ResourceOwnerPasswordGrantHandler({
|
|||||||
secret: oauth2Model.clientSecret,
|
secret: oauth2Model.clientSecret,
|
||||||
scopes: oauth2Model.scope != null ? [oauth2Model.scope!] : null,
|
scopes: oauth2Model.scope != null ? [oauth2Model.scope!] : null,
|
||||||
basicAuth: false,
|
basicAuth: false,
|
||||||
|
httpClient: baseClient,
|
||||||
);
|
);
|
||||||
log("Created Client with id: ${client.identifier}");
|
log("Created Client with id: ${client.identifier}");
|
||||||
log("Created Client with sec: ${client.secret}");
|
log("Created Client with sec: ${client.secret}");
|
||||||
@@ -178,5 +212,13 @@ Future<oauth2.Client> oAuth2ResourceOwnerPasswordGrantHandler({
|
|||||||
log('Failed to save credentials: $e');
|
log('Failed to save credentials: $e');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean up the HTTP client
|
||||||
|
httpClientManager.closeClient(requestId);
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
|
} catch (e) {
|
||||||
|
// Clean up the HTTP client on error
|
||||||
|
httpClientManager.closeClient(requestId);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user