Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code style & docblocks #56

Merged
merged 7 commits into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ This plugin helps you integrate your Laravel WebApp with [Auth0](https://auth0.c

## Installation

Check our docs page to get a complete guide on how to install it in an existing project or download a pre configured seedproject:
Check our docs page to get a complete guide on how to install it in an existing project or download a pre-configured seed project:

* Regular webapp: https://auth0.com/docs/quickstart/webapp/laravel/
* Web API: https://auth0.com/docs/quickstart/backend/php-laravel/
Expand Down
74 changes: 50 additions & 24 deletions src/Auth0/Login/Auth0JWTUser.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
<?php namespace Auth0\Login;
<?php

namespace Auth0\Login;

/**
* This class represents a generic user initialized with the user information
* given by Auth0 and provides a way to access to the decoded JWT data.
*
*/
class Auth0JWTUser implements \Illuminate\Contracts\Auth\Authenticatable {

class Auth0JWTUser implements \Illuminate\Contracts\Auth\Authenticatable
{
private $userInfo;

function __construct ($userInfo) {
/**
* Auth0JWTUser constructor.
*
* @param $userInfo
*/
public function __construct($userInfo)
{
$this->userInfo = get_object_vars($userInfo);
}

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifierName() {
public function getAuthIdentifierName()
{
return $this->userInfo['sub'];
}

/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier() {
public function getAuthIdentifier()
{
return $this->userInfo['sub'];
}

Expand All @@ -36,42 +45,59 @@ public function getAuthIdentifier() {
*
* @return string
*/
public function getAuthPassword() {
return null;
public function getAuthPassword()
{
return;
}

public function getRememberToken() {
return null;
/**
*/
public function getRememberToken()
{
return;
}

public function setRememberToken($value) {

/**
* @param $value
*/
public function setRememberToken($value)
{
}

public function getRememberTokenName() {
return null;
/**
*/
public function getRememberTokenName()
{
return;
}

/**
* Add a generic getter to get all the properties of the userInfo
* Add a generic getter to get all the properties of the userInfo.
*
* @return the related value or null if it is not set
*/
public function __get($name) {

public function __get($name)
{
if (!array_key_exists($name, $this->userInfo)) {
return null;
return;
}

return $this->userInfo[$name];
}

public function getUserInfo() {
/**
* @return array
*/
public function getUserInfo()
{
return $this->userInfo;
}

public function __toString() {
/**
* @return string
*/
public function __toString()
{
return json_encode($this->userInfo);
}

}
79 changes: 54 additions & 25 deletions src/Auth0/Login/Auth0Service.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Auth0\Login;
<?php

namespace Auth0\Login;

use Config;
use Auth0\SDK\API\Authentication;
Expand All @@ -8,18 +10,21 @@
/**
* Service that provides access to the Auth0 SDK.
*/
class Auth0Service {
class Auth0Service
{
private $auth0;
private $apiuser;
private $_onLoginCb = null;
private $rememberUser = false;

/**
* Creates an instance of the Auth0 SDK using
* the config set in the laravel way and using a LaravelSession
* as a store mechanism
* as a store mechanism.
*/
private function getSDK()
{
if (is_null($this->auth0))
{
if (is_null($this->auth0)) {
$auth0Config = config('laravel-auth0');

$auth0Config['store'] = new LaravelSessionStore();
Expand All @@ -28,72 +33,93 @@ private function getSDK()

$this->auth0 = $auth0->get_oauth_client($auth0Config['client_secret'], $auth0Config['redirect_uri']);
}
return $this->auth0;

return $this->auth0;
}

/**
* Logs the user out from the SDK.
*/
public function logout() {
public function logout()
{
$this->getSDK()->logout();
}

/**
* If the user is logged in, returns the user information
* If the user is logged in, returns the user information.
*
* @return array with the User info as described in https://docs.auth0.com/user-profile and the user access token
*/
public function getUser() {
public function getUser()
{
// Get the user info from auth0
$auth0 = $this->getSDK();
$user = $auth0->getUser();

if ($user === null) return null;
if ($user === null) {
return;
}

return [
'profile' => $user,
'accessToken' => $auth0->getAccessToken()
'accessToken' => $auth0->getAccessToken(),
];
}

private $_onLoginCb = null;
/**
* Sets a callback to be called when the user is logged in
* @param callback $cb A function that receives an auth0User and receives a Laravel user
* Sets a callback to be called when the user is logged in.
*
* @param callback $cb A function that receives an auth0User and receives a Laravel user
*/
public function onLogin($cb) {
public function onLogin($cb)
{
$this->_onLoginCb = $cb;
}

public function hasOnLogin () {
/**
* @return bool
*/
public function hasOnLogin()
{
return $this->_onLoginCb !== null;
}

public function callOnLogin($auth0User) {
/**
* @param $auth0User
*
* @return mixed
*/
public function callOnLogin($auth0User)
{
return call_user_func($this->_onLoginCb, $auth0User);
}

private $rememberUser = false;
/**
* Use this to either enable or disable the "remember" function for users
* Use this to either enable or disable the "remember" function for users.
*
* @param null $value
*
* @return bool|null
*/
public function rememberUser($value = null) {
if($value !== null){
public function rememberUser($value = null)
{
if ($value !== null) {
$this->rememberUser = $value;
}

return $this->rememberUser;
}

private $apiuser;
/**
* @param $encUser
*
* @return mixed
*/
public function decodeJWT($encUser)
{
try {
$cache = \App::make('\Auth0\SDK\Helpers\Cache\CacheHandler');
} catch(BindingResolutionException $e) {
} catch (BindingResolutionException $e) {
$cache = null;
}

Expand All @@ -116,8 +142,11 @@ public function decodeJWT($encUser)
return $this->apiuser;
}


public function jwtuser() {
/**
* @return mixed
*/
public function jwtuser()
{
return $this->apiuser;
}
}
Loading