Skip to content

Commit

Permalink
DP-460 Upgrade to PHP 8.1 / Laravel 9
Browse files Browse the repository at this point in the history
- Update dependencies
- Get rid of global helper functions
  • Loading branch information
daniilly committed Feb 27, 2023
1 parent 61f16e8 commit fa70d52
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"prefer-stable": true,
"require": {
"dreamfactory/df-oauth": "~0.16",
"phpseclib/phpseclib": "2.0.*"
"phpseclib/phpseclib": "3.0.*"
},
"autoload": {
"psr-4": {
Expand Down
21 changes: 11 additions & 10 deletions src/Components/OidcProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Cache;
use Config;
use Log;
use Arr;

/**
* Class OidcProvider
Expand Down Expand Up @@ -195,7 +196,7 @@ public function getUserFromTokenResponse($response)
*/
protected function parseAccessToken($body)
{
$token = array_get($body, 'access_token');
$token = Arr::get($body, 'access_token');
if (empty($token)) {
$token = '--NOT-AVAILABLE--';
}
Expand All @@ -211,7 +212,7 @@ protected function parseAccessToken($body)
*/
protected function validateIdToken(array $response)
{
$idToken = array_get($response, 'id_token');
$idToken = Arr::get($response, 'id_token');

if ($this->validateIdToken === true) {
if (empty($this->jwksUri)) {
Expand All @@ -221,9 +222,9 @@ protected function validateIdToken(array $response)
$kid = $header['kid'];
$publicKeyInfo = $this->getProviderPublicKeyInfo($kid);
$payload = $this->verifySignature($publicKeyInfo, $idToken);
$this->verifyIssuer(array_get($payload, 'iss'));
$this->verifyAudience(array_get($payload, 'aud'), array_get($payload, 'azp'));
$this->verifyExpiry(array_get($payload, 'exp'));
$this->verifyIssuer(Arr::get($payload, 'iss'));
$this->verifyAudience(Arr::get($payload, 'aud'), Arr::get($payload, 'azp'));
$this->verifyExpiry(Arr::get($payload, 'exp'));

return $payload;
} elseif (!empty($idToken)) {
Expand Down Expand Up @@ -330,7 +331,7 @@ protected function getProviderPublicKeyInfo($kid)
if (empty($key)) {
$keys = $this->getProviderKeys();
foreach ($keys as $k) {
if (array_get($k, 'kid') === $kid) {
if (Arr::get($k, 'kid') === $kid) {
$key = $k;
Cache::put(static::getJwksCacheKey($kid), $key, Config::get('df.default_cache_ttl'));
}
Expand All @@ -352,7 +353,7 @@ protected function getProviderKeys()
$response = $this->getHttpClient()->get($this->jwksUri);
$keys = json_decode($response->getBody()->getContents(), true);

return array_get($keys, 'keys');
return Arr::get($keys, 'keys');
}

/**
Expand All @@ -366,8 +367,8 @@ protected function getProviderKeys()
protected function verifySignature($keyData, $idToken)
{
try {
$kty = array_get($keyData, 'kty');
$alg = array_get($keyData, 'alg', 'RS256');
$kty = Arr::get($keyData, 'kty');
$alg = Arr::get($keyData, 'alg', 'RS256');
if ($kty === 'RSA') {
$modulus = new BigInteger($this->encoder->decode($keyData['n']), (int)substr($alg, 2));
$exponent = new BigInteger($this->encoder->decode($keyData['e']), (int)substr($alg, 2));
Expand Down Expand Up @@ -429,7 +430,7 @@ protected function getTokenUrl()
*/
protected function getTokenFields($code)
{
return array_add(parent::getTokenFields($code), 'grant_type', 'authorization_code');
return Arr::add(parent::getTokenFields($code), 'grant_type', 'authorization_code');
}

/**
Expand Down
27 changes: 14 additions & 13 deletions src/Models/OidcConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use GuzzleHttp\Client;
use Cache;
use Config;
use Arr;

class OidcConfig extends BaseServiceConfigModel
{
Expand Down Expand Up @@ -72,13 +73,13 @@ class OidcConfig extends BaseServiceConfigModel
*/
public function validate($data, $throwException = true)
{
$discovery = array_get($data, 'discovery_document');
$discovery = Arr::get($data, 'discovery_document');

if (empty($discovery)) {
$this->rules['auth_endpoint'] = 'required';
$this->rules['token_endpoint'] = 'required';
$this->rules['scopes'] = 'required';
if (boolval(array_get($data, 'validate_id_token'))) {
if (boolval(Arr::get($data, 'validate_id_token'))) {
$this->rules['jwks_uri'] = 'required';
}
}
Expand All @@ -95,19 +96,19 @@ public function setDiscoveryDocumentAttribute($value)
$data = static::getDiscoveryData($value);
if (!empty($data)) {
if (!isset($this->attributes['auth_endpoint'])) {
$this->attributes['auth_endpoint'] = array_get($data, 'authorization_endpoint');
$this->attributes['auth_endpoint'] = Arr::get($data, 'authorization_endpoint');
}
if (!isset($this->attributes['token_endpoint'])) {
$this->attributes['token_endpoint'] = array_get($data, 'token_endpoint');
$this->attributes['token_endpoint'] = Arr::get($data, 'token_endpoint');
}
if (!isset($this->attributes['user_endpoint'])) {
$this->attributes['user_endpoint'] = array_get($data, 'userinfo_endpoint');
$this->attributes['user_endpoint'] = Arr::get($data, 'userinfo_endpoint');
}
if (!isset($this->attributes['jwks_uri'])) {
$this->attributes['jwks_uri'] = array_get($data, 'jwks_uri');
$this->attributes['jwks_uri'] = Arr::get($data, 'jwks_uri');
}
if (!isset($this->attributes['scopes'])) {
$this->attributes['scopes'] = implode(',', array_get($data, 'scopes_supported'));
$this->attributes['scopes'] = implode(',', Arr::get($data, 'scopes_supported'));
}
}
}
Expand All @@ -117,7 +118,7 @@ public function setDiscoveryDocumentAttribute($value)
*/
public function setAuthEndpointAttribute($value)
{
$dd = array_get($this->attributes, 'discovery_document');
$dd = Arr::get($this->attributes, 'discovery_document');
if (empty($value) && !empty($dd)) {
$value = static::getDiscoveryData($dd, 'authorization_endpoint');
}
Expand All @@ -130,7 +131,7 @@ public function setAuthEndpointAttribute($value)
*/
public function setTokenEndpointAttribute($value)
{
$dd = array_get($this->attributes, 'discovery_document');
$dd = Arr::get($this->attributes, 'discovery_document');
if (empty($value) && !empty($dd)) {
$value = static::getDiscoveryData($dd, 'token_endpoint');
}
Expand All @@ -143,7 +144,7 @@ public function setTokenEndpointAttribute($value)
*/
public function setUserEndpointAttribute($value)
{
$dd = array_get($this->attributes, 'discovery_document');
$dd = Arr::get($this->attributes, 'discovery_document');
if (empty($value) && !empty($dd)) {
$value = static::getDiscoveryData($dd, 'userinfo_endpoint');
}
Expand All @@ -156,7 +157,7 @@ public function setUserEndpointAttribute($value)
*/
public function setJwksUriAttribute($value)
{
$dd = array_get($this->attributes, 'discovery_document');
$dd = Arr::get($this->attributes, 'discovery_document');
if (empty($value) && !empty($dd)) {
$value = static::getDiscoveryData($dd, 'jwks_uri');
}
Expand All @@ -169,7 +170,7 @@ public function setJwksUriAttribute($value)
*/
public function setScopesAttribute($value)
{
$dd = array_get($this->attributes, 'discovery_document');
$dd = Arr::get($this->attributes, 'discovery_document');
if (empty($value) && !empty($dd)) {
$value = implode(',', static::getDiscoveryData($dd, 'scopes_supported'));
}
Expand Down Expand Up @@ -200,7 +201,7 @@ public static function getDiscoveryData($dd, $key = null)
if (empty($key)) {
return $data;
} else {
return array_get($data, $key);
return Arr::get($data, $key);
}
} else {
throw new InternalServerErrorException('Failed to retrieve discovery document. Please check service configuration.');
Expand Down
21 changes: 11 additions & 10 deletions src/Services/OIDC.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DreamFactory\Core\OAuth\Services\BaseOAuthService;
use DreamFactory\Core\Oidc\Components\OidcProvider;
use DreamFactory\Core\Oidc\Resources\SSO;
use Arr;

class OIDC extends BaseOAuthService
{
Expand All @@ -28,17 +29,17 @@ class OIDC extends BaseOAuthService
protected function setProvider($config)
{
$this->provider = new OidcProvider(
array_get($config, 'client_id'),
array_get($config, 'client_secret'),
array_get($config, 'redirect_url')
Arr::get($config, 'client_id'),
Arr::get($config, 'client_secret'),
Arr::get($config, 'redirect_url')
);
$this->provider->setDiscoveryEndpoint(array_get($config, 'discovery_document'));
$this->provider->setAuthEndpoint(array_get($config, 'auth_endpoint'));
$this->provider->setTokenEndpoint(array_get($config, 'token_endpoint'));
$this->provider->setUserEndpoint(array_get($config, 'user_endpoint'));
$this->provider->setJwksUri(array_get($config, 'jwks_uri'));
$this->provider->validateIdToken = boolval(array_get($config, 'validate_id_token'));;
$scopes = array_map('trim', explode(',', array_get($config, 'scopes')));
$this->provider->setDiscoveryEndpoint(Arr::get($config, 'discovery_document'));
$this->provider->setAuthEndpoint(Arr::get($config, 'auth_endpoint'));
$this->provider->setTokenEndpoint(Arr::get($config, 'token_endpoint'));
$this->provider->setUserEndpoint(Arr::get($config, 'user_endpoint'));
$this->provider->setJwksUri(Arr::get($config, 'jwks_uri'));
$this->provider->validateIdToken = boolval(Arr::get($config, 'validate_id_token'));;
$scopes = array_map('trim', explode(',', Arr::get($config, 'scopes')));
$this->provider->setScopes($scopes);
}

Expand Down

0 comments on commit fa70d52

Please sign in to comment.