@boxyhq/saml-jackson 0.2.1-beta.158 → 0.2.1-beta.164

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/README.md CHANGED
@@ -71,6 +71,20 @@ router.post('/api/v1/saml/config', async (req, res) => {
71
71
  });
72
72
  }
73
73
  });
74
+ // fetch config
75
+ router.get('/api/v1/saml/config', async (req, res) => {
76
+ try {
77
+ // apply your authentication flow (or ensure this route has passed through your auth middleware)
78
+ ...
79
+
80
+ // only when properly authenticated, call the config function
81
+ res.json(await apiController.config(req.query));
82
+ } catch (err) {
83
+ res.status(500).json({
84
+ error: err.message,
85
+ });
86
+ }
87
+ });
74
88
 
75
89
  // OAuth 2.0 flow
76
90
  router.get('/oauth/authorize', async (req, res) => {
@@ -198,7 +212,7 @@ The response returns a JSON with `client_id` and `client_secret` that can be sto
198
212
  This endpoint can be used to return metadata about an existing SAML config. This can be used to check and display the details to your customers. You can use either `clientID` or `tenant` and `product` combination.
199
213
 
200
214
  ```
201
- curl --location --request POST 'http://localhost:6000/api/v1/saml/config/get' \
215
+ curl -G --location 'http://localhost:6000/api/v1/saml/config' \
202
216
  --header 'Authorization: Api-Key <Jackson API Key>' \
203
217
  --header 'Content-Type: application/x-www-form-urlencoded' \
204
218
  --data-urlencode 'tenant=boxyhq.com' \
@@ -206,7 +220,7 @@ curl --location --request POST 'http://localhost:6000/api/v1/saml/config/get' \
206
220
  ```
207
221
 
208
222
  ```
209
- curl --location --request POST 'http://localhost:6000/api/v1/saml/config/get' \
223
+ curl -G --location 'http://localhost:6000/api/v1/saml/config' \
210
224
  --header 'Authorization: Api-Key <Jackson API Key>' \
211
225
  --header 'Content-Type: application/x-www-form-urlencoded' \
212
226
  --data-urlencode 'clientID=<Client ID>'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@boxyhq/saml-jackson",
3
- "version": "0.2.1-beta.158",
3
+ "version": "0.2.1-beta.164",
4
4
  "license": "Apache 2.0",
5
5
  "description": "SAML 2.0 service",
6
6
  "main": "src/index.js",
@@ -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
package/src/jackson.js CHANGED
@@ -114,7 +114,7 @@ internalApp.post(apiPath + '/config', async (req, res) => {
114
114
  }
115
115
  });
116
116
 
117
- internalApp.post(apiPath + '/config/get', async (req, res) => {
117
+ internalApp.get(apiPath + '/config', async (req, res) => {
118
118
  try {
119
119
  const apiKey = extractAuthToken(req);
120
120
  if (!validateApiKey(apiKey)) {
@@ -122,7 +122,7 @@ internalApp.post(apiPath + '/config/get', async (req, res) => {
122
122
  return;
123
123
  }
124
124
 
125
- res.json(await apiController.getConfig(req.body));
125
+ res.json(await apiController.getConfig(req.query));
126
126
  } catch (err) {
127
127
  res.status(500).json({
128
128
  error: err.message,
@@ -0,0 +1,161 @@
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 CLIENT_ID = '75edb050796a0eb1cf2cfb0da7245f85bc50baa7';
12
+ const PROVIDER = 'accounts.google.com';
13
+ const OPTIONS = {
14
+ externalUrl: 'https://my-cool-app.com',
15
+ samlAudience: 'https://saml.boxyhq.com',
16
+ samlPath: '/sso/oauth/saml',
17
+ db: {
18
+ engine: 'mem',
19
+ },
20
+ };
21
+
22
+ tap.before(async () => {
23
+ const controller = await require('../index.js')(OPTIONS);
24
+
25
+ apiController = controller.apiController;
26
+ });
27
+
28
+ tap.teardown(async () => {
29
+ process.exit(0);
30
+ });
31
+
32
+ tap.test('controller/api', async (t) => {
33
+ const metadataPath = path.join(__dirname, '/data/metadata');
34
+ const config = await readConfig(metadataPath);
35
+
36
+ t.test('.config()', async (t) => {
37
+ t.test('when required fields are missing or invalid', async (t) => {
38
+ t.test('when `rawMetadata` is empty', async (t) => {
39
+ const body = Object.assign({}, config[0]);
40
+ delete body['rawMetadata'];
41
+
42
+ try {
43
+ await apiController.config(body);
44
+ t.fail('Expecting JacksonError.');
45
+ } catch (err) {
46
+ t.equal(err.message, 'Please provide rawMetadata');
47
+ t.equal(err.statusCode, 400);
48
+ }
49
+
50
+ t.end();
51
+ });
52
+
53
+ t.test('when `defaultRedirectUrl` is empty', async (t) => {
54
+ const body = Object.assign({}, config[0]);
55
+ delete body['defaultRedirectUrl'];
56
+
57
+ try {
58
+ await apiController.config(body);
59
+ t.fail('Expecting JacksonError.');
60
+ } catch (err) {
61
+ t.equal(err.message, 'Please provide a defaultRedirectUrl');
62
+ t.equal(err.statusCode, 400);
63
+ }
64
+
65
+ t.end();
66
+ });
67
+
68
+ t.test('when `redirectUrl` is empty', async (t) => {
69
+ const body = Object.assign({}, config[0]);
70
+ delete body['redirectUrl'];
71
+
72
+ try {
73
+ await apiController.config(body);
74
+ t.fail('Expecting JacksonError.');
75
+ } catch (err) {
76
+ t.equal(err.message, 'Please provide redirectUrl');
77
+ t.equal(err.statusCode, 400);
78
+ }
79
+
80
+ t.end();
81
+ });
82
+
83
+ t.test('when `tenant` is empty', async (t) => {
84
+ const body = Object.assign({}, config[0]);
85
+ delete body['tenant'];
86
+
87
+ try {
88
+ await apiController.config(body);
89
+ t.fail('Expecting JacksonError.');
90
+ } catch (err) {
91
+ t.equal(err.message, 'Please provide tenant');
92
+ t.equal(err.statusCode, 400);
93
+ }
94
+
95
+ t.end();
96
+ });
97
+
98
+ t.test('when `product` is empty', async (t) => {
99
+ const body = Object.assign({}, config[0]);
100
+ delete body['product'];
101
+
102
+ try {
103
+ await apiController.config(body);
104
+ t.fail('Expecting JacksonError.');
105
+ } catch (err) {
106
+ t.equal(err.message, 'Please provide product');
107
+ t.equal(err.statusCode, 400);
108
+ }
109
+
110
+ t.end();
111
+ });
112
+
113
+ t.test('when `rawMetadata` is not a valid XML', async (t) => {
114
+ const body = Object.assign({}, config[0]);
115
+ body['rawMetadata'] = 'not a valid XML';
116
+
117
+ try {
118
+ await apiController.config(body);
119
+ t.fail('Expecting Error.');
120
+ } catch (err) {
121
+ t.match(err.message, /Non-whitespace before first tag./);
122
+ }
123
+
124
+ t.end();
125
+ });
126
+ });
127
+
128
+ t.test('when the request is good', async (t) => {
129
+ const body = Object.assign({}, config[0]);
130
+
131
+ const kdStub = sinon.stub(dbutils, 'keyDigest').returns(CLIENT_ID);
132
+ const rbStub = sinon
133
+ .stub(crypto, 'randomBytes')
134
+ .returns('f3b0f91eb8f4a9f7cc2254e08682d50b05b5d36262929e7f');
135
+
136
+ const response = await apiController.config(body);
137
+ t.ok(kdStub.called);
138
+ t.ok(rbStub.calledOnce);
139
+ t.equal(response.client_id, CLIENT_ID);
140
+ t.equal(
141
+ response.client_secret,
142
+ 'f3b0f91eb8f4a9f7cc2254e08682d50b05b5d36262929e7f'
143
+ );
144
+ t.equal(response.provider, PROVIDER);
145
+
146
+ const savedConf = await apiController.getConfig({
147
+ clientID: CLIENT_ID,
148
+ });
149
+ t.equal(savedConf.provider, PROVIDER);
150
+
151
+ dbutils.keyDigest.restore();
152
+ crypto.randomBytes.restore();
153
+
154
+ t.end();
155
+ });
156
+
157
+ t.end();
158
+ });
159
+
160
+ t.end();
161
+ });