2amigos/yii2-usuario

HOW TO: Linkedin Auth with OpenID?

Closed this issue · 1 comments

Hello,

I noticed that LinkedIn has transitioned to OpenID Connect Authentication, as detailed in their update here.

I'm curious to know if it's possible to integrate this new authentication method with the yii2-usuario extension. Additionally, I see that OpenID Connect is supported in Yii2 through the yii2-authclient extension, as mentioned in the documentation.

Could you provide any guidance on how to implement this with yii2-usuario?

Thank you in advance for your assistance!

Best,
Tobi

ok, here is the solution of 09/2024:

extend end customize the LinkedIn auth client:

<?php

namespace app\components\authclient;

use yii\web\HttpException;
use Yii;

class LinkedIn extends \Da\User\AuthClient\LinkedIn
{

    public $attributeNames = [
        'id',
        'firstName',
        'lastName',
    ];


    /**
     * {@inheritdoc}
     */
    public function init()
    {
        parent::init();
        if ($this->scope === null) {
            $this->scope = implode(' ', [
                'openid',
                'profile',
                'email',
            ]);
        }
    }

    /**
     * {@inheritdoc}
     */
    protected function defaultNormalizeUserAttributeMap()
    {

        return [
            'client_id' => function ($attributes) {
                return $attributes['sub'];
            },
            'first_name' => function ($attributes) {
                return $attributes['given_name'];
            },
            'last_name' => function ($attributes) {
                return $attributes['family_name'];
            },

        ];
    }

    /**
     * {@inheritdoc}
     */
    protected function initUserAttributes()
    {
        $attributes = $this->api('userinfo?projection=(' . implode(',', $this->attributeNames) . ')', 'GET');

        return $this->api('userinfo', 'GET');
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function applyAccessTokenToRequest($request, $accessToken)
    {
        $data = $request->getData();
        $data['oauth2_access_token'] = $accessToken->getToken();
        $request->setData($data);
    }


    public function getUserId()
    {
        $attributes = $this->getUserAttributes();
        return $attributes['sub'];
    }

    /**
     * {@inheritdoc}
     */
    protected function defaultName()
    {
        return 'linkedin';
    }

    /**
     * {@inheritdoc}
     */
    protected function defaultTitle()
    {
        return 'LinkedIn';
    }
}

with the component in config like this:

'authClientCollection' => [
            'class' => \yii\authclient\Collection::class,
            'clients' => [
                'linkedin' => [
                    'class' => \app\components\authclient\LinkedIn::class,
                    'clientId' => 'xxx',
                    'clientSecret' => 'xxx',
                    'scope' => 'openid profile email',
                ],
            ],
        ],