Skip to content

Commit

Permalink
Keycloak dependency added and a lot of exceptions thrown.
Browse files Browse the repository at this point in the history
+ stevenmaguire/oauth2-keycloak package added
+ Added oauth-keyclock example implemtation file
* Added AeroGearAuthErrorException, AeroGearBadRequestException,
  AeroGearMissingOAuthTokenException, AeroGearNotFoundException
  • Loading branch information
ramlev committed Mar 19, 2016
1 parent aac64c4 commit b4a694a
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 108 deletions.
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ A library which intgrates with the the [Redhat Aerogear Unified Push](https://ae
$ composer require napp/aero-gear-push


## Keycloak oauth token usage

All, but SenderPushRequest(), have a OAuthToken dependency, and to generate the token, look in examples/oauth-keycloak.md

An example on how to set the token.

```php
$request->setOauthToken($token);
```



## Usage

Example on how to use this library.
Expand Down Expand Up @@ -51,7 +63,7 @@ For information about how to format the single methods which is accepting arrays

#### CreateApplicationRequest()
##### Required methods
```setBearer() # oAuth token```
```setOauthToken() # oAuth token```
```setName() # string```

##### Optional methods
Expand All @@ -67,7 +79,7 @@ json

#### UpdateApplicationRequest()
##### Required methods
```setBearer() # oAuth token```
```setOauthToken() # oAuth token```
```setName() # string```

##### Optional methods
Expand All @@ -83,7 +95,7 @@ json

#### DeleteApplicationRequest($pushAppId)
##### Required methods
```setBearer() # oAuth token```
```setOauthToken() # oAuth token```

##### Optional methods

Expand All @@ -96,7 +108,7 @@ json

#### CreateIosVariantRequest($pushAppId)
##### Required methods
```setBearer() # oAuth token```
```setOauthToken() # oAuth token```
```setCertificate() # fopen file resource```
```setPassphrase() # string```
```setProduction() # string ('true' or 'false')'```
Expand All @@ -115,7 +127,7 @@ json

#### CreateSimplePushVariantRequest($pushAppId)
##### Required methods
```setBearer() # oAuth token```
```setOauthToken() # oAuth token```

##### Optional methods
```setName() # string```
Expand All @@ -132,7 +144,7 @@ json

#### CreateAndroidVariantRequest($pushAppId)
##### Required methods
```setBearer() # oAuth token```
```setOauthToken() # oAuth token```
```setGoogleKey() # string```

##### Optional methods
Expand Down Expand Up @@ -168,7 +180,7 @@ json

#### GetApplicationInstallationRequest()
##### Required methods
```setBearer() # oAuth token```
```setOauthToken() # oAuth token```
```setVariantId() # string```

##### Optional methods
Expand All @@ -183,7 +195,7 @@ json

#### GetApplicationRequest()
##### Required methods
```setBearer() # oAuth token```
```setOauthToken() # oAuth token```

##### Optional methods
```setPageNumber() # integer```
Expand All @@ -200,7 +212,7 @@ json

#### GetMetricsMessagesRequest()
##### Required methods
```setBearer() # oAuth token```
```setOauthToken() # oAuth token```

##### Optional methods
```setPageNumber() # integer```
Expand All @@ -215,7 +227,7 @@ json

#### GetMetricsDashboardRequest()
##### Required methods
```setBearer() # oAuth token```
```setOauthToken() # oAuth token```

##### Optional methods
```setType() # string {active, warnings}```
Expand All @@ -229,7 +241,7 @@ json

#### GetSysInfoHealthRequest()
##### Required methods
```setBearer() # oAuth token```
```setOauthToken() # oAuth token```

##### Optional methods

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
}
},
"require": {
"guzzlehttp/guzzle": "^6.1"
"guzzlehttp/guzzle": "^6.1",
"stevenmaguire/oauth2-keycloak": "0.1.0"
}
}
59 changes: 59 additions & 0 deletions examples/oauth-keycloak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

# KEYCLOAK PROVIDER for OAUTH 2

## Authorization flow

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

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

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
exit('Invalid state, make sure HTTP sessions are enabled.');

} 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());
}

// Use this to interact with an API on the users behalf
echo $token->getToken();
}
```

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

$token = $provider->getAccessToken('refresh_token', [
'refresh_token' => $token->getRefreshToken(),
]);
```
Loading

0 comments on commit b4a694a

Please sign in to comment.