nuxeo / nuxeo-js-client

JavaScript client library for Nuxeo API
Other
18 stars 20 forks source link

Add Requesting an Access Token with a JWT #88

Closed raymond10 closed 4 years ago

raymond10 commented 5 years ago

Hi @troger ,

I open a new issue to make an evolution of nuxeo-js-client about my question : https://answers.nuxeo.com/general/q/f89b2c3d6f594909b5a6bcb8e2ceff0c/nuxeo-js-client-OAuth-2-JWT-Flow

In nuxeo/lib/oauth2.js, in const oauth2 = ... can you add this ?

/**
   * Fetches an OAuth2 access token.
   * @param {string} [baseURL] - Base URL of the Nuxeo Platform.
   * @param {string} [clientId] - The OAuth2 client id.
   * @param {string} [jwtToken] - An JSON web token.
   * @param {object} [params] - Optional parameters.
   * @returns {string}
   */
  fetchAccessToken: (baseURL, clientId, jwtToken, params = {}) => {
    if (!baseURL) {
      throw new Error('Missing `baseURL` argument');
    }
    if (!clientId) {
      throw new Error('Missing `clientId` argument');
    }
    if (!jwtToken) {
      throw new Error('Missing `jwtToken` argument');
    }

    const defaultParams = { assertion, grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', client_id: clientId };
    const body = extend(true, defaultParams, params);
    return fetchAccessToken(baseURL, body);
  }

In nuxeo/nuxeo.js, var oauth2 = ... add this too.

/**
   * Fetches an OAuth2 access token.
   * @param {string} [baseURL] - Base URL of the Nuxeo Platform.
   * @param {string} [clientId] - The OAuth2 client id.
   * @param {string} [jwtToken] - An JSON web token.
   * @param {object} [params] - Optional parameters.
   * @returns {string}
   */
  fetchAccessToken: (baseURL, clientId, jwtToken, params = {}) => {
    if (!baseURL) {
      throw new Error('Missing `baseURL` argument');
    }
    if (!clientId) {
      throw new Error('Missing `clientId` argument');
    }
    if (!jwtToken) {
      throw new Error('Missing `jwtToken` argument');
    }

    const defaultParams = { assertion, grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', client_id: clientId };
    const body = extend(true, defaultParams, params);
    return fetchAccessToken(baseURL, body);
  }

==Update== According this issue : https://answers.nuxeo.com/general/q/75fb1d38fbc048f59cf01d895f004335/Requesting-an-access-token-with-a-jwt, it seems that grant_type and assertion is not supported yet in nuxeo ? I saw in code package org.nuxeo.ecm.platform.oauth2.Constants and org.nuxeo.ecm.platform.oauth2.NuxeoOAuth2Servlet @troger is the main contributor

Thanks for your reply

troger commented 5 years ago

I've created https://jira.nuxeo.com/browse/NXJS-187.

raymond10 commented 5 years ago

@troger ,

I see that the jira is assigned but the last inrerraction is on June.

Thanks

raymond10 commented 4 years ago

hi @troger ,

the jira still unresolved since 05/04/19. Have an information about the resolution ?

thanks

troger commented 4 years ago

Hi @raymond10 ,

We've started to add the helpers, work is in progress in https://github.com/nuxeo/nuxeo-js-client/pull/95.

Will be backported on the 3.x version when done.

troger commented 4 years ago

Hi,

New version 3.16.1 has been released with the helper to acquire an OAuth 2.0 access token with a JWT token. https://www.npmjs.com/package/nuxeo/v/3.16.1

You can now use Nuxeo.oauth2.fetchAccessTokenFromJWTToken(baseURL, clientId, jwtToken[, params]).