@boxyhq/saml-jackson 0.2.1-beta.158 → 0.2.1-beta.161
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/controller/api.js +22 -0
- package/src/test/api.test.js +156 -0
package/package.json
CHANGED
package/src/controller/api.js
CHANGED
@@ -2,6 +2,7 @@ const saml = require('../saml/saml.js');
|
|
2
2
|
const x509 = require('../saml/x509.js');
|
3
3
|
const dbutils = require('../db/utils.js');
|
4
4
|
const { indexNames } = require('./utils.js');
|
5
|
+
const { JacksonError } = require('./error.js');
|
5
6
|
|
6
7
|
const crypto = require('crypto');
|
7
8
|
|
@@ -22,6 +23,27 @@ const extractHostName = (url) => {
|
|
22
23
|
const config = async (body) => {
|
23
24
|
const { rawMetadata, defaultRedirectUrl, redirectUrl, tenant, product } =
|
24
25
|
body;
|
26
|
+
|
27
|
+
if (!rawMetadata) {
|
28
|
+
throw new JacksonError('Please provide rawMetadata', 400);
|
29
|
+
}
|
30
|
+
|
31
|
+
if (!defaultRedirectUrl) {
|
32
|
+
throw new JacksonError('Please provide a defaultRedirectUrl', 400);
|
33
|
+
}
|
34
|
+
|
35
|
+
if (!redirectUrl) {
|
36
|
+
throw new JacksonError('Please provide redirectUrl', 400);
|
37
|
+
}
|
38
|
+
|
39
|
+
if (!tenant) {
|
40
|
+
throw new JacksonError('Please provide tenant', 400);
|
41
|
+
}
|
42
|
+
|
43
|
+
if (!product) {
|
44
|
+
throw new JacksonError('Please provide product', 400);
|
45
|
+
}
|
46
|
+
|
25
47
|
const idpMetadata = await saml.parseMetadataAsync(rawMetadata);
|
26
48
|
|
27
49
|
// extract provider
|
@@ -0,0 +1,156 @@
|
|
1
|
+
const tap = require('tap');
|
2
|
+
const path = require('path');
|
3
|
+
const sinon = require('sinon');
|
4
|
+
const crypto = require('crypto');
|
5
|
+
|
6
|
+
const readConfig = require('../read-config');
|
7
|
+
const dbutils = require('../db/utils');
|
8
|
+
|
9
|
+
let apiController;
|
10
|
+
|
11
|
+
const options = {
|
12
|
+
externalUrl: 'https://my-cool-app.com',
|
13
|
+
samlAudience: 'https://saml.boxyhq.com',
|
14
|
+
samlPath: '/sso/oauth/saml',
|
15
|
+
db: {
|
16
|
+
engine: 'mem',
|
17
|
+
},
|
18
|
+
};
|
19
|
+
|
20
|
+
tap.before(async () => {
|
21
|
+
const controller = await require('../index.js')(options);
|
22
|
+
|
23
|
+
apiController = controller.apiController;
|
24
|
+
});
|
25
|
+
|
26
|
+
tap.teardown(async () => {
|
27
|
+
process.exit(0);
|
28
|
+
});
|
29
|
+
|
30
|
+
tap.test('controller/api', async (t) => {
|
31
|
+
const metadataPath = path.join(__dirname, '/data/metadata');
|
32
|
+
const config = await readConfig(metadataPath);
|
33
|
+
|
34
|
+
t.test('.config()', async (t) => {
|
35
|
+
t.test('when required fields are missing or invalid', async (t) => {
|
36
|
+
t.test('when `rawMetadata` is empty', async (t) => {
|
37
|
+
const body = Object.assign({}, config[0]);
|
38
|
+
delete body['rawMetadata'];
|
39
|
+
|
40
|
+
try {
|
41
|
+
await apiController.config(body);
|
42
|
+
t.fail('Expecting JacksonError.');
|
43
|
+
} catch (err) {
|
44
|
+
t.equal(err.message, 'Please provide rawMetadata');
|
45
|
+
t.equal(err.statusCode, 400);
|
46
|
+
}
|
47
|
+
|
48
|
+
t.end();
|
49
|
+
});
|
50
|
+
|
51
|
+
t.test('when `defaultRedirectUrl` is empty', async (t) => {
|
52
|
+
const body = Object.assign({}, config[0]);
|
53
|
+
delete body['defaultRedirectUrl'];
|
54
|
+
|
55
|
+
try {
|
56
|
+
await apiController.config(body);
|
57
|
+
t.fail('Expecting JacksonError.');
|
58
|
+
} catch (err) {
|
59
|
+
t.equal(err.message, 'Please provide a defaultRedirectUrl');
|
60
|
+
t.equal(err.statusCode, 400);
|
61
|
+
}
|
62
|
+
|
63
|
+
t.end();
|
64
|
+
});
|
65
|
+
|
66
|
+
t.test('when `redirectUrl` is empty', async (t) => {
|
67
|
+
const body = Object.assign({}, config[0]);
|
68
|
+
delete body['redirectUrl'];
|
69
|
+
|
70
|
+
try {
|
71
|
+
await apiController.config(body);
|
72
|
+
t.fail('Expecting JacksonError.');
|
73
|
+
} catch (err) {
|
74
|
+
t.equal(err.message, 'Please provide redirectUrl');
|
75
|
+
t.equal(err.statusCode, 400);
|
76
|
+
}
|
77
|
+
|
78
|
+
t.end();
|
79
|
+
});
|
80
|
+
|
81
|
+
t.test('when `tenant` is empty', async (t) => {
|
82
|
+
const body = Object.assign({}, config[0]);
|
83
|
+
delete body['tenant'];
|
84
|
+
|
85
|
+
try {
|
86
|
+
await apiController.config(body);
|
87
|
+
t.fail('Expecting JacksonError.');
|
88
|
+
} catch (err) {
|
89
|
+
t.equal(err.message, 'Please provide tenant');
|
90
|
+
t.equal(err.statusCode, 400);
|
91
|
+
}
|
92
|
+
|
93
|
+
t.end();
|
94
|
+
});
|
95
|
+
|
96
|
+
t.test('when `product` is empty', async (t) => {
|
97
|
+
const body = Object.assign({}, config[0]);
|
98
|
+
delete body['product'];
|
99
|
+
|
100
|
+
try {
|
101
|
+
await apiController.config(body);
|
102
|
+
t.fail('Expecting JacksonError.');
|
103
|
+
} catch (err) {
|
104
|
+
t.equal(err.message, 'Please provide product');
|
105
|
+
t.equal(err.statusCode, 400);
|
106
|
+
}
|
107
|
+
|
108
|
+
t.end();
|
109
|
+
});
|
110
|
+
|
111
|
+
t.test('when `rawMetadata` is not a valid XML', async (t) => {
|
112
|
+
const body = Object.assign({}, config[0]);
|
113
|
+
body['rawMetadata'] = 'not a valid XML';
|
114
|
+
|
115
|
+
try {
|
116
|
+
await apiController.config(body);
|
117
|
+
t.fail('Expecting Error.');
|
118
|
+
} catch (err) {
|
119
|
+
t.match(err.message, /Non-whitespace before first tag./);
|
120
|
+
}
|
121
|
+
|
122
|
+
t.end();
|
123
|
+
});
|
124
|
+
});
|
125
|
+
|
126
|
+
t.test('when the request is good', async (t) => {
|
127
|
+
const body = Object.assign({}, config[0]);
|
128
|
+
|
129
|
+
sinon
|
130
|
+
.stub(dbutils, 'keyDigest')
|
131
|
+
.returns('75edb050796a0eb1cf2cfb0da7245f85bc50baa7');
|
132
|
+
|
133
|
+
sinon
|
134
|
+
.stub(crypto, 'randomBytes')
|
135
|
+
.returns('f3b0f91eb8f4a9f7cc2254e08682d50b05b5d36262929e7f');
|
136
|
+
|
137
|
+
const response = await apiController.config(body);
|
138
|
+
|
139
|
+
t.equal(response.client_id, '75edb050796a0eb1cf2cfb0da7245f85bc50baa7');
|
140
|
+
t.equal(
|
141
|
+
response.client_secret,
|
142
|
+
'f3b0f91eb8f4a9f7cc2254e08682d50b05b5d36262929e7f'
|
143
|
+
);
|
144
|
+
t.equal(response.provider, 'accounts.google.com');
|
145
|
+
|
146
|
+
dbutils.keyDigest.restore();
|
147
|
+
crypto.randomBytes.restore();
|
148
|
+
|
149
|
+
t.end();
|
150
|
+
});
|
151
|
+
|
152
|
+
t.end();
|
153
|
+
});
|
154
|
+
|
155
|
+
t.end();
|
156
|
+
});
|