@hitchy/plugin-auth 0.3.5 → 0.3.7
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 +15 -15
- package/.gitlab-ci.yml +0 -69
- package/coverage/base.css +0 -224
- package/coverage/block-navigation.js +0 -87
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +0 -236
- package/coverage/plugin-auth/api/controller/index.html +0 -116
- package/coverage/plugin-auth/api/controller/user.js.html +0 -373
- package/coverage/plugin-auth/api/model/authorization/index.html +0 -116
- package/coverage/plugin-auth/api/model/authorization/rule.js.html +0 -232
- package/coverage/plugin-auth/api/model/index.html +0 -146
- package/coverage/plugin-auth/api/model/role.js.html +0 -205
- package/coverage/plugin-auth/api/model/user-to-role.js.html +0 -172
- package/coverage/plugin-auth/api/model/user.js.html +0 -757
- package/coverage/plugin-auth/api/policy/authentication.js.html +0 -784
- package/coverage/plugin-auth/api/policy/authorization.js.html +0 -187
- package/coverage/plugin-auth/api/policy/index.html +0 -146
- package/coverage/plugin-auth/api/policy/user.js.html +0 -484
- package/coverage/plugin-auth/api/service/auth/index.html +0 -116
- package/coverage/plugin-auth/api/service/auth/manager.js.html +0 -964
- package/coverage/plugin-auth/api/service/authentication/index.html +0 -131
- package/coverage/plugin-auth/api/service/authentication/passport.js.html +0 -298
- package/coverage/plugin-auth/api/service/authentication/strategies.js.html +0 -934
- package/coverage/plugin-auth/api/service/authorization/index.html +0 -146
- package/coverage/plugin-auth/api/service/authorization/node.js.html +0 -949
- package/coverage/plugin-auth/api/service/authorization/policy-generator.js.html +0 -391
- package/coverage/plugin-auth/api/service/authorization/tree.js.html +0 -988
- package/coverage/plugin-auth/config/auth.js.html +0 -145
- package/coverage/plugin-auth/config/index.html +0 -116
- package/coverage/plugin-auth/index.html +0 -116
- package/coverage/plugin-auth/index.js.html +0 -388
- package/coverage/prettify.css +0 -1
- package/coverage/prettify.js +0 -2
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +0 -196
- package/coverage/tmp/coverage-2884-1653923449663-0.json +0 -1
- package/docs/.vuepress/config.js +0 -55
- package/docs/.vuepress/styles/palette.styl +0 -4
- package/docs/api/config.md +0 -180
- package/docs/api/controller/readme.md +0 -10
- package/docs/api/controller/user.md +0 -44
- package/docs/api/model/authorization-rule.md +0 -38
- package/docs/api/model/readme.md +0 -13
- package/docs/api/model/role.md +0 -22
- package/docs/api/model/user-to-role.md +0 -26
- package/docs/api/model/user.md +0 -50
- package/docs/api/policy/authentication.md +0 -44
- package/docs/api/policy/authorization.md +0 -27
- package/docs/api/policy/readme.md +0 -12
- package/docs/api/policy/user.md +0 -22
- package/docs/api/readme.md +0 -52
- package/docs/api/routing.md +0 -101
- package/docs/api/service/auth-manager.md +0 -86
- package/docs/api/service/authentication-passport.md +0 -21
- package/docs/api/service/authentication-strategies.md +0 -22
- package/docs/api/service/authorization-node.md +0 -102
- package/docs/api/service/authorization-policy-generator.md +0 -66
- package/docs/api/service/authorization-tree.md +0 -123
- package/docs/api/service/readme.md +0 -15
- package/docs/guides/getting-started.md +0 -69
- package/docs/guides/idp-login.png +0 -0
- package/docs/guides/idp-saml-cert.png +0 -0
- package/docs/guides/openid-connect.md +0 -164
- package/docs/guides/readme.md +0 -12
- package/docs/guides/saml.md +0 -161
- package/docs/introduction.md +0 -78
- package/docs/readme.md +0 -11
package/docs/api/routing.md
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
prev: config.md
|
|
3
|
-
next: false
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# Routing defaults
|
|
7
|
-
|
|
8
|
-
Unless [disabled in runtime configuration](config.md#config-auth-prefix), this plugin is always setting up routes providing basic user authentication to a client.
|
|
9
|
-
|
|
10
|
-
::: warning Customizable prefix
|
|
11
|
-
The following documentation relies on default prefix `/api/auth`. You may [change this default in runtime configuration](config.md#config-auth-prefix). Adopt URLs listed below according to your setup if required.
|
|
12
|
-
:::
|
|
13
|
-
|
|
14
|
-
Some basic initialization is required in every request. This can't be disabled using runtime configuration.
|
|
15
|
-
|
|
16
|
-
## `POST /api/auth/login/:strategy?`
|
|
17
|
-
|
|
18
|
-
This route is authenticating some user by processing its credentials found in request body which is either JSON-formatted or some URL-encoded form data.
|
|
19
|
-
|
|
20
|
-
Actual format of provided credentials depend on passport strategy being used. The built-in local strategy used by default expects `username` and `password` being provided in properties named accordingly:
|
|
21
|
-
|
|
22
|
-
```http request
|
|
23
|
-
POST /api/auth/login HTTP/1.0
|
|
24
|
-
Content-Type: application/x-www-form-urlencoded
|
|
25
|
-
|
|
26
|
-
username=john.doe&password=my-secret-token
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
The result is always provided as JSON object.
|
|
30
|
-
|
|
31
|
-
:::tip Background
|
|
32
|
-
This routing combines policy [authentication.login](policy/authentication.md#login) with controller [user.authenticate](controller/user.md#authenticate).
|
|
33
|
-
:::
|
|
34
|
-
|
|
35
|
-
## `GET /api/auth/login/:strategy`
|
|
36
|
-
|
|
37
|
-
This route is integrating authentication code for optionally picking up external authentication on returning from remote service. You need to provide a strategy here.
|
|
38
|
-
|
|
39
|
-
## `GET /api/auth/logout`
|
|
40
|
-
|
|
41
|
-
This route is triggering removal any server-side session data suitable for implicitly re-authenticating some current user.
|
|
42
|
-
|
|
43
|
-
The result is provided as JSON object.
|
|
44
|
-
|
|
45
|
-
:::tip Background
|
|
46
|
-
This routing combines policy [authentication.logout](policy/authentication.md#logout) with controller [user.unauthenticate](controller/user.md#unauthenticate).
|
|
47
|
-
:::
|
|
48
|
-
|
|
49
|
-
## `GET /api/auth/current`
|
|
50
|
-
|
|
51
|
-
The route delivers information on some currently authenticated user as JSON object.
|
|
52
|
-
|
|
53
|
-
If no user is authenticated, the result is:
|
|
54
|
-
|
|
55
|
-
```json
|
|
56
|
-
{
|
|
57
|
-
"success": true,
|
|
58
|
-
"authenticated": false
|
|
59
|
-
}
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
Following a successful authentication, the result is similar to this one:
|
|
63
|
-
|
|
64
|
-
```json
|
|
65
|
-
{
|
|
66
|
-
"success": true,
|
|
67
|
-
"authenticated": {
|
|
68
|
-
"uuid": "12345678-1234-1234-1234-1234-123456789012",
|
|
69
|
-
"name": "john.doe",
|
|
70
|
-
"roles": ["users", "customers"]
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
:::tip Background
|
|
76
|
-
This routing targets controller [user.getCurrent](controller/user.md#getcurrent).
|
|
77
|
-
:::
|
|
78
|
-
|
|
79
|
-
## `POST /api/auth/password`
|
|
80
|
-
|
|
81
|
-
This route is processing request for changing some previously authenticated user's token a.k.a. password.
|
|
82
|
-
|
|
83
|
-
All information is read from request body which is either JSON-formatted or some URL-encoded form data. It must contain properties `current` and `next` providing user's current password and the one to become user's next password.
|
|
84
|
-
|
|
85
|
-
```http request
|
|
86
|
-
POST /api/auth/passowrd HTTP/1.0
|
|
87
|
-
Content-Type: application/x-www-form-urlencoded
|
|
88
|
-
|
|
89
|
-
current=my-old-secret&next=my-new-secret
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
The result is provided as JSON object.
|
|
93
|
-
|
|
94
|
-
::: warning Current user, only
|
|
95
|
-
By intention, this request handler is limited to changing current user's password, only. A custom request handler is required to change password of different users.
|
|
96
|
-
:::
|
|
97
|
-
|
|
98
|
-
:::tip Background
|
|
99
|
-
This routing combines policy [user.changePassword](policy/user.md#changepassword) with controller [user.changePassword](controller/user.md#changepassword).
|
|
100
|
-
:::
|
|
101
|
-
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
prev: authentication-strategies.md
|
|
3
|
-
next: authorization-tree.md
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# AuthManager
|
|
7
|
-
|
|
8
|
-
This service provides several helper functions commonly useful on managing authentication and authorization at runtime.
|
|
9
|
-
|
|
10
|
-
::: tip FYI
|
|
11
|
-
Due to supporting both parts, the name is reduced to common prefix _Auth_ by intention.
|
|
12
|
-
:::
|
|
13
|
-
|
|
14
|
-
## Properties
|
|
15
|
-
|
|
16
|
-
### adminRole
|
|
17
|
-
|
|
18
|
-
This property commonly exposes name of role implicitly granting access to any resource without regards to existing authorization rules in runtime configuration or in local database. The resulting name depends on current runtime environment and runtime configuration.
|
|
19
|
-
|
|
20
|
-
## Methods
|
|
21
|
-
|
|
22
|
-
### asUser()
|
|
23
|
-
|
|
24
|
-
**Signature:** `async asUser( user, [ createIfMissing ] ): User`
|
|
25
|
-
|
|
26
|
-
This wrapper is meant to assure that provided user is actually an instance of model [User](../model/user.md) existing in local database.
|
|
27
|
-
|
|
28
|
-
It takes a user's name, ...
|
|
29
|
-
|
|
30
|
-
```javascript
|
|
31
|
-
const user = await AuthManager.asUser( "john.doe" );
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
... an object with property name selecting desired user (ignoring any other property in that object) ...
|
|
35
|
-
|
|
36
|
-
```javascript
|
|
37
|
-
const user = await AuthManager.asUser( { name: "john.doe", foo: "bar" } );
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
... or the user's existing instance as input.
|
|
41
|
-
|
|
42
|
-
```javascript
|
|
43
|
-
const user = await AuthManager.asUser( new User( uuid ) );
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
It retrieves instance of user selected either way. Due to accepting instances of [User](../model/user.md), it can be safely used multiple times.
|
|
47
|
-
|
|
48
|
-
Optional boolean parameter `createIfMissing` must be set true to implicitly create selected user in local database if missing. Otherwise, the method throws an error causing promised result to be rejected.
|
|
49
|
-
|
|
50
|
-
### asRole()
|
|
51
|
-
|
|
52
|
-
**Signature:** `async asRole( role, [ createIfMissing ] ): Role`
|
|
53
|
-
|
|
54
|
-
This is the counterpart to [asUser()](#asuser) for simplifying access on roles. The same semantics apply.
|
|
55
|
-
|
|
56
|
-
### listRolesOfUser()
|
|
57
|
-
|
|
58
|
-
**Signature:** `async listRolesOfUser( user, [ createIfMissing ], [ uuidsOnly ] ): Roles[]`
|
|
59
|
-
|
|
60
|
-
The method takes a user and qualifies it via [asUser()](#asuser) before fetching list of user's associated roles. Argument for optional `createIfMissing` is forwarded to `asUser()` and `false` by default. Argument for optional `uuidsOnly` must be set explicitly to prevent listed instances of [Role](../model/role.md) to implicitly load their records from database.
|
|
61
|
-
|
|
62
|
-
### listUsersOfRole()
|
|
63
|
-
|
|
64
|
-
**Signature:** `async listUsersOfRole( role, [ createIfMissing ], [ uuidsOnly ] ): User[]`
|
|
65
|
-
|
|
66
|
-
This method is the counterpart to [listRolesOfUser()](#listrolesofuser): it is querying local database for users associated with a given role. Provided role is qualified via [asRole()](#asrole) with argument `createIfMissing` being forwarded.
|
|
67
|
-
|
|
68
|
-
Optional boolean parameter `uuidsOnly` must be set to prevent implicit retrieval of either listed user's record from database.
|
|
69
|
-
|
|
70
|
-
### grantRoleToUser()
|
|
71
|
-
|
|
72
|
-
**Signature:** `async grantRoleToUser( role, user, [ createIfMissing] ): void`
|
|
73
|
-
|
|
74
|
-
This helper is provided for conveniently [associating](../model/user-to-role.md) a [user](../model/user.md) with a [role](../model/role.md). Provided arguments for `role` and `user` are qualified with [asRole()](#asrole) and [asUser()](#asuser). Optional argument for `createIfMissing` is forwarded to either method internally.
|
|
75
|
-
|
|
76
|
-
### revokeRoleFromUser()
|
|
77
|
-
|
|
78
|
-
**Signature:** `async revokeRoleFromUser( role, user, [ createIfMissing] ): void`
|
|
79
|
-
|
|
80
|
-
This helper is provided for conveniently removing any existing [association](../model/user-to-role.md) between a [user](../model/user.md) and a [role](../model/role.md). Provided arguments for `role` and `user` are qualified with [asRole()](#asrole) and [asUser()](#asuser). Optional argument for `createIfMissing` is forwarded to either method internally.
|
|
81
|
-
|
|
82
|
-
### createAdminIfMissing()
|
|
83
|
-
|
|
84
|
-
**Signature:** `async createAdminIfMissing(): void`
|
|
85
|
-
|
|
86
|
-
This method is used during bootstrap to make sure there is always a user associated with configured [administrator role](#adminrole) preventing any authorization management from locking out all existing users.
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
prev: /api/service/
|
|
3
|
-
next: authentication-strategies.md
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# AuthenticationPassport
|
|
7
|
-
|
|
8
|
-
This service integrates [Passport](https://www.passportjs.org/) with Hitchy's request routing. The service itself represents instance of Passport created for integration with Hitchy-based application.
|
|
9
|
-
|
|
10
|
-
This service is primarily for internal use. You should use it in edge cases to additionally customize Passport, only.
|
|
11
|
-
|
|
12
|
-
## Methods
|
|
13
|
-
|
|
14
|
-
### integrateWithHitchy()
|
|
15
|
-
|
|
16
|
-
**Signature:** `integrateWithHitchy(): void`
|
|
17
|
-
|
|
18
|
-
This method has been implemented for internal use. It is executed on every incoming request
|
|
19
|
-
|
|
20
|
-
* to set up handlers persisting authenticated user's information to server-side session and restoring it from there in follow-up requests and
|
|
21
|
-
* to instruct passport to [use](https://www.passportjs.org/docs/configure/#strategies) every configured strategy.
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
prev: authentication-passport.md
|
|
3
|
-
next: auth-manager.md
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# AuthenticationStrategies
|
|
7
|
-
|
|
8
|
-
This service helps with picking a default strategy. In addition, it exposes a strategy for local authentication. The latter is used on integrating [Passport](https://www.passportjs.org/) while bootstrapping your application and this plugin.
|
|
9
|
-
|
|
10
|
-
## Methods
|
|
11
|
-
|
|
12
|
-
### defaultStrategy()
|
|
13
|
-
|
|
14
|
-
**Signature:** `defaultStrategy(): string`
|
|
15
|
-
|
|
16
|
-
Provides name of strategy to use by default depending on current application's configuration.
|
|
17
|
-
|
|
18
|
-
### generateLocal()
|
|
19
|
-
|
|
20
|
-
**Signature:** `generateLocal(): Strategy`
|
|
21
|
-
|
|
22
|
-
Creates custom instance of Passport's LocalStrategy set up for integrating with Hitchy's request routing.
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
prev: authorization-tree.md
|
|
3
|
-
next: authorization-policy-generator.md
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# AuthorizationNode
|
|
7
|
-
|
|
8
|
-
This class is primarily used by [AuthorizationTree](authorization-tree.md) to describe a tree of nodes.
|
|
9
|
-
|
|
10
|
-
## Properties
|
|
11
|
-
|
|
12
|
-
### parent
|
|
13
|
-
|
|
14
|
-
Refers to parent authorization node in a tree of nodes. It is nullish on top-most node of tree.
|
|
15
|
-
|
|
16
|
-
### name
|
|
17
|
-
|
|
18
|
-
Provides segment of resource name used to address this node in context of its parent node.
|
|
19
|
-
|
|
20
|
-
## Methods
|
|
21
|
-
|
|
22
|
-
### clear()
|
|
23
|
-
|
|
24
|
-
**Signature:** `clear(): AuthorizationNode`
|
|
25
|
-
|
|
26
|
-
Clears thread of current node by recursively invoking this very method on superordinated nodes before deleting any list of users and roles tracked in context of current node for being granted or revoked access on resource represented by current node.
|
|
27
|
-
|
|
28
|
-
Returns this node for daisy-chaining calls.
|
|
29
|
-
|
|
30
|
-
### addRole()
|
|
31
|
-
|
|
32
|
-
**Signature:** `addRole( roleName, [ accept ] ): AuthorizationNode`
|
|
33
|
-
|
|
34
|
-
Adds named role to current node for being granted or revoked access depending on optionally provided argument `accept`. On omitting or on providing `true`, access on resource represented by current node is granted to named role. Otherwise, access is revoked.
|
|
35
|
-
|
|
36
|
-
Returns this node for daisy-chaining calls.
|
|
37
|
-
|
|
38
|
-
### addUser()
|
|
39
|
-
|
|
40
|
-
**Signature:** `addUser( userName, [ accept ] ): AuthorizationNode`
|
|
41
|
-
|
|
42
|
-
Adds named user to current node for being granted or revoked access depending on optionally provided argument `accept`. On omitting or on providing `true`, access on resource represented by current node is granted to named user. Otherwise, access is revoked.
|
|
43
|
-
|
|
44
|
-
Returns this node for daisy-chaining calls.
|
|
45
|
-
|
|
46
|
-
### removeRole()
|
|
47
|
-
|
|
48
|
-
**Signature:** `removeRole( roleName, [ accept ] ): AuthorizationNode`
|
|
49
|
-
|
|
50
|
-
Revokes previously granted or revoked access on resource represented by current node to/from named role. Optionally provided argument `accept` must be equivalent to what was provided on adding role before.
|
|
51
|
-
|
|
52
|
-
Returns this node for daisy-chaining calls.
|
|
53
|
-
|
|
54
|
-
### removeUser()
|
|
55
|
-
|
|
56
|
-
**Signature:** `removeUser( userName, [ accept ] ): AuthorizationNode`
|
|
57
|
-
|
|
58
|
-
Revokes previously granted or revoked access on resource represented by current node to/from named user. Optionally provided argument `accept` must be equivalent to what was provided on adding role before.
|
|
59
|
-
|
|
60
|
-
Returns this node for daisy-chaining calls.
|
|
61
|
-
|
|
62
|
-
### getChild()
|
|
63
|
-
|
|
64
|
-
**Signature:** `getChild( name, [ addIfMissing ] ): AuthorizationNode`
|
|
65
|
-
|
|
66
|
-
Retrieves reference on node subordinated to current one representing immediate child of current resource in a hierarchy of resources.
|
|
67
|
-
|
|
68
|
-
Provided `name` must be a single segment's name, only. Optional argument `addIfMissing` must be `true` to always return another node. Otherwise, undefined is returned if selected child node does not exist.
|
|
69
|
-
|
|
70
|
-
### isAuthorized()
|
|
71
|
-
|
|
72
|
-
**Signature:** `isAuthorized( userName, roleName ): number`
|
|
73
|
-
|
|
74
|
-
Checks if current nodes' lists of grants and revokes for accessing represented resource are containing provided user name or role name.
|
|
75
|
-
|
|
76
|
-
The method returns
|
|
77
|
-
|
|
78
|
-
* -1 if user/role is revoked access on represented resource,
|
|
79
|
-
* 1 if user/role is granted access on represented resource and
|
|
80
|
-
* 0 if current node doesn't affect provided user or role.
|
|
81
|
-
|
|
82
|
-
If either provided name of a user or role is granted access while the other is rejected, the method throws exception.
|
|
83
|
-
|
|
84
|
-
### isSpare()
|
|
85
|
-
|
|
86
|
-
**Signature:** `isSpare(): boolean`
|
|
87
|
-
|
|
88
|
-
Detects if current node and all of its child nodes are spare by means of not granting or revoking access on their represented resource to/from any user or role.
|
|
89
|
-
|
|
90
|
-
This information is used on garbage collection to drop data in runtime memory which isn't actually required anymore.
|
|
91
|
-
|
|
92
|
-
### gc()
|
|
93
|
-
|
|
94
|
-
**Signature:** `gc(): AuthorizationNode`
|
|
95
|
-
|
|
96
|
-
Runs garbage collection on thread of current node returning a reduced copy of current node's thread for replacement in scope of parent node. `undefined` is returned in case of current node being available for removal.
|
|
97
|
-
|
|
98
|
-
### path()
|
|
99
|
-
|
|
100
|
-
**Signature:** `path(): string`
|
|
101
|
-
|
|
102
|
-
Recursively compiles qualified name of resource represented by current node.
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
prev: authorization-node.md
|
|
3
|
-
next: ../config.md
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# AuthorizationPolicyGenerator
|
|
7
|
-
|
|
8
|
-
This service is generating functions suitable for [policy-handling](https://core.hitchy.org/internals/routing-basics.html#policies) requests. Generated functions filter mismatching requests thus help with writing more self-explanatory request routing configurations.
|
|
9
|
-
|
|
10
|
-
::: tip
|
|
11
|
-
Users with administrator role always pass any generated policy.
|
|
12
|
-
:::
|
|
13
|
-
|
|
14
|
-
## Methods
|
|
15
|
-
|
|
16
|
-
### hasRole()
|
|
17
|
-
|
|
18
|
-
**Signature:** `hasRole( roles ): Hitchy.Core.RequestPolicyHandler`
|
|
19
|
-
|
|
20
|
-
Generates request policy handler testing if a provided request's user is associated with one of the roles listed here. If request's user is associated with either role, the request processing is continued. Otherwise this policy responds with HTTP status code 403.
|
|
21
|
-
|
|
22
|
-
Argument `roles` is
|
|
23
|
-
|
|
24
|
-
* a string naming single role without any prefix or suffix or
|
|
25
|
-
* an array of such strings.
|
|
26
|
-
|
|
27
|
-
Returned function can be injected into runtime configuration for policies like this:
|
|
28
|
-
|
|
29
|
-
```javascript
|
|
30
|
-
module.exports = function() {
|
|
31
|
-
const { hasRole } = this.runtime.service.AuthorizationPolicyGenerator;
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
policies: {
|
|
35
|
-
"GET /api/premium": hasRole( "customer" ),
|
|
36
|
-
"GET /api/public": hasRole( [ "guest", "customer", "manager" ] ),
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### mayAccess( resource )
|
|
43
|
-
|
|
44
|
-
**Signature:** `mayAccess( resource ): Hitchy.Core.RequestPolicyHandler`
|
|
45
|
-
|
|
46
|
-
Generates request policy handler testing if a provided request's user is authorized to access any of the listed resources.
|
|
47
|
-
|
|
48
|
-
Argument `resource` is
|
|
49
|
-
|
|
50
|
-
* a string naming single resource or
|
|
51
|
-
* an array of such resource names.
|
|
52
|
-
|
|
53
|
-
Resulting handler can be injected into runtime configuration for policies like this:
|
|
54
|
-
|
|
55
|
-
```javascript
|
|
56
|
-
module.exports = function() {
|
|
57
|
-
const { mayAccess } = this.runtime.service.AuthorizationPolicyGenerator;
|
|
58
|
-
|
|
59
|
-
return {
|
|
60
|
-
policies: {
|
|
61
|
-
"GET /api/premium": mayAccess( "plan.premium" ),
|
|
62
|
-
"GET /api/export": mayAccess( [ "plan.premium", "level.manager" ] ),
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
```
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
prev: auth-manager.md
|
|
3
|
-
next: authorization-node.md
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# AuthorizationTree
|
|
7
|
-
|
|
8
|
-
This service implements class for tracking state of authorizations by means of granting or revoking access on named resources to/from users and role in a tree of [nodes](authorization-node.md).
|
|
9
|
-
|
|
10
|
-
In opposition to other services, this service is primarily a class which could be instantiated to describe a tree of authorization settings. It complies with existing services by [exposing a single instance as current tree of authorizations](#current), only.
|
|
11
|
-
|
|
12
|
-
On starting application, this plugin is processing runtime configuration and local database for existing [authorization rules](../model/authorization-rule.md) building this tree in runtime memory for improved authorizations testing during request routing.
|
|
13
|
-
|
|
14
|
-
## Properties
|
|
15
|
-
|
|
16
|
-
### current
|
|
17
|
-
|
|
18
|
-
This static property exposes single instance of current runtime representing the tree which has been set up by the plugin on starting application. This instance is implicitly updated by some actions affecting existing users, roles and authorization rules in local database.
|
|
19
|
-
|
|
20
|
-
## Methods
|
|
21
|
-
|
|
22
|
-
::: warning Instance methods
|
|
23
|
-
In opposition to other services, these methods are instance methods and thus are available e.g. on current tree, only.
|
|
24
|
-
|
|
25
|
-
Instead of writing
|
|
26
|
-
|
|
27
|
-
```javascript
|
|
28
|
-
AuthorizationTree.addRule( rule )
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
you must write
|
|
32
|
-
|
|
33
|
-
```javascript
|
|
34
|
-
AuthorizationTree.current.addRule( rule )
|
|
35
|
-
```
|
|
36
|
-
:::
|
|
37
|
-
|
|
38
|
-
### selectNode()
|
|
39
|
-
|
|
40
|
-
**Signature:** `selectNode( selector, [ addIfMissing ], [ callback ] ): AuthorizationNode`
|
|
41
|
-
|
|
42
|
-
This method descends into tree of nodes according to provided selector which is a resource's hierarchical name. If optional argument `addIfMissing` is `true`, any missing node is created on the fly assuring to always return a node representing selected resource.
|
|
43
|
-
|
|
44
|
-
When providing callback in third argument, it is invoked on every existing (or implicitly created) node passed while descending into tree. The callback is invoked with
|
|
45
|
-
|
|
46
|
-
* current node,
|
|
47
|
-
* segment of selector a.k.a. resource name selecting this node in context of its parent,
|
|
48
|
-
* the segment's index in list of segments extracted from selector and
|
|
49
|
-
* the full list of segments to be processed.
|
|
50
|
-
|
|
51
|
-
The callback may return `false` to prematurely stop descending into tree.
|
|
52
|
-
|
|
53
|
-
### clear()
|
|
54
|
-
|
|
55
|
-
**Signature:** `clear(): AuthorizationTree`
|
|
56
|
-
|
|
57
|
-
Clears tree by recursive deleting all its nodes. Returns tree itself for daisy-chaining calls.
|
|
58
|
-
|
|
59
|
-
### addRule()
|
|
60
|
-
|
|
61
|
-
**Signature:** `addRule( rule ): AuthorizationTree`
|
|
62
|
-
|
|
63
|
-
Adjusts tree to represent provided [authorization rule](../model/authorization-rule.md). The provided rule may be instance of model [AuthorizationRule](../model/authorization-rule.md) or any other object resembling authorization rules by providing equivalent properties `selector`, `user`, `role` and `accept`.
|
|
64
|
-
|
|
65
|
-
::: warning Note
|
|
66
|
-
Adding a rule doesn't imply to eventually grant access on some resource.
|
|
67
|
-
:::
|
|
68
|
-
|
|
69
|
-
The method returns current tree for daisy-chaining calls.
|
|
70
|
-
|
|
71
|
-
### removeRule()
|
|
72
|
-
|
|
73
|
-
**Signature:** `removeRule( rule ): AuthorizationTree`
|
|
74
|
-
|
|
75
|
-
This is the counterpart of [addRule()](#addrule). It adjusts tree to stop representing provided [authorization rule](../model/authorization-rule.md).
|
|
76
|
-
|
|
77
|
-
::: warning Note
|
|
78
|
-
Removing a rule doesn't imply to eventually revoke access on some resource. It's reverting some previous change of tree, only.
|
|
79
|
-
:::
|
|
80
|
-
|
|
81
|
-
::: warning Note
|
|
82
|
-
When adding a rule multiple times, it will be counted as such. You need to remove the rule as many times as it has been added before to actually remove it from tree.
|
|
83
|
-
:::
|
|
84
|
-
|
|
85
|
-
The method returns current tree for daisy-chaining calls.
|
|
86
|
-
|
|
87
|
-
### isAuthorized()
|
|
88
|
-
|
|
89
|
-
**Signature:** `isAuthorized( selector, user, role, [ acceptByDefault ] ): boolean`
|
|
90
|
-
|
|
91
|
-
This method checks, whether tree is currently granting or revoking access on a selected resource to/from user and/or role.
|
|
92
|
-
|
|
93
|
-
::: warning Note
|
|
94
|
-
Access on a resource can be granted or revoked on particular resource. It might be granted/revoked on a superordinated resource, too.
|
|
95
|
-
:::
|
|
96
|
-
|
|
97
|
-
The method takes a user or a role and checks if it is affected by any rule tracked for either node of tree which is passed on descending into tree according to provided selector. If there is no rule on any passed node affecting provided user or role, the default provided in optional fourth argument is used, which is false by default, thus preventing access by default.
|
|
98
|
-
|
|
99
|
-
The method returns `true`, if access is granted, and `false` otherwise.
|
|
100
|
-
|
|
101
|
-
### gc()
|
|
102
|
-
|
|
103
|
-
**Signature:** `gc( [ force ] ): void`
|
|
104
|
-
|
|
105
|
-
This method is meant to search tree for sparse threads and remove them if necessary. It is implicitly invoked each time a rule is removed with [removeRule()](#removerule), though garbage collection is triggered after seeing a certain number of calls, only. You can enforce a garbage collection by setting optional argument `force`.
|
|
106
|
-
|
|
107
|
-
### loadFromDatabase()
|
|
108
|
-
|
|
109
|
-
**Signature:** `async loadFromDatabase(): void`
|
|
110
|
-
|
|
111
|
-
This method is reading all existing instances of [AuthorizationRule](../model/authorization-rule.md) from local database invoking [addRule()](#addrule) on every found rule.
|
|
112
|
-
|
|
113
|
-
### loadFromConfiguration()
|
|
114
|
-
|
|
115
|
-
**Signature:** `loadFromConfiguration( configuration ): void`
|
|
116
|
-
|
|
117
|
-
The method searches provided configuration for describing rules in one of several supported formats and adds them to current tree. The provided configuration can be
|
|
118
|
-
|
|
119
|
-
* an object mapping resource names into
|
|
120
|
-
* a string naming user or role granted or revoked access on named resource,
|
|
121
|
-
* a string naming multiple users and/or roles separated by comma with each granted or revoked access on resource,
|
|
122
|
-
* an array of either sort of string or
|
|
123
|
-
* an object providing `users` and `roles` or `grants` and `revokes` in separate properties each supporting a format similar to the one of strings described above and another map of relative resource names into rules for resources subordinated to current one.
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
prev: ../
|
|
3
|
-
next: false
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# Services
|
|
7
|
-
|
|
8
|
-
Select one of the provided [services](https://core.hitchy.org/internals/components.html#services):
|
|
9
|
-
|
|
10
|
-
* [AuthenticationPassport](authentication-passport.md)
|
|
11
|
-
* [AuthenticationStrategies](authentication-strategies.md)
|
|
12
|
-
* [AuthManager](auth-manager.md)
|
|
13
|
-
* [AuthorizationTree](authorization-tree.md)
|
|
14
|
-
* [AuthorizationNode](authorization-node.md)
|
|
15
|
-
* [AuthorizationPolicyGenerator](authorization-policy-generator.md)
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
prev: ../guides/
|
|
3
|
-
next: openid-connect.md
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# Quick Start
|
|
7
|
-
|
|
8
|
-
This document is a step-by-step guide for basically setting up [Hitchy](https://core.hitchy.org/) with support for authentication and authorization using this plugin.
|
|
9
|
-
|
|
10
|
-
## Setup Project
|
|
11
|
-
|
|
12
|
-
Create a folder for your application and enter it:
|
|
13
|
-
|
|
14
|
-
```bash
|
|
15
|
-
mkdir my-new-app
|
|
16
|
-
cd my-new-app
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
Initialize your application's package management so dependencies are tracked:
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
npm init -y
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
Install [@hitchy/core](https://www.npmjs.com/package/@hitchy/core) and [@hitchy/plugin-auth](https://www.npmjs.com/package/@hitchy/plugin-auth) as dependencies:
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
npm install @hitchy/core @hitchy/plugin-auth
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
:::tip
|
|
32
|
-
Plugin [@hitchy/plugin-auth](https://www.npmjs.com/package/@hitchy/plugin-auth) is
|
|
33
|
-
implicitly fetching [@hitchy/plugin-odem](https://www.npmjs.com/package/@hitchy/plugin-odem), [@hitchy/plugin-session](https://www.npmjs.com/package/@hitchy/plugin-session) and [@hitchy/plugin-cookies](https://www.npmjs.com/package/@hitchy/plugin-cookies) as dependencies.
|
|
34
|
-
:::
|
|
35
|
-
|
|
36
|
-
## Start Hitchy
|
|
37
|
-
|
|
38
|
-
At this point Hitchy is ready. Thus, start it with:
|
|
39
|
-
|
|
40
|
-
```bash
|
|
41
|
-
hitchy start
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
:::warning Hitchy not found?
|
|
45
|
-
If Hitchy isn't found this might be due to issues with your Node.js and npm setup. In most cases using **npx** solves this issue for now:
|
|
46
|
-
|
|
47
|
-
```bash
|
|
48
|
-
npx hitchy start
|
|
49
|
-
```
|
|
50
|
-
:::
|
|
51
|
-
|
|
52
|
-
Keep it running while trying out next.
|
|
53
|
-
|
|
54
|
-
:::tip
|
|
55
|
-
Whenever you want to stop Hitchy press Ctrl+C to gracefully shut it down.
|
|
56
|
-
:::
|
|
57
|
-
|
|
58
|
-
## Try It Out
|
|
59
|
-
|
|
60
|
-
The plugin [injects special endpoints for managing a user's authentication](../api/routing.md) by default. You should be able to authenticate with a request like this:
|
|
61
|
-
|
|
62
|
-
```http request
|
|
63
|
-
POST /api/auth/login HTTP/1.0
|
|
64
|
-
Content-Type: application/x-www-form-urlencoded
|
|
65
|
-
|
|
66
|
-
username=admin&password=nimda
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
This works due to the default [configuration](../api/config.md#config-auth-admin) for implicitly [setting up administrator account](/api/service/auth-manager.md#createadminifmissing) on application start.
|
|
Binary file
|
|
Binary file
|