Giter VIP home page Giter VIP logo

oada-id-client-js's Introduction

@oada/id-client

Coverage Status npm Downloads/week code style: prettier License

TypeScript/JavaScript client library for OADA identity. Can be used both in NodeJS and in the browser.

Getting Started

Installation

For use in NodeJS or with something like webpack:

yarn add @oada/id-client

Browser Code Generation

The code to use in the browser can be generated with the following command:

yarn bundle

This will create the file bundle.js.

Examples

High-Level Node.JS wrapper Usage

This version of the library wraps the core functionality for easy use in typical Node.JS uses.

It will pop up a window using the default browser to take the user through the needed flows and the return the resulting token(s).

getIDToken(domain, options)

Asynchronous function for generating an ID token request against an OADA identity provider.

Parameters

domain string of domain with which to log in the user.

options object containing at least the following properties:

Optional OpenID Connect parameters placed in options as string properties will be used (e.g. display, prompt, login_hint).

Usage Example

const options = {
  metadata: {
    /* See spec linked above */
  },
};

const domain = /* Set domain based on text box, dropdown, etc. */;

// Promise will resolve after user completes the flow in the browser
const idToken = await oadaIdClient.getIDToken(domain, options);
console.dir(idToken);

getAccessToken(domain, options)

Asynchronous function for generating an access token request against an OADA compliant API.

Parameters

domain string of domain from which to get an OADA API access token. The value passed to the function can be overridden by a query or form parameter with a name of domain.

options object containing at least the following properties:

  • metadata object containing client metadata, or string of a software_statement JWT
  • scope space separated string of OAuth scopes for the request access token to have.
  • privateKey a private JWK for use in the JWT bearer client auth (required for code flow)
  • params Optional OpenID Connect parameters placed in params as string properties will be used (e.g. display, prompt, login_hint)

Usage Example

const options = {
  metadata: {
    /* See spec linked above */
  },
  scope: 'some.oada.defined.scope',
};

const domain = /* Set domain based on text box, dropdown, etc. */;

// Promise will resolve after user completes the flow in the browser
const accessToken = await oadaIdClient.getAccessToken(domain, options);
console.dir(accessToken);

Connect Style "Middleware" Wrapper Usage

This version of the library wraps the core functionality for use as connect style "middleware". This can be used in a Node.JS server using a compatible web development framework, such as express.

For a working example of using this wrapper, see the on server example.

getIDToken(domain, options)

Middleware for generating an ID token request against an OADA identity provider.

Parameters

domain string of domain with which to log in the user. The value passed to the function can be overridden by a query or form parameter with a name of domain.

options object containing at least the following properties:

Usage Example

const options = {
  metadata: {
    /* See spec linked above */
  },
  privateKey: {
    pem: fs.readFileSync('/path/to/key.pem'),
    kid: 'key_id_corresponding_to_pem',
  },
};

app.use(
  '/getIdToken',
  oadaIdClient.getIDToken('some.oada-identity-provider.com', options)
);

getAccessToken(domain, options)

Middleware for generating an access token request against an OADA compliant API.

Parameters

domain string of domain from which to get an OADA API access token. The value passed to the function can be overridden by a query or form parameter with a name of domain.

options object containing at least the following properties:

  • metadata object containing client metadata, or string of a software_statement JWT
  • privateKey a private JWK for use in the JWT bearer client auth (required for code flow)
  • scope space separated string of OAuth scopes for the request access token to have.
  • params Optional OpenID Connect parameters placed in params as string properties will be used (e.g. display, prompt, login_hint)

Usage Example

const options = {
  metadata: {
    /* See spec linked above */
  },
  privateKey: {
    pem: fs.readFileSync('/path/to/key.pem'),
    kid: 'key_id_corresponding_to_pem',
  },
  scope: 'some.oada.defined.scope',
};

app.use(
  '/getAccessToken',
  oadaIdClient.getAccessToken('some.oada-cloud-provider.com', options)
);

handleRedirect()

Middleware for handling redirects from getIDToken or getAccessToken middlewares. In most cases, you will apply this middleware in two locations, one to receive getIDToken redirects and another to receive getAccessToken redirects.

Usage Example

// Handle ID token redirects
app.use(
  '/url/referenced/by/getIDToken/redirect_uri',
  oadaIdClient.handleRedirect()
);
app.use(
  '/url/referenced/by/getIDToken/redirect_uri',
  function (req, res, next) {
    // ID token is in req.token
    console.dir(req.token);
  }
);

// Handle access token redirects
app.use(
  '/url/referenced/by/getAccessToken/redirect_uri',
  oadaIdClient.handleRedirect()
);
app.use(
  '/url/referenced/by/getAccessToken/redirect_uri',
  function (req, res, next) {
    // Access token is in req.token
    console.dir(req.token);
  }
);

Browser Wrapper Usage

This version of the library wraps the core functionality for easy use in the browser.

For a working example of using this wrapper, see the in browser example.

getIDToken(domain, options)

Asynchronous function for generating an ID token request against an OADA identity provider.

Parameters

domain string of domain with which to log in the user.

options object containing at least the following properties:

Optional OpenID Connect parameters placed in options as string properties will be used (e.g. display, prompt, login_hint).

Usage Example

const options = {
  metadata: {
    /* See spec linked above */
  },
};

const domain = /* Set domain based on text box, dropdown, etc. */;

const idToken = await oadaIdClient.getIDToken(domain, options);
console.dir(idToken);

getAccessToken(domain, options)

Asynchronous function for generating an access token request against an OADA compliant API.

Parameters

domain string of domain from which to get an OADA API access token. The value passed to the function can be overridden by a query or form parameter with a name of domain.

options object containing at least the following properties:

Usage Example

const options = {
  metadata: {
    /* See spec linked above */
  },
  scope: 'some.oada.defined.scope',
};

const domain = /* Set domain based on text box, dropdown, etc. */;

const accessToken = await oadaIdClient.getAccessToken(domain, options);
console.dir(accessToken);

handleRedirect()

Function for handling redirects generated by getIDToken or getAccessToken function. Simply needs to be called by the page served from the URL corresponding to redirect_uri.

Usage Example

<!-- Page served at redirect_uri for getIDToken and/or getAccessToken -->
<html>
  <head>
    <script src="path/to/library/browser/code.js"></script>
    <script>
      oadaIdClient.handleRedirect();
    </script>
  </head>
</html>

Base Library Usage

Not yet documented.

References

  1. OpenID Connect Core 1.0
  2. OAuth 2.0 Authorization Framework
  3. JSON Web Key (JWK) Draft 31
  4. OAuth 2.0 Dynamic Client Registration Protocol

oada-id-client-js's People

Contributors

abalmos avatar aultac avatar awlayton avatar cyrusbowman avatar dependabot[bot] avatar sanoel avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

oada-id-client-js's Issues

Switch to webpack

Browser version currently uses browserify to build, which is not really a thing anymore.

Missing return on line 40 of core.js

Looks like callback will be called twice if it ever gets inside that if statement because it will go on executing the rest of the function. Line 40 of core.js.

/userinfo

We should support fetching the /userinfo document for an OpenID Connect flow with and profile type scope.

Allow redirecting to the original page

The test right now have a starting page test/index.html and a separate page test/redirect.html for the OAUTH redirect_uri. It would be nice to make the library smart enough to handle both being test/index.html (for example).

ID token handling

The library should decode/parse the ID token before giving it to the client.

Should this be 3 separate modules?

I'm thinking core.js, middleware.js, and browser.js should each be their own node module with the latter 2 depending on core.js.

Thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.