knpuniversity / oauth2-client-bundle

Easily talk to an OAuth2 server for social functionality in Symfony
https://symfonycasts.com
MIT License
786 stars 146 forks source link

Google get user_fields #291

Open mcgoode opened 3 years ago

mcgoode commented 3 years ago

I have attached the following additional fields I want to grab when authenticating a user as we want to use our google gsuite to manage users. When I set the following fields I get nothing from them in the GoogleAuthenticaor.php. Am I requesting the fields incorrectly or is this the incorrect way to get the additional fields?

GoogleAuthenticator.php


    /**
     * @inheritDoc
     */
    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        /** @var GoogleUser $googleUser */
        $googleUser = $this->getGoogleClient()
            ->fetchUserFromToken($credentials);

        dump($googleUser);

        $email = $googleUser->getEmail();

        $employee = $this->em->getRepository(Employee::class)
            ->findOneBy(['email' => $email]);

        dd($employee);
        if (!$employee) {
            $employee = new Employee();
            $employee->setEmail($googleUser->getEmail());
            $employee->setFirstName($googleUser->getFirstName());
            $employee->setLastName($googleUser->getLastName());
            $employee->setLastLoginDate(new DateTime('now'));
            $employee->set(new DateTime('now'));
            $this->em->persist($employee);
            $this->em->flush();
        }

        return $employee;
    }

The first dump give me...

GoogleAuthenticator.php on line 72:
League\OAuth2\Client\Provider\GoogleUser {#311 ▼
  #response: array:9 [▼
    "sub" => "103937093083488639449"
    "name" => "Chris Goode"
    "given_name" => "Chris"
    "family_name" => "Goode"
    "picture" => "https://lh4.googleusercontent.com/-rD0i_RAwOOo/AAAAAAAAAAI/AAAAAAAAAAA/AMZuuck2URdgdfcIhMl2aSdWa094_nzg-A/s96-c/photo.jpg"
    "email" => "chris@****.com"
    "email_verified" => true
    "locale" => "en"
    "hd" => "****.com"
  ]
}

the DD gives

null

knpu_oauth2_client.yaml

# Optional value for additional fields to be requested from the user profile. If set, these values will be included with the defaults. More details: https://developers.google.com/+/web/api/rest/latest/people
            user_fields:
                - id
                - kind
                - isAdmin
                - creationTime
                - orgUnitPath
                - externalIds
                - organizations
                - phones
mcgoode commented 3 years ago

also @weaverryan the link in the documentation is broken More details: https://developers.google.com/+/web/api/rest/latest/people I am unsure where it was supposed to go.

weaverryan commented 3 years ago

Hey Chris!

mhmm, I’m not sure... as this bundle is just that little in between layer. But first, about the broken link in the docs, I’m sure that DID work at one time. So something must have changed on Google’s end. It could be they moved some URLs... or that this functionality has dramatically changed.

Looking at the library we wrap - https://github.com/thephpleague/oauth2-google - the options they use vs the options we support look much different. That makes me think we may be still supporting an old version of that library or googles api in our bundle. Indeed, the code for the Google integration has barely been touched in 5 years.

tl;dr my guess is that our Google integration is totally outdated and someone needs to re-make it to match the REAL options of the underlying library.

I hope this... helps a bit :).

VincentLanglet commented 2 years ago

user_fields was removed here: https://github.com/thephpleague/oauth2-google/pull/65/files

So it should be remove from this bundle too.