Skip to content

Commit dce584c

Browse files
authored
Merge pull request #127 from auth0/5.x.x-dev
v5.0.0 - Oauth2 Api authentication
2 parents f5960df + 9038b84 commit dce584c

39 files changed

+756
-1579
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ vendor
44
.idea
55
.DS_Store
66
.env
7+
.env.us
78
examples/basic-oauth/.env
89
composer.lock

README.md

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Check our docs page to get a complete guide on how to install it in an existing
3030

3131
```php
3232
// HS256 tokens
33+
use Auth0\SDK\JWTVerifier;
34+
3335
$verifier = new JWTVerifier([
3436
'valid_audiences' => [$client_id],
3537
'client_secret' => $client_secret
@@ -39,7 +41,7 @@ $decoded = $verifier->verifyAndDecode($jwt);
3941

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

65-
use Auth0\SDK\API\Authentication;
67+
use Auth0\SDK\Auth0;
6668

6769
$domain = 'YOUR_NAMESPACE';
6870
$client_id = 'YOUR_CLIENT_ID';
6971
$client_secret = 'YOUR_CLIENT_SECRET';
7072
$redirect_uri = 'http://YOUR_APP/callback';
7173

72-
$auth0 = new Authentication($domain, $client_id);
73-
74-
$oAuthClient = $auth0->get_oauth_client($client_secret, $redirect_uri);
75-
$profile = $oAuthClient->getUser();
76-
77-
if (!$profile) {
74+
$auth0 = new Auth0([
75+
'domain' => $domain,
76+
'client_id' => $client_id,
77+
'client_secret' => $client_secret,
78+
'redirect_uri' => $redirect_uri,
79+
'audience' => 'urn:test:api',
80+
'persist_id_token' => true,
81+
'persist_access_token' => true,
82+
'persist_refresh_token' => true,
83+
]);
7884

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

81-
header("Location: $authorize_url");
82-
exit;
87+
if (!$userInfo) {
88+
$auth0->login();
8389
}
8490

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

118-
$auth0Api = new Authentication($domain, $client_id, $client_secret);
124+
$auth0Api = new Authentication($domain, $client_id, $client_secret);
119125

120-
$tokens = $auth0Api->authorize_with_ro('[email protected]','thepassword');
126+
// getting an access token with client credentials grant
127+
$access_token = $auth0Api->client_credentials([
128+
'audience' => 'urn:test:api',
129+
'scope' => 'do:something read:somethingelse',
130+
]);
121131

122-
$access_token = $auth0Api->get_access_token();
132+
// getting an access token with password realm grant
133+
$access_token = $auth0Api->login([
134+
'username' => '[email protected]',
135+
'password' => 'shh',
136+
'realm' => 'Username-Password-Authentication',
137+
]);
123138
```
124139

125140
## Troubleshoot
@@ -222,7 +237,7 @@ $ composer install
222237
$ php -S localhost:3000
223238
```
224239

225-
## Migration guide
240+
## Migration guide
226241

227242
### from 1.x
228243

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"php": ">=5.5",
1414
"guzzlehttp/guzzle": "~6.0",
1515
"ext-json": "*",
16-
"adoy/oauth2": "^1.3",
17-
"firebase/php-jwt" : "^3.0"
16+
"firebase/php-jwt" : "^4.0"
1817
},
1918
"require-dev": {
2019
"phpunit/phpunit": "4.6.*",

examples/basic-api/src/Main.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App;
44

5+
use Auth0\SDK\JWTVerifier;
6+
57
class Main {
68

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

1214
try {
13-
$this->tokenInfo = \Auth0\SDK\Auth0JWT::decode($token, getenv('AUTH0_CLIENT_ID'), getenv('AUTH0_CLIENT_SECRET'));
14-
$this->token = $token;
15+
$verifier = new JWTVerifier([
16+
'supported_algs' => ['RS256'],
17+
'valid_audiences' => [getenv('AUTH0_AUDIENCE')],
18+
'authorized_iss' => ['https://' . getenv('AUTH0_DOMAIN') . '/']
19+
]);
20+
21+
$this->token = $token;
22+
$this->tokenInfo = $verifier->verifyAndDecode($token);
1523
}
1624
catch(\Auth0\SDK\Exception\CoreException $e) {
1725
throw $e;
@@ -29,7 +37,7 @@ public function privatePing(){
2937

3038
$auth0Api = new \Auth0\SDK\Auth0Api($this->token, getenv('AUTH0_DOMAIN'));
3139
$userData = $auth0Api->users->get($this->tokenInfo->sub);
32-
40+
3341
return array(
3442
"status" => 'ok',
3543
"message" => 'Shh, it\' secret',

examples/basic-oauth/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Basic sample for securing a WebApp with Auth0",
44
"require": {
55
"vlucas/phpdotenv": "1.1.1",
6-
"auth0/auth0-php": "~3.0"
6+
"auth0/auth0-php": "^5.0"
77
},
88
"license": "MIT",
99
"authors": [

examples/basic-oauth/config.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/basic-oauth/create_user.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

examples/basic-oauth/index.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44
require_once 'helpers.php';
55
require_once 'dotenv-loader.php';
66

7-
use Auth0\SDK\API\Authentication;
7+
use Auth0\SDK\Auth0;
88

99
$domain = getenv('AUTH0_DOMAIN');
1010
$client_id = getenv('AUTH0_CLIENT_ID');
1111
$client_secret = getenv('AUTH0_CLIENT_SECRET');
1212
$redirect_uri = getenv('AUTH0_CALLBACK_URL');
1313

14-
$auth0 = new Authentication($domain, $client_id);
15-
16-
$auth0Oauth = $auth0->get_oauth_client($client_secret, $redirect_uri, [
14+
$auth0 = new Auth0([
15+
'domain' => $domain,
16+
'client_id' => $client_id,
17+
'client_secret' => $client_secret,
18+
'redirect_uri' => $redirect_uri,
1719
'persist_id_token' => true,
1820
'persist_refresh_token' => true,
1921
]);
2022

21-
$userInfo = $auth0Oauth->getUser();
23+
$userInfo = $auth0->getUser();
2224

2325
if (isset($_REQUEST['logout'])) {
24-
$auth0Oauth->logout();
26+
$auth0->logout();
2527
session_destroy();
2628
header("Location: /");
2729
}
@@ -36,5 +38,4 @@
3638

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

39-
40-
require 'login.php';
41+
$auth0->login();

examples/basic-oauth/logeduser.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<a href="?logout">Logout</a>
22
<a href="?update-metadata">Update Metadata</a>
3-
<a href="?create-user">Create User</a>
43

54

65
<?php

examples/basic-oauth/login.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)