-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '4.13' of https://github.com/craftcms/cms into 5.5
# Conflicts: # CHANGELOG-WIP.md
- Loading branch information
Showing
6 changed files
with
175 additions
and
1 deletion.
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
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,57 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\filters; | ||
|
||
use Craft; | ||
use craft\elements\User; | ||
use yii\filters\auth\HttpBasicAuth; | ||
use yii\web\IdentityInterface; | ||
|
||
/** | ||
* Filter for adding basic HTTP authentication user credentials to site requests. | ||
* | ||
* @see https://www.yiiframework.com/doc/api/2.0/yii-filters-auth-httpbasicauth | ||
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com> | ||
* @since 5.5.0 | ||
*/ | ||
class BasicHttpAuthLogin extends HttpBasicAuth | ||
{ | ||
use SiteFilterTrait, BasicHttpAuthTrait; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public $realm; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function __construct($config = []) | ||
{ | ||
parent::__construct($config + [ | ||
'auth' => [$this, 'auth'], | ||
'realm' => Craft::$app->getSystemName(), | ||
]); | ||
} | ||
|
||
protected function auth($username, $password): ?IdentityInterface | ||
{ | ||
if (!$username || !$password) { | ||
return null; | ||
} | ||
|
||
$user = User::find()->username($username)->one(); | ||
$identity = $user?->findIdentity($user->id); | ||
|
||
if ($identity && Craft::$app->getSecurity()->validatePassword($password, $identity->password)) { | ||
return $identity; | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\filters; | ||
|
||
use Craft; | ||
use craft\helpers\App; | ||
use yii\base\InvalidConfigException; | ||
use yii\filters\auth\HttpBasicAuth; | ||
|
||
/** | ||
* Filter for adding basic HTTP authentication with static credentials to site requests. | ||
* | ||
* @see https://www.yiiframework.com/doc/api/2.0/yii-filters-auth-httpbasicauth | ||
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com> | ||
* @since 5.5.0 | ||
*/ | ||
class BasicHttpAuthStatic extends HttpBasicAuth | ||
{ | ||
use SiteFilterTrait, BasicHttpAuthTrait; | ||
|
||
public ?string $username = null; | ||
public ?string $password = null; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public $realm; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function __construct($config = []) | ||
{ | ||
parent::__construct($config + [ | ||
'username' => App::env('CRAFT_HTTP_BASIC_AUTH_USERNAME'), | ||
'password' => App::env('CRAFT_HTTP_BASIC_AUTH_PASSWORD'), | ||
'realm' => Craft::$app->getSystemName(), | ||
]); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function beforeAction($action): bool | ||
{ | ||
if (!$this->username || !$this->password) { | ||
throw new InvalidConfigException('Basic authentication is not configured.'); | ||
} | ||
|
||
$currentUser = Craft::$app->getUser()->getIdentity(); | ||
|
||
if ($currentUser) { | ||
return true; | ||
} | ||
|
||
list($username, $password) = Craft::$app->getRequest()->getAuthCredentials(); | ||
|
||
if ($username === $this->username && $password === $this->password) { | ||
return true; | ||
} | ||
|
||
$response = Craft::$app->getResponse(); | ||
$this->challenge($response); | ||
$this->handleFailure($response); | ||
|
||
return 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,33 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\filters; | ||
|
||
use Craft; | ||
use yii\web\UnauthorizedHttpException; | ||
|
||
/** | ||
* Trait BasicHttpAuthTrait | ||
* | ||
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com> | ||
* @since 5.5.0 | ||
*/ | ||
trait BasicHttpAuthTrait | ||
{ | ||
/** | ||
* Detach behavior and manually handle exception so error handling | ||
* isn't called recursively when already handling an exception (e.g. 404s) | ||
*/ | ||
public function handleFailure($response): void | ||
{ | ||
$this->detach(); | ||
|
||
Craft::$app->getErrorHandler()->handleException( | ||
new UnauthorizedHttpException('Your request was made with invalid credentials.'), | ||
); | ||
} | ||
} |
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