UNPKG

1.89 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2018 One Hill Technologies, LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17const { BO } = require ('base-object');
18
19/**
20 * @class Policy
21 *
22 * Base class for all policies. All policies must override the runCheck()
23 * method, which is used to evaluate the policy.
24 */
25module.exports = BO.extend ({
26 /// The default code for a policy failure.
27 failureCode: 'policy_failed',
28
29 /// The default message for a policy failure.
30 failureMessage: 'The request did not satisfy a required policy.',
31
32 /**
33 * Set the parameters of the policy. The arguments to this method will be the
34 * ones passed to the check() definition.
35 */
36 setParameters () {
37
38 },
39
40 /**
41 * Run the policy check. This method can be evaluated either synchronously or
42 * asynchronously. If you are evaluating the policy synchronously, then you must
43 * return a boolean value. True if the policy passes, or false if the policy fails.
44 *
45 * If you are evaluating the policy asynchronously, then you must return a Promise.
46 * The promise must settle with a boolean value.
47 *
48 * If there is an error, then you can throw an exception, or settle a promise by
49 * rejecting it with an exception. The latter is the preferred approach.
50 *
51 * @params req Incoming request
52 * @return {Boolean|Promise} Policy evaluation
53 */
54 runCheck: null
55});
56