This repository was archived by the owner on Dec 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathFactorInstantiator.js
More file actions
70 lines (60 loc) · 1.99 KB
/
FactorInstantiator.js
File metadata and controls
70 lines (60 loc) · 1.99 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
'use strict';
var utils = require('../utils');
var Factor = require('./Factor');
var SmsFactor = require('./SmsFactor');
var GoogleAuthenticatorFactor = require('./GoogleAuthenticatorFactor');
var Resource = require('./Resource');
/**
* Retrieves the constructor for a correct {@link Factor} instance ({@link SmsFactor} or
* {@link GoogleAuthenticatorFactor}) for corresponding JSON data for the factor.
*
* @private
*
* @param {FactorData} factor
* The data for the factor object
*
* @throws Error If the type is not defined or if it is not a valid type (SMS or google-authenticator)
*/
function getFactorConstructor(/* factor */) {
var data = arguments[0];
if (!data || (typeof data.type === 'undefined')) {
throw new Error('Factor instances must have a defined type');
}
var type = data.type.toLowerCase();
switch (type) {
case 'sms':
return SmsFactor;
case 'google-authenticator':
return GoogleAuthenticatorFactor;
default:
return Factor;
}
}
/**
* @private
*
* @class FactorInstantiator
*
* @description
* The constructor for {@link Factor} instances ({@link SmsFactor} or
* {@link GoogleAuthenticatorFactor}). It parses the data to construct the
* correct constructor, and calls it with the data. It is used for polymorphic
* factor instantiation. It augments {@link Resource} to adhere to the
* interface used for instantiation in {@link ResourceFactory}.
*
* @augments Resource
*/
function FactorInstantiator() {
var Ctor = getFactorConstructor.apply(this, arguments);
var argsWithContext = Array.prototype.slice.call(arguments);
// Adds an initial parameter. It will be the function context (this)
// for the bind call. It does not matter because `new` overwrites, it,
// however, so we're just setting it to null here for syntactic reasons.
argsWithContext.unshift(null);
return new (Ctor.bind.apply(Ctor, argsWithContext))();
}
utils.inherits(FactorInstantiator, Resource);
module.exports = {
Constructor: FactorInstantiator,
getConstructor: getFactorConstructor
};