forked from mozilla/persona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelegated-primary-test.js
executable file
·129 lines (108 loc) · 4 KB
/
delegated-primary-test.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
require('./lib/test_env.js');
const
assert = require('assert'),
vows = require('vows'),
path = require('path'),
util = require('util');
const TEST_DOMAIN = 'example.domain',
TEST_DOMAIN_PATH = path.join(__dirname,
'..', 'example', 'primary', '.well-known', 'browserid'),
TEST_ORIGIN = 'http://127.0.0.1:10002',
TEST_DELEGATE_DOMAIN = 'delegate.example.domain',
TEST_DELEGATE_DOMAIN_PATH = path.join(__dirname,
'..', 'example', 'delegated_primary', '.well-known', 'browserid');
// Good examples
process.env['SHIMMED_PRIMARIES'] =
'example.domain|http://127.0.0.1:10005|' + TEST_DOMAIN_PATH;
process.env['SHIMMED_PRIMARIES'] += "," +
'delegate.example.domain|http://127.0.0.1:10005|' + TEST_DELEGATE_DOMAIN_PATH;
// A series of redirects delegate0.domain -> delegate1.domain -> ... delegate11.domain
function mk_delegate(i) {
var f = util.format;
var p = path.join(__dirname, 'data', f('delegate%s.domain', i), '.well-known', 'browserid');
process.env['SHIMMED_PRIMARIES'] += "," +
f("delegate%s.domain|http://127.0.0.1:10005|%s", i, p);
}
for (var i=0; i <= 10; i++) {
mk_delegate(i);
}
// delegates to hozed.domain
process.env['SHIMMED_PRIMARIES'] += "," +
util.format("hozed.domain|http://127.0.0.1:10005|%s", path.join(__dirname, 'data',
'hozed.domain', '.well-known', 'browserid'));
// Next two delegate to each other forming a cycle
process.env['SHIMMED_PRIMARIES'] += "," +
util.format("cycle.domain|http://127.0.0.1:10005|%s", path.join(__dirname, 'data',
'cycle.domain', '.well-known', 'browserid'));
process.env['SHIMMED_PRIMARIES'] += "," +
util.format("cycle2.domain|http://127.0.0.1:10005|%s", path.join(__dirname, 'data',
'cycle2.domain', '.well-known', 'browserid'));
var primary = require('../lib/primary.js');
var suite = vows.describe('delegated-primary');
// DB test look
// Tests related to domains that delegate their authority to another
// primary.
// now let's generate an assertion using this user
suite.addBatch({
"Retrieving a public key is straight forward": {
topic: function() {
return primary.getPublicKey(TEST_DOMAIN, this.callback);
},
"succeeds": function(err, pubKey) {
assert.equal(pubKey.keysize, '256');
assert.equal(pubKey.algorithm, 'RS');
}
}
});
suite.addBatch({
"Retrieving a public key should follow authority delegation": {
topic: function() {
return primary.getPublicKey(TEST_DELEGATE_DOMAIN, this.callback);
},
"succeeds": function(err, pubKey) {
assert.equal(pubKey.keysize, '256');
assert.equal(pubKey.algorithm, 'RS');
}
}
});
suite.addBatch({
"Cycles should be detected": {
topic: function() {
return primary.getPublicKey('cycle.domain', this.callback);
},
"succeeds": function(err, pubKey) {
assert.strictEqual(err,
'Circular reference in delegating authority {"cycle.domain":0,"cycle2.domain":1}');
}
}
});
suite.addBatch({
"We should not follow an infinite series of delegations of authority": {
topic: function() {
return primary.getPublicKey('delegate0.domain', this.callback);
},
"succeeds": function(err, pubKey) {
assert.strictEqual(err,
'Too many hops while delegating authority ["delegate0.domain","delegate1.domain",' +
'"delegate2.domain","delegate3.domain","delegate4.domain","delegate5.domain",' +
'"delegate6.domain"]');
}
}
});
suite.addBatch({
"A domain delegating to itself is hozed...": {
topic: function() {
return primary.getPublicKey('hozed.domain', this.callback);
},
"succeeds": function(err, pubKey) {
assert.strictEqual(err.indexOf('Circular reference in delegating authority '), 0);
}
}
});
// run or export the suite.
if (process.argv[1] === __filename) suite.run();
else suite.export(module);