1.1.1 • Published 4 years ago

vince-module-devhelper-authstring v1.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

Dev Helper - Auth String

When testing integrations to external connections, we can use this helper. For instance when testing projects based on vince-module-m3api-coordinator or vince-module-m3api-rest modules.

It returns a URI and an authentication string that can be passed as a HTTP authorization header.

It should ONLY be used during development, not in production.

Features

  • Supports OAuth2 Resource Owner Password Grant (like M3 MT) or HTTP Basic Authentication (like M3 on-prem)
  • OAuth2: Automatically requests a new token or refreshes the token if it's about to expire, or already expired.
  • OAuth2: Caches the connection to reduce HTTP requests

Possible connections

Currently the following connections names are available:

  • M3_MT_IONAPIGW M3 Multi Tenant VINCE_DEM via ION API Gateway as VINADM37 user
  • M3_ST_IONAPIGW M3 Single Tenant Primeport via ION API Gateway as FSHUSR10 user
  • M3_ST_DIRECT M3 Single Tenant Primeport via direct connection ("on-prem"-like) as FSHUSR10 user

More can be added if needed, for simple HTTP services such as NOBB REST API, etc.

Installing & Running

npm install --save-dev bitbucket:vincesoftware/vince-module-devhelper-authstring

In test scripts, do something like this:

import {getConnection} from 'vince-module-devhelper-authstring';

test('Test', async () => {
    const connection = await getConnection('M3_MT_IONAPIGW');

    const result = await axios.get(connection.baseUri + '/m3api-rest/execute/MRS001MI/GetUserInfo', {
        headers: {
            authorization: connection.authString
        }
    });
    expect(result.data).toBeDefined();
})

Response format

{
    baseUri: 'https://https://mingle-ionapi.eu1.inforcloudsuite.com/VINCE_DEM/M3',
    authString: 'Bearer AOSUDhashdhasdLONGASSTOKEN',
    tokenSource: 'NEW' // can be either NEW, REFRESH, CACHE (for OAuth2) or undefined (for Basic Authentication)
}

tokenSource can be relevant to use in tests in certain scenarios.

NOTE that the helper will output some logging to the console.

Running inside a Bitbucket Pipeline

Your repository SSH key needs to be added to this repositories SSH keys here

Instructions here

Script

If needed, the authString can be extracted using command line, for use in a pipeline

pipelines:
  default:
    - step:
        name: build and test
        caches:
          - node
        script:
          - npm ci
          #### helper begins here
          # install the helper
          - npm install --save-dev bitbucket:vincesoftware/vince-module-devhelper-authstring
          # set environemnt variable AUTH_STRING 
          - export AUTH_STRING=$(node node_modules/vince-module-devhelper-authstring/lib/index.js --auth M3_ST_DIRECT)
          # set environemnt variable BASE_URI
          - export BASE_URI=$(node node_modules/vince-module-devhelper-authstring/lib/index.js --uri M3_ST_DIRECT)
          #### helper ends here
          # now build with these environment variables
          - npm run build
          - npm run test
1.1.1

4 years ago