-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new components security-bundle and security-http (#58)
* Added new components security-bundle and security-http
- Loading branch information
0 parents
commit 8639e5b
Showing
31 changed files
with
1,121 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: Close Pull Request | ||
|
||
on: | ||
pull_request_target: | ||
types: [ opened ] | ||
|
||
jobs: | ||
run: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: superbrothers/close-pull-request@v3 | ||
with: | ||
comment: "Hi, this is a READ-ONLY repository, please submit your PR on the https://github.com/mineadmin/components repository.<br><br> This Pull Request will close automatically.<br><br> Thanks! " |
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,24 @@ | ||
on: | ||
push: | ||
tags: | ||
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 | ||
|
||
name: Release | ||
|
||
jobs: | ||
release: | ||
name: Release | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
- name: Create Release | ||
id: create_release | ||
uses: actions/create-release@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
tag_name: ${{ github.ref }} | ||
release_name: Release ${{ github.ref }} | ||
draft: false | ||
prerelease: false |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 MineAdmin | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,3 @@ | ||
# Hyperf Security Bundle | ||
|
||
像 Symfony/security 那样提供用户认证、授权、协程/请求安全上下文等功能 |
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,38 @@ | ||
{ | ||
"name": "mineadmin/security-bundle", | ||
"description": "MineAdmin Security bundle,类似 Symfony/Security 组件,提供用户认证、授权、安全上下文等功能。", | ||
"license": "MIT", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "xmo", | ||
"email": "[email protected]", | ||
"role": "Developer" | ||
}, | ||
{ | ||
"name": "zds", | ||
"email": "[email protected]", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php": ">=8.1", | ||
"hyperf/framework": "^3.1", | ||
"friendsofhyperf/encryption": "^3.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Mine\\SecurityBundle\\": "src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Mine\\SecurityBundle\\Tests\\": "tests" | ||
} | ||
}, | ||
"extra": { | ||
"hyperf": { | ||
"config": "Mine\\SecurityBundle\\ConfigProvider" | ||
} | ||
} | ||
} |
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,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\SecurityBundle; | ||
|
||
use Hyperf\Database\Model\Builder; | ||
use Mine\SecurityBundle\Contract\UserInterface; | ||
use Mine\SecurityBundle\Contract\UserProviderInterface; | ||
use Mine\SecurityBundle\Event\Login; | ||
use Mine\SecurityBundle\Event\Validated; | ||
use Mine\SecurityBundle\Event\Verified; | ||
use Mine\SecurityBundle\Exception\NotFoundUserEntityException; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
|
||
use function Hyperf\Support\value; | ||
|
||
abstract class AbstractUserProvider implements UserProviderInterface | ||
{ | ||
public function __construct( | ||
private readonly EventDispatcherInterface $dispatcher, | ||
private readonly Config $config | ||
) {} | ||
|
||
public function retrieveByToken(string $token): ?object | ||
{ | ||
return value(function (Builder $builder, UserInterface $user, string $token) { | ||
return $builder->where($user->getRememberTokenName(), $token)->first(); | ||
}, $this->getUserEntity()->getSecurityBuilder(), $this->getUserEntity(), $token); | ||
} | ||
|
||
public function updateRememberToken(UserInterface $user, string $token): bool | ||
{ | ||
return value(function (Builder $builder, UserInterface $user, string $token) { | ||
return $builder->update([ | ||
$user->getRememberTokenName() => $token, | ||
]); | ||
}, $user->getSecurityBuilder(), $user, $token); | ||
} | ||
|
||
public function retrieveById(mixed $identifier): ?object | ||
{ | ||
return value( | ||
function (Builder $builder, UserInterface $entity, mixed $identifier) { | ||
return $builder->where($entity->getIdentifierName(), $identifier)->first(); | ||
}, | ||
$this->getUserEntity()->getSecurityBuilder(), | ||
$this->getUserEntity(), | ||
$identifier | ||
); | ||
} | ||
|
||
public function credentials(array $credentials): false|UserInterface | ||
{ | ||
$userEntity = $this->getUserEntity(); | ||
$builder = $userEntity->getSecurityBuilder(); | ||
$identifierName = $userEntity->getIdentifierName(); | ||
if (isset($credentials[$identifierName])) { | ||
/** | ||
* @var UserInterface $entity | ||
*/ | ||
$entity = $builder->where($identifierName, $credentials[$identifierName])->first(); | ||
if ($entity === null) { | ||
return false; | ||
} | ||
if ($this->verifyPassword($entity, $credentials['password'])) { | ||
$this->dispatcher->dispatch(new Login($entity)); | ||
return $entity; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
protected function verifyPassword(UserInterface $user, string $password): bool | ||
{ | ||
if (password_verify($password, $user->getPassword())) { | ||
$this->dispatcher->dispatch(new Verified($user)); | ||
return true; | ||
} | ||
$this->dispatcher->dispatch(new Validated($user)); | ||
return false; | ||
} | ||
|
||
protected function getUserEntity(): UserInterface | ||
{ | ||
$entityClass = $this->config->get('entity', '\\App\\Model\\User'); | ||
if (! class_exists($entityClass)) { | ||
new NotFoundUserEntityException(); | ||
} | ||
return new $entityClass(); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\SecurityBundle; | ||
|
||
use Hyperf\Contract\ConfigInterface; | ||
|
||
class Config | ||
{ | ||
public const PREFIX = 'security'; | ||
|
||
public function __construct( | ||
private readonly ConfigInterface $config | ||
) {} | ||
|
||
public function get(string $key, mixed $default = null): mixed | ||
{ | ||
return $this->config->get(self::PREFIX . '.' . $key, $default); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\SecurityBundle; | ||
|
||
class ConfigProvider | ||
{ | ||
public function __invoke(): array | ||
{ | ||
return []; | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\SecurityBundle\Context; | ||
|
||
use Hyperf\Context\Context as CRT; | ||
use Mine\SecurityBundle\Contract\ContextInterface; | ||
|
||
class Context implements ContextInterface | ||
{ | ||
public const CONTEXT_PREFIX = 'mine.security.context'; | ||
|
||
public function get(string $name, mixed $default = null): mixed | ||
{ | ||
return CRT::get(self::CONTEXT_PREFIX . '.' . $name, $default); | ||
} | ||
|
||
public function has(string $name): bool | ||
{ | ||
return CRT::has(self::CONTEXT_PREFIX . '.' . $name); | ||
} | ||
|
||
public function set(string $name, mixed $value): void | ||
{ | ||
CRT::set(self::CONTEXT_PREFIX . '.' . $name, $value); | ||
} | ||
|
||
public function getOrSet(string $name, mixed $callable): mixed | ||
{ | ||
return CRT::getOrSet(self::CONTEXT_PREFIX . '.' . $name, $callable); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\SecurityBundle\Contract; | ||
|
||
interface ContextInterface | ||
{ | ||
public function get(string $name, mixed $default = null): mixed; | ||
|
||
public function has(string $name): bool; | ||
|
||
public function set(string $name, mixed $value): void; | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\SecurityBundle\Contract; | ||
|
||
interface TokenInterface | ||
{ | ||
public function user(...$params): ?UserInterface; | ||
} |
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\SecurityBundle\Contract; | ||
|
||
use Hyperf\Database\Model\Builder; | ||
|
||
interface UserInterface | ||
{ | ||
public function getIdentifier(): string; | ||
|
||
public function getIdentifierName(): string; | ||
|
||
public function getRememberToken(): string; | ||
|
||
public function setRememberToken(string $token): void; | ||
|
||
public function getRememberTokenName(): string; | ||
|
||
public function getPassword(): string; | ||
|
||
public function setPassword(string $password): void; | ||
|
||
public function getSecurityBuilder(): Builder; | ||
|
||
public function setAttribute(string $key, mixed $value); | ||
|
||
public function getAttributes(): array; | ||
} |
Oops, something went wrong.