import * as accounts from '../model/accounts' ; import * as digitalocean from '../model/digitalocean' ; import * as gcp from '../model/gcp' ; import { DigitalOceanAccount } from './digitalocean_account' ; import { GcpAccount } from './gcp_account' ; import { ShadowboxSettings } from "./server_install" ; type DigitalOceanAccountJson = { accessToken : string ; } ; type GcpAccountJson = { refreshToken : string ; } ; /**
 * Manages connected cloud provider accounts.
 */ export class CloudAccounts implements accounts . CloudAccounts { private readonly LEGACY_DIGITALOCEAN_STORAGE_KEY = 'LastDOToken' ; private readonly DIGITALOCEAN_ACCOUNT_STORAGE_KEY = 'accounts.digitalocean' ; private readonly GCP_ACCOUNT_STORAGE_KEY = 'accounts.gcp' ; private digitalOceanAccount : DigitalOceanAccount = null ; private gcpAccount : GcpAccount = null ; constructor ( private shadowboxSettings , private isDebugMode , private storage = localStorage ) { this . load ( ) ; } /** See  CloudAccounts connectDigitalOceanAccount ( accessToken ) { this . digitalOceanAccount = this . createDigitalOceanAccount ( accessToken ) ; this . save ( ) ; return this . digitalOceanAccount ; } /** See  CloudAccounts connectGcpAccount ( refreshToken ) { this . gcpAccount = this . createGcpAccount ( refreshToken ) ; this . save ( ) ; return this . gcpAccount ; } /** See  CloudAccounts disconnectDigitalOceanAccount ( ) { this . digitalOceanAccount = null ; this . save ( ) ; } /** See  CloudAccounts disconnectGcpAccount ( ) { this . gcpAccount = null ; this . save ( ) ; } /** See  CloudAccounts getDigitalOceanAccount ( ) { return this . digitalOceanAccount ; } /** See  CloudAccounts getGcpAccount ( ) { return this . gcpAccount ; } /** Loads the saved cloud accounts from disk. */ private load ( ) { const digitalOceanAccountJsonString = this . storage . getItem ( this . DIGITALOCEAN_ACCOUNT_STORAGE_KEY ) ; if ( ! digitalOceanAccountJsonString ) { const digitalOceanToken = this . loadLegacyDigitalOceanToken ( ) ; if ( digitalOceanToken ) { this . digitalOceanAccount = this . createDigitalOceanAccount ( digitalOceanToken ) ; this . save ( ) ; } } else { const digitalOceanAccountJson = JSON . parse ( digitalOceanAccountJsonString ) ; this . digitalOceanAccount = this . createDigitalOceanAccount ( digitalOceanAccountJson . accessToken ) ; } const gcpAccountJsonString = this . storage . getItem ( this . GCP_ACCOUNT_STORAGE_KEY ) ; if ( gcpAccountJsonString ) { const gcpAccountJson = JSON . parse ( this . storage . getItem ( this . GCP_ACCOUNT_STORAGE_KEY ) ) ; this . gcpAccount = this . createGcpAccount ( gcpAccountJson . refreshToken ) ; } } /** Loads legacy DigitalOcean access token. */ private loadLegacyDigitalOceanToken ( ) { return this . storage . getItem ( this . LEGACY_DIGITALOCEAN_STORAGE_KEY ) ; } /** Replace the legacy DigitalOcean access token. */ private saveLegacyDigitalOceanToken ( accessToken ? ) { if ( accessToken ) { this . storage . setItem ( this . LEGACY_DIGITALOCEAN_STORAGE_KEY , accessToken ) ; } else { this . storage . removeItem ( this . LEGACY_DIGITALOCEAN_STORAGE_KEY ) ; } } private createDigitalOceanAccount ( accessToken ) { return new DigitalOceanAccount ( 'do' , accessToken , this . shadowboxSettings , this . isDebugMode ) ; } private createGcpAccount ( refreshToken ) { return new GcpAccount ( 'gcp' , refreshToken , this . shadowboxSettings ) ; } private save ( ) { if ( this . digitalOceanAccount ) { const accessToken = this . digitalOceanAccount . getAccessToken ( ) ; const digitalOceanAccountJson = { accessToken } ; this . storage . setItem ( this . DIGITALOCEAN_ACCOUNT_STORAGE_KEY , JSON . stringify ( digitalOceanAccountJson ) ) ; this . saveLegacyDigitalOceanToken ( accessToken ) ; } else { this . storage . removeItem ( this . DIGITALOCEAN_ACCOUNT_STORAGE_KEY ) ; this . saveLegacyDigitalOceanToken ( null ) ; } if ( this . gcpAccount ) { const refreshToken = this . gcpAccount . getRefreshToken ( ) ; const gcpAccountJson = { refreshToken } ; this . storage . setItem ( this . GCP_ACCOUNT_STORAGE_KEY , JSON . stringify ( gcpAccountJson ) ) ; } else { this . storage . removeItem ( this . GCP_ACCOUNT_STORAGE_KEY ) ; } } }