Skip to content
This repository has been archived by the owner on Oct 21, 2021. It is now read-only.

How To Get an Authentication Token for System Users TypeScript Code Sample

Bryan Tarantina edited this page Jun 14, 2018 · 1 revision

How to get an auth token: Typescript code snippet

const request = require('request-promise');
const errors  = require('request-promise/errors');

export async function getToken(mmo_host: string, 
                               mmo_instance_id: string, 
                               mmo_solution_id: string,
                               mmo_organization_id: string,
                               apikey: string): Promise<string> {
  const iamTokenData = await getBluemixIamToken(apikey);
  return await exchangeToken(mmo_host, mmo_instance_id, mmo_solution_id , mmo_organization_id , iamTokenData);
}

export function getBluemixIamToken(apikey: string): Promise<BluemixIamToken> {
  const parameters = {
    grant_type : 'urn:ibm:params:oauth:grant-type:apikey',
    apikey     : apikey
  };
  const requestOptions = {
    uri: 'https://iam.ng.bluemix.net/oidc/token',
    method: 'POST',
    headers: {
      'Accept': 'application/json'
    },
    form: parameters, // adds header 'Content-Type' = 'application/x-www-form-urlencoded'
    json: true
  };
  return request(requestOptions)
    .then((resp: BluemixIamToken) => {
      return resp;
    })
    .catch(errors.StatusCodeError, (rpError: any) => {
      Promise.reject(rpError.message);
    })
    .catch(errors.RequestError, (rpError: any) => {
      Promise.reject(rpError.error);
    });
}

export function exchangeToken(mmo_host: string, 
                              mmo_instance_id: string, 
                              mmo_solution_id: string,
                              mmo_organization_id: string,
                              iamTokenData: BluemixIamToken): Promise<string> {
  const mmo_endpoint_url = 'https://' + mmo_host + '/' + mmo_instance_id + '/onboarding' +
                           '/v1/iam/exchange_token' +
                           '/solution/' + mmo_solution_id + 
                           '/organization/' + mmo_organization_id;
  const requestOptions = {
    uri: mmo_endpoint_url,
    method: 'POST',
    headers: {
      'Accept': 'application/json'
    },
    body: iamTokenData,
    json: true
  };
  return request(requestOptions)
    .then((resp: ExchangeTokenResponse) => {
      return resp.onboarding_token;
    })
    .catch(errors.StatusCodeError, (rpError: any) => {
      Promise.reject(rpError.message);
    })
    .catch(errors.RequestError, (rpError: any) => {
      Promise.reject(rpError.error);
    });
}

export interface BluemixIamToken {
  access_token : string;
  token_type   : string;
  expires_in   : number;
  expiration   : number;
}

export interface ExchangeTokenResponse {
  onboarding_token: string;
}
Clone this wiki locally