summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/authentication/u2f/util.js
blob: b706481c02f68c83f1e666d91e71e57a96fcae84 (plain)
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
function isOpera(userAgent) {
  return userAgent.indexOf('Opera') >= 0 || userAgent.indexOf('OPR') >= 0;
}

function getOperaVersion(userAgent) {
  const match = userAgent.match(/OPR[^0-9]*([0-9]+)[^0-9]+/);
  return match ? parseInt(match[1], 10) : false;
}

function isChrome(userAgent) {
  return userAgent.indexOf('Chrom') >= 0 && !isOpera(userAgent);
}

function getChromeVersion(userAgent) {
  const match = userAgent.match(/Chrom(?:e|ium)\/([0-9]+)\./);
  return match ? parseInt(match[1], 10) : false;
}

export function canInjectU2fApi(userAgent) {
  const isSupportedChrome = isChrome(userAgent) && getChromeVersion(userAgent) >= 41;
  const isSupportedOpera = isOpera(userAgent) && getOperaVersion(userAgent) >= 40;
  const isMobile =
    userAgent.indexOf('droid') >= 0 ||
    userAgent.indexOf('CriOS') >= 0 ||
    /\b(iPad|iPhone|iPod)(?=;)/.test(userAgent);
  return (isSupportedChrome || isSupportedOpera) && !isMobile;
}

export default function importU2FLibrary() {
  if (window.u2f) {
    return Promise.resolve(window.u2f);
  }

  const userAgent = typeof navigator !== 'undefined' ? navigator.userAgent : '';
  if (canInjectU2fApi(userAgent) || (gon && gon.test_env)) {
    return import(/* webpackMode: "eager" */ 'vendor/u2f').then(() => window.u2f);
  }

  return Promise.reject();
}