forked from google/u2f-ref-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternalhelper.js
More file actions
114 lines (103 loc) · 3.15 KB
/
externalhelper.js
File metadata and controls
114 lines (103 loc) · 3.15 KB
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
// 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 helper that interacts with external (not in this
* browser/app) helpers.
*/
'use strict';
/**
* @typedef {{
source: string,
appId: string,
sendMessage: Function,
defaultError: number
* }}
*/
var ExternalHelperConfig;
/**
* @param {!HelperRequest} request The request to handle.
* @param {!ExternalHelperConfig} helperConfig How to reach the external helper.
* @constructor
* @implements {RequestHandler}
*/
function ExternalHandler(request, helperConfig) {
/** @private {!HelperRequest} */
this.request_ = request;
/** @private {!ExternalHelperConfig} */
this.helperConfig_ = helperConfig;
}
/**
* @param {RequestHandlerCallback} cb Called with the result of the request,
* and an optional source for the result.
* @return {boolean} Whether this handler accepted the request.
*/
ExternalHandler.prototype.run = function(cb) {
var self = this;
this.helperConfig_.sendMessage(this.helperConfig_.appId, this.request_,
function(response) {
if (self.closed_) {
console.log(
UTIL_fmt('got a response from external helper after close'));
return;
}
if (!response || !response.type) {
// A missing or malformed response implies the helper's output
// can't be trusted: report the default error.
cb(self.makeDefaultErrorReply_(self.request_));
return;
}
console.log(UTIL_fmt('got a response from external helper'));
console.log(response);
cb(response, self.helperConfig_.source);
});
return true;
};
/** Closes this handler. */
ExternalHandler.prototype.close = function() {
/** @private {boolean} */
this.closed_ = true;
};
/**
* Makes a default, generic error response to the given request.
* @param {HelperRequest} request The request.
* @return {HelperReply} The reply to the request.
* @private
*/
ExternalHandler.prototype.makeDefaultErrorReply_ = function(request) {
return makeHelperErrorResponse(request,
/** @type {DeviceStatusCodes} */ (this.helperConfig_.defaultError));
};
/**
* @param {!ExternalHelperConfig} helperConfig How to reach the external helper.
* @constructor
* @implements {RequestHelper}
*/
function ExternalHelper(helperConfig) {
/** @private {!ExternalHelperConfig} */
this.helperConfig_ = helperConfig;
}
/**
* Gets a handler for a request.
* @param {HelperRequest} request The request to handle.
* @return {RequestHandler} A handler for the request.
*/
ExternalHelper.prototype.getHandler = function(request) {
return new ExternalHandler(request, this.helperConfig_);
};
/**
* Gets the helper's app id.
* @return {string} The helper's app id.
*/
ExternalHelper.prototype.getHelperAppId = function() {
return this.helperConfig_.appId;
};
/**
* (Re)sets the helper's app id.
* @param {string} helperId
*/
ExternalHelper.prototype.setHelperAppId = function(helperId) {
this.helperConfig_.appId = helperId;
};