Skip to content

Commit bfbcbbd

Browse files
authored
Merge pull request #1 from sashaaro/master
base implementation
2 parents a2c37c3 + aa9d53a commit bfbcbbd

21 files changed

+720
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
vendor
3+
composer.lock

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# flexauth
2-
FlexAuth: Framework independent library
2+
FlexAuth: independent library for symfony security
3+
4+
Allows switching the `UserProvider` at Runtime using environment variables.

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "linkorb/flex-auth",
3+
"description": "This libraries provides flex auth for symfony security in runtime",
4+
"keywords": ["auth"],
5+
"license": "MIT",
6+
"autoload":{
7+
"psr-4":{
8+
"FlexAuth\\": "src/"
9+
}
10+
},
11+
"require": {
12+
"php": "^7.0",
13+
"symfony/security": "^4.0"
14+
},
15+
"require-dev": {
16+
},
17+
"suggest": {
18+
"userbase/client": "Required for support userbase type format",
19+
"symfony/doctrine-bridge": "Required for support flex-auth entity type format. Prefer ^4.0",
20+
"symfony/http-foundation": "Required for support flex-auth jwt type format. Prefer ^4.0",
21+
"firebase/php-jwt": "Required for support flex-auth jwt type format. Prefer ^5.0"
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace FlexAuth;
4+
5+
/**
6+
* Class AuthFlexTypeCallbackProvider
7+
* @author Aleksandr Arofikin <sashaaro@gmail.com>
8+
*/
9+
class AuthFlexTypeCallbackProvider implements AuthFlexTypeProviderInterface
10+
{
11+
/** @var callable */
12+
private $callback;
13+
14+
public function __construct(callable $callback)
15+
{
16+
$this->callback = $callback;
17+
}
18+
19+
public function provide(): array
20+
{
21+
return call_user_func($this->callback);
22+
}
23+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace FlexAuth;
4+
5+
/**
6+
* Class AuthFlexTypeProviderFactory
7+
* @author Aleksandr Arofikin <sashaaro@gmail.com>
8+
*/
9+
class AuthFlexTypeProviderFactory
10+
{
11+
public static function fromEnv(string $envVar)
12+
{
13+
return new AuthFlexTypeCallbackProvider(function () use($envVar) {
14+
return self::resolveParamsFromEnv($envVar);
15+
});
16+
}
17+
18+
public static function resolveParamsFromEnv($envVar)
19+
{
20+
if (!array_key_exists($envVar, $_ENV)) {
21+
throw new \Exception(sprintf('Env variable "%s" is not found', $envVar));
22+
}
23+
$type = $_ENV[$envVar];
24+
25+
try {
26+
$params = self::resolveParamsFromLine($type);
27+
} catch (\InvalidArgumentException $e) {
28+
$params = [];
29+
foreach ($_ENV as $key => $value) {
30+
if (strpos($key, $envVar.'_') === 0) {
31+
$paramKey = substr($key, 0, strlen($key.'_'));
32+
$params[strtolower($paramKey)] = $value;
33+
}
34+
}
35+
$params['type'] = $type;
36+
}
37+
38+
return $params;
39+
}
40+
41+
/**
42+
* @param $line string Type and params as string in format type?param1=value1&param2=value2
43+
* @example entity?class=\App\Entities\User&property=username
44+
* @example userbase?dsn=https://username:password@userbase.example.com
45+
*
46+
* @return array
47+
*/
48+
public static function resolveParamsFromLine(string $line): array {
49+
$parts = [];
50+
preg_match('/([A-Z0-9_]+)\?((.|\n)+)/i', $line , $parts);
51+
52+
if (!array_key_exists(2, $parts)) {
53+
throw new \InvalidArgumentException();
54+
}
55+
56+
$stringParams = $parts[2];
57+
foreach (explode("&", $stringParams) as $keyValue) {
58+
[$key, $value] = explode("=", $keyValue);
59+
if ($key && $value) {
60+
$params[$key] = $value;
61+
}
62+
}
63+
$params['type'] = $parts[1];
64+
65+
return $params;
66+
}
67+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace FlexAuth;
4+
5+
/**
6+
* Interface AuthFlexTypeProviderInterface
7+
* @author Aleksandr Arofikin <sashaaro@gmail.com>
8+
*/
9+
interface AuthFlexTypeProviderInterface
10+
{
11+
public function provide(): array;
12+
}

src/Security/FlexUserProvider.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace FlexAuth\Security;
4+
5+
use FlexAuth\UserProviderFactory;
6+
use Symfony\Component\Security\Core\User\UserInterface;
7+
use Symfony\Component\Security\Core\User\UserProviderInterface;
8+
9+
/**
10+
* Class FlexUserProvider
11+
* @author Aleksandr Arofikin <sashaaro@gmail.com>
12+
*/
13+
class FlexUserProvider implements UserProviderInterface
14+
{
15+
/** @var UserProviderFactory */
16+
protected $userProviderFactory;
17+
18+
public function __construct(UserProviderFactory $userProviderFactory)
19+
{
20+
$this->userProviderFactory = $userProviderFactory;
21+
}
22+
23+
public function loadUserByUsername($username)
24+
{
25+
return $this->userProviderFactory->create()->loadUserByUsername($username);
26+
}
27+
28+
public function refreshUser(UserInterface $user)
29+
{
30+
return $this->userProviderFactory->create()->refreshUser($user);
31+
}
32+
33+
public function supportsClass($class)
34+
{
35+
return $this->userProviderFactory->create()->supportsClass($class);
36+
}
37+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace FlexAuth\Type\Entity;
4+
5+
use Doctrine\Common\Persistence\ManagerRegistry;
6+
use FlexAuth\Type\UserProviderFactoryInterface;
7+
use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
8+
9+
/**
10+
* Class EntityUserProviderFactory
11+
* @author Aleksandr Arofikin <sashaaro@gmail.com>
12+
*/
13+
class EntityUserProviderFactory implements UserProviderFactoryInterface
14+
{
15+
const TYPE = 'entity';
16+
17+
private $managerRegistry;
18+
19+
public function __construct(ManagerRegistry $managerRegistry)
20+
{
21+
$this->managerRegistry = $managerRegistry;
22+
}
23+
24+
public function create($params)
25+
{
26+
return new EntityUserProvider($this->managerRegistry, $params['class'], $params['property']);
27+
}
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace FlexAuth\Type;
4+
5+
/**
6+
* Class InvalidParamsException
7+
* @author Aleksandr Arofikin <sashaaro@gmail.com>
8+
*/
9+
class InvalidParamsException extends \Exception
10+
{
11+
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace FlexAuth\Type\JWT;
4+
5+
use Symfony\Component\Security\Core\User\User;
6+
use Symfony\Component\Security\Core\User\UserInterface;
7+
8+
/**
9+
* Class DefaultJWTUserFactory
10+
* @author Aleksandr Arofikin <sashaaro@gmail.com>
11+
*/
12+
class DefaultJWTUserFactory implements JWTUserFactoryInterface
13+
{
14+
public function createFromPayload($payload): UserInterface
15+
{
16+
return new User($payload['username'], null, $payload['roles']);
17+
}
18+
}

0 commit comments

Comments
 (0)