Skip to content

Commit

Permalink
Merge pull request #127 from auth0/5.x.x-dev
Browse files Browse the repository at this point in the history
v5.0.0 - Oauth2 Api authentication
  • Loading branch information
glena authored Feb 22, 2017
2 parents f5960df + 9038b84 commit dce584c
Show file tree
Hide file tree
Showing 39 changed files with 756 additions and 1,579 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ vendor
.idea
.DS_Store
.env
.env.us
examples/basic-oauth/.env
composer.lock
47 changes: 31 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Check our docs page to get a complete guide on how to install it in an existing

```php
// HS256 tokens
use Auth0\SDK\JWTVerifier;

$verifier = new JWTVerifier([
'valid_audiences' => [$client_id],
'client_secret' => $client_secret
Expand All @@ -39,7 +41,7 @@ $decoded = $verifier->verifyAndDecode($jwt);

// RS256 tokens
$verifier = new JWTVerifier([
'suported_algs' => ['RS256'],
'supported_algs' => ['RS256'],
'valid_audiences' => [$client_id],
'authorized_iss' => [$domain]
]);
Expand All @@ -51,7 +53,7 @@ $decoded = $verifier->verifyAndDecode($jwt);
Accepted params:
- **cache**: Receives an instance of `Auth0\SDK\Helpers\Cache\CacheHandler` (Supported `FileSystemCacheHandler` and `NoCacheHandler`). Defaults to `NoCacheHandler`.
- **guzzle_options**: Configuration propagated to guzzle when fetching the JWKs.
- **suported_algs**: `RS256` and `HS256` supported. Defaults to `HS256`.
- **supported_algs**: `RS256` and `HS256` supported. Defaults to `HS256`.
- **valid_audiences**: List of audiences that identifies the API (usefull for multitenant environments).
- **authorized_iss**: List of issues authorized to sign tokens for the API.
- **client_secret**: Client secret used to verify the token signature (only for `HS256`).
Expand All @@ -62,24 +64,28 @@ Accepted params:
```php
require __DIR__ . '/vendor/autoload.php';

use Auth0\SDK\API\Authentication;
use Auth0\SDK\Auth0;

$domain = 'YOUR_NAMESPACE';
$client_id = 'YOUR_CLIENT_ID';
$client_secret = 'YOUR_CLIENT_SECRET';
$redirect_uri = 'http://YOUR_APP/callback';

$auth0 = new Authentication($domain, $client_id);

$oAuthClient = $auth0->get_oauth_client($client_secret, $redirect_uri);
$profile = $oAuthClient->getUser();

if (!$profile) {
$auth0 = new Auth0([
'domain' => $domain,
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirect_uri,
'audience' => 'urn:test:api',
'persist_id_token' => true,
'persist_access_token' => true,
'persist_refresh_token' => true,
]);

$authorize_url = $auth0->get_authorize_link('code', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
$userInfo = $auth0->getUser();

header("Location: $authorize_url");
exit;
if (!$userInfo) {
$auth0->login();
}

var_dump($profile);
Expand Down Expand Up @@ -115,11 +121,20 @@ $domain = "account.auth0.com";
$client_id = '...';
$client_secret = '...'; // This is optional, only needed for impersonation or t fetch an access token

$auth0Api = new Authentication($domain, $client_id, $client_secret);
$auth0Api = new Authentication($domain, $client_id, $client_secret);

$tokens = $auth0Api->authorize_with_ro('theemail@test.com','thepassword');
// getting an access token with client credentials grant
$access_token = $auth0Api->client_credentials([
'audience' => 'urn:test:api',
'scope' => 'do:something read:somethingelse',
]);

$access_token = $auth0Api->get_access_token();
// getting an access token with password realm grant
$access_token = $auth0Api->login([
'username' => 'someone@example.com',
'password' => 'shh',
'realm' => 'Username-Password-Authentication',
]);
```

## Troubleshoot
Expand Down Expand Up @@ -222,7 +237,7 @@ $ composer install
$ php -S localhost:3000
```

## Migration guide
## Migration guide

### from 1.x

Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"php": ">=5.5",
"guzzlehttp/guzzle": "~6.0",
"ext-json": "*",
"adoy/oauth2": "^1.3",
"firebase/php-jwt" : "^3.0"
"firebase/php-jwt" : "^4.0"
},
"require-dev": {
"phpunit/phpunit": "4.6.*",
Expand Down
14 changes: 11 additions & 3 deletions examples/basic-api/src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App;

use Auth0\SDK\JWTVerifier;

class Main {

protected $token;
Expand All @@ -10,8 +12,14 @@ class Main {
public function setCurrentToken($token) {

try {
$this->tokenInfo = \Auth0\SDK\Auth0JWT::decode($token, getenv('AUTH0_CLIENT_ID'), getenv('AUTH0_CLIENT_SECRET'));
$this->token = $token;
$verifier = new JWTVerifier([
'supported_algs' => ['RS256'],
'valid_audiences' => [getenv('AUTH0_AUDIENCE')],
'authorized_iss' => ['https://' . getenv('AUTH0_DOMAIN') . '/']
]);

$this->token = $token;
$this->tokenInfo = $verifier->verifyAndDecode($token);
}
catch(\Auth0\SDK\Exception\CoreException $e) {
throw $e;
Expand All @@ -29,7 +37,7 @@ public function privatePing(){

$auth0Api = new \Auth0\SDK\Auth0Api($this->token, getenv('AUTH0_DOMAIN'));
$userData = $auth0Api->users->get($this->tokenInfo->sub);

return array(
"status" => 'ok',
"message" => 'Shh, it\' secret',
Expand Down
2 changes: 1 addition & 1 deletion examples/basic-oauth/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Basic sample for securing a WebApp with Auth0",
"require": {
"vlucas/phpdotenv": "1.1.1",
"auth0/auth0-php": "~3.0"
"auth0/auth0-php": "^5.0"
},
"license": "MIT",
"authors": [
Expand Down
10 changes: 0 additions & 10 deletions examples/basic-oauth/config.php

This file was deleted.

43 changes: 0 additions & 43 deletions examples/basic-oauth/create_user.php

This file was deleted.

17 changes: 9 additions & 8 deletions examples/basic-oauth/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
require_once 'helpers.php';
require_once 'dotenv-loader.php';

use Auth0\SDK\API\Authentication;
use Auth0\SDK\Auth0;

$domain = getenv('AUTH0_DOMAIN');
$client_id = getenv('AUTH0_CLIENT_ID');
$client_secret = getenv('AUTH0_CLIENT_SECRET');
$redirect_uri = getenv('AUTH0_CALLBACK_URL');

$auth0 = new Authentication($domain, $client_id);

$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
$auth0 = new Auth0([
'domain' => $domain,
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirect_uri,
'persist_id_token' => true,
'persist_refresh_token' => true,
]);

$userInfo = $auth0Oauth->getUser();
$userInfo = $auth0->getUser();

if (isset($_REQUEST['logout'])) {
$auth0Oauth->logout();
$auth0->logout();
session_destroy();
header("Location: /");
}
Expand All @@ -36,5 +38,4 @@

if ($userInfo) require 'logeduser.php';


require 'login.php';
$auth0->login();
1 change: 0 additions & 1 deletion examples/basic-oauth/logeduser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<a href="?logout">Logout</a>
<a href="?update-metadata">Update Metadata</a>
<a href="?create-user">Create User</a>


<?php
Expand Down
43 changes: 0 additions & 43 deletions examples/basic-oauth/login.php

This file was deleted.

37 changes: 23 additions & 14 deletions examples/basic-oauth/update-metadata.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
<?php

if (isset($_REQUEST['update']) && $_REQUEST['update']) {

$newMetadata = json_decode($_REQUEST["metadata"], true);

$auth0Oauth->updateUserMetadata($newMetadata);

echo "<div>UPDATED!!!</div>";
}

$userInfo = $auth0Oauth->getUser();


use Auth0\SDK\API\Management;

if (isset($_REQUEST['update']) && $_REQUEST['update']) {

$managementApi = new Management($auth0->getIdToken(), $domain);

$newMetadata = json_decode($_REQUEST["metadata"], true);

$userInfo = $managementApi->users->update(
$userInfo['user_id'],
[ 'user_metadata' => $newMetadata ]
);

$auth0->setUser($userInfo);

echo "<div>UPDATED!!!</div>";
}

$userInfo = $auth0->getUser();

?>

<form action="?update-metadata" method="POST">

<textarea name='metadata'>
<?php echo json_encode($userInfo["user_metadata"]); ?>
</textarea>
</textarea>

<input type="submit" name="update" value="Update" />
</form>
2 changes: 1 addition & 1 deletion examples/basic-webapp/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Basic sample for securing a WebApp with Auth0",
"require": {
"vlucas/phpdotenv": "2.3.0",
"auth0/auth0-php": "~4.0"
"auth0/auth0-php": "^5.0"
},
"license": "MIT",
"authors": [
Expand Down
Loading

0 comments on commit dce584c

Please sign in to comment.