From aad6757cb7a3c874c1ac6380043a84445a8a05c5 Mon Sep 17 00:00:00 2001 From: Antonin Cezard Date: Thu, 5 Jan 2023 16:18:45 +0100 Subject: [PATCH] feat: Add new method to fetch konnector token in native context This will allow the flagship app to use a different token on each connector launch, even from the same slug --- docs/api/cozy-stack-client.md | 30 +++++++++++++++++++ packages/cozy-stack-client/src/OAuthClient.js | 18 +++++++++++ 2 files changed, 48 insertions(+) diff --git a/docs/api/cozy-stack-client.md b/docs/api/cozy-stack-client.md index f73e9dc92..9c1be53bd 100644 --- a/docs/api/cozy-stack-client.md +++ b/docs/api/cozy-stack-client.md @@ -1248,6 +1248,7 @@ through OAuth. * [.getAuthCodeURL(options)](#OAuthClient+getAuthCodeURL) ⇒ string * [.getAccessCodeFromURL(pageURL, stateCode)](#OAuthClient+getAccessCodeFromURL) ⇒ string * [.fetchAccessToken(accessCode, oauthOptionsArg, uri, codeVerifier)](#OAuthClient+fetchAccessToken) ⇒ Promise + * [.fetchKonnectorToken(slug)](#OAuthClient+fetchKonnectorToken) ⇒ Promise.<string> * [.fetchSessionCode()](#OAuthClient+fetchSessionCode) ⇒ [Promise.<SessionCodeRes>](#SessionCodeRes) * [.fetchSessionCodeWithPassword()](#OAuthClient+fetchSessionCodeWithPassword) ⇒ [Promise.<SessionCodeRes>](#SessionCodeRes) * [.loginFlagship()](#OAuthClient+loginFlagship) ⇒ Promise.<(AccessTokenRes\|TwoFactorNeededRes\|SessionCodeRes)> @@ -1374,6 +1375,20 @@ Exchanges an access code for an access token. This function does **not** update | uri | string | — To use when OAuthClient is not yet registered (during login process) | | codeVerifier | string | — The PKCE code verifier (see https://docs.cozy.io/en/cozy-stack/auth/#pkce-extension) | + + +### oAuthClient.fetchKonnectorToken(slug) ⇒ Promise.<string> +Used by the flagship application in order to create a token for the konnector with the given slug. +This token can then be used by the client-side konnector to make requests to cozy-stack. +The flagship app will need to use its own access token to request the konnector token. + +**Kind**: instance method of [OAuthClient](#OAuthClient) +**Returns**: Promise.<string> - - A promise that resolves with a new token + +| Param | Type | Description | +| --- | --- | --- | +| slug | string | The slug of the konnector | + ### oAuthClient.fetchSessionCode() ⇒ [Promise.<SessionCodeRes>](#SessionCodeRes) @@ -2316,6 +2331,7 @@ Document representing a io.cozy.oauth.clients * [.getAuthCodeURL(options)](#OAuthClient+getAuthCodeURL) ⇒ string * [.getAccessCodeFromURL(pageURL, stateCode)](#OAuthClient+getAccessCodeFromURL) ⇒ string * [.fetchAccessToken(accessCode, oauthOptionsArg, uri, codeVerifier)](#OAuthClient+fetchAccessToken) ⇒ Promise + * [.fetchKonnectorToken(slug)](#OAuthClient+fetchKonnectorToken) ⇒ Promise.<string> * [.fetchSessionCode()](#OAuthClient+fetchSessionCode) ⇒ [Promise.<SessionCodeRes>](#SessionCodeRes) * [.fetchSessionCodeWithPassword()](#OAuthClient+fetchSessionCodeWithPassword) ⇒ [Promise.<SessionCodeRes>](#SessionCodeRes) * [.loginFlagship()](#OAuthClient+loginFlagship) ⇒ Promise.<(AccessTokenRes\|TwoFactorNeededRes\|SessionCodeRes)> @@ -2442,6 +2458,20 @@ Exchanges an access code for an access token. This function does **not** update | uri | string | — To use when OAuthClient is not yet registered (during login process) | | codeVerifier | string | — The PKCE code verifier (see https://docs.cozy.io/en/cozy-stack/auth/#pkce-extension) | + + +### oAuthClient.fetchKonnectorToken(slug) ⇒ Promise.<string> +Used by the flagship application in order to create a token for the konnector with the given slug. +This token can then be used by the client-side konnector to make requests to cozy-stack. +The flagship app will need to use its own access token to request the konnector token. + +**Kind**: instance method of [OAuthClient](#OAuthClient) +**Returns**: Promise.<string> - - A promise that resolves with a new token + +| Param | Type | Description | +| --- | --- | --- | +| slug | string | The slug of the konnector | + ### oAuthClient.fetchSessionCode() ⇒ [Promise.<SessionCodeRes>](#SessionCodeRes) diff --git a/packages/cozy-stack-client/src/OAuthClient.js b/packages/cozy-stack-client/src/OAuthClient.js index 260ed75c3..e80bb4285 100644 --- a/packages/cozy-stack-client/src/OAuthClient.js +++ b/packages/cozy-stack-client/src/OAuthClient.js @@ -395,6 +395,24 @@ class OAuthClient extends CozyStackClient { return new AccessToken(result) } + /** + * Used by the flagship application in order to create a token for the konnector with the given slug. + * This token can then be used by the client-side konnector to make requests to cozy-stack. + * The flagship app will need to use its own access token to request the konnector token. + * + * @param {string} slug - The slug of the konnector + * @returns {Promise} - A promise that resolves with a new token + */ + async fetchKonnectorToken(slug) { + try { + return await this.fetchJSON('POST', `/auth/tokens/konnectors/${slug}`) + } catch (error) { + throw new Error( + `oAuthClient.fetchKonnectorToken(): Could not create a token for the konnector with slug "${slug}". \n\n${error}` + ) + } + } + /** * @typedef SessionCodeRes * @property {string} session_code The value of the session code