- @override
Handles the command input.
Override this method to perform actions for this command.
Return value is the value returned to the command line operation. Return 0 for success.
Source
@override
Future<int> handle() async {
if (clientID == null) {
displayError("Option --id required.");
return 1;
}
if (secret == null && redirectUri != null) {
displayError(
"A client that supports the authorization code flow must be a confidential client");
displayProgress(
"Using option --redirect-uri creates a client that supports the authorization code flow. Either provide --secret or remove --redirect-uri.");
return 1;
}
var dataModel = new ManagedDataModel.fromCurrentMirrorSystem();
ManagedContext.defaultContext = new ManagedContext(dataModel, persistentStore);
var credentials = AuthUtility.generateAPICredentialPair(clientID, secret, redirectURI: redirectUri)
..allowedScopes = allowedScopes?.map((s) => new AuthScope(s))?.toList();
var managedCredentials = new ManagedClient()
..id = credentials.id
..hashedSecret = credentials.hashedSecret
..salt = credentials.salt
..redirectURI = credentials.redirectURI
..allowedScope = credentials.allowedScopes?.map((s) => s.scopeString)?.join(" ");
var query = new Query<ManagedClient>()..values = managedCredentials;
try {
await query.insert();
displayInfo("Success", color: CLIColor.green);
displayProgress("Client with ID '$clientID' has been added.");
if (secret != null) {
displayProgress(
"The client secret has been hashed. You must store it elsewhere, as it cannot be retrieved.");
}
if (managedCredentials.allowedScope != null) {
displayProgress("Allowed scope: ${managedCredentials.allowedScope}");
}
return 0;
} on QueryException catch (e) {
displayError("Adding Client Failed");
if (e.event == QueryExceptionEvent.conflict) {
PostgreSQLException underlying = e.underlyingException;
if (underlying.constraintName == "_authclient_redirecturi_key") {
displayProgress(
"Redirect URI '$redirectUri' already exists for another client.");
} else {
displayProgress("Client ID '$clientID' already exists.");
}
return 1;
}
var underlying = e.underlyingException;
if (underlying is PostgreSQLException) {
if (underlying.code == PostgreSQLErrorCode.undefinedTable) {
displayProgress(
"No table to store OAuth 2.0 client exists. Have you run 'aqueduct db upgrade'?");
} else {
displayProgress("$e");
}
} else {
displayProgress("$e");
}
}
return 1;
}