All files / framework/utils AppStorage.ts

87.5% Statements 70/80
57.14% Branches 16/28
78.57% Functions 11/14
87.18% Lines 68/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180                                1125x   1125x 1125x   1125x             2x 2x     2x       2x 2x   2x 14x 14x 14x   14x 14x 14x 14x   14x 1x 13x 1x     14x 14x 14x     2x       3x     3x       3x 3x     3x 3x   3x 12x 12x 12x 12x   12x 12x 12x   12x   12x 12x   12x 9x     12x 12x     3x     1125x   20x     2x     3x       1125x   2x 2x   2x           17x 17x             1x   1x 1x 1x     1x       2x       1x 1x                                           2x 2x     1125x    
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
/* eslint-disable @typescript-eslint/explicit-function-return-type */
// ignoring ts and eslint issues because this is a legacy file
 
////////////////////////////////////////////////////////////////////////
// THIS FILE IS A LEGACY - DO NOT USE IT DIRECTLY OR EXTEND ITS USAGE //
////////////////////////////////////////////////////////////////////////
 
import * as SecureStorage from 'secure-web-storage'
 
const SecureStorageConstructor = SecureStorage.default
const keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
 
const buildLoginUrlFrom401Response = () => {
  const { href } = window.location
  const prefix = window.HARNESS_ENABLE_NG_AUTH_UI ? '/auth/#/signin?returnUrl=' : '#/login?returnUrl='
  return href.includes(prefix) ? href : prefix + encodeURIComponent(href)
}
 
function encode64(input) {
  input = escape(input)
  let output = ''
  let chr1,
    chr2,
    chr3 = ''
  let enc1,
    enc2,
    enc3,
    enc4 = ''
  let i = 0
 
  do {
    chr1 = input.charCodeAt(i++)
    chr2 = input.charCodeAt(i++)
    chr3 = input.charCodeAt(i++)
 
    enc1 = chr1 >> 2
    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
    enc4 = chr3 & 63
 
    if (isNaN(chr2)) {
      enc3 = enc4 = 64
    } else if (isNaN(chr3)) {
      enc4 = 64
    }
 
    output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4)
    chr1 = chr2 = chr3 = ''
    enc1 = enc2 = enc3 = enc4 = ''
  } while (i < input.length)
 
  return output
}
 
function decode64(input) {
  let output = ''
  let chr1,
    chr2,
    chr3 = ''
  let enc1,
    enc2,
    enc3,
    enc4 = ''
  let i = 0
 
  // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  const base64test = /[^A-Za-z0-9+/=]/g
  input = input.replace(base64test, '')
 
  do {
    enc1 = keyStr.indexOf(input.charAt(i++))
    enc2 = keyStr.indexOf(input.charAt(i++))
    enc3 = keyStr.indexOf(input.charAt(i++))
    enc4 = keyStr.indexOf(input.charAt(i++))
 
    chr1 = (enc1 << 2) | (enc2 >> 4)
    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
    chr3 = ((enc3 & 3) << 6) | enc4
 
    output = output + String.fromCharCode(chr1)
 
    Eif (enc3 !== 64) {
      output = output + String.fromCharCode(chr2)
    }
    if (enc4 !== 64) {
      output = output + String.fromCharCode(chr3)
    }
 
    chr1 = chr2 = chr3 = ''
    enc1 = enc2 = enc3 = enc4 = ''
  } while (i < input.length)
 
  return unescape(output)
}
 
const secureStorage = new SecureStorageConstructor(localStorage, {
  hash: function hash(key) {
    return key
  },
  encrypt: function encrypt(data) {
    return encode64(data)
  },
  decrypt: function decrypt(data) {
    return decode64(data)
  }
})
 
export default class AppStorage {
  static set(name, value) {
    Eif (value && name !== 'DataStore_apps') {
      secureStorage.setItem(name, value)
    }
    Iif (value && name === 'DataStore_apps') {
      localStorage.setItem(name, value)
    }
  }
 
  static get(name) {
    try {
      return secureStorage.getItem(name)
    } catch (event) {
      window.location = buildLoginUrlFrom401Response()
    }
  }
 
  static getAll() {
    const _obj = {}
 
    for (let i = 0, len = secureStorage.length; i < len; ++i) {
      const key = secureStorage.key(i)
      _obj.key = AppStorage.get(key)
    }
 
    return _obj
  }
 
  static has(name) {
    return AppStorage.get(name) ? true : false
  }
 
  static remove(name) {
    Eif (name !== 'DataStore_apps') {
      secureStorage.removeItem(name)
    } else {
      localStorage.removeItem(name)
    }
  }
 
  static cleanupOnAccountSwitch() {
    // clear AppStorage, only keep important fields:
    const storage = {
      token: AppStorage.get('token'),
      username: AppStorage.get('username') || '',
      email: AppStorage.get('email') || ''
    }
 
    AppStorage.clear()
 
    Object.keys(storage).forEach(key => {
      AppStorage.set(key, storage[key])
    })
  }
 
  static clear() {
    secureStorage.clear()
    localStorage.clear()
  }
 
  static decode64 = decode64
}