Skip to content

Commit 6c8c055

Browse files
committed
Store access tokens
1 parent 5c5c100 commit 6c8c055

5 files changed

Lines changed: 93 additions & 4 deletions

File tree

ProcessMaker/Http/Controllers/Admin/DevLinkController.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProcessMaker\Http\Controllers\Admin;
44

55
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Http;
67
use Laravel\Passport\Client;
78
use ProcessMaker\Http\Controllers\Controller;
89
use ProcessMaker\Models\DevLink;
@@ -17,6 +18,8 @@ public function index(Request $request)
1718
return redirect($updatedDevLink->getOauthRedirectUrl());
1819
}
1920

21+
$this->storeOauthCredentials($request);
22+
2023
return view('admin.devlink.index');
2124
}
2225

@@ -62,4 +65,29 @@ private function storeClientCredentials(Request $request)
6265

6366
return false;
6467
}
68+
69+
private function storeOauthCredentials(Request $request)
70+
{
71+
if (
72+
$request->has('state') &&
73+
$request->has('code')
74+
) {
75+
$devlink = DevLink::where('state', $request->input('state'))->firstOrFail();
76+
77+
$response = Http::asForm()->post($devlink->url . '/oauth/token', [
78+
'grant_type' => 'authorization_code',
79+
'client_id' => $devlink->client_id,
80+
'client_secret' => $devlink->client_secret,
81+
'redirect_uri' => route('devlink.index'),
82+
'code' => $request->input('code'),
83+
]);
84+
85+
$response = $response->json();
86+
$devlink->update([
87+
'access_token' => $response['access_token'],
88+
'refresh_token' => $response['refresh_token'],
89+
'expires_in' => $response['expires_in'],
90+
]);
91+
}
92+
}
6593
}

ProcessMaker/Models/DevLink.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProcessMaker\Models;
44

55
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Support\Str;
67
use ProcessMaker\Models\ProcessMakerModel;
78

89
class DevLink extends ProcessMakerModel
@@ -27,8 +28,18 @@ public function getOauthRedirectUrl()
2728
'client_id' => $this->client_id,
2829
'redirect_url' => route('devlink.index'),
2930
'resource_type' => 'code',
31+
'state' => $this->generateNewState(),
3032
]);
3133

3234
return $this->url . '/oauth/authorize?' . $params;
3335
}
36+
37+
private function generateNewState()
38+
{
39+
$uuid = (string) Str::orderedUuid();
40+
$this->state = $uuid;
41+
$this->saveOrFail();
42+
43+
return $uuid;
44+
}
3445
}

database/migrations/2024_08_26_183754_create_dev_links_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function up()
2222
$table->text('access_token')->nullable();
2323
$table->text('refresh_token')->nullable();
2424
$table->integer('expires_in')->nullable();
25+
$table->uuid('state')->unique()->nullable();
2526
$table->timestamps();
2627
});
2728
}

tests/Feature/Admin/DevLinkTest.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Feature\Admin;
44

5+
use Illuminate\Support\Facades\Http;
56
use Laravel\Passport\Client;
67
use ProcessMaker\Models\DevLink;
78
use Tests\Feature\Shared\RequestHelper;
@@ -11,23 +12,64 @@ class DevLinkTest extends TestCase
1112
{
1213
use RequestHelper;
1314

14-
public function testIndex()
15+
public function testIndexStoreClientCredentials()
1516
{
1617
$devLink = DevLink::factory()->create();
18+
1719
$params = [
1820
'devlink_id' => $devLink->id,
1921
'client_id' => 123,
2022
'client_secret' => 'abc123',
2123
];
2224

2325
$response = $this->webCall('GET', route('devlink.index', $params));
24-
$response->assertRedirect($devLink->getOauthRedirectUrl());
2526

2627
$devLink->refresh();
28+
29+
$expectedRedirectParams = [
30+
'client_id' => 123,
31+
'redirect_url' => route('devlink.index'),
32+
'resource_type' => 'code',
33+
'state' => $devLink->state,
34+
];
35+
$expectedRedirectUrl = $devLink->url . '/oauth/authorize?' . http_build_query($expectedRedirectParams);
36+
$response->assertRedirect($expectedRedirectUrl);
37+
2738
$this->assertEquals($devLink->client_id, $params['client_id']);
2839
$this->assertEquals($devLink->client_secret, $params['client_secret']);
2940
}
3041

42+
public function testIndexStoreOauthCredentials()
43+
{
44+
$devLink = DevLink::factory()->create([
45+
'url' => 'http://remote-instance.test',
46+
]);
47+
48+
// Generate state uuid
49+
$devLink->getOauthRedirectUrl();
50+
51+
$getTokenUrl = $devLink->url . '/oauth/token';
52+
Http::fake([
53+
$getTokenUrl => Http::response([
54+
'access_token' => '123abc',
55+
'refresh_token' => '456def',
56+
'expires_in' => 3600,
57+
]),
58+
]);
59+
60+
$params = [
61+
'state' => $devLink->state,
62+
'code' => '12345',
63+
];
64+
$response = $this->webCall('GET', route('devlink.index', $params));
65+
$response->assertStatus(200);
66+
67+
$devLink->refresh();
68+
$this->assertEquals($devLink->access_token, '123abc');
69+
$this->assertEquals($devLink->refresh_token, '456def');
70+
$this->assertEquals($devLink->expires_in, 3600);
71+
}
72+
3173
public function testGetOauthClient()
3274
{
3375
$devLink = DevLink::factory()->create([

tests/Model/DevLinkTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@ public function testGetOauthRedirectUrl()
3131
'client_id' => 123,
3232
]);
3333

34+
$actualUrl = $devLink->getOauthRedirectUrl();
35+
36+
// Refresh devlink to get state created by getOauthRedirectUrl
37+
$devLink->refresh();
38+
$state = $devLink->state;
39+
3440
$expectedQueryString = http_build_query([
3541
'client_id' => 123,
3642
'redirect_url' => route('devlink.index'),
3743
'resource_type' => 'code',
44+
'state' => $state,
3845
]);
3946

4047
$this->assertEquals(
41-
$devLink->getOauthRedirectUrl(),
42-
$devLink->url . '/oauth/authorize?' . $expectedQueryString
48+
$devLink->url . '/oauth/authorize?' . $expectedQueryString,
49+
$actualUrl,
4350
);
4451
}
4552
}

0 commit comments

Comments
 (0)