Skip to content

Commit

Permalink
Merge pull request #7 from DirectLease/update-to-auth0-v8-and-php8
Browse files Browse the repository at this point in the history
Update to auth0 v8 and php8
  • Loading branch information
lars-lemon8 authored Jun 8, 2023
2 parents e275ec7 + f9a957f commit 6a7388a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 24 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ The following config settings are **required**:
* **client_secret** - the client secret of your auth0 application
* **domain** - the domain of your auth0 application
* **redirect_uri** - Auth0 needs a fully qualified URL to your site. Default value is /auth/callback which will be turned into https://www.mysite.com/auth/callback
* **scope** - the scope of attributes you want to retrieve from the user who logs in at Auth0.
Default is 'openid email profile'
* **cookie_secret** - A long secret value auth0 uses to encrypt session cookies, refer to Auth0 docs for explanation
* **scope** - the scope of attributes you want to retrieve from the user who logs in at Auth0. NOTICE: This is now an array instead of a string.
Default is - 'openid' - 'email' - 'profile'
* **persisent_login** - Do you want the use the persistent login of SilverStripe, true or false.

The following config settings are **optional** based on your implementation:
Expand Down
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
"authors": [
{
"name": "Arno Bor",
"email": "[email protected]"
}
"email": "[email protected]"
},
{
"name": "Lars Prakken",
"email": "[email protected]"
},

],
"support": {
"issues": "https://github.com/DirectLease/Auth0/issues"
Expand All @@ -17,10 +22,10 @@
"silverstripe/framework": "4.*",
"silverstripe/versioned": "^1.0",
"silverstripe/vendor-plugin": "^1.0",
"auth0/auth0-php": "^5.7",
"auth0/auth0-php": "^8",
"guzzlehttp/guzzle": "^7",
"ext-json": "*",
"tractorcow/silverstripe-fluent": "^6"
"tractorcow/silverstripe-fluent": "^5"
},
"extra": {
"installer-name": "auth0",
Expand Down
72 changes: 54 additions & 18 deletions src/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Class Auth0ApiController
*
* @author Arno Bor
* @author Arno Bor, Lars Prakken
* @package auth0
*/
class ApiController extends Controller
Expand Down Expand Up @@ -48,6 +48,7 @@ class ApiController extends Controller
private $namespace;
private $url;
private $persistent_login;
private $cookie_secret;

public function __construct()
{
Expand All @@ -68,7 +69,7 @@ public function __construct()
$this->namespace = $this->config()->get('namespace');
$this->url = 'https://' . $this->domain;
$this->persistent_login = $this->config()->get('persistent_login');

$this->cookie_secret = $this->config()->get('cookie_secret');
}

public function signup() {
Expand Down Expand Up @@ -118,23 +119,29 @@ public function login($isSignup = false)

$this->setup($redirect_to);

$this->auth0->login('','',$extraAuth0Params);
$redirect = $this->redirect_uri .= '?redirect_to=' . $redirect_to;

return $this->redirect($this->auth0->login($redirect,$extraAuth0Params));
}

public function logout()
{
$identityStore = Injector::inst()->get(IdentityStore::class);
$identityStore->logOut($this->request);

$auth_api = new Authentication($this->domain, $this->client_id);
//$auth_api = new Authentication(['domain'=>$this->domain, 'clientId'=>$this->client_id]);

$this->setup();
$auth_api = $this->auth0->authentication();

$this->auth0->logout();

$this->redirect($auth_api->get_logout_link(Director::absoluteBaseURL(), $this->client_id));
//$this->redirect('/');
$this->redirect($auth_api->getLogoutLink(Director::absoluteBaseURL()));
//$this->redirect($auth_api->get_logout_link(Director::absoluteBaseURL(), $this->client_id));
}



/**
* Get the authenticated user and login in SS
*
Expand All @@ -145,15 +152,39 @@ public function logout()
public function callback()
{
$this->setup();

$redirect_to = $this->request->getVar('redirect_to');
$user = $this->auth0->getUser();

if ($this->auth0->getExchangeParameters()) {
// Have the SDK complete the authentication flow:
$this->auth0->exchange();
}



//$user = $this->auth0->getUser();
$user = $this->auth0->getCredentials();

if ($user === null) {
// The user isn't logged in.
echo 'not logged in';
return;
}

$user = (array)$user->user;

/*echo "<pre/>";
print_r($user);
die();*/
// the namespace is set in the Auth0 rule for
// adding app_metadata and user_metadata to the response
$namespace = $this->namespace;

if (!$user) {
die ('no user');
return false;
}

if (isset($user[$namespace . "user_metadata"])) {
$user["user_metadata"] = $user[$namespace . "user_metadata"];
unset($user[$namespace . "user_metadata"]);
Expand Down Expand Up @@ -184,20 +215,24 @@ public function callback()
throw new \Error("No member was found with the default emailaddress: $default_mailaddress");
}
}
/* echo "<pre/>";
print_r($user);
die();*/

self::updateUserData($user, false);


$this->redirect($redirect_to);
}

/**
* A function that for given email returns the auth0id can be used when initing this controller and than call this function
* This function is not exposed externally
*
* @param string email email
* @return mixed
*/

/**
* A function that for given email returns the auth0id can be used when initing this controller and than call this function
* This function is not exposed externally
*
* @param string email email
* @return mixed
*/
public function getIdByEmail(string $email)
{
$email_string = ':"' . urlencode($email) . '"';
Expand Down Expand Up @@ -534,10 +569,11 @@ private function setup($url = null)
try {
$this->auth0 = new Auth0([
'domain' => $this->domain,
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'redirect_uri' => $redirect,
'clientId' => $this->client_id,
'clientSecret' => $this->client_secret,
'redirectUri' => $redirect,
'scope' => $this->scope,
'cookieSecret' => $this->cookie_secret
]);
}
catch (\Auth0\SDK\Exception\CoreException $e) {
Expand Down

0 comments on commit 6a7388a

Please sign in to comment.