Skip to content

Commit

Permalink
update 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Irsad Arief committed Feb 20, 2020
1 parent 3db15b0 commit 4e788cb
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 31 deletions.
41 changes: 19 additions & 22 deletions README.md
Expand Up @@ -12,6 +12,8 @@ Untuk menginstall, gunakan composer:
```
composer require irsadarief/jkd-sso
```
Untuk yang belum menginstall composer, informasi mengenai instalasi dan penggunaan composer dapat diakses [disini](https://github.com/composer/composer).

## Penggunaan

Untuk menggunakan library ini bisa dengan `\IrsadArief\OAuth2\Client\Provider\Keycloak` sebagai provider.
Expand All @@ -20,71 +22,66 @@ Untuk `authServerUrl` Anda dapat menuliskan https://sso.bps.go.id .

untuk `realm` anda dapat menuliskan `pegawai-bps`


## Contoh Kode

```php
$provider = new Stevenmaguire\OAuth2\Client\Provider\Keycloak([
'authServerUrl' => '{keycloak-server-url}',
'realm' => '{keycloak-realm}',
$provider = new IrsadArief\OAuth2\Client\Provider\Keycloak([
'authServerUrl' => 'https://sso.bps.go.id',
'realm' => 'pegawai-bps',
'clientId' => '{keycloak-client-id}',
'clientSecret' => '{keycloak-client-secret}',
'redirectUri' => 'https://example.com/callback-url',
'encryptionAlgorithm' => 'RS256', // optional
'encryptionKeyPath' => '../key.pem' // optional
'encryptionKey' => 'contents_of_key_or_certificate' // optional
'redirectUri' => 'https://example.com/callback-url'
]);

if (!isset($_GET['code'])) {

// If we don't have an authorization code then get one
// Untuk mendapatkan authorization code
$authUrl = $provider->getAuthorizationUrl();
$_SESSION['oauth2state'] = $provider->getState();
header('Location: '.$authUrl);
exit;

// Check given state against previously stored one to mitigate CSRF attack
// Mengecek state yang disimpan saat ini untuk memitigasi serangan CSRF
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

unset($_SESSION['oauth2state']);
exit('Invalid state, make sure HTTP sessions are enabled.');
exit('Invalid state');

} else {

// Try to get an access token (using the authorization coe grant)
try {
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
} catch (Exception $e) {
exit('Failed to get access token: '.$e->getMessage());
exit('Gagal mendapatkan akses token : '.$e->getMessage());
}

// Optional: Now you have a token you can look up a users profile data
// Opsional: Setelah mendapatkan token, anda dapat melihat data profil pengguna
try {

// We got an access token, let's now get the user's details
$user = $provider->getResourceOwner($token);

// Use these details to create a new profile
printf('Hello %s!', $user->getName());

} catch (Exception $e) {
exit('Failed to get resource owner: '.$e->getMessage());
exit('Gagal Mendapatkan Data Pengguna: '.$e->getMessage());
}

// Use this to interact with an API on the users behalf
// Gunakan token ini untuk berinteraksi dengan API di sisi pengguna
echo $token->getToken();
}
```

## Memperbarui Token

```php
$provider = new Stevenmaguire\OAuth2\Client\Provider\Keycloak([
'authServerUrl' => '{keycloak-server-url}',
'realm' => '{keycloak-realm}',
'clientId' => '{keycloak-client-id}',
'clientSecret' => '{keycloak-client-secret}',
$provider = new IrsadArief\OAuth2\Client\Provider\Keycloak([
'authServerUrl' => 'https://sso.bps.go.id',
'realm' => 'pegawai-bps',
'clientId' => '{keycloak-client-id}',
'clientSecret' => '{keycloak-client-secret}',
'redirectUri' => 'https://example.com/callback-url',
]);

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -29,12 +29,12 @@
},
"autoload": {
"psr-4": {
"IrsadArief\\OAuth2\\Client\\": "src/"
"JKD\\SSO\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"IrsadArief\\OAuth2\\Client\\Test\\": "test/src/"
"JKD\\SSO\\Client\\Test\\": "test/src/"
}
}
}
2 changes: 1 addition & 1 deletion examples/index.php
Expand Up @@ -4,7 +4,7 @@

session_start();

$provider = new IrsadArief\OAuth2\Client\Provider\Keycloak([
$provider = new JKD\SSO\Client\Provider\Keycloak([
'authServerUrl' => '',
'realm' => '',
'clientId' => '',
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace IrsadArief\OAuth2\Client\Provider\Exception;
namespace JKD\SSO\Client\Provider\Exception;

use Exception;

Expand Down
4 changes: 2 additions & 2 deletions src/Provider/Keycloak.php
@@ -1,6 +1,6 @@
<?php

namespace IrsadArief\OAuth2\Client\Provider;
namespace JKD\SSO\Client\Provider;

use Exception;
use Firebase\JWT\JWT;
Expand All @@ -9,7 +9,7 @@
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use Psr\Http\Message\ResponseInterface;
use IrsadArief\OAuth2\Client\Provider\Exception\EncryptionConfigurationException;
use JKD\SSO\Client\Provider\Exception\EncryptionConfigurationException;

class Keycloak extends AbstractProvider
{
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/KeycloakResourceOwner.php
@@ -1,6 +1,6 @@
<?php

namespace IrsadArief\OAuth2\Client\Provider;
namespace JKD\SSO\Client\Provider;

use League\OAuth2\Client\Provider\ResourceOwnerInterface;

Expand Down
4 changes: 2 additions & 2 deletions test/src/Provider/KeycloakTest.php
Expand Up @@ -5,7 +5,7 @@
$mockFileGetContents = null;
}

namespace Stevenmaguire\OAuth2\Client\Provider
namespace JKD\SSO\Client\Provider
{
function file_get_contents()
{
Expand All @@ -21,7 +21,7 @@ function file_get_contents()
}
}

namespace Stevenmaguire\OAuth2\Client\Test\Provider
namespace JKD\SSO\Client\Test\Provider
{
use League\OAuth2\Client\Tool\QueryBuilderTrait;
use Mockery as m;
Expand Down

0 comments on commit 4e788cb

Please sign in to comment.