forked from google/u2f-ref-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelperwhitelist.js
87 lines (80 loc) · 2.58 KB
/
helperwhitelist.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
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
// 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 Implements a whitelist of external request helpers.
*/
'use strict';
/**
* Implements a whitelist of external request helpers.
* @constructor
*/
function RequestHelperWhitelist() {
/**
* Maps extension id -> mnemonic
* @private {Object<string, string>}
*/
this.allowedExtensionIds_ = {};
/**
* Maps b64(sha256(extension id)) -> mnemonic
* @private {Object<string, string>}
*/
this.allowedBlindedExtensionIds_ = {};
}
/**
* @param {string} id Extension id.
* @return {boolean} Whether this extension is allowed by this whitelist.
*/
RequestHelperWhitelist.prototype.isExtensionAllowed = function(id) {
return this.getExtensionMnemonicOrEmptyString_(id) != null;
};
/**
* @param {string} id Extension id.
* @return {?string} The mnemonic for this extension id, or null if none was
* set or this extension is not allowed.
*/
RequestHelperWhitelist.prototype.getExtensionMnemonic = function(id) {
var mnemonic = this.getExtensionMnemonicOrEmptyString_(id);
if (mnemonic == '') {
return null;
}
return mnemonic;
};
/**
* @param {string} id Extension id.
* @return {?string} The mnemonic for this extension id, which may be the empty
* string if none was set, or null if this extension id is not allowed.
* @private
*/
RequestHelperWhitelist.prototype.getExtensionMnemonicOrEmptyString_ =
function(id) {
if (this.allowedExtensionIds_.hasOwnProperty(id)) {
return this.allowedExtensionIds_[id];
}
var blindedId = B64_encode(sha256HashOfString(id));
if (this.allowedBlindedExtensionIds_.hasOwnProperty(blindedId)) {
return this.allowedBlindedExtensionIds_[blindedId];
}
return null;
};
/**
* Adds the extension id to the whitelist.
* @param {string} id Extension id.
* @param {string=} opt_mnemonic Name by which to refer to this extension.
*/
RequestHelperWhitelist.prototype.addAllowedExtension =
function(id, opt_mnemonic) {
this.allowedExtensionIds_[id] = opt_mnemonic || '';
};
/**
* Adds the blinded extension id to the whitelist.
* @param {string} blindedId Blinded extension id, i.e. Base64-encoded SHA256
* hash of the extension id.
* @param {string=} opt_mnemonic Name by which to refer to this extension.
*/
RequestHelperWhitelist.prototype.addAllowedBlindedExtension =
function(blindedId, opt_mnemonic) {
this.allowedBlindedExtensionIds_[blindedId] = opt_mnemonic || '';
};