Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@advanced-rest-client/api-authorization-method

advanced-rest-client20Apache-2.00.1.5TypeScript support: included

An element to render an UI for various authorization methods with support of AMF data model for web APIs

web-components, authorization-forms, http, oauth1, oauth2, basic, ntlm, api, amf

readme

api-authorization-method

A web component that extends @advanced-rest-client/authorization-method to add ability to process API security configuration. The component works with the AML model generated by AMF parser. After applying amf and security properties to the element it, if possible, determines what are the authorization settings for the method, and applies default values.

For example, OAuth 2 can be configured in a number of different ways. When the security model is applied to the element it renders only those properties that the server requires to authenticate the user.

This element adds support for the following security description:

  • OAuth 2
  • OAuth 2 with annotation (see RAML's docs)
  • OAuth 1
  • RAML's custom scheme
  • Pass Through
  • Api Key (OAS)
  • Bearer (OAS)

This component fully support OAS security schemes.

Usage

The component extends @advanced-rest-client/authorization-method package to add API model support. The base component renders basic authorization methods. The element requires to apply AML's JSON+LD model to the amf property and scheme definition to the security property.

Installation

npm install --save @advanced-rest-client/api-authorization-method

In an html file

<html>
  <head>
    <script type="module">
      import '@advanced-rest-client/api-authorization-method/api-authorization-method.js';
    </script>
  </head>
  <body>
    <api-authorization-method type="OAuth 2" redirecturi="..."></api-authorization-method>
    <script>
    (async () => {
      const model = await getAmfModel();
      const oauthSecurity = getSecurity(model, '/endpoint', 'get');
      const element = document.querySelector('api-authorization-method');
      element.amf = model;
      element.security = oauthSecurity;
      element.onchange = (e) => {
        console.log(e.target.validate(), e.target.serialize());
      };
    })();
    </script>
  </body>
</html>

In a LitElement

import { LitElement, html } from 'lit-element';
import '@advanced-rest-client/api-authorization-method/api-authorization-method.js';

class SampleElement extends LitElement {
  static get properties() {
    return {
      amfModel: { type: Array },
      endpoint: { type: String },
      method: { type: String },
    };
  }

  get security() {
    const { amfModel, endpoint, method } = this;
    return this.readSecurityFor(amfModel, endpoint, method);
  }

  readSecurityFor(amf, endpoint, method) {
    // implement me
  }

  render() {
    const { amfModel, security } = this;
    return html`
    <api-authorization-method
      type="OAuth 2"
      .amf="${amfModel}"
      .security="${security}"
      @change="${this._securityChangeHandler}"></api-authorization-method>
    `;
  }

  _securityChangeHandler(e) {
    console.log('current authorization settings', e.target.serialize());
  }
}
customElements.define('sample-element', SampleElement);

Applying AMF model

First step is to pass the whole generated AMF model to the amf property. It is required to properly resolve internal model dependencies and to properly read keys in JSON+LD compact model.

Second step is to extract the correct security definition for a method. It is added to a http://a.ml/vocabularies/apiContract#supportedOperation object. The security setting that should be applied to the security property has type of http://a.ml/vocabularies/security#ParametrizedSecurityScheme.

An example script that applies the values can look like the following.

<api-authorization-method type="OAuth 2" id="auth"></api-authorization-method>
<script>
(async () => {
  const model = await getAmfModelSomehow();
  const security = readSecurityFor(model, '/endpoint', 'GET');
  const method = document.getElementById('auth');
  method.amf = model;
  method.security = security;
})();
</script>

The getAmfModelSomehow() function can download pre-generated model or run AMF parser directly from RAML or OAS specification. Then the readSecurityFor() function looks for security definition in /endpoint endpoint, inside GET method. When ready the values are applied to the element.

The order of setting type, amf, and security parameters doesn't matter as the data processing starts asynchronously. However, if the type does not match passed security the security settings is ignored.

A note on clearing settings property. When an undefined or any incompatible value is set to the settings property, the view won't change. Incompatible value is just ignored. Remove the element from the DOM if no longer applicable, change type property to something else, or apply new settings with the new values.

Exception

OAS' Api Key method support logical AND operation. This means that in this case the security parameter should receive the array of defined for the operation security schemes. That is, the array of items that is under security.scheme property when accessing security definition in the AMF model.

See demo/index.js for an example of how this is handled (setData() function).

Development

git clone https://github.com/advanced-rest-client/api-authorization-method
cd api-authorization-method
npm install

Running the demo locally

npm start

Running the tests

npm test

API components

This components is a part of API components ecosystem

changelog

0.1.0 (2019-12-17)

Update

  • initial commit - work in progress 1197a4f by Pawel

0.1.1 (2019-12-17)

Build

  • bumping version 09ecc72 by Pawel Psztyc

Update

  • updating base component ade9f08 by Pawel Psztyc
  • added types f3a18d9 by Pawel Psztyc
  • adding validation to custom scheme cc40aad by Pawel Psztyc
  • adding parameters update function to custom method 6281869 by Pawel Psztyc
  • upgrading to AMF 4 adddfe0 by Pawel Psztyc
  • starting writing tests bddf970 by Pawel
  • removing unused API files 30ed54c by Pawel
  • updating remplates for custom/Pass Through f054850 by Pawel
  • finalizing oauth 2 implementation 367e77b by Pawel Psztyc
  • adding commit config 630f635 by Pawel Psztyc
  • finalizing OAuth2 API support 3413faa by Pawel Psztyc
  • starting working on OAuth 2 client c727287 by Pawel Psztyc
  • initial commit - work in progress 1197a4f by Pawel

Documentation

  • fixing misspell in readme file d819e22 by Pawel Psztyc
  • updating documentation f88c595 by Pawel Psztyc
  • updating readme file 99499a7 by Pawel
  • updating readme file a5bf910 by Pawel Psztyc

Features

  • adding support for OAuth 1 0b08c64 by Pawel
  • adding support for pass through authorization 9bd8daf by Pawel Psztyc

Bug Fixes

  • fixing condition check ac8f017 by Pawel Psztyc

Testing

  • adding Travis configuration to connect to Sauce Labs f85951c by Pawel Psztyc
  • adding SL intergation files and scripts 0503b1f by Pawel Psztyc
  • finalizing tests for the element 50136f7 by Pawel Psztyc
  • adding more tests a2c807d by Pawel
  • finishing tests for custom schemes 7b2b056 by Pawel Psztyc

0.1.1 (2020-01-22)

Build

  • bumping version 09ecc72 by Pawel Psztyc

Update

  • upgrading depdnencies b15014b by Pawel Psztyc
  • updating dependencies 790fa8e by Pawel Psztyc
  • updating base component ade9f08 by Pawel Psztyc
  • added types f3a18d9 by Pawel Psztyc
  • adding validation to custom scheme cc40aad by Pawel Psztyc
  • adding parameters update function to custom method 6281869 by Pawel Psztyc
  • upgrading to AMF 4 adddfe0 by Pawel Psztyc
  • starting writing tests bddf970 by Pawel
  • removing unused API files 30ed54c by Pawel
  • updating remplates for custom/Pass Through f054850 by Pawel
  • finalizing oauth 2 implementation 367e77b by Pawel Psztyc
  • adding commit config 630f635 by Pawel Psztyc
  • finalizing OAuth2 API support 3413faa by Pawel Psztyc
  • starting working on OAuth 2 client c727287 by Pawel Psztyc

Documentation

  • fixing misspell in readme file d819e22 by Pawel Psztyc
  • updating documentation f88c595 by Pawel Psztyc
  • updating readme file 99499a7 by Pawel
  • updating readme file a5bf910 by Pawel Psztyc

Features

  • adding support for OAuth 1 0b08c64 by Pawel
  • adding support for pass through authorization 9bd8daf by Pawel Psztyc

Bug Fixes

  • fixing condition check ac8f017 by Pawel Psztyc

Testing

  • adding Travis configuration to connect to Sauce Labs f85951c by Pawel Psztyc
  • adding SL intergation files and scripts 0503b1f by Pawel Psztyc
  • finalizing tests for the element 50136f7 by Pawel Psztyc
  • adding more tests a2c807d by Pawel
  • finishing tests for custom schemes 7b2b056 by Pawel Psztyc

0.1.2 (2020-01-23)

Build

  • bumping version 63d1bb3 by Pawel Psztyc
  • bumping version 09ecc72 by Pawel Psztyc

Continuous integration

  • updated Travis configuration to connect to Sauce Labs 0cee240 by Pawel Psztyc
  • updating travis config 9d681dc by Pawel Psztyc

Update

  • upgrading amf-helper-mixin 15803b9 by Pawel Psztyc
  • [ci skip] automated merge master->stage. syncing main branches 16abb12 by Ci agent
  • upgrading depdnencies b15014b by Pawel Psztyc
  • updating dependencies 790fa8e by Pawel Psztyc
  • updating base component ade9f08 by Pawel Psztyc
  • added types f3a18d9 by Pawel Psztyc
  • adding validation to custom scheme cc40aad by Pawel Psztyc
  • adding parameters update function to custom method 6281869 by Pawel Psztyc
  • upgrading to AMF 4 adddfe0 by Pawel Psztyc
  • starting writing tests bddf970 by Pawel
  • removing unused API files 30ed54c by Pawel
  • updating remplates for custom/Pass Through f054850 by Pawel
  • finalizing oauth 2 implementation 367e77b by Pawel Psztyc
  • adding commit config 630f635 by Pawel Psztyc
  • finalizing OAuth2 API support 3413faa by Pawel Psztyc
  • starting working on OAuth 2 client c727287 by Pawel Psztyc

Documentation

  • fixing misspell in readme file d819e22 by Pawel Psztyc
  • updating documentation f88c595 by Pawel Psztyc
  • updating readme file 99499a7 by Pawel
  • updating readme file a5bf910 by Pawel Psztyc

Features

  • adding OAS' Api Key scheme ea24e9a by Pawel Psztyc
  • adding support for OAuth 1 0b08c64 by Pawel
  • adding support for pass through authorization 9bd8daf by Pawel Psztyc

Bug Fixes

  • fixing condition check ac8f017 by Pawel Psztyc

Testing

  • adding Travis configuration to connect to Sauce Labs f85951c by Pawel Psztyc
  • adding SL intergation files and scripts 0503b1f by Pawel Psztyc
  • finalizing tests for the element 50136f7 by Pawel Psztyc
  • adding more tests a2c807d by Pawel
  • finishing tests for custom schemes 7b2b056 by Pawel Psztyc

0.1.3 (2020-02-13)

Build

  • bumping version 53b6597 by Pawel
  • bumping version 63d1bb3 by Pawel Psztyc

Continuous integration

  • updated Travis configuration to connect to Sauce Labs 0cee240 by Pawel Psztyc
  • updating travis config 9d681dc by Pawel Psztyc

Update

  • updating husky 119e735 by Pawel
  • adding a function to check whether model is a RAML model ff49fe0 by Pawel
  • adding forces settings reset after grant types are updated 28d638d by Pawel
  • updating husky 385bdaf by Pawel
  • adding support for OAS' OAuth 2 flows 6622175 by Pawel Psztyc
  • [ci skip] automated merge master->stage. syncing main branches 6c0c260 by Ci agent
  • upgrading amf-helper-mixin 15803b9 by Pawel Psztyc
  • [ci skip] automated merge master->stage. syncing main branches 16abb12 by Ci agent
  • upgrading depdnencies b15014b by Pawel Psztyc
  • updating dependencies 790fa8e by Pawel Psztyc

Documentation

  • updating readme file 947edd0 by Pawel

Features

  • adding OAS' Api Key scheme ea24e9a by Pawel Psztyc

Bug Fixes

  • adding type check for security settings 999e77c by Pawel

Testing

  • adding tests for OAS grant types (flows) 3a52bac by Pawel

0.1.4 (2020-02-13)

Build

Update

  • [ci skip] automated merge master->stage. syncing main branches 6254d5e by Ci agent
  • updating husky 119e735 by Pawel
  • adding a function to check whether model is a RAML model ff49fe0 by Pawel
  • adding forces settings reset after grant types are updated 28d638d by Pawel
  • updating husky 385bdaf by Pawel
  • adding support for OAS' OAuth 2 flows 6622175 by Pawel Psztyc
  • [ci skip] automated merge master->stage. syncing main branches 6c0c260 by Ci agent

Documentation

  • updating readme file 947edd0 by Pawel

Features

  • adding support for OAS bearer token 1206bb7 by Pawel

Bug Fixes

  • adding type check for security settings 999e77c by Pawel

Testing

  • adding timeout command to SL tests ee06589 by Pawel
  • adding tests for OAS grant types (flows) 3a52bac by Pawel

0.1.5 (2020-02-13)

Build

  • bumping version 03f021f by Pawel Psztyc
  • bumping version 66ae357 by Pawel

Continuous integration

  • increasing mocha timeout to 10s c1232da by Pawel Psztyc

Update

  • adding types for API Key eee282c by Pawel Psztyc
  • [ci skip] automated merge master->stage. syncing main branches cb66d03 by Ci agent
  • [ci skip] automated merge master->stage. syncing main branches 6254d5e by Ci agent

Features

  • adding clear() method 6c6298e by Pawel Psztyc
  • adding support for OAS bearer token 1206bb7 by Pawel

Testing

  • adding timeout command to SL tests ee06589 by Pawel