Skip to content

Commit

Permalink
Merge pull request #5 from humhub/fix/auth-deprecations
Browse files Browse the repository at this point in the history
Fix LinkedIn Auth Deprecations
  • Loading branch information
luke- committed Jul 1, 2024
2 parents 283c7a5 + 38c41d7 commit 5bd1a49
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use humhub\components\Event;
use humhub\modules\user\authclient\Collection;
use humhubContrib\auth\linkedin\authclient\LinkedinAuth;
use humhubContrib\auth\linkedin\authclient\LinkedinAuthV2;
use humhubContrib\auth\linkedin\models\ConfigureForm;

class Events
Expand All @@ -19,7 +20,7 @@ public static function onAuthClientCollectionInit($event)

if (!empty(ConfigureForm::getInstance()->enabled)) {
$authClientCollection->setClient('linkedin', [
'class' => LinkedinAuth::class,
'class' => ConfigureForm::getInstance()->useV2 ? LinkedinAuthV2::class : LinkedinAuth::class,
'clientId' => ConfigureForm::getInstance()->clientId,
'clientSecret' => ConfigureForm::getInstance()->clientSecret
]);
Expand Down
60 changes: 60 additions & 0 deletions authclient/LinkedinAuthV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace humhubContrib\auth\linkedin\authclient;

use yii\authclient\clients\LinkedIn;
use humhub\helpers\ArrayHelper;

/**
* LinkedIn allows authentication via LinkedIn OAuth.
*/
class LinkedinAuthV2 extends LinkedIn
{

/**
* @inheritdoc
*/
protected function defaultViewOptions()
{
return [
'popupWidth' => 860,
'popupHeight' => 480,
'cssIcon' => 'fa fa-linkedin',
'buttonBackgroundColor' => '#e0492f',
];
}

public function init()
{
$this->scope = implode(' ', [
'openid',
'profile',
'email',
]);
}

protected function initUserAttributes()
{
return $this->api('userinfo', 'GET');
}

/**
* @inheritdoc
*/
protected function defaultNormalizeUserAttributeMap()
{
return [
'id' => 'sub',
'username' => 'displayName',
'firstname' => function ($attributes) {
return ArrayHelper::getValue($attributes, 'given_name', '');
},
'lastname' => function ($attributes) {
return ArrayHelper::getValue($attributes, 'family_name', '');
},
'email' => function ($attributes) {
return ArrayHelper::getValue($attributes, 'email', '');
},
];
}
}
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

1.0.2 (Unreleased)
----------------------
- Fix #5: Fixed LinkedIn Auth Deprecations. Added `useV2` checkbox to switch beetween new and old auth clients

1.0.1 (April 20, 2023)
----------------------
- Fix #1: Normalized user attributes for registration capability
Expand Down
7 changes: 5 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ A new button "LinkedIn" will appear on the login page.

## Configuration

Please follow the [LinkedIn instructions](https://www.linkedin.com/help/linkedin/answer/5070/log-in-with-linkedin-credentials?lang=en) to create the required ` OAuth client ID` credentials.
Please follow the to create the required ` OAuth client ID` credentials.

[V1](https://www.linkedin.com/help/linkedin/answer/5070/log-in-with-linkedin-credentials?lang=en)
[V2](https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2)

Once you have the **Client ID** and **Client Secret** created there, the values must then be entered in the module configuration at: `Administration -> Modules -> LinkedIn Auth -> Settings`.
This page also displays the `Authorized redirect URI`, which must be inserted in LinkedIn in the corresponding field.
This page also displays the `Authorized redirect URI`, which must be inserted in LinkedIn in the corresponding field. If after this signing will not work check `useV2` checkbox and try again.



Expand Down
10 changes: 9 additions & 1 deletion models/ConfigureForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ class ConfigureForm extends Model
*/
public $redirectUri;

/**
* @var boolean use signing with LinkedIn version 2
*/
public $useV2;

/**
* @inheritdoc
*/
public function rules()
{
return [
[['clientId', 'clientSecret'], 'required'],
[['enabled'], 'boolean'],
[['enabled', 'useV2'], 'boolean'],
];
}

Expand All @@ -52,6 +57,7 @@ public function attributeLabels()
'enabled' => Yii::t('AuthLinkedinModule.base', 'Enabled'),
'clientId' => Yii::t('AuthLinkedinModule.base', 'Client ID'),
'clientSecret' => Yii::t('AuthLinkedinModule.base', 'Client secret'),
'useV2' => Yii::t('AuthLinkedinModule.base', 'Use v2'),
];
}

Expand All @@ -77,6 +83,7 @@ public function loadSettings()
$this->enabled = (boolean)$settings->get('enabled');
$this->clientId = $settings->get('clientId');
$this->clientSecret = $settings->get('clientSecret');
$this->useV2 = $settings->get('useV2');

$this->redirectUri = Url::to(['/user/auth/external', 'authclient' => 'linkedin'], true);
}
Expand All @@ -92,6 +99,7 @@ public function saveSettings()
$module->settings->set('enabled', (boolean)$this->enabled);
$module->settings->set('clientId', $this->clientId);
$module->settings->set('clientSecret', $this->clientSecret);
$module->settings->set('useV2', $this->useV2);

return true;
}
Expand Down
16 changes: 6 additions & 10 deletions views/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,29 @@
<div class="panel panel-default">
<div class="panel-heading">
<?= Yii::t('AuthLinkedinModule.base', '<strong>LinkedIn</strong> Sign-In configuration') ?></div>

<div class="panel-body">
<p>
<?= Html::a(Yii::t('AuthLinkedinModule.base', 'LinkedIn Documentation'), 'https://www.linkedin.com/help/linkedin/answer/5070/log-in-with-linkedin-credentials?lang=en', ['class' => 'btn btn-primary pull-right btn-sm', 'target' => '_blank']); ?>
<?= Yii::t('AuthLinkedinModule.base', 'Please follow the LinkedIn instructions to create the required <strong>OAuth client</strong> credentials.'); ?>
<br/>
</p>
<p>
<?= Html::a(Yii::t('AuthLinkedinModule.base', 'LinkedIn Documentation') . ' v1', 'https://www.linkedin.com/help/linkedin/answer/5070/log-in-with-linkedin-credentials?lang=en', ['class' => 'btn btn-primary btn-sm', 'target' => '_blank']); ?>
<?= Html::a(Yii::t('AuthLinkedinModule.base', 'LinkedIn Documentation') . ' v2', 'https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2', ['class' => 'btn btn-primary btn-sm', 'target' => '_blank']); ?>
</p>
<br/>

<?php $form = ActiveForm::begin(['id' => 'configure-form', 'enableClientValidation' => false, 'enableClientScript' => false]); ?>

<?= $form->field($model, 'enabled')->checkbox(); ?>

<br/>
<?= $form->field($model, 'useV2')->checkbox(); ?>
<br/>
<?= $form->field($model, 'clientId'); ?>
<?= $form->field($model, 'clientSecret'); ?>

<br/>
<?= $form->field($model, 'redirectUri')->textInput(['readonly' => true]); ?>
<br/>

<div class="form-group">
<?= Html::submitButton(Yii::t('base', 'Save'), ['class' => 'btn btn-primary', 'data-ui-loader' => '']) ?>
</div>

<?php ActiveForm::end(); ?>

</div>
</div>
</div>

0 comments on commit 5bd1a49

Please sign in to comment.