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

RE: Got Access Token but auth fails. #11

Closed
Methraen opened this issue Sep 16, 2022 · 5 comments
Closed

RE: Got Access Token but auth fails. #11

Methraen opened this issue Sep 16, 2022 · 5 comments

Comments

@Methraen
Copy link

When I try to authenticate I get UserBadge with Oauth client_public_id instead of getting user's identifier (email in my case) so auth fails... any ideas (with v4.0.3)

probably related to #10

@michel1011
Copy link

michel1011 commented Sep 22, 2022

I got the same issue. It seems to give the public_id instead of the email of the user. (I'm using the password grant)
And I'm using version 3.1.2 because of Symfony 5.4 LTS.

@drekinov
Copy link

drekinov commented Oct 25, 2022

Symfony 5.4 and v3 of current package/fork
i have some success which is not fully tested but seems to work.

Override Security/Authenticator/Oauth2Authenticator.php with service container

<service id="fos_oauth_server.security.authenticator.manager" class="\App\Infrastructure\Symfony\Security\Oauth2Authenticator"></service>

then \App\Infrastructure\Symfony\Security\Oauth2Authenticator: copy package Oauth2Authenticator and make following changes:

# public function authenticate
return new SelfValidatingPassport(
                new UserBadge($client->getUserIdentifier(), function () use ($client) {
                    return $client;
                }), [$accessTokenBadge]
            )

public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
    {
        /** @var AccessTokenBadge $accessTokenBadge */
        $accessTokenBadge = $passport->getBadge(AccessTokenBadge::class);
        $token = new OAuthToken($accessTokenBadge->getRoles());
        $token->setToken($accessTokenBadge->getAccessToken()->getToken());
        $token->setAuthenticated(true);
        $user = $accessTokenBadge->getAccessToken()->getUser();
        if ($user === null) {
            $user = new ClientCredentialsDummyUser();
        }
        $token->setUser($user);

        return $token;
    }

ClientCredentialsDummyUser is empty class implementing Symfony\Component\Security\Core\User\UserInterface

i should mention that solution is partly inspired by thephpleague/oauth2-server-bundle and solution for Symfony 5.4 compatibility with FOSOAuthServerBundle

@BladeMF
Copy link

BladeMF commented Dec 8, 2022

It's better for the createAuthenticatedToken to be like this:

	public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
	{
		/** @var AccessTokenBadge $accessTokenBadge */
		$accessTokenBadge = $passport->getBadge(AccessTokenBadge::class);
		$token = new OAuthToken($accessTokenBadge->getRoles());
		$token->setToken($accessTokenBadge->getAccessToken()->getToken());
		if (!empty($user = $accessTokenBadge->getAccessToken()->getUser())) {
			$token->setUser($user);
		}

		return $token;
	}

The user in the token should be the authenticated user and not the OAuthClient.

@Methraen Methraen closed this as not planned Won't fix, can't repro, duplicate, stale Dec 13, 2022
@budby
Copy link

budby commented Jan 23, 2023

@Methraen Hi! We also faced this problem. But we don't understand why getUserIdentifier() is taken from the client and not from the user... don't want to do another fork to fix this.

@drekinov
Copy link

drekinov commented Mar 9, 2023

as addition with Symfony 6.x
createAuthenticatedToken should be renamed as createToken
it seems working but token is not OauthToken but default PostAuthenticatedToken and as side effect you loose access to token string and etc after authenication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants