forked from google/u2f-ref-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptotokenapprovedorigins.js
59 lines (56 loc) · 1.78 KB
/
cryptotokenapprovedorigins.js
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
// Copyright 2014 Google Inc. All rights reserved
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
/**
* @fileoverview Provides an implementation of approved origins that relies
* on the chrome.cryptotokenPrivate.requestPermission API.
* (and only) allows google.com to use security keys.
*
*/
'use strict';
/**
* Allows the caller to check whether the user has approved the use of
* security keys from an origin.
* @constructor
* @implements {ApprovedOrigins}
*/
function CryptoTokenApprovedOrigin() {}
/**
* Checks whether the origin is approved to use security keys. (If not, an
* approval prompt may be shown.)
* @param {string} origin The origin to approve.
* @param {number=} opt_tabId A tab id to display approval prompt in.
* For this implementation, the tabId is always necessary, even though
* the type allows undefined.
* @return {Promise<boolean>} A promise for the result of the check.
*/
CryptoTokenApprovedOrigin.prototype.isApprovedOrigin =
function(origin, opt_tabId) {
return new Promise(function(resolve, reject) {
if (opt_tabId === undefined) {
resolve(false);
return;
}
var tabId = /** @type {number} */ (opt_tabId);
tabInForeground(tabId).then(function(result) {
if (!result) {
resolve(false);
return;
}
if (!chrome.tabs || !chrome.tabs.get) {
reject();
return;
}
chrome.tabs.get(tabId, function(tab) {
if (chrome.runtime.lastError) {
resolve(false);
return;
}
var tabOrigin = getOriginFromUrl(tab.url);
resolve(tabOrigin == origin);
});
});
});
};