-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a local package for the Socialite Package for the PingIdentity provider. --------- Co-authored-by: Zack Galbreath <[email protected]> Co-authored-by: William Allen <[email protected]>
- Loading branch information
1 parent
6e8d60a
commit 2a48ce0
Showing
12 changed files
with
270 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions
13
resources/providers/PingIdentity/PingIdentityExtendSocialite.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\PingIdentity; | ||
|
||
use SocialiteProviders\Manager\SocialiteWasCalled; | ||
|
||
class PingIdentityExtendSocialite | ||
{ | ||
public function handle(SocialiteWasCalled $socialiteWasCalled): void | ||
{ | ||
$socialiteWasCalled->extendSocialite('pingidentity', Provider::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\PingIdentity; | ||
|
||
use GuzzleHttp\RequestOptions; | ||
use SocialiteProviders\Manager\OAuth2\AbstractProvider; | ||
use SocialiteProviders\Manager\OAuth2\User; | ||
|
||
class Provider extends AbstractProvider | ||
{ | ||
public const IDENTIFIER = 'PINGIDENTITY'; | ||
|
||
/** | ||
* The scopes being requested. | ||
* | ||
* @var array | ||
*/ | ||
protected $scopes = [ | ||
'openid', | ||
'profile', | ||
'email', | ||
]; | ||
|
||
protected $scopeSeparator = ' '; | ||
|
||
/** | ||
* Get the authentication URL for the provider. | ||
* | ||
* @param string $state | ||
*/ | ||
protected function getAuthUrl($state): string | ||
{ | ||
$auth_url = $this->buildAuthUrlFromBase($this->getInstanceUri().'/as/authorization.oauth2', $state); | ||
$auth_url .= "&acr_values=Single_Factor&prompt=login"; | ||
return $auth_url; | ||
} | ||
|
||
/** | ||
* Get the token URL for the provider. | ||
*/ | ||
protected function getTokenUrl(): string | ||
{ | ||
return $this->getInstanceUri() . '/as/token.oauth2?acr_values=Single_Factor&prompt=login'; | ||
} | ||
|
||
/** | ||
* Map the raw user array to a Socialite User instance. | ||
*/ | ||
protected function mapUserToObject(array $user): \Laravel\Socialite\Two\User | ||
{ | ||
return (new User())->setRaw($user)->map([ | ||
'nickname' => $user['name'], | ||
'name' => $user['given_name'] . " " . $user['family_name'], | ||
'email' => $user['email'], | ||
]); | ||
} | ||
|
||
/** | ||
* Get the Instance URL for the provider. | ||
*/ | ||
protected function getInstanceUri(): string | ||
{ | ||
return $this->getConfig('instance_uri', 'https://auth.pingone.com/'); | ||
} | ||
|
||
/** | ||
* Get the raw user for the given access token. | ||
* | ||
* @param string $token | ||
* @return array<string> | ||
*/ | ||
protected function getUserByToken($token): array | ||
{ | ||
$response = $this->getHttpClient()->get($this->getInstanceUri() . '/idp/userinfo.openid', [ | ||
RequestOptions::HEADERS => [ | ||
'Authorization' => "Bearer $token", | ||
], | ||
]); | ||
|
||
return json_decode((string) $response->getBody(), true); | ||
} | ||
|
||
/** | ||
* Additional configuration key values that may be set | ||
* @return array<string> | ||
*/ | ||
public static function additionalConfigKeys(): array | ||
{ | ||
return ['instance_uri']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "socialiteproviders/pingidentity", | ||
"description": "PingIdentity OAuth2 Provider for Laravel Socialite", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"keywords": [ | ||
"pingidentity", | ||
"laravel", | ||
"oauth", | ||
"provider", | ||
"socialite" | ||
], | ||
"authors": [ | ||
{ | ||
} | ||
], | ||
"require": { | ||
"php": "^8.0", | ||
"ext-json": "*", | ||
"socialiteproviders/manager": "^4.4" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SocialiteProviders\\PingIdentity\\": "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"packages": { | ||
"socialiteproviders/pingidentity": { | ||
"1.0.0": { @composer.json } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
use Slides\Saml2\Events\SignedIn as Saml2SignedInEvent; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
use Tests\TestCase; | ||
use Laravel\Socialite\Facades\Socialite; | ||
|
||
class LoginAndRegistration extends TestCase | ||
{ | ||
|
@@ -250,6 +251,45 @@ public function testSaml2LoginListener() : void | |
Mockery::close(); | ||
} | ||
|
||
public function testPingIdentity() : void | ||
{ | ||
// Verify that the PingIdentity button doesn't appear by default. | ||
$response = $this->get('/login'); | ||
$response->assertDontSeeText('PingIdentity'); | ||
|
||
// Enable PingIdentity, verify the button appears. | ||
config(['services.pingidentity.enable' => true]); | ||
$response = $this->get('/login'); | ||
$response->assertSeeText('PingIdentity'); | ||
} | ||
|
||
/** | ||
* Test PingIdentity authentication | ||
*/ | ||
public function testPingIdentityProvider() : void | ||
{ | ||
// Stolen from: https://laracasts.com/discuss/channels/testing/testing-laravel-socialite-callback | ||
$abstractUser = Mockery::mock('Laravel\Socialite\Two\User'); | ||
$abstractUser->shouldReceive('getId') | ||
->andReturn(1234567890) | ||
->shouldReceive('getEmail') | ||
->andReturn('[email protected]') | ||
->shouldReceive('getNickname') | ||
->andReturn('Pseudo') | ||
->shouldReceive('getName') | ||
->andReturn('Arlette Laguiller') | ||
->shouldReceive('getAvatar') | ||
->andReturn('https://en.gravatar.com/userimage'); | ||
|
||
$provider = Mockery::mock('Laravel\Socialite\PingIdentity\Provider'); | ||
$provider->shouldReceive('user')->andReturn($abstractUser); | ||
|
||
Socialite::shouldReceive('driver')->with('pingidentity')->andReturn($provider); | ||
|
||
$response = $this->get("auth/pingidentity/callback"); | ||
$response->assertRedirect("/register?fname=Arlette&lname=Laguiller&email=cdash%40test.com"); | ||
} | ||
|
||
public function testRegisterUserWhenDisabled() : void | ||
{ | ||
// Create a user by sending proper data | ||
|