From dcda13432c80060cd84d4cb5f2af422d280ab895 Mon Sep 17 00:00:00 2001 From: Chase Coney Date: Mon, 20 Nov 2017 23:31:47 -0600 Subject: [PATCH] Add Okta Provider (#114) * Add Okta Provider * Fix styles. * Clean up composer json * Add extraConfig values for base_url --- OktaExtendSocialite.php | 16 ++++++ Provider.php | 116 ++++++++++++++++++++++++++++++++++++++++ composer.json | 18 +++++++ 3 files changed, 150 insertions(+) create mode 100644 OktaExtendSocialite.php create mode 100644 Provider.php create mode 100644 composer.json diff --git a/OktaExtendSocialite.php b/OktaExtendSocialite.php new file mode 100644 index 0000000..ff93401 --- /dev/null +++ b/OktaExtendSocialite.php @@ -0,0 +1,16 @@ +extendSocialite('okta', __NAMESPACE__.'\Provider'); + } +} diff --git a/Provider.php b/Provider.php new file mode 100644 index 0000000..af93623 --- /dev/null +++ b/Provider.php @@ -0,0 +1,116 @@ +getConfig('base_url'); + } + + /** + * {@inheritdoc} + */ + public static function additionalConfigKeys() + { + return ['base_url']; + } + + /** + * {@inheritdoc} + */ + protected function getAuthUrl($state) + { + return $this->buildAuthUrlFromBase($this->getOktaUrl().'/oauth2/v1/authorize', $state); + } + + /** + * {@inheritdoc} + */ + protected function getTokenUrl() + { + return $this->getOktaUrl().'/oauth2/v1/token'; + } + + /** + * {@inheritdoc} + */ + protected function getUserByToken($token) + { + $response = $this->getHttpClient()->get($this->getOktaUrl().'/oauth2/v1/userinfo', [ + 'headers' => [ + 'Authorization' => 'Bearer '.$token, + ], + ]); + + return json_decode($response->getBody(), true); + } + + /** + * {@inheritdoc} + */ + protected function mapUserToObject(array $user) + { + return (new User())->setRaw($user)->map([ + 'id' => array_get($user, 'sub'), + 'email' => array_get($user, 'email'), + 'email_verified' => array_get($user, 'email_verified', false), + 'nickname' => array_get($user, 'nickname'), + 'name' => array_get($user, 'name'), + 'first_name' => array_get($user, 'given_name'), + 'last_name' => array_get($user, 'family_name'), + 'profileUrl' => array_get($user, 'profile'), + 'address' => array_get($user, 'address'), + 'phone' => array_get($user, 'phone'), + ]); + } + + /** + * {@inheritdoc} + */ + protected function getTokenFields($code) + { + return array_merge(parent::getTokenFields($code), [ + 'grant_type' => 'authorization_code', + ]); + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..17c7ce7 --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "socialiteproviders/okta", + "description": "Okta OAuth2 Provider for Laravel Socialite", + "license": "MIT", + "authors": [{ + "name": "Chase Coney", + "email": "chase.coney@gmail.com" + }], + "require": { + "php": "^5.6 || ^7.0", + "socialiteproviders/manager": "~2.0 || ~3.0" + }, + "autoload": { + "psr-4": { + "SocialiteProviders\\Okta\\": "" + } + } +}