diff --git a/src/base-apis.js b/src/base-apis.js index 3dc8ced2a..2892d9af7 100644 --- a/src/base-apis.js +++ b/src/base-apis.js @@ -1803,10 +1803,11 @@ MatrixBaseApis.prototype.registerWithIdentityServer = function(hsOpenIdToken) { }; /** - * Requests an email verification token directly from an Identity Server. + * Requests an email verification token directly from an identity server. * - * Note that the Homeserver offers APIs to proxy this API for specific - * situations, allowing for better feedback to the user. + * This API is used as part of binding an email for discovery on an identity + * server. The validation data that results should be passed to the + * `bindThreePid` method to complete the binding process. * * @param {string} email The email address to request a token for * @param {string} clientSecret A secret binary string generated by the client. @@ -1818,12 +1819,12 @@ MatrixBaseApis.prototype.registerWithIdentityServer = function(hsOpenIdToken) { * @param {string} nextLink Optional If specified, the client will be redirected * to this link after validation. * @param {module:client.callback} callback Optional. - * @param {string} identityAccessToken The `access_token` field of the Identity - * Server `/account/register` response (see {@link registerWithIdentityServer}). + * @param {string} identityAccessToken The `access_token` field of the identity + * server `/account/register` response (see {@link registerWithIdentityServer}). * * @return {module:client.Promise} Resolves: TODO * @return {module:http-api.MatrixError} Rejects: with an error response. - * @throws Error if no Identity Server is set + * @throws Error if no identity server is set */ MatrixBaseApis.prototype.requestEmailToken = async function( email, @@ -1864,6 +1865,74 @@ MatrixBaseApis.prototype.requestEmailToken = async function( } }; +/** + * Requests a MSISDN verification token directly from an identity server. + * + * This API is used as part of binding a MSISDN for discovery on an identity + * server. The validation data that results should be passed to the + * `bindThreePid` method to complete the binding process. + * + * @param {string} phoneCountry The ISO 3166-1 alpha-2 code for the country in + * which phoneNumber should be parsed relative to. + * @param {string} phoneNumber The phone number, in national or international + * format + * @param {string} clientSecret A secret binary string generated by the client. + * It is recommended this be around 16 ASCII characters. + * @param {number} sendAttempt If an identity server sees a duplicate request + * with the same sendAttempt, it will not send another SMS. + * To request another SMS to be sent, use a larger value for + * the sendAttempt param as was used in the previous request. + * @param {string} nextLink Optional If specified, the client will be redirected + * to this link after validation. + * @param {module:client.callback} callback Optional. + * @param {string} identityAccessToken The `access_token` field of the Identity + * Server `/account/register` response (see {@link registerWithIdentityServer}). + * + * @return {module:client.Promise} Resolves: TODO + * @return {module:http-api.MatrixError} Rejects: with an error response. + * @throws Error if no identity server is set + */ +MatrixBaseApis.prototype.requestMsisdnToken = async function( + phoneCountry, + phoneNumber, + clientSecret, + sendAttempt, + nextLink, + callback, + identityAccessToken, +) { + const params = { + client_secret: clientSecret, + country: phoneCountry, + phone_number: phoneNumber, + send_attempt: sendAttempt, + next_link: nextLink, + }; + + try { + const response = await this._http.idServerRequest( + undefined, "POST", "/validate/msisdn/requestToken", + params, httpApi.PREFIX_IDENTITY_V2, identityAccessToken, + ); + // TODO: Fold callback into above call once v1 path below is removed + if (callback) callback(null, response); + return response; + } catch (err) { + if (err.cors === "rejected" || err.httpStatus === 404) { + // Fall back to deprecated v1 API for now + // TODO: Remove this path once v2 is only supported version + // See https://github.com/vector-im/riot-web/issues/10443 + logger.warn("IS doesn't support v2, falling back to deprecated v1"); + return await this._http.idServerRequest( + callback, "POST", "/validate/msisdn/requestToken", + params, httpApi.PREFIX_IDENTITY_V1, + ); + } + if (callback) callback(err); + throw err; + } +}; + /** * Submits an MSISDN token to the identity server *