Skip to content

Commit 503042e

Browse files
authored
Merge pull request #59 from chadicus/DOD-4315
Allow API Gateway Authentication
2 parents 5e9c114 + a80f570 commit 503042e

File tree

7 files changed

+98
-45
lines changed

7 files changed

+98
-45
lines changed

.coveralls.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

.github/workflows/php.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: PHP Composer
2+
3+
on:
4+
push:
5+
branches: [ v3.x ]
6+
pull_request:
7+
branches: [ v3.x ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-20.04
12+
strategy:
13+
matrix:
14+
php-versions: ['7.0', '7.3', '7.4']
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
- name: Install PHP
19+
uses: shivammathur/setup-php@v2
20+
with:
21+
php-version: ${{ matrix.php-versions }}
22+
- name: Validate composer.json and composer.lock
23+
run: composer validate
24+
- name: Install dependencies
25+
run: composer install --prefer-dist --no-progress
26+
- name: Run PHPCS
27+
run: composer run-script lint
28+
- name: Run PHPUnit
29+
run: composer run-script test

.scrutinizer.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

.travis.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
},
4141
"require-dev": {
4242
"helmich/mongomock": "^2.1",
43-
"php-coveralls/php-coveralls": "^2.1",
4443
"phpunit/phpunit": "^6.5.2",
4544
"squizlabs/php_codesniffer": "^3.2",
4645
"subjective-php/psr-cache-mongodb": "^2.1"
@@ -50,5 +49,9 @@
5049
},
5150
"autoload-dev": {
5251
"psr-4": { "TraderInteractive\\Api\\": "tests" }
52+
},
53+
"scripts": {
54+
"lint": "vendor/bin/phpcs",
55+
"test": "vendor/bin/phpunit"
5356
}
5457
}

src/Authentication.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,43 @@ public static function createOwnerCredentials(
138138
return new self($getTokenRequestFunc);
139139
}
140140

141+
/**
142+
* Creates a new instance of Authentication for Client Credentials grant type
143+
*
144+
* @param string $clientId The oauth client id
145+
* @param string $clientSecret The oauth client secret
146+
* @param string $authUrl The oauth auth url
147+
* @param string $tokenResource The access token resource of the API
148+
*
149+
* @return Authentication
150+
*/
151+
public static function createApiGatewayClientCredentials(
152+
string $clientId,
153+
string $clientSecret,
154+
string $authUrl,
155+
string $tokenResource = 'token'
156+
) : Authentication {
157+
$getTokenRequestFunc = function (
158+
string $unusedBaseUrl,
159+
string $unusedRefreshToken = null
160+
) use (
161+
$clientId,
162+
$clientSecret,
163+
$authUrl,
164+
$tokenResource
165+
) {
166+
$data = ['client_id' => $clientId, 'client_secret' => $clientSecret, 'grant_type' => 'client_credentials'];
167+
return new Request(
168+
'POST',
169+
"{$authUrl}/oauth2/{$tokenResource}",
170+
['Content-Type' => 'application/x-www-form-urlencoded'],
171+
Http::buildQueryString($data)
172+
);
173+
};
174+
175+
return new self($getTokenRequestFunc);
176+
}
177+
141178
/**
142179
* Extracts an access token from the given API response
143180
*

tests/AuthenticationTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,34 @@ public function getTokenRequestClientCredentialsCustomTokenResource()
166166
);
167167
$this->assertSame(['Content-Type' => ['application/x-www-form-urlencoded']], $request->getHeaders());
168168
}
169+
170+
/**
171+
* @test
172+
* @covers ::createApiGatewayClientCredentials
173+
*/
174+
public function createApiGatewayClientCredentials()
175+
{
176+
$auth = Authentication::createApiGatewayClientCredentials('not under test', 'not under test', 'http://auth');
177+
$this->assertInstanceOf('\TraderInteractive\Api\Authentication', $auth);
178+
}
179+
180+
/**
181+
* @test
182+
* @covers ::createApiGatewayClientCredentials
183+
* @covers ::getTokenRequest
184+
*/
185+
public function getTokenRequestApiGatewayClientCredentials()
186+
{
187+
$auth = Authentication::createApiGatewayClientCredentials('id', 'secret', 'authUrl');
188+
$request = $auth->getTokenRequest('baseUrl');
189+
$this->assertSame('authUrl/oauth2/token', (string)$request->getUri());
190+
$this->assertSame('POST', $request->getMethod());
191+
$this->assertSame(
192+
'client_id=id&client_secret=secret&grant_type=client_credentials',
193+
(string)$request->getBody()
194+
);
195+
$this->assertSame(['Content-Type' => ['application/x-www-form-urlencoded']], $request->getHeaders());
196+
}
169197
}
170198

171199
function time()

0 commit comments

Comments
 (0)