Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpBasicAuth broke Session #15658

Closed
dicrtarasov opened this issue Feb 17, 2018 · 15 comments
Closed

HttpBasicAuth broke Session #15658

dicrtarasov opened this issue Feb 17, 2018 · 15 comments
Assignees
Labels
Milestone

Comments

@dicrtarasov
Copy link
Contributor

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
@SilverFire
Copy link
Member

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

What's inside?

@dicrtarasov
Copy link
Contributor Author

Not matter.... strcmp username and password.

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

destruct session anytime.

@SilverFire
Copy link
Member

Not matter.... strcmp username and password

No, it matters. Please, post the code here

@yii-bot
Copy link

yii-bot commented Feb 17, 2018

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.

@dicrtarasov
Copy link
Contributor Author

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.

@samdark
Copy link
Member

samdark commented Feb 17, 2018

Would you please check if it works with 2.0.13.1?

@dicrtarasov
Copy link
Contributor Author

Already have 2.0.13.1

@dicrtarasov
Copy link
Contributor Author

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

@samdark
Copy link
Member

samdark commented Feb 17, 2018

Would you then please try code from master branch?

@dicrtarasov
Copy link
Contributor Author

ok, 15 minutes, please ...

@dicrtarasov
Copy link
Contributor Author

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

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

@dicrtarasov
Copy link
Contributor Author

@SilverFire SilverFire self-assigned this Feb 17, 2018
@SilverFire SilverFire added the type:bug Bug label Feb 17, 2018
@SilverFire SilverFire added this to the 2.0.14 milestone Feb 17, 2018
@SilverFire
Copy link
Member

SilverFire commented Feb 17, 2018

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

@SilverFire
Copy link
Member

@dicrtarasov did the fix solve your problem?

@dicrtarasov
Copy link
Contributor Author

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

mikehaertl added a commit to mikehaertl/yii2 that referenced this issue Aug 10, 2021
mikehaertl added a commit to mikehaertl/yii2 that referenced this issue Aug 10, 2021
bizley pushed a commit that referenced this issue Aug 11, 2021
#18649)

* Issue #18646 Cleanup auth data from session if findIdentity() returns null

* Issue #18646 Refactor fix to remove stale identity data from session

* Issue #18646 Fix test for HttpBasicAuth (#15658)

Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
Co-authored-by: Bizley <pawel@positive.codes>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants