soajs.controller 4.3.27 → 4.3.28

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.
@@ -435,6 +435,18 @@ function callTool(req, configuration, config, entry, args) {
435
435
  if (!headers.access_token && req.query && req.query.access_token) {
436
436
  headers.access_token = req.query.access_token;
437
437
  }
438
+ //NOTE: the token goes as access_token, never as Authorization: a service that falls back to the
439
+ // gateway for an interConnect peer passes access_token on, not a lowercase authorization
440
+ // header, and oauth refuses a call that carries both
441
+ if (!headers.access_token && headers.authorization) {
442
+ let matches = headers.authorization.match(/^Bearer\s+(\S+)/i);
443
+ if (matches) {
444
+ headers.access_token = matches[1];
445
+ }
446
+ }
447
+ if (headers.access_token) {
448
+ delete headers.authorization;
449
+ }
438
450
  let clientIP = req.getClientIP ? req.getClientIP() : null;
439
451
  let forwarded = req.headers["x-forwarded-for"];
440
452
  if (clientIP) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "soajs.controller",
3
3
  "description": "soajs multi tenant API gateway",
4
- "version": "4.3.27",
4
+ "version": "4.3.28",
5
5
  "author": {
6
6
  "name": "soajs team",
7
7
  "email": "team@soajs.org"
@@ -411,6 +411,23 @@ describe("Unit test for: mw - soajsRoutes mcp", () => {
411
411
 
412
412
  let entry = {service: "urac", version: "2", tool: coreTools[1]};
413
413
 
414
+ it("a bearer token is passed on as access_token, not as authorization", (done) => {
415
+ reply = {status: 200, body: {result: true, data: {}}};
416
+ let req = makeReq({gatewayPort: gatewayPort, headers: {"authorization": "Bearer agentToken"}});
417
+ tools.callTool(req, configuration, config, entry, {id: "u1", name: "Ann"}).then(() => {
418
+ assert.strictEqual(received.headers.access_token, "agentToken");
419
+ assert.ok(!received.headers.authorization);
420
+ reply = {status: 200, body: {result: true, data: {}}};
421
+ let req2 = makeReq({gatewayPort: gatewayPort, headers: {"authorization": "Basic xyz"}});
422
+ return tools.callTool(req2, configuration, config, entry, {id: "u1", name: "Ann"});
423
+ }).then(() => {
424
+ // not a bearer token: left as it is
425
+ assert.strictEqual(received.headers.authorization, "Basic xyz");
426
+ assert.ok(!received.headers.access_token);
427
+ done();
428
+ }).catch(done);
429
+ });
430
+
414
431
  it("the api call goes through the gateway port with the caller's credentials", (done) => {
415
432
  reply = {status: 200, body: {result: true, data: {updated: true}}};
416
433
  let req = makeReq({
@@ -423,7 +440,8 @@ describe("Unit test for: mw - soajsRoutes mcp", () => {
423
440
  assert.strictEqual(received.url, "/urac/v2/user/u1");
424
441
  assert.deepStrictEqual(JSON.parse(received.body), {profile: {name: "Ann"}});
425
442
  assert.strictEqual(received.headers.key, "extKey");
426
- assert.strictEqual(received.headers.authorization, "Bearer abc");
443
+ // one token only, as access_token
444
+ assert.ok(!received.headers.authorization);
427
445
  assert.strictEqual(received.headers.access_token, "tok");
428
446
  assert.strictEqual(received.headers["device-id"], "d1");
429
447
  // the mcp request's own key is not forwarded, a write gets a new one