/*
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 { createClient, SERVICE_TYPES } from 'matrix-js-sdk';

import MatrixClientPeg from './MatrixClientPeg';
import { Service, startTermsFlow, TermsNotSignedError } from './Terms';

export default class IdentityAuthClient {
    /**
     * Creates a new identity auth client
     * @param {string} identityUrl The URL to contact the identity server with.
     * When provided, this class will operate solely within memory, refusing to
     * persist any information such as tokens. Default null (not provided).
     */
    constructor(identityUrl = null) {
        this.accessToken = null;
        this.authEnabled = true;

        if (identityUrl) {
            // XXX: We shouldn't have to create a whole new MatrixClient just to
            // do identity server auth. The functions don't take an identity URL
            // though, and making all of them take one could lead to developer
            // confusion about what the idBaseUrl does on a client. Therefore, we
            // just make a new client and live with it.
            this.tempClient = createClient({
                baseUrl: "", // invalid by design
                idBaseUrl: identityUrl,
            });
        } else {
            // Indicates that we're using the real client, not some workaround.
            this.tempClient = null;
        }
    }

    get _matrixClient() {
        return this.tempClient ? this.tempClient : MatrixClientPeg.get();
    }

    _writeToken() {
        if (this.tempClient) return; // temporary client: ignore
        window.localStorage.setItem("mx_is_access_token", this.accessToken);
    }

    _readToken() {
        if (this.tempClient) return null; // temporary client: ignore
        return window.localStorage.getItem("mx_is_access_token");
    }

    hasCredentials() {
        return this.accessToken != null; // undef or null
    }

    // Returns a promise that resolves to the access_token string from the IS
    async getAccessToken() {
        if (!this.authEnabled) {
            // The current IS doesn't support authentication
            return null;
        }

        let token = this.accessToken;
        if (!token) {
            token = this._readToken();
        }

        if (!token) {
            token = await this.registerForToken();
            if (token) {
                this.accessToken = token;
                this._writeToken();
            }
            return token;
        }

        try {
            await this._checkToken(token);
        } catch (e) {
            if (e instanceof TermsNotSignedError) {
                // Retrying won't help this
                throw e;
            }
            // Retry in case token expired
            token = await this.registerForToken();
            if (token) {
                this.accessToken = token;
                this._writeToken();
            }
        }

        return token;
    }

    async _checkToken(token) {
        try {
            await this._matrixClient.getIdentityAccount(token);
        } catch (e) {
            if (e.errcode === "M_TERMS_NOT_SIGNED") {
                console.log("Identity Server requires new terms to be agreed to");
                await startTermsFlow([new Service(
                    SERVICE_TYPES.IS,
                    this._matrixClient.getIdentityServerUrl(),
                    token,
                )]);
                return;
            }
            throw e;
        }

        // We should ensure the token in `localStorage` is cleared
        // appropriately. We already clear storage on sign out, but we'll need
        // additional clearing when changing ISes in settings as part of future
        // privacy work.
        // See also https://github.com/vector-im/riot-web/issues/10455.
    }

    async registerForToken() {
        try {
            const hsOpenIdToken = await MatrixClientPeg.get().getOpenIdToken();
            const { access_token: identityAccessToken } =
                await this._matrixClient.registerWithIdentityServer(hsOpenIdToken);
            await this._checkToken(identityAccessToken);
            return identityAccessToken;
        } catch (e) {
            if (e.cors === "rejected" || e.httpStatus === 404) {
                // Assume IS only supports 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
                console.warn("IS doesn't support v2 auth");
                this.authEnabled = false;
                return;
            }
            throw e;
        }
    }
}

@@ -2530,28 +2530,38 @@
 AccessToken(
+check=true
 ) %7B%0A
-
         if (
@@ -2850,32 +2850,37 @@
 egisterForToken(
+check
 );%0A            i
@@ -3012,32 +3012,57 @@
 ken;%0A        %7D%0A%0A
+        if (check) %7B%0A    
         try %7B%0A  
@@ -3051,32 +3051,36 @@
           try %7B%0A
+    
             awai
@@ -3098,32 +3098,36 @@
 ckToken(token);%0A
+    
         %7D catch 
@@ -3140,24 +3140,28 @@
             
+    
 if (e instan
@@ -3196,32 +3196,36 @@
                 
+    
 // Retrying won'
@@ -3244,32 +3244,36 @@
                 
+   
+ 
 throw e;%0A       
@@ -3269,34 +3269,42 @@
  e;%0A            
-%7D%0A
+    %7D%0A    
             // R
@@ -3334,32 +3334,36 @@
 red%0A            
+  
+  
 token = await th
@@ -3377,32 +3377,36 @@
 sterForToken();%0A
+    
             if (
@@ -3422,32 +3422,36 @@
                 
+    
 this.accessToken
@@ -3468,32 +3468,36 @@
                 
+ 
+   
 this._writeToken
@@ -3492,32 +3492,50 @@
 ._writeToken();%0A
+                %7D%0A
             %7D%0A  
@@ -4463,24 +4463,34 @@
 terForToken(
+check=true
 ) %7B%0A        
@@ -4709,32 +4709,32 @@
 hsOpenIdToken);%0A
-
             awai
@@ -4720,32 +4720,43 @@
 en);%0A           
+ if (check)
  await this._che

Support IS token handling without checking terms