Skip to content

Commit 5252689

Browse files
committed
fix(linkedin): drop deprecated r_basicprofile from default scopes
Make r_basicprofile opt-in via LINKEDIN_EXTRA_SCOPES so self-hosted users unblock by default and ops with legacy/enterprise products keep working. Why --- LinkedIn rejects OAuth authorize requests with a generic "Bummer, something went wrong" page when an app asks for a scope it can't grant. `r_basicprofile` is a legacy scope deprecated in 2018; new LinkedIn dev apps don't have it, so every self-hosted user hits the rejection immediately on `/connect/linkedin`. The two products LinkedIn actually grants to standard apps today are: - Sign In with LinkedIn using OpenID Connect → `openid profile email` - Share on LinkedIn → `w_member_social` That set is enough for the connect flow. The only piece of data `r_basicprofile` was buying us is `/v2/me`'s `vanityName` (pretty `linkedin.com/in/<slug>`). `fetchVanityName()` already handles HTTP failure gracefully (returns null), and the only downstream consumer — `LinkedInPagePublisher`'s post-URL builder — already falls back to a numeric `linkedin.com/feed/update/<id>` URL when `$account->username` is null. Backward compatibility ---------------------- Ops with legacy or enterprise LinkedIn products approved on their dev app (so they DO have `r_basicprofile`) can opt back in via env: LINKEDIN_EXTRA_SCOPES=r_basicprofile `LinkedInController::resolveScopes()` merges this comma-separated list into the default scope array. The connect flow's `Socialite::scopes()` call then includes the legacy scope, preserving the pre-PR behaviour end-to-end (including `vanityName` lookup). Net effect for users without `r_basicprofile`: - Connect flow works (was previously rejected by LinkedIn). - Posts publish exactly the same way. - Generated post URLs use the numeric form instead of the vanity slug. Tests ----- - `linkedin connect requests the default scope set when LINKEDIN_EXTRA_SCOPES is unset` - `linkedin connect appends LINKEDIN_EXTRA_SCOPES to the default scope set` - Existing `splits comma-separated approvedScopes` fixture updated to match the new default set.
1 parent c152db5 commit 5252689

5 files changed

Lines changed: 101 additions & 4 deletions

File tree

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ LINKEDIN_CLIENT_ID=
103103
LINKEDIN_CLIENT_SECRET=
104104
LINKEDIN_CLIENT_REDIRECT="${APP_URL}/accounts/linkedin/callback"
105105
LINKEDIN_PAGE_CLIENT_REDIRECT="${APP_URL}/accounts/linkedin-page/callback"
106+
# Optional: comma-separated extra OAuth scopes appended to the personal
107+
# LinkedIn connect flow. Use this if your LinkedIn dev app has legacy or
108+
# enterprise products approved — e.g. `r_basicprofile` re-enables
109+
# vanityName lookup via /v2/me. Leave empty for the safe default set.
110+
# LINKEDIN_EXTRA_SCOPES=r_basicprofile
106111

107112
# X / Twitter (https://developer.twitter.com)
108113
X_CLIENT_ID=

app/Http/Controllers/Auth/LinkedInController.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class LinkedInController extends SocialController
2626
'openid',
2727
'profile',
2828
'email',
29-
'r_basicprofile',
3029
'w_member_social',
3130
];
3231

@@ -42,7 +41,26 @@ public function connect(Request $request): Response|RedirectResponse
4241

4342
$this->authorize('manageAccounts', $workspace);
4443

45-
return $this->redirectToProvider($request, $this->driver, $this->scopes);
44+
return $this->redirectToProvider($request, $this->driver, $this->resolveScopes());
45+
}
46+
47+
/**
48+
* Merge $this->scopes with any extra scopes the operator configured via
49+
* `LINKEDIN_EXTRA_SCOPES` — comma-separated, e.g. `r_basicprofile`. Useful
50+
* when the connected LinkedIn dev app has legacy or enterprise products
51+
* not covered by the default Sign-In + Share-on-LinkedIn pair.
52+
*/
53+
protected function resolveScopes(): array
54+
{
55+
$extra = (string) config('services.linkedin.extra_scopes', '');
56+
57+
if ($extra === '') {
58+
return $this->scopes;
59+
}
60+
61+
$extraScopes = array_filter(array_map('trim', explode(',', $extra)));
62+
63+
return array_values(array_unique([...$this->scopes, ...$extraScopes]));
4664
}
4765

4866
public function callback(Request $request): View

config/services.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
'client_id' => env('LINKEDIN_CLIENT_ID'),
4242
'client_secret' => env('LINKEDIN_CLIENT_SECRET'),
4343
'redirect' => env('LINKEDIN_CLIENT_REDIRECT'),
44+
// Comma-separated list of OAuth scopes to request beyond the defaults
45+
// (openid, profile, email, w_member_social). Useful for apps with
46+
// legacy or enterprise products approved — e.g. set
47+
// `LINKEDIN_EXTRA_SCOPES=r_basicprofile` to re-enable vanityName
48+
// lookup via /v2/me.
49+
'extra_scopes' => env('LINKEDIN_EXTRA_SCOPES'),
4450
],
4551

