Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow API Gateway Authentication #59

Merged
merged 7 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .coveralls.yml

This file was deleted.

29 changes: 29 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: PHP Composer

on:
push:
branches: [ v3.x ]
pull_request:
branches: [ v3.x ]

jobs:
build:
runs-on: ubuntu-20.04
strategy:
matrix:
php-versions: ['7.0', '7.3', '7.4']
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
- name: Validate composer.json and composer.lock
run: composer validate
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run PHPCS
run: composer run-script lint
- name: Run PHPUnit
run: composer run-script test
19 changes: 0 additions & 19 deletions .scrutinizer.yml

This file was deleted.

22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
},
"require-dev": {
"helmich/mongomock": "^2.1",
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^6.5.2",
"squizlabs/php_codesniffer": "^3.2",
"subjective-php/psr-cache-mongodb": "^2.1"
Expand All @@ -50,5 +49,9 @@
},
"autoload-dev": {
"psr-4": { "TraderInteractive\\Api\\": "tests" }
},
"scripts": {
"lint": "vendor/bin/phpcs",
"test": "vendor/bin/phpunit"
}
}
37 changes: 37 additions & 0 deletions src/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,43 @@ public static function createOwnerCredentials(
return new self($getTokenRequestFunc);
}

/**
* Creates a new instance of Authentication for Client Credentials grant type
*
* @param string $clientId The oauth client id
* @param string $clientSecret The oauth client secret
* @param string $authUrl The oauth auth url
* @param string $tokenResource The access token resource of the API
*
* @return Authentication
*/
public static function createApiGatewayClientCredentials(
string $clientId,
string $clientSecret,
string $authUrl,
string $tokenResource = 'token'
) : Authentication {
$getTokenRequestFunc = function (
string $unusedBaseUrl,
string $unusedRefreshToken = null
) use (
$clientId,
$clientSecret,
$authUrl,
$tokenResource
) {
$data = ['client_id' => $clientId, 'client_secret' => $clientSecret, 'grant_type' => 'client_credentials'];
return new Request(
'POST',
"{$authUrl}/oauth2/{$tokenResource}",
['Content-Type' => 'application/x-www-form-urlencoded'],
Http::buildQueryString($data)
);
};

return new self($getTokenRequestFunc);
}

/**
* Extracts an access token from the given API response
*
Expand Down
28 changes: 28 additions & 0 deletions tests/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,34 @@ public function getTokenRequestClientCredentialsCustomTokenResource()
);
$this->assertSame(['Content-Type' => ['application/x-www-form-urlencoded']], $request->getHeaders());
}

/**
* @test
* @covers ::createApiGatewayClientCredentials
*/
public function createApiGatewayClientCredentials()
{
$auth = Authentication::createApiGatewayClientCredentials('not under test', 'not under test', 'http://auth');
$this->assertInstanceOf('\TraderInteractive\Api\Authentication', $auth);
}

/**
* @test
* @covers ::createApiGatewayClientCredentials
* @covers ::getTokenRequest
*/
public function getTokenRequestApiGatewayClientCredentials()
{
$auth = Authentication::createApiGatewayClientCredentials('id', 'secret', 'authUrl');
$request = $auth->getTokenRequest('baseUrl');
$this->assertSame('authUrl/oauth2/token', (string)$request->getUri());
$this->assertSame('POST', $request->getMethod());
$this->assertSame(
'client_id=id&client_secret=secret&grant_type=client_credentials',
(string)$request->getBody()
);
$this->assertSame(['Content-Type' => ['application/x-www-form-urlencoded']], $request->getHeaders());
}
}

function time()
Expand Down