Yii2: HttpBasicAuth broke Session

Created on 17 Feb 2018  路  15Comments  路  Source: yiisoft/yii2

Using HttpBasicAuth makes Session unusable, because PHPSESSIONID regenerates to new every request. Authentication is working fine, but session become empty.

Example, adding auth behavior.

class Sync1CModule extends Module {
    public function behaviors() {
        return [
            'basicAuth' => [
                'class' => HttpBasicAuth::class,
                'auth' => function($username, $password) {
                              ... 
                }
            ]
        ];
    }
}

The reason is in yii\web\User::switchIdentity()

    public function switchIdentity($identity, $duration = 0)
    {
         .................
        $session = Yii::$app->getSession();
        if (!YII_ENV_TEST) {
            $session->regenerateID(true);
        }
       ..................
}

| Q | A
| ---------------- | ---
| Yii version | 2.0.13
| PHP version | 7.0
| Operating system | Debian 8

bug

All 15 comments

                'auth' => function($username, $password) {
                              ... 
                }

What's inside?

Not matter.... strcmp username and password.

 $session = Yii::$app->getSession();
        if (!YII_ENV_TEST) {
            $session->regenerateID(true);
        }

destruct session anytime.

Not matter.... strcmp username and password

No, it matters. Please, post the code here

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

_This is an automated comment, triggered by adding the label status:need more info._

Ok :))))))

$user = UserModel::find()->where(['email' => $username])->one();
return !empty($user) && $user->validatePassword($password) ? $user : null;

So usermodel returned ok, authentication done, so how it prevent yii\web\User from

if (!YII_ENV_TEST) {
            $session->regenerateID(true);
        }

My workaround is to set User::$enableSession to false.

Would you please check if it works with 2.0.13.1?

Already have 2.0.13.1

define('YII_ENV_TEST', true) is also workaround :)))

Would you then please try code from master branch?

ok, 15 minutes, please ...

same result, PHPSESSIONID changes every request.
To resolve this problem temporary I have to turn session off:

\Yii::$app->user->enableSession = false;

Thank you for the report, fixed. Please, try code in master branch to confirm

@dicrtarasov did the fix solve your problem?

Thank you very mutch. Your modification of User component completely fix this problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jpodpro picture jpodpro  路  3Comments

Locustv2 picture Locustv2  路  3Comments

newscloud picture newscloud  路  3Comments

psfpro picture psfpro  路  3Comments

schmunk42 picture schmunk42  路  3Comments