4652
'linkedin-openid' => [

docker/.env.docker.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ LINKEDIN_CLIENT_ID=
9090
LINKEDIN_CLIENT_SECRET=
9191
LINKEDIN_CLIENT_REDIRECT="${APP_URL}/accounts/linkedin/callback"
9292
LINKEDIN_PAGE_CLIENT_REDIRECT="${APP_URL}/accounts/linkedin-page/callback"
93+
# Optional: comma-separated extra OAuth scopes appended to the personal
94+
# LinkedIn connect flow. Use this if your LinkedIn dev app has legacy or
95+
# enterprise products approved — e.g. `r_basicprofile` re-enables
96+
# vanityName lookup via /v2/me. Leave empty for the safe default set.
97+
# LINKEDIN_EXTRA_SCOPES=r_basicprofile
9398

9499
X_CLIENT_ID=
95100
X_CLIENT_SECRET=

tests/Feature/Social/LinkedInControllerTest.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,69 @@
3939
expect(session('social_connect_workspace'))->toBe($this->workspace->id);
4040
});
4141

42+
test('linkedin connect requests the default scope set when LINKEDIN_EXTRA_SCOPES is unset', function () {
43+
config(['services.linkedin.extra_scopes' => null]);
44+
45+
$captured = [];
46+
47+
$driverMock = Mockery::mock();
48+
$driverMock->shouldReceive('scopes')
49+
->withArgs(function (array $scopes) use (&$captured) {
50+
$captured = $scopes;
51+
52+
return true;
53+
})
54+
->andReturnSelf();
55+
$driverMock->shouldReceive('redirect')->andReturn(Mockery::mock([
56+
'getTargetUrl' => 'https://www.linkedin.com/oauth/v2/authorization?test=1',
57+
]));
58+
59+
Socialite::shouldReceive('driver')
60+
->with('linkedin')
61+
->andReturn($driverMock);
62+
63+
$this->actingAs($this->user)
64+
->withHeader('X-Inertia', 'true')
65+
->get(route('app.social.linkedin.connect'));
66+
67+
expect($captured)->toEqualCanonicalizing([
68+
'openid', 'profile', 'email', 'w_member_social',
69+
]);
70+
});
71+
72+
test('linkedin connect appends LINKEDIN_EXTRA_SCOPES to the default scope set', function () {
73+
// Backward-compatibility: ops who have legacy products approved on
74+
// their LinkedIn app (e.g. r_basicprofile) opt back in via env.
75+
config(['services.linkedin.extra_scopes' => 'r_basicprofile, r_emailaddress']);
76+
77+
$captured = [];
78+
79+
$driverMock = Mockery::mock();
80+
$driverMock->shouldReceive('scopes')
81+
->withArgs(function (array $scopes) use (&$captured) {
82+
$captured = $scopes;
83+
84+
return true;
85+
})
86+
->andReturnSelf();
87+
$driverMock->shouldReceive('redirect')->andReturn(Mockery::mock([
88+
'getTargetUrl' => 'https://www.linkedin.com/oauth/v2/authorization?test=1',
89+
]));
90+
91+
Socialite::shouldReceive('driver')
92+
->with('linkedin')
93+
->andReturn($driverMock);
94+
95+
$this->actingAs($this->user)
96+
->withHeader('X-Inertia', 'true')
97+
->get(route('app.social.linkedin.connect'));
98+
99+
expect($captured)->toEqualCanonicalizing([
100+
'openid', 'profile', 'email', 'w_member_social',
101+
'r_basicprofile', 'r_emailaddress',
102+
]);
103+
});
104+
42105
test('linkedin oauth callback creates account', function () {
43106
session([
44107
'social_connect_workspace' => $this->workspace->id,
@@ -99,7 +162,7 @@
99162
// splits on space (the OAuth 2.0 default), so approvedScopes lands as
100163
// a single-element array with the whole CSV inside. The save path
101164
// must normalize back to individual tokens.
102-
$socialiteUser->approvedScopes = ['email,openid,profile,r_basicprofile,w_member_social'];
165+
$socialiteUser->approvedScopes = ['email,openid,profile,w_member_social'];
103166

104167
Socialite::shouldReceive('driver')
105168
->with('linkedin')
@@ -116,7 +179,7 @@
116179

117180
$account = SocialAccount::where('platform_user_id', 'abc123xyz')->first();
118181
expect($account->scopes)->toEqualCanonicalizing([
119-
'email', 'openid', 'profile', 'r_basicprofile', 'w_member_social',
182+
'email', 'openid', 'profile', 'w_member_social',
120183
]);
121184
});
122185

0 commit comments

Comments
 (0)