diff options
author | 2015-05-13 07:30:12 -0700 | |
---|---|---|
committer | 2015-05-13 07:30:12 -0700 | |
commit | 1c3d2759dd986e2ccf58f918374f783c34a85fb2 (patch) | |
tree | 8863406ce82004db9a64cc9b6a1b6a9a450cca32 | |
parent | 07cbf7b4d04041e511c66d8d931eb32e6fc41b45 (diff) | |
parent | 358b7163405c171d4321812679bb4055dfc589d9 (diff) |
Merge pull request #1572 from stanley-cheung/php_add_support_for_jwt_auth_uri
PHP: add support for JWT auth URI
-rwxr-xr-x | src/php/lib/Grpc/BaseStub.php | 30 | ||||
-rwxr-xr-x | src/php/tests/interop/interop_client.php | 35 |
2 files changed, 58 insertions, 7 deletions
diff --git a/src/php/lib/Grpc/BaseStub.php b/src/php/lib/Grpc/BaseStub.php index cb2537fe98..b84b6b86f8 100755 --- a/src/php/lib/Grpc/BaseStub.php +++ b/src/php/lib/Grpc/BaseStub.php @@ -39,6 +39,7 @@ namespace Grpc; */ class BaseStub { + private $hostname; private $channel; // a callback function @@ -51,6 +52,7 @@ class BaseStub { * metadata array, and returns an updated metadata array */ public function __construct($hostname, $opts) { + $this->hostname = $hostname; $this->update_metadata = null; if (isset($opts['update_metadata'])) { if (is_callable($opts['update_metadata'])) { @@ -69,6 +71,18 @@ class BaseStub { $channel->close(); } + /** + * constructs the auth uri for the jwt + */ + private function _get_jwt_aud_uri($method) { + $last_slash_idx = strrpos($method, '/'); + if ($last_slash_idx === false) { + return false; + } + $service_name = substr($method, 0, $last_slash_idx); + return "https://" . $this->hostname . $service_name; + } + /* This class is intended to be subclassed by generated code, so all functions begin with "_" to avoid name collisions. */ @@ -87,9 +101,11 @@ class BaseStub { $metadata = array()) { $call = new UnaryCall($this->channel, $method, $deserialize); $actual_metadata = $metadata; + $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, - $actual_metadata); + $actual_metadata, + $jwt_aud_uri); } $call->start($argument, $actual_metadata); return $call; @@ -112,9 +128,11 @@ class BaseStub { $metadata = array()) { $call = new ClientStreamingCall($this->channel, $method, $deserialize); $actual_metadata = $metadata; + $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, - $actual_metadata); + $actual_metadata, + $jwt_aud_uri); } $call->start($arguments, $actual_metadata); return $call; @@ -136,9 +154,11 @@ class BaseStub { $metadata = array()) { $call = new ServerStreamingCall($this->channel, $method, $deserialize); $actual_metadata = $metadata; + $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, - $actual_metadata); + $actual_metadata, + $jwt_aud_uri); } $call->start($argument, $actual_metadata); return $call; @@ -157,9 +177,11 @@ class BaseStub { $metadata = array()) { $call = new BidiStreamingCall($this->channel, $method, $deserialize); $actual_metadata = $metadata; + $jwt_aud_uri = $this->_get_jwt_aud_uri($method); if (is_callable($this->update_metadata)) { $actual_metadata = call_user_func($this->update_metadata, - $actual_metadata); + $actual_metadata, + $jwt_aud_uri); } $call->start($actual_metadata); return $call; diff --git a/src/php/tests/interop/interop_client.php b/src/php/tests/interop/interop_client.php index 98a367a905..bf8d0cd93c 100755 --- a/src/php/tests/interop/interop_client.php +++ b/src/php/tests/interop/interop_client.php @@ -144,6 +144,21 @@ function computeEngineCreds($stub, $args) { } /** + * Run the jwt token credentials auth test. + * Passes when run against the cloud server as of 2015-05-12 + * @param $stub Stub object that has service methods + * @param $args array command line args + */ +function jwtTokenCreds($stub, $args) { + $jsonKey = json_decode( + file_get_contents(getenv(Google\Auth\CredentialsLoader::ENV_VAR)), + true); + $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true); + hardAssert($result->getUsername() == $jsonKey['client_email'], + 'invalid email returned'); +} + +/** * Run the client_streaming test. * Passes when run against the Node server as of 2015-04-30 * @param $stub Stub object that has service methods @@ -266,7 +281,11 @@ if (!array_key_exists('server_host', $args) || throw new Exception('Missing argument'); } -$server_address = $args['server_host'] . ':' . $args['server_port']; +if ($args['server_port'] == 443) { + $server_address = $args['server_host']; +} else { + $server_address = $args['server_host'] . ':' . $args['server_port']; +} if (!array_key_exists('server_host_override', $args)) { $args['server_host_override'] = 'foo.test.google.fr'; @@ -284,9 +303,16 @@ $opts = [ 'credentials' => $credentials, ]; -if (array_key_exists('oauth_scope', $args)) { - $auth = Google\Auth\ApplicationDefaultCredentials::getCredentials( +if (in_array($args['test_case'], array( + 'service_account_creds', + 'compute_engine_creds', + 'jwt_token_creds'))) { + if ($args['test_case'] == 'jwt_token_creds') { + $auth = Google\Auth\ApplicationDefaultCredentials::getCredentials(); + } else { + $auth = Google\Auth\ApplicationDefaultCredentials::getCredentials( $args['oauth_scope']); + } $opts['update_metadata'] = $auth->getUpdateMetadataFunc(); } @@ -323,6 +349,9 @@ switch ($args['test_case']) { case 'compute_engine_creds': computeEngineCreds($stub, $args); break; + case 'jwt_token_creds': + jwtTokenCreds($stub, $args); + break; default: exit(1); } |