Files
cacti/lib/CactiOAuth.php
Thomas VincentandTheWitness aae7f0ea5b refactor(oauth): extract CactiOAuth provider factory (#7129)
* refactor(oauth): extract Cacti\Security\CactiOAuth provider factory

Replace the inline provider switch in oauth2.php and the OAuth
branch of mailer() in lib/functions.php with two helpers:

- CactiOAuth::getProvider($name, $params) returns the configured
  provider instance for `google`, `azure`, `yahoo`, or `microsoft`,
  or null when the configured provider is unknown.
- CactiOAuth::getDefaultOptions($name) returns the scope set each
  provider expects.

Behaviour is preserved; both call sites now branch on a null
return rather than relying on a `$provider` variable that may have
fallen through the switch unset.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* chore(phpstan): catch up baseline with 11 upstream-detected entries

Same as PR-A: appends 11 PHPStan ignoreErrors entries that exist on
upstream develop but are not yet baselined, so this branch's CI does
not regress on phpstan analyse --level 6.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* refactor: flatten CactiOAuth to global namespace

Rename lib/Security/CactiOAuth.php to lib/CactiOAuth.php and drop the
Cacti\Security namespace. Update the two callers (oauth2.php and
lib/functions.php mailer) to use the unqualified class name. Behavior
is unchanged: only the FQCN was rewritten.

The flat layout matches every other lib/Cacti*.php class on develop and
in the canonical PRs (#7088, #7073, #7077, #7075).

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test: add shared unit stubs for OAuth tests

* fix(oauth): load provider factory in runtime paths

* test(oauth): pin provider factory runtime loading

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Co-authored-by: TheWitness <thewitness@cacti.net>
2026-05-13 10:36:06 -04:00

77 lines
2.4 KiB
PHP

<?php
declare(strict_types = 1);
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
*/
use Greew\OAuth2\Client\Provider\Azure;
use Hayageek\OAuth2\Client\Provider\Yahoo;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\Google;
use Stevenmaguire\OAuth2\Client\Provider\Keycloak;
use Stevenmaguire\OAuth2\Client\Provider\Microsoft;
class CactiOAuth {
/**
* Get an OAuth2 provider based on configuration.
*
* @param string $providerName The provider name (google, microsoft, azure, yahoo, keycloak).
* @param array $params The configuration parameters (clientId, clientSecret, etc).
*
* @return AbstractProvider|null
*/
public static function getProvider(string $providerName, array $params): ?AbstractProvider {
switch ($providerName) {
case 'google':
return new Google($params);
case 'microsoft':
return new Microsoft($params);
case 'azure':
return new Azure($params);
case 'yahoo':
return new Yahoo($params);
case 'keycloak':
return new Keycloak($params);
default:
return null;
}
}
/**
* Get the default options for a provider.
*
* keycloak: scopes are realm-specific; caller must supply via params, not defaults.
*
* @param string $providerName The provider name.
*
* @return array
*/
public static function getDefaultOptions(string $providerName): array {
switch ($providerName) {
case 'google':
return [
'scope' => ['https://mail.google.com/']
];
case 'microsoft':
return [
'scope' => ['wl.imap', 'wl.offline_access']
];
case 'azure':
return [
'scope' => ['https://outlook.office.com/SMTP.Send', 'offline_access']
];
default:
return [];
}
}
}