diff --git a/src/IdentityAuthClient.js b/src/IdentityAuthClient.js new file mode 100644 index 0000000000..94109ff265 --- /dev/null +++ b/src/IdentityAuthClient.js @@ -0,0 +1,60 @@ +/* +Copyright 2019 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import MatrixClientPeg from './MatrixClientPeg'; + +export default class IdentityAuthClient { + constructor() { + this.accessToken = null; + } + + hasCredentials() { + return this.accessToken != null; // undef or null + } + + // Returns a promise that resolves to the access_token string from the IS + async getAccessToken() { + let token = this.accessToken; + if (!token) { + token = window.localStorage.getItem("mx_is_access_token"); + } + + if (!token) { + return this.registerForToken(); + } + + try { + return await this._checkToken(token); + } catch (e) { + return await this.registerForToken(); + } + } + + _checkToken(token) { + // TODO: Test current API token via /account endpoint + return token; + } + + async registerForToken() { + const hsOpenIdToken = await MatrixClientPeg.get().getOpenIdToken(); + const { access_token: isAccessToken } = + await MatrixClientPeg.get().registerWithIdentityServer(hsOpenIdToken); + await this._checkToken(isAccessToken); + this.accessToken = isAccessToken; + window.localStorage.setItem("mx_is_access_token", isAccessToken); + return isAccessToken; + } +} diff --git a/src/components/views/dialogs/AddressPickerDialog.js b/src/components/views/dialogs/AddressPickerDialog.js index 53e630ff8f..38b478d462 100644 --- a/src/components/views/dialogs/AddressPickerDialog.js +++ b/src/components/views/dialogs/AddressPickerDialog.js @@ -2,6 +2,7 @@ Copyright 2015, 2016 OpenMarket Ltd Copyright 2017, 2018, 2019 New Vector Ltd Copyright 2019 Michael Telatynski <7t3chguy@gmail.com> +Copyright 2019 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -18,13 +19,15 @@ limitations under the License. import React from 'react'; import PropTypes from 'prop-types'; + import { _t, _td } from '../../../languageHandler'; import sdk from '../../../index'; import MatrixClientPeg from '../../../MatrixClientPeg'; import Promise from 'bluebird'; import { addressTypes, getAddressType } from '../../../UserAddress.js'; import GroupStore from '../../../stores/GroupStore'; -import * as Email from "../../../email"; +import * as Email from '../../../email'; +import IdentityAuthClient from '../../../IdentityAuthClient'; const TRUNCATE_QUERY_LIST = 40; const QUERY_USER_DIRECTORY_DEBOUNCE_MS = 200; @@ -513,13 +516,11 @@ module.exports = React.createClass({ if (cancelled) return null; try { - const hsAccountToken = await MatrixClientPeg.get().getOpenIdToken(); + const authClient = new IdentityAuthClient(); + const isAccessToken = await authClient.getAccessToken(); if (cancelled) return null; - const isAccountToken = await MatrixClientPeg.get().registerWithIdentityServer(hsAccountToken); - if (cancelled) return null; - - const lookup = await MatrixClientPeg.get().lookupThreePid(medium, address, undefined, isAccountToken); + const lookup = await MatrixClientPeg.get().lookupThreePid(medium, address, undefined, isAccessToken); if (cancelled || lookup === null || !lookup.mxid) return null; const profile = await MatrixClientPeg.get().getProfileInfo(lookup.mxid);