attraction/google-iap-authentication

Authenticate users using Google IAP

2.0.0 2021-01-13 16:36 UTC

This package is auto-updated.

Last update: 2024-04-13 23:56:56 UTC


README

A simple class to help you authenticate the users accessing your ressources thru Google Identity-Aware Proxy.

Installation

It's recommended that you use Composer to install google-iap-authentication.

$ composer require attraction/google-iap-authentication:^2.0

This will install google-iap-authentication and all required dependencies. google-iap-authentication requires PHP 7.3 or newer.

Usage

use Attraction\GoogleIAPAuthentication;

// Project ID and Number are available in Google Cloud Project Settings - https://console.cloud.google.com/iam-admin/settings
$projectId = 'YOUR_PROJECT_ID';
$projectNumber = 'YOUR_PROJECT_NUMBER';

// You can also use your preferred framework to fetch the headers, here we use getallheaders() to make it simple.
$idToken = getallheaders()['X-Goog-Iap-Jwt-Assertion'] ?? '';

$iap = new GoogleIAPAuthentication($projectId, $projectNumber);

try {
    $user = $iap->validateAssertion($idToken);
} catch(Throwable $e) {
    print(sprintf('Unable to proceed: %s',$e->getMessage()));
}

print(sprintf('User logged in - Email Address: %s - User ID: %s',$user->emailAddress,$user->userId));