diff --git a/README.md b/README.md index 942bf3e..4cb2e53 100644 --- a/README.md +++ b/README.md @@ -31,12 +31,19 @@ ANIKEEN_ID_SECRET= ANIKEEN_ID_CALLBACK_URL=http://localhost/auth/callback ``` +To switch from `production` to `staging` use following variable: + +``` +ANIKEEN_ID_MODE=staging +``` + You will need to add an entry to the services configuration file so that after config files are cached for usage in production environment (Laravel command `artisan config:cache`) all config is still available. Add to `config/services.php` file: ```php 'anikeen' => [ + 'mode' => env('ANIKEEN_ID_MODE'), 'client_id' => env('ANIKEEN_ID_KEY'), 'client_secret' => env('ANIKEEN_ID_SECRET'), 'redirect' => env('ANIKEEN_ID_CALLBACK_URL'), diff --git a/README.stub b/README.stub index d93c71f..096c67e 100644 --- a/README.stub +++ b/README.stub @@ -31,12 +31,19 @@ ANIKEEN_ID_SECRET= ANIKEEN_ID_CALLBACK_URL=http://localhost/auth/callback ``` +To switch from `production` to `staging` use following variable: + +``` +ANIKEEN_ID_MODE=staging +``` + You will need to add an entry to the services configuration file so that after config files are cached for usage in production environment (Laravel command `artisan config:cache`) all config is still available. Add to `config/services.php` file: ```php 'anikeen' => [ + 'mode' => env('ANIKEEN_ID_MODE'), 'client_id' => env('ANIKEEN_ID_KEY'), 'client_secret' => env('ANIKEEN_ID_SECRET'), 'redirect' => env('ANIKEEN_ID_CALLBACK_URL'), diff --git a/src/Id/AnikeenId.php b/src/Id/AnikeenId.php index 4b3947a..a91f72c 100644 --- a/src/Id/AnikeenId.php +++ b/src/Id/AnikeenId.php @@ -51,6 +51,11 @@ class AnikeenId */ private static string $baseUrl = 'https://id.anikeen.com/api/'; + /** + * The staging base URL for Anikeen ID API. + */ + private static string $stagingBaseUrl = 'https://staging.id.anikeen.com/api/'; + /** * The key for the access token. */ @@ -105,6 +110,9 @@ class AnikeenId if ($redirectUri = config('services.anikeen.redirect')) { $this->setRedirectUri($redirectUri); } + if (config('services.anikeen.mode') === 'staging') { + self::setBaseUrl(self::$stagingBaseUrl); + } if ($baseUrl = config('services.anikeen.base_url')) { self::setBaseUrl($baseUrl); } diff --git a/src/Id/Socialite/Provider.php b/src/Id/Socialite/Provider.php index 0166a10..a1dfd96 100644 --- a/src/Id/Socialite/Provider.php +++ b/src/Id/Socialite/Provider.php @@ -4,6 +4,7 @@ namespace Anikeen\Id\Socialite; use Anikeen\Id\Enums\Scope; use GuzzleHttp\Exception\GuzzleException; +use Illuminate\Http\Request; use Illuminate\Support\Arr; use Laravel\Socialite\Two\ProviderInterface; use SocialiteProviders\Manager\OAuth2\AbstractProvider; @@ -26,13 +27,25 @@ class Provider extends AbstractProvider implements ProviderInterface */ protected $scopeSeparator = ' '; + /** + * Get the base URL for the API. + */ + protected function getBaseUrl(): string + { + $mode = $this->config['mode'] ?? 'production'; + + return $mode === 'staging' + ? 'https://staging.id.anikeen.com' + : 'https://id.anikeen.com'; + } + /** * {@inheritdoc} */ protected function getAuthUrl($state): string { return $this->buildAuthUrlFromBase( - 'https://id.anikeen.com/oauth/authorize', $state + $this->getBaseUrl() . '/oauth/authorize', $state ); } @@ -41,7 +54,7 @@ class Provider extends AbstractProvider implements ProviderInterface */ protected function getTokenUrl(): string { - return 'https://id.anikeen.com/oauth/token'; + return $this->getBaseUrl() . '/oauth/token'; } /** @@ -52,7 +65,7 @@ class Provider extends AbstractProvider implements ProviderInterface protected function getUserByToken($token) { $response = $this->getHttpClient()->get( - 'https://id.anikeen.com/api/v1/user', [ + $this->getBaseUrl() . '/api/v1/user', [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $token,