-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from faouzic/feature/26106
[FEATURE#26106] Version 2
- Loading branch information
Showing
3 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace ETNA\Silex\Provider\Auth; | ||
|
||
use Silex\Application; | ||
use Silex\Api\BootableProviderInterface; | ||
use Pimple\ServiceProviderInterface; | ||
use Pimple\Container; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class Auth implements ServiceProviderInterface, , BootableProviderInterface | ||
{ | ||
private $auth_config; | ||
private $app; | ||
|
||
public function __construct($auth_config = null) | ||
{ | ||
$auth_config = $auth_config ?: [ | ||
"auth.api_path" => "^/?", | ||
"auth.force_guest" => true, | ||
"auth.cookie_expiration" => false, | ||
"auth.before_function" => [$this, 'authBeforeFunction'] | ||
]; | ||
$this->auth_config = $auth_config; | ||
|
||
$auth_url = getenv("AUTH_URL"); | ||
$auth_cookie_expiration = getenv("AUTH_COOKIE_EXPIRATION"); | ||
|
||
if (false === $auth_url) { | ||
throw new \Exception("AUTH_URL doesn't exist"); | ||
} | ||
|
||
$this->auth_config["auth.authenticator_url"] = $auth_url; | ||
if (false !== $auth_cookie_expiration) { | ||
// transforme la chaine 'false' reçu de l'env en booleen. | ||
$auth_cookie_expiration = ($auth_cookie_expiration === 'false') ? false : $auth_cookie_expiration; | ||
|
||
$this->auth_config["auth.cookie_expiration"] = $auth_cookie_expiration; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @{inherit doc} | ||
*/ | ||
public function register(Container $app) | ||
{ | ||
$this->app = $app; | ||
|
||
if (true !== isset($app["application_env"])) { | ||
throw new \Exception('$app["application_env"] is not set'); | ||
} | ||
|
||
if (true !== isset($app["application_name"])) { | ||
throw new \Exception('$app["application_name"] is not set'); | ||
} | ||
|
||
if (true !== isset($app["application_path"])) { | ||
throw new \Exception('$app["application_path"] is not set'); | ||
} | ||
|
||
$this->auth_config["auth.app_name"] = $app["application_name"]; | ||
|
||
foreach ($this->auth_config as $conf_name => $conf_value) { | ||
$app[$conf_name] = $conf_value; | ||
} | ||
|
||
$app["auth.public_key.tmp_path"] = "{$app['application_path']}/tmp/public-{$app['application_env']}.key"; | ||
$app->register(new AuthServiceProvider()); | ||
} | ||
|
||
public function authBeforeFunction(Request $req) | ||
{ | ||
// On autorise les OPTIONS sans auth | ||
if ('OPTIONS' === $req->getMethod()) { | ||
return; | ||
} | ||
|
||
if (!isset($req->user)) { | ||
return $this->app->json("Authorization Required", 401); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @{inherit doc} | ||
*/ | ||
public function boot(Application $app) | ||
{ | ||
$app->before($app['auth.before_function']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters