Skip to content
PHP Ecosystem
Explore

HttpBasicAuth

API reference › yii › filters › auth › HttpBasicAuth

HttpBasicAuth is an action filter that supports the HTTP Basic authentication method.

You may use HttpBasicAuth by attaching it as a behavior to a controller or module, like the following:

public function behaviors()
{
return [
'basicAuth' => [
'class' => \yii\filters\auth\HttpBasicAuth::class,
],
];
}

The default implementation of HttpBasicAuth uses the loginByAccessToken() method of the user application component and only passes the user name. This implementation is used for authenticating API clients.

If you want to authenticate users using username and password, you should provide the $auth function for example like the following:

public function behaviors()
{
return [
'basicAuth' => [
'class' => \yii\filters\auth\HttpBasicAuth::class,
'auth' => function ($username, $password) {
$user = User::find()->where(['username' => $username])->one();
if ($user && $user->validatePassword($password)) {
return $user;
}
return null;
},
],
];
}

Tip: In case authentication does not work like expected, make sure your web server passes username and password to $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] variables. If you are using Apache with PHP-CGI, you might need to add this line to your .htaccess file:

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
PropertyTypeDescriptionDefined by
$authcallable|nullA PHP callable that will authenticate the user with the HTTP basic auth information. Default: null.HttpBasicAuth
$exceptarrayList of action IDs that this filter should not apply to. Default: [].yii\base\ActionFilter
$onlyarrayList of action IDs that this filter should apply to. Default: [].yii\base\ActionFilter
$optionalarrayList of action IDs that this filter will be applied to, but auth failure will not lead to error. Default: []. Available since 2.0.7.AuthMethod
$owneryii\base\T|nullThe owner of this behavior Default: null.yii\base\Behavior
$realmstringThe HTTP authentication realm Default: 'api'.HttpBasicAuth
$requestyii\web\Request|nullThe current request. Default: null.AuthMethod
$responseyii\web\Response|nullThe response to be sent. Default: null.AuthMethod
$useryii\web\User|nullThe user object representing the user authentication status. Default: null.AuthMethod
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
afterAction()This method is invoked right after an action is executed.yii\base\ActionFilter
afterFilter()yii\base\ActionFilter
attach()Attaches the behavior object to the component.yii\base\ActionFilter
authenticate()Authenticates the current user.HttpBasicAuth
beforeAction()This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.AuthMethod
beforeFilter()yii\base\ActionFilter
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
challenge()Generates challenges upon authentication failure.HttpBasicAuth
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
detach()Detaches the behavior object from the component.yii\base\ActionFilter
events()Declares event handlers for the $owner’s events.yii\base\Behavior
handleFailure()Handles authentication failure.AuthMethod
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
getActionId() protectedReturns an action ID by converting Action::$uniqueId into an ID relative to the module.yii\base\ActionFilter
isActive() protectedReturns a value indicating whether the filter is active for the given action.yii\base\ActionFilter
isOptional() protectedChecks, whether authentication is optional for the given action.AuthMethod
public callable|null $auth = null

A PHP callable that will authenticate the user with the HTTP basic auth information.

The callable receives a username and a password as its parameters. It should return an identity object that matches the username and password. Null should be returned if there is no such identity. The callable will be called only if current user is not authenticated.

The following code is a typical implementation of this callable:

function ($username, $password) {
return \app\models\User::findOne([
'username' => $username,
'password' => $password,
]);
}

If this property is not set, the username information will be considered as an access token while the password information will be ignored. The User::loginByAccessToken() method will be called to authenticate and login the user.

public function authenticate(mixed $user, mixed $request, mixed $response): yii\web\IdentityInterface|null

Authenticates the current user.

Parameters

ParameterTypeDescription
$usermixed
$requestmixed
$responsemixed

Returns

yii\web\IdentityInterface|null: The authenticated user identity. If authentication information is not provided, null will be returned.

Throws

public function challenge(mixed $response): mixed

Generates challenges upon authentication failure.

For example, some appropriate HTTP headers may be generated.

Parameters

ParameterTypeDescription
$responsemixed

Returns

mixed