Skip to content
Snippets Groups Projects
Commit 1c3d2759 authored by Tim Emiola's avatar Tim Emiola
Browse files

Merge pull request #1572 from stanley-cheung/php_add_support_for_jwt_auth_uri

PHP: add support for JWT auth URI
parents 07cbf7b4 358b7163
No related branches found
No related tags found
No related merge requests found
...@@ -39,6 +39,7 @@ namespace Grpc; ...@@ -39,6 +39,7 @@ namespace Grpc;
*/ */
class BaseStub { class BaseStub {
private $hostname;
private $channel; private $channel;
// a callback function // a callback function
...@@ -51,6 +52,7 @@ class BaseStub { ...@@ -51,6 +52,7 @@ class BaseStub {
* metadata array, and returns an updated metadata array * metadata array, and returns an updated metadata array
*/ */
public function __construct($hostname, $opts) { public function __construct($hostname, $opts) {
$this->hostname = $hostname;
$this->update_metadata = null; $this->update_metadata = null;
if (isset($opts['update_metadata'])) { if (isset($opts['update_metadata'])) {
if (is_callable($opts['update_metadata'])) { if (is_callable($opts['update_metadata'])) {
...@@ -69,6 +71,18 @@ class BaseStub { ...@@ -69,6 +71,18 @@ class BaseStub {
$channel->close(); $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 /* This class is intended to be subclassed by generated code, so all functions
begin with "_" to avoid name collisions. */ begin with "_" to avoid name collisions. */
...@@ -87,9 +101,11 @@ class BaseStub { ...@@ -87,9 +101,11 @@ class BaseStub {
$metadata = array()) { $metadata = array()) {
$call = new UnaryCall($this->channel, $method, $deserialize); $call = new UnaryCall($this->channel, $method, $deserialize);
$actual_metadata = $metadata; $actual_metadata = $metadata;
$jwt_aud_uri = $this->_get_jwt_aud_uri($method);
if (is_callable($this->update_metadata)) { if (is_callable($this->update_metadata)) {
$actual_metadata = call_user_func($this->update_metadata, $actual_metadata = call_user_func($this->update_metadata,
$actual_metadata); $actual_metadata,
$jwt_aud_uri);
} }
$call->start($argument, $actual_metadata); $call->start($argument, $actual_metadata);
return $call; return $call;
...@@ -112,9 +128,11 @@ class BaseStub { ...@@ -112,9 +128,11 @@ class BaseStub {
$metadata = array()) { $metadata = array()) {
$call = new ClientStreamingCall($this->channel, $method, $deserialize); $call = new ClientStreamingCall($this->channel, $method, $deserialize);
$actual_metadata = $metadata; $actual_metadata = $metadata;
$jwt_aud_uri = $this->_get_jwt_aud_uri($method);
if (is_callable($this->update_metadata)) { if (is_callable($this->update_metadata)) {
$actual_metadata = call_user_func($this->update_metadata, $actual_metadata = call_user_func($this->update_metadata,
$actual_metadata); $actual_metadata,
$jwt_aud_uri);
} }
$call->start($arguments, $actual_metadata); $call->start($arguments, $actual_metadata);
return $call; return $call;
...@@ -136,9 +154,11 @@ class BaseStub { ...@@ -136,9 +154,11 @@ class BaseStub {
$metadata = array()) { $metadata = array()) {
$call = new ServerStreamingCall($this->channel, $method, $deserialize); $call = new ServerStreamingCall($this->channel, $method, $deserialize);
$actual_metadata = $metadata; $actual_metadata = $metadata;
$jwt_aud_uri = $this->_get_jwt_aud_uri($method);
if (is_callable($this->update_metadata)) { if (is_callable($this->update_metadata)) {
$actual_metadata = call_user_func($this->update_metadata, $actual_metadata = call_user_func($this->update_metadata,
$actual_metadata); $actual_metadata,
$jwt_aud_uri);
} }
$call->start($argument, $actual_metadata); $call->start($argument, $actual_metadata);
return $call; return $call;
...@@ -157,9 +177,11 @@ class BaseStub { ...@@ -157,9 +177,11 @@ class BaseStub {
$metadata = array()) { $metadata = array()) {
$call = new BidiStreamingCall($this->channel, $method, $deserialize); $call = new BidiStreamingCall($this->channel, $method, $deserialize);
$actual_metadata = $metadata; $actual_metadata = $metadata;
$jwt_aud_uri = $this->_get_jwt_aud_uri($method);
if (is_callable($this->update_metadata)) { if (is_callable($this->update_metadata)) {
$actual_metadata = call_user_func($this->update_metadata, $actual_metadata = call_user_func($this->update_metadata,
$actual_metadata); $actual_metadata,
$jwt_aud_uri);
} }
$call->start($actual_metadata); $call->start($actual_metadata);
return $call; return $call;
......
...@@ -143,6 +143,21 @@ function computeEngineCreds($stub, $args) { ...@@ -143,6 +143,21 @@ function computeEngineCreds($stub, $args) {
'invalid email returned'); 'invalid email returned');
} }
/**
* 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. * Run the client_streaming test.
* Passes when run against the Node server as of 2015-04-30 * Passes when run against the Node server as of 2015-04-30
...@@ -266,7 +281,11 @@ if (!array_key_exists('server_host', $args) || ...@@ -266,7 +281,11 @@ if (!array_key_exists('server_host', $args) ||
throw new Exception('Missing argument'); 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)) { if (!array_key_exists('server_host_override', $args)) {
$args['server_host_override'] = 'foo.test.google.fr'; $args['server_host_override'] = 'foo.test.google.fr';
...@@ -284,9 +303,16 @@ $opts = [ ...@@ -284,9 +303,16 @@ $opts = [
'credentials' => $credentials, 'credentials' => $credentials,
]; ];
if (array_key_exists('oauth_scope', $args)) { if (in_array($args['test_case'], array(
$auth = Google\Auth\ApplicationDefaultCredentials::getCredentials( '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']); $args['oauth_scope']);
}
$opts['update_metadata'] = $auth->getUpdateMetadataFunc(); $opts['update_metadata'] = $auth->getUpdateMetadataFunc();
} }
...@@ -323,6 +349,9 @@ switch ($args['test_case']) { ...@@ -323,6 +349,9 @@ switch ($args['test_case']) {
case 'compute_engine_creds': case 'compute_engine_creds':
computeEngineCreds($stub, $args); computeEngineCreds($stub, $args);
break; break;
case 'jwt_token_creds':
jwtTokenCreds($stub, $args);
break;
default: default:
exit(1); exit(1);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment