meadow-endpoints 3.0.2 → 3.0.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "meadow-endpoints",
3
- "version": "3.0.2",
3
+ "version": "3.0.5",
4
4
  "description": "Automatic API endpoints for Meadow data.",
5
5
  "main": "source/Meadow-Endpoints.js",
6
6
  "scripts": {
@@ -46,6 +46,14 @@ var MeadowAuthorizers = function()
46
46
  _AuthorizerFunctions[pAuthorizerHash] = fAuthorizer;
47
47
  };
48
48
 
49
+ /**
50
+ * Get a specific authorizer.
51
+ */
52
+ var getAuthorizer = function(pAuthorizerHash)
53
+ {
54
+ return _AuthorizerFunctions[pAuthorizerHash];
55
+ };
56
+
49
57
  if (_AuthorizationMode === 'SimpleOwnership')
50
58
  {
51
59
  // Map in the authorizers for simple ownership mode
@@ -55,7 +63,6 @@ var MeadowAuthorizers = function()
55
63
  setAuthorizer('MyCustomer', require(__dirname+'/authorizers/Meadow-Authorizer-MyCustomer.js'));
56
64
  }
57
65
 
58
-
59
66
  /**
60
67
  * This method runs a authorizer at a specific hash, and returns true.
61
68
  * Or it returns false if there was no authorizer there.
@@ -191,6 +198,7 @@ var MeadowAuthorizers = function()
191
198
  var tmpNewMeadowAuthorizer = (
192
199
  {
193
200
  setAuthorizer: setAuthorizer,
201
+ getAuthorizer: getAuthorizer,
194
202
  authorize: authorize,
195
203
  authorizeRequest: authorizeRequest,
196
204
 
@@ -435,8 +435,13 @@ var MeadowEndpoints = function()
435
435
  return tmpCallback('Endpoint \'' + pMethod + '\' does not exist!'); //might be better as an exception
436
436
  }
437
437
 
438
- //TODO: should switch depending on type
438
+ // TODO: should switch depending on type
439
+ // TODO: should we keep this around, just make a deep copy of 'pOptions'
439
440
  var pRequest = {params: pData, formattedParams: pData, body: pData};
441
+ if (typeof(pOptions) === 'object' && typeof(pOptions.header) === 'function') {
442
+ // carry over header function
443
+ pRequest.header = pOptions.header.bind(pOptions);
444
+ }
440
445
  var pResponse = {};
441
446
 
442
447
  libAsync.waterfall([
@@ -541,6 +546,18 @@ var MeadowEndpoints = function()
541
546
  enumerable: true
542
547
  });
543
548
 
549
+ /**
550
+ * EndpointAuthenticators
551
+ *
552
+ * @property endpointAuthorizers
553
+ * @type object
554
+ */
555
+ Object.defineProperty(tmpNewMeadowEndpointObject, 'endpointAuthorizers',
556
+ {
557
+ get: function() { return _Authorizers; },
558
+ enumerable: true
559
+ });
560
+
544
561
  /**
545
562
  * EndpointAuthenticators
546
563
  *
@@ -95,7 +95,7 @@ var doCreate = function(pRecord, pRequest, pResponse, fCallback)
95
95
  pRequest.RecordCreateErrorMessage = pError;
96
96
  // Also push the record to the created record stack with an error message
97
97
  pRequest.CreatedRecords.push(pRecord);
98
- pRequest.CommonServices.log.error('Error creating record:'+pError, {SessionID:pRequest.UserSession.SessionID, RequestID:pRequest.RequestUUID, RequestURL:pRequest.url, Action:pRequest.DAL.scope+'-'+pRequest.MeadowOperation}, pRequest);
98
+ pRequest.CommonServices.log.error('Error creating record:'+pError, {SessionID:pRequest.UserSession.SessionID, RequestID:pRequest.RequestUUID, RequestURL:pRequest.url, Action:pRequest.DAL.scope+'-'+pRequest.MeadowOperation, Stack: pError.stack }, pRequest);
99
99
  }
100
100
 
101
101
  return fCallback();
@@ -135,7 +135,7 @@ var doUpdate = function(pRecordToModify, pRequest, pResponse, fCallback, pOption
135
135
  pRequest.RecordUpdateError = true;
136
136
  pRequest.RecordUpdateErrorMessage = pError;
137
137
  pRequest.UpdatedRecords.push(pRecordToModify);
138
- pRequest.CommonServices.log.error('Error updating record:'+pError, {SessionID:pRequest.UserSession.SessionID, RequestID:pRequest.RequestUUID, RequestURL:pRequest.url, Action:pRequest.DAL.scope+'-'+pRequest.MeadowOperation}, pRequest);
138
+ pRequest.CommonServices.log.error('Error updating record:'+pError, {SessionID:pRequest.UserSession.SessionID, RequestID:pRequest.RequestUUID, RequestURL:pRequest.url, Action:pRequest.DAL.scope+'-'+pRequest.MeadowOperation, Stack: pError.stack }, pRequest);
139
139
  }
140
140
 
141
141
  return fCallback();
@@ -96,7 +96,7 @@ var doUpsert = function(pRecordToUpsert, pRequest, pResponse, fCallback)
96
96
  pRequest.RecordUpsertError = true;
97
97
  pRequest.RecordUpsertErrorMessage = pError;
98
98
  pRequest.UpsertedRecords.push(pRecordToUpsert);
99
- pRequest.CommonServices.log.error('Error upserting record:'+pError, {SessionID:pRequest.UserSession.SessionID, RequestID:pRequest.RequestUUID, RequestURL:pRequest.url, Action:pRequest.DAL.scope+'-'+pRequest.MeadowOperation}, pRequest);
99
+ pRequest.CommonServices.log.error('Error upserting record:'+pError, {SessionID:pRequest.UserSession.SessionID, RequestID:pRequest.RequestUUID, RequestURL:pRequest.url, Action:pRequest.DAL.scope+'-'+pRequest.MeadowOperation, Stack: pError.stack }, pRequest);
100
100
  }
101
101
 
102
102
  return fCallback();
@@ -467,6 +467,28 @@ suite
467
467
  }
468
468
  );
469
469
  test
470
+ (
471
+ 'read: define a custom authorization behavior',
472
+ function(fDone)
473
+ {
474
+ const defaultAuthorizer = _MeadowEndpoints.endpointAuthorizers.getAuthorizer('Allow');
475
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Allow', function(req, next) { req.MeadowAuthorization = false; return next(); });
476
+ _Orator.webServer.get('/CustomHotRodRoute/:IDRecord', _MeadowEndpoints.endpointAuthenticators.Read, _MeadowEndpoints.wireState, _MeadowEndpoints.endpoints.Read);
477
+ libSuperTest('http://localhost:9080/')
478
+ .get('CustomHotRodRoute/2')
479
+ .end(
480
+ function (pError, pResponse)
481
+ {
482
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Allow', defaultAuthorizer);
483
+ //TODO: it's weird that we don't get an error here for access denied...
484
+ var tmpResult = JSON.parse(pResponse.text);
485
+ Expect(tmpResult.Error).to.equal('UNAUTHORIZED ACCESS IS NOT ALLOWED');
486
+ fDone();
487
+ }
488
+ );
489
+ }
490
+ );
491
+ test
470
492
  (
471
493
  'read: define a custom route and get a record with it',
472
494
  function(fDone)
@@ -506,6 +528,32 @@ suite
506
528
  }
507
529
  );
508
530
  test
531
+ (
532
+ 'read: get a specific record which resolved to Deny authorization, but with a Deny authorizer that just allows',
533
+ function(fDone)
534
+ {
535
+ _Meadow.schemaFull.authorizer.Manager = {};
536
+ _Meadow.schemaFull.authorizer.Manager.Read = 'Deny';
537
+ const defaultAuthorizer = _MeadowEndpoints.endpointAuthorizers.getAuthorizer('Deny');
538
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Deny', function(req, next) { req.MeadowAuthorization = true; return next(); });
539
+
540
+ libSuperTest('http://localhost:9080/')
541
+ .get('1.0/FableTest/2')
542
+ .end(
543
+ function (pError, pResponse)
544
+ {
545
+ // Reset authorization
546
+ _Meadow.schemaFull.authorizer.Manager.Read = 'Allow';
547
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Deny', defaultAuthorizer);
548
+
549
+ const responseBody = JSON.parse(pResponse.text);
550
+ Expect(responseBody.IDAnimal).to.equal(2);
551
+ fDone();
552
+ }
553
+ );
554
+ }
555
+ );
556
+ test
509
557
  (
510
558
  'read: get a specific record with a bad parameter',
511
559
  function(fDone)