apigee-auth

Apigee-Auth retrieves Apigee OAuth2 Access Tokens and automatically utilize refresh tokens when the current access token has expired so that we can always retrieve the next valid access token.

Usage no npm install needed!

<script type="module">
  import apigeeAuth from 'https://cdn.skypack.dev/apigee-auth';
</script>

README

Apigee-Auth

Apigee-Auth retrieves Apigee OAuth2 Access Tokens from a username/password. It automatically utilize refresh tokens when the current access token has expired so that we can always retrieve the next valid access token.

Install

$ npm install apigee-auth

Basic Usage

const ApigeeAuth = require('apigee-auth')
const apigeeAuth = new ApigeeAuth('username', 'password')

apigeeAuth.getToken().then(response => {
  console.log(response.access_token)
})

2FA/MFA tokens (optional)

If you have two factor authentication enabled, you could pass in your TOTP secret as the 3rd parameter.

const apigeeAuth = new ApigeeAuth('username', 'password', 'your-2fa-totp-secret')

Please note that if you are using 2FA this way, you are no longer using two factor authentication because you may have compromised your 2FA secret by storing your 2FA secret outside your mobile device or mobile key.

This optional parameter is provided as convenience if you must require 2FA in your logins but still want to automate Apigee Management APIs in your environment.

Reference

getToken()

Creating a reusable axios instance

If you use axios, you could create a reusable instance using axios interceptors:

const client = axios.create()

client.interceptors.request.use(
  async config => {
    const { access_token } = await apigeeAuth.getToken()
    return Promise.resolve({
      ...config,
      headers: {
        Authorization: `Bearer ${access_token}`
      }
    })
  },
  err => Promise.reject(err)
)