google-auth-library-php icon indicating copy to clipboard operation
google-auth-library-php copied to clipboard

Implement IAP/JWT client

Open nohn opened this issue 9 years ago • 15 comments

Please implement the functionality required to connect to IAP protected services like in the Python Auth Client: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/iap/make_iap_request.py

nohn avatar May 16 '17 10:05 nohn

@nohn Can you explain what the missing auth is?

bshaffer avatar Aug 14 '17 20:08 bshaffer

The basic documentation is on https://cloud.google.com/iap/docs/authentication-howto. There a bit more on https://cloudplatform.googleblog.com/2017/04/Getting-started-with-Cloud-Identity-Aware-Proxy.html. It seems like at least the target_audience claim is missing, but I'm not sure, if that's all.

nohn avatar Aug 15 '17 07:08 nohn

+1

babarot avatar Sep 25 '17 08:09 babarot

We are working on this! cc @ryanmats

bshaffer avatar Sep 25 '17 20:09 bshaffer

When will it be implemented?

babarot avatar Sep 28 '17 13:09 babarot

@b4b4r07 Is the request to support the field target_audience in the JWT payload? I haven't tested it out, but it may be possible through something like this: https://gist.github.com/bshaffer/35fc2fb65df200c5e3a201c70c466c8b

bshaffer avatar Sep 28 '17 20:09 bshaffer

Thank you. Does that script really work? I tried but I got an error. As far as I can see, making IAP request needs to refresh token. However that script doesn't do that.

babarot avatar Sep 29 '17 06:09 babarot

It won't work until #171 is merged, and then you will be able to do the following:

$oauth->setAdditionalClaims([
    'target_audience' => $clientID,
]);

I've updated the sample to reflect this, but if you could pull down the branch and test, that would be awesome! You can do so by changing composer.json to look something like this (and then running composer update):

{
    "require": {
        "google/auth": "dev-additional-claims as v1.0.1"
    }
}

bshaffer avatar Sep 29 '17 18:09 bshaffer

If someone on this thread could confirm this change to the auth library enables a successful IAP call, I will get the change merged and a new version tagged.

bshaffer avatar Oct 03 '17 16:10 bshaffer

$ php test.php
PHP Fatal error:  Uncaught Error: Class 'Google\Auth\OAuth2' not found in /home/project/iaphp/test.php:18
Stack trace:
#0 {main}
  thrown in /home/project/iaphp/test.php on line 18
$ cat composer.json
{
    "required": {
        "google/auth": "dev-additional-claims as v1.0.1"
    },
    "require": {
        "google/auth": "dev-additional-claims as v1.0.1"
    }
}

nohn avatar Oct 04 '17 09:10 nohn

@nohn you are getting an autoload error. Presumably composer is not set up properly.

bshaffer avatar Oct 04 '17 18:10 bshaffer

@nohn also, composer should use "require" and not "required".

bshaffer avatar Oct 09 '17 21:10 bshaffer

  1. Composer is latest stable (1.5.2)
  2. Install is clean
$ composer self-update
You are already using composer version 1.5.2 (stable channel).
$ cat > composer.json 
{
    "require": {
        "google/auth": "dev-additional-claims as v1.0.1"
    }
}
$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 7 installs, 0 updates, 0 removals
  - Installing psr/cache (1.0.1): Loading from cache
  - Installing psr/http-message (1.0.1): Loading from cache
  - Installing guzzlehttp/psr7 (1.4.2): Loading from cache
  - Installing guzzlehttp/promises (v1.3.1): Loading from cache
  - Installing guzzlehttp/guzzle (6.3.0): Loading from cache
  - Installing firebase/php-jwt (v5.0.0): Loading from cache
  - Installing google/auth (dev-additional-claims 82c081a): Cloning 82c081a6fd from cache
guzzlehttp/guzzle suggests installing psr/log (Required for using the Log middleware)
Writing lock file
Generating autoload files
$ php test.php
PHP Fatal error:  Uncaught Error: Class 'Google\Auth\OAuth2' not found in /home/project/iap-php/test.php:18
Stack trace:
#0 {main}
  thrown in /home/project/iap-php/test.php on line 18

nohn avatar Oct 10 '17 04:10 nohn

Hi all. I have confirmed that @bshaffer 's fix to google-auth-library-php enables a successful IAP call.

Here is a code snippet that I have tested and works.

Make sure to add "google/auth":"dev-additional-claims as v1.0.1" to the require section of your composer.json file before running this. Also make sure to visit https://console.cloud.google.com/iam-admin/iap/ and enable IAP for an application of your choice and give your Service Account key access to this newly protected site. Fetch your client ID by clicking on the ellipses button next to your App Engine app listing on the IAP page and clicking 'Edit OAuth client.'

We will be adding this code sample to https://github.com/GoogleCloudPlatform/php-docs-samples soon.

ryanmats avatar Oct 10 '17 20:10 ryanmats

@nohn you need to include the autoloader at the top of test.php

require __DIR__ . '/vendor/autoload.php';

bshaffer avatar Oct 23 '17 04:10 bshaffer