mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 10:49:49 +08:00
feat: implement client credentials grant handling
This commit is contained in:
@@ -202,8 +202,21 @@ Future<HttpRequestModel> handleAuth(
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case OAuth2GrantType.clientCredentials:
|
case OAuth2GrantType.clientCredentials:
|
||||||
// TODO: Handle this case.
|
final client = await oAuth2ClientCredentialsHandler(
|
||||||
throw UnimplementedError();
|
oauth2Model: oauth2,
|
||||||
|
credentialsFile: credentialsFile,
|
||||||
|
);
|
||||||
|
debugPrint(client.credentials.accessToken);
|
||||||
|
|
||||||
|
// Add the access token to the request headers
|
||||||
|
updatedHeaders.add(
|
||||||
|
NameValueModel(
|
||||||
|
name: 'Authorization',
|
||||||
|
value: 'Bearer ${client.credentials.accessToken}',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
updatedHeaderEnabledList.add(true);
|
||||||
|
break;
|
||||||
case OAuth2GrantType.resourceOwnerPassword:
|
case OAuth2GrantType.resourceOwnerPassword:
|
||||||
// TODO: Handle this case.
|
// TODO: Handle this case.
|
||||||
throw UnimplementedError();
|
throw UnimplementedError();
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import 'dart:io';
|
|||||||
import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';
|
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';
|
||||||
|
|
||||||
Future<oauth2.Client> oAuth2AuthorizationCodeGrantHandler({
|
Future<oauth2.Client> oAuth2AuthorizationCodeGrantHandler({
|
||||||
required String identifier,
|
required String identifier,
|
||||||
required String secret,
|
required String secret,
|
||||||
@@ -73,3 +75,53 @@ Future<oauth2.Client> oAuth2AuthorizationCodeGrantHandler({
|
|||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<oauth2.Client> oAuth2ClientCredentialsHandler({
|
||||||
|
required AuthOAuth2Model oauth2Model,
|
||||||
|
required File credentialsFile,
|
||||||
|
}) async {
|
||||||
|
// Try to use saved credentials
|
||||||
|
if (await credentialsFile.exists()) {
|
||||||
|
try {
|
||||||
|
final json = await credentialsFile.readAsString();
|
||||||
|
final credentials = oauth2.Credentials.fromJson(json);
|
||||||
|
|
||||||
|
if (credentials.accessToken.isNotEmpty && !credentials.isExpired) {
|
||||||
|
log('Using existing valid credentials');
|
||||||
|
// TODO: This adds the client_id parameter to the body instead of the header
|
||||||
|
return oauth2.clientCredentialsGrant(
|
||||||
|
Uri.parse(oauth2Model.authorizationUrl),
|
||||||
|
oauth2Model.clientId,
|
||||||
|
oauth2Model.clientSecret,
|
||||||
|
scopes: oauth2Model.scope != null ? [oauth2Model.scope!] : null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log('Error reading existing credentials: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log("Creating Client with id: ${oauth2Model.clientId}");
|
||||||
|
log("Creating Client with sec: ${oauth2Model.clientSecret}");
|
||||||
|
|
||||||
|
// Otherwise, perform the client credentials grant
|
||||||
|
final client = await oauth2.clientCredentialsGrant(
|
||||||
|
Uri.parse(oauth2Model.authorizationUrl),
|
||||||
|
oauth2Model.clientId,
|
||||||
|
oauth2Model.clientSecret,
|
||||||
|
scopes: oauth2Model.scope != null ? [oauth2Model.scope!] : null,
|
||||||
|
);
|
||||||
|
log("Created Client with id: ${client.identifier}");
|
||||||
|
log("Created Client with sec: ${client.secret}");
|
||||||
|
log("Created Client with sec: ${client.credentials.toJson()}");
|
||||||
|
|
||||||
|
log('Successfully authenticated via client credentials grant');
|
||||||
|
|
||||||
|
try {
|
||||||
|
await credentialsFile.writeAsString(client.credentials.toJson());
|
||||||
|
log('Saved credentials to file');
|
||||||
|
} catch (e) {
|
||||||
|
log('Failed to save credentials: $e');
|
||||||
|
}
|
||||||
|
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,5 +5,3 @@ export 'http_response_utils.dart';
|
|||||||
export 'string_utils.dart' hide RandomStringGenerator;
|
export 'string_utils.dart' hide RandomStringGenerator;
|
||||||
export 'uri_utils.dart';
|
export 'uri_utils.dart';
|
||||||
export 'auth/handle_auth.dart';
|
export 'auth/handle_auth.dart';
|
||||||
export 'auth/oauth2_webview_utils.dart';
|
|
||||||
export 'auth/handle_auth_webview.dart';
|
|
||||||
|
|||||||
Reference in New Issue
Block a user