meadow-endpoints 2.0.15 → 2.0.17

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": "2.0.15",
3
+ "version": "2.0.17",
4
4
  "description": "Automatic API endpoints for Meadow data.",
5
5
  "main": "source/Meadow-Endpoints.js",
6
6
  "scripts": {
@@ -44,6 +44,14 @@ var MeadowAuthorizers = function()
44
44
  _AuthorizerFunctions[pAuthorizerHash] = fAuthorizer;
45
45
  };
46
46
 
47
+ /**
48
+ * Get a specific authorizer.
49
+ */
50
+ var getAuthorizer = function(pAuthorizerHash)
51
+ {
52
+ return _AuthorizerFunctions[pAuthorizerHash];
53
+ };
54
+
47
55
 
48
56
  // Map in the default authorizers
49
57
  setAuthorizer('Allow', require(__dirname+'/authorizers/Meadow-Authorizer-Allow.js'));
@@ -175,6 +183,7 @@ var MeadowAuthorizers = function()
175
183
  var tmpNewMeadowAuthorizer = (
176
184
  {
177
185
  setAuthorizer: setAuthorizer,
186
+ getAuthorizer: getAuthorizer,
178
187
  authorize: authorize,
179
188
  authorizeRequest: authorizeRequest,
180
189
 
@@ -544,6 +544,18 @@ var MeadowEndpoints = function()
544
544
  enumerable: true
545
545
  });
546
546
 
547
+ /**
548
+ * EndpointAuthenticators
549
+ *
550
+ * @property endpointAuthorizers
551
+ * @type object
552
+ */
553
+ Object.defineProperty(tmpNewMeadowEndpointObject, 'endpointAuthorizers',
554
+ {
555
+ get: function() { return _Authorizers; },
556
+ enumerable: true
557
+ });
558
+
547
559
  /**
548
560
  * EndpointAuthenticators
549
561
  *
@@ -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();
@@ -465,6 +465,28 @@ suite
465
465
  }
466
466
  );
467
467
  test
468
+ (
469
+ 'read: define a custom authorization behavior',
470
+ function(fDone)
471
+ {
472
+ const defaultAuthorizer = _MeadowEndpoints.endpointAuthorizers.getAuthorizer('Allow');
473
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Allow', function(req, next) { req.MeadowAuthorization = false; return next(); });
474
+ _Orator.webServer.get('/CustomHotRodRoute/:IDRecord', _MeadowEndpoints.endpointAuthenticators.Read, _MeadowEndpoints.wireState, _MeadowEndpoints.endpoints.Read);
475
+ libSuperTest('http://localhost:9080/')
476
+ .get('CustomHotRodRoute/2')
477
+ .end(
478
+ function (pError, pResponse)
479
+ {
480
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Allow', defaultAuthorizer);
481
+ //TODO: it's weird that we don't get an error here for access denied...
482
+ var tmpResult = JSON.parse(pResponse.text);
483
+ Expect(tmpResult.Error).to.equal('UNAUTHORIZED ACCESS IS NOT ALLOWED');
484
+ fDone();
485
+ }
486
+ );
487
+ }
488
+ );
489
+ test
468
490
  (
469
491
  'read: define a custom route and get a record with it',
470
492
  function(fDone)
@@ -504,6 +526,32 @@ suite
504
526
  }
505
527
  );
506
528
  test
529
+ (
530
+ 'read: get a specific record which resolved to Deny authorization, but with a Deny authorizer that just allows',
531
+ function(fDone)
532
+ {
533
+ _Meadow.schemaFull.authorizer.Manager = {};
534
+ _Meadow.schemaFull.authorizer.Manager.Read = 'Deny';
535
+ const defaultAuthorizer = _MeadowEndpoints.endpointAuthorizers.getAuthorizer('Deny');
536
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Deny', function(req, next) { req.MeadowAuthorization = true; return next(); });
537
+
538
+ libSuperTest('http://localhost:9080/')
539
+ .get('1.0/FableTest/2')
540
+ .end(
541
+ function (pError, pResponse)
542
+ {
543
+ // Reset authorization
544
+ _Meadow.schemaFull.authorizer.Manager.Read = 'Allow';
545
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Deny', defaultAuthorizer);
546
+
547
+ const responseBody = JSON.parse(pResponse.text);
548
+ Expect(responseBody.IDAnimal).to.equal(2);
549
+ fDone();
550
+ }
551
+ );
552
+ }
553
+ );
554
+ test
507
555
  (
508
556
  'read: get a specific record with a bad parameter',
509
557
  function(fDone)