Skip to content

Commit

Permalink
Add Okta Provider (#114)
Browse files Browse the repository at this point in the history
* Add Okta Provider

* Fix styles.

* Clean up composer json

* Add extraConfig values for base_url
  • Loading branch information
chaseconey authored and faustbrian committed Nov 21, 2017
0 parents commit dcda134
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
16 changes: 16 additions & 0 deletions OktaExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace SocialiteProviders\Okta;

use SocialiteProviders\Manager\SocialiteWasCalled;

class OktaExtendSocialite
{
/**
* Execute the provider.
*/
public function handle(SocialiteWasCalled $socialiteWasCalled)
{
$socialiteWasCalled->extendSocialite('okta', __NAMESPACE__.'\Provider');
}
}
116 changes: 116 additions & 0 deletions Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace SocialiteProviders\Okta;

use SocialiteProviders\Manager\OAuth2\User;
use Laravel\Socialite\Two\ProviderInterface;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;

class Provider extends AbstractProvider implements ProviderInterface
{
/**
* Unique Provider Identifier.
*/
const IDENTIFIER = 'OKTA';

/**
* Scopes defintions.
*
* @see http://developer.okta.com/docs/api/resources/oidc.html#scopes
*/
const SCOPE_OPENID = 'openid';
const SCOPE_PROFILE = 'profile';
const SCOPE_EMAIL = 'email';
const SCOPE_ADDRESS = 'address';
const SCOPE_PHONE = 'phone';
const SCOPE_OFFLINE_ACCESS = 'offline_access';

/**
* {@inheritdoc}
*/
protected $scopes = [
'openid',
'profile',
'email',
];

/**
* {@inheritdoc}
*/
protected $scopeSeparator = ' ';

/**
* {@inheritdoc}
*/
protected function getOktaUrl()
{
return $this->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',
]);
}
}
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "socialiteproviders/okta",
"description": "Okta OAuth2 Provider for Laravel Socialite",
"license": "MIT",
"authors": [{
"name": "Chase Coney",
"email": "[email protected]"
}],
"require": {
"php": "^5.6 || ^7.0",
"socialiteproviders/manager": "~2.0 || ~3.0"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\Okta\\": ""
}
}
}

0 comments on commit dcda134

Please sign in to comment.