Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Latest commit

 

History

History
51 lines (40 loc) · 1.34 KB

README.md

File metadata and controls

51 lines (40 loc) · 1.34 KB

@rtbhouse/google-iap-auth

Helper library to perfrorm requests to OIDC-authenticated resources (Cloud Identity-Aware Proxy)

Installation

npm install @rtbhouse/google-iap-auth

Usage

To use this library you must have the following:

  • Identity Aware Proxy protected resource
  • Service account with permissions to read protected resource
  • OAuth credentials with key file in JSON format ( more on generating Service Account json keys)

Example usage with got:

import fs from 'fs';
import got from "got";
import { GoogleIapAuth } from "@rtbhouse/google-iap-auth";


const keyStr = fs.readFileSync('key.json', 'utf-8');
const keyData = JSON.parse(keyStr);
const googleIapAuth = new GoogleIapAuth("<oauth_client_id>", keyData);
const authorizedGot = got.extend({
  hooks: {
    beforeRequest: [
      async options => {
        options.headers.Authorization = `Bearer ${await googleIapAuth.getToken()}`;
      }
    ]
  },
  responseType: "json",
  followRedirect: false,
  mutableDefaults: true
});

(async () => {
  const response = await authorizedGot(
    "https://some.iap.protected.resource.com/"
  );
  console.log(response.statusCode);
  console.log(response.body);
})();