@guardian/pan-domain-node 0.5.0 → 1.0.0
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/.github/PULL_REQUEST_TEMPLATE.md +3 -0
- package/.github/workflows/ci.yml +29 -0
- package/.github/workflows/node-release.yml +57 -0
- package/CHANGELOG.md +118 -0
- package/CODEOWNERS +1 -0
- package/LICENSE +201 -0
- package/README.md +95 -0
- package/dist/src/api.d.ts +30 -9
- package/dist/src/api.js +13 -8
- package/dist/src/panda.d.ts +1 -1
- package/dist/src/panda.js +50 -12
- package/dist/src/utils.d.ts +6 -4
- package/dist/src/utils.js +23 -3
- package/dist/test/panda.test.js +196 -23
- package/dist/test/utils.test.js +10 -5
- package/package.json +1 -1
- package/src/api.ts +63 -10
- package/src/panda.ts +56 -19
- package/src/utils.ts +30 -4
- package/test/panda.test.ts +226 -25
- package/test/utils.test.ts +12 -6
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
on:
|
|
3
|
+
workflow_dispatch:
|
|
4
|
+
pull_request:
|
|
5
|
+
|
|
6
|
+
# triggering CI default branch improves caching
|
|
7
|
+
# see https://docs.github.com/en/free-pro-team@latest/actions/guides/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache
|
|
8
|
+
push:
|
|
9
|
+
branches:
|
|
10
|
+
- main
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
CI:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- name: Checkout
|
|
18
|
+
uses: actions/checkout@v4
|
|
19
|
+
- uses: actions/setup-node@v4
|
|
20
|
+
with:
|
|
21
|
+
node-version-file: '.nvmrc'
|
|
22
|
+
cache: npm
|
|
23
|
+
cache-dependency-path: 'package-lock.json'
|
|
24
|
+
|
|
25
|
+
- name: JS Build
|
|
26
|
+
run: |
|
|
27
|
+
npm ci
|
|
28
|
+
npm run build
|
|
29
|
+
npm test
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
name: CD
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches:
|
|
5
|
+
- main
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
CD:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
|
|
12
|
+
permissions:
|
|
13
|
+
contents: write
|
|
14
|
+
id-token: write
|
|
15
|
+
pull-requests: write
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v4
|
|
19
|
+
- uses: actions/setup-node@v3
|
|
20
|
+
with:
|
|
21
|
+
node-version-file: ".nvmrc"
|
|
22
|
+
cache: npm
|
|
23
|
+
cache-dependency-path: "package-lock.json"
|
|
24
|
+
|
|
25
|
+
- name: Install
|
|
26
|
+
run: npm ci
|
|
27
|
+
|
|
28
|
+
- name: Build
|
|
29
|
+
run: npm run build
|
|
30
|
+
|
|
31
|
+
- name: Test
|
|
32
|
+
run: npm run test
|
|
33
|
+
|
|
34
|
+
- name: Use GitHub App Token
|
|
35
|
+
uses: actions/create-github-app-token@v1
|
|
36
|
+
id: app-token
|
|
37
|
+
with:
|
|
38
|
+
app-id: ${{ secrets.GU_CHANGESETS_APP_ID }}
|
|
39
|
+
private-key: ${{ secrets.GU_CHANGESETS_PRIVATE_KEY }}
|
|
40
|
+
|
|
41
|
+
- name: Set git user to Gu Changesets app
|
|
42
|
+
run: |
|
|
43
|
+
git config user.name "gu-changesets-release-pr[bot]"
|
|
44
|
+
git config user.email "gu-changesets-release-pr[bot]@users.noreply.github.com"
|
|
45
|
+
|
|
46
|
+
- name: Create Release Pull Request or Publish to npm
|
|
47
|
+
id: changesets
|
|
48
|
+
uses: changesets/action@v1
|
|
49
|
+
with:
|
|
50
|
+
publish: npx changeset publish
|
|
51
|
+
title: "🦋 Release package updates"
|
|
52
|
+
commit: "Bump package version"
|
|
53
|
+
setupGitUser: false
|
|
54
|
+
|
|
55
|
+
env:
|
|
56
|
+
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
|
|
57
|
+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,123 @@
|
|
|
1
1
|
# @guardian/pan-domain-node
|
|
2
2
|
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- f91598a: # Changes
|
|
8
|
+
Adds a 24-hour grace period after cookie expiry, during which requests will still be considered authenticated.
|
|
9
|
+
|
|
10
|
+
This is modelled by changing `AuthenticatedStatus` into a discriminated union with the following properties (among others):
|
|
11
|
+
|
|
12
|
+
### `success`
|
|
13
|
+
|
|
14
|
+
Whether to treat request as authenticated or not. **This will remain true after cookie expiry for the length of the grace period.**
|
|
15
|
+
|
|
16
|
+
[Almost all consumers](https://docs.google.com/spreadsheets/d/19XABaP9ua935TYJARkL8tstnizL69gIJ9av8C3UQBYw/edit?gid=0#gid=0) of this library check **only** the `AUTHORISED` status right now. These should all be able to switch to checking just this single boolean, implicitly getting grace period functionality in the process.
|
|
17
|
+
|
|
18
|
+
### `shouldRefreshCredentials`
|
|
19
|
+
|
|
20
|
+
Whether to try and get fresh credentials.
|
|
21
|
+
|
|
22
|
+
This allows page endpoints to redirect to auth, and API endpoints to tell the frontend to show a warning message to the user.
|
|
23
|
+
|
|
24
|
+
### `mustRefreshByEpochTimeMillis`
|
|
25
|
+
|
|
26
|
+
The time at which the grace period ends and the request will be treated as unauthenticated. This allows library consumers to warn the user in the app UI when they are near the end of the grace period, as Composer does: https://github.com/guardian/flexible-content/pull/5210
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
Panda cookie: issued expires `mustRefreshByEpochTimeMillis`
|
|
30
|
+
| | |
|
|
31
|
+
|--1 hour--| |
|
|
32
|
+
Grace period: [------------- 24 hours ------]
|
|
33
|
+
|
|
34
|
+
`success`: --false-][-true-----------------------------------][-false-------->
|
|
35
|
+
`shouldRefreshCredentials` [-false---][-true------------------------]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
# Why have we made this change?
|
|
39
|
+
|
|
40
|
+
The Panda authentication cookie expires after 1 hour, and top-level navigation requests (page loads) trigger automatic re-authentication after this point.
|
|
41
|
+
|
|
42
|
+
Unfortunately API requests cannot trigger re-authentication on their own, and background refresh mechanisms (e.g. iframe-based method used by [Pandular](https://github.com/guardian/pandular)) are increasingly blocked by browsers due to third-party cookie restrictions.
|
|
43
|
+
|
|
44
|
+
We would like to enforce a 24-hour grace period
|
|
45
|
+
|
|
46
|
+
# How to update consuming code
|
|
47
|
+
|
|
48
|
+
At a minimum, switch from
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
const authResult = await panda.verify(cookieHeader);
|
|
52
|
+
if (
|
|
53
|
+
authResult.status === AuthenticationStatus.AUTHORISED &&
|
|
54
|
+
authResult.user
|
|
55
|
+
) {
|
|
56
|
+
return authResult.user.email;
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
to
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
const authResult = await panda.verify(cookieHeader);
|
|
64
|
+
if (authResult.success) {
|
|
65
|
+
return authResult.user.email;
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
This will implicitly give you grace period functionality, because `success` will remain true during the grace period.
|
|
70
|
+
|
|
71
|
+
However, we **strongly** recommend all consumers to take account of `shouldRefreshCredentials`. What you do with the result should depend on whether your endpoint can trigger re-auth.
|
|
72
|
+
|
|
73
|
+
## Endpoints that can refresh credentials
|
|
74
|
+
|
|
75
|
+
Endpoints that **can** refresh credentials, e.g. page endpoints that can redirect to an auth flow, should send the user to re-auth if `shouldRefreshCredentials` is `true`:
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
const authResult = await panda.verify(headers.cookie);
|
|
79
|
+
if (authResult.success) {
|
|
80
|
+
if (authResult.shouldRefreshCredentials) {
|
|
81
|
+
// Send for auth
|
|
82
|
+
} else {
|
|
83
|
+
// Can perform action with user
|
|
84
|
+
return authResult.user;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Endpoints that cannot refresh credentials
|
|
90
|
+
|
|
91
|
+
Endpoints that **cannot** refresh credentials, e.g. API endpoints, should log appropriately and return something to the client that can be used to warn the user that they need to refresh their session.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
const authResult = await panda.verify(headers.cookie);
|
|
95
|
+
if (authResult.success) {
|
|
96
|
+
const user = authResult.user;
|
|
97
|
+
// Handle request
|
|
98
|
+
// When returning response:
|
|
99
|
+
if (authResult.shouldRefreshCredentials) {
|
|
100
|
+
const mustRefreshByEpochTimeMillis =
|
|
101
|
+
authResult.mustRefreshByEpochTimeMillis;
|
|
102
|
+
const remainingTime = mustRefreshByEpochTimeMillis - Date.now();
|
|
103
|
+
console.warn(
|
|
104
|
+
`Stale Panda auth, will expire in ${remainingTime} milliseconds`
|
|
105
|
+
);
|
|
106
|
+
// Can still return 200, but depending on the type of API,
|
|
107
|
+
// we may want to return some extra information so the client
|
|
108
|
+
// can warn the user they need to refresh their session.
|
|
109
|
+
} else {
|
|
110
|
+
// It's a fresh session. Nothing to worry about!
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## 0.5.1
|
|
116
|
+
|
|
117
|
+
### Patch Changes
|
|
118
|
+
|
|
119
|
+
- bdd4adb: Testing changeset release and capitalising some text
|
|
120
|
+
|
|
3
121
|
## 0.5.0
|
|
4
122
|
|
|
5
123
|
### Minor Changes
|
package/CODEOWNERS
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* @guardian/workflow-and-collaboration
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Pan Domain Node
|
|
2
|
+
|
|
3
|
+
Pan domain authentication provides distributed authentication for multiple webapps running in the same domain. Each
|
|
4
|
+
application can authenticate users against an OAuth provider and store the authentication information in a common cookie.
|
|
5
|
+
Each application can read this cookie and check if the user is allowed in the specific application and allow access accordingly.
|
|
6
|
+
|
|
7
|
+
This means that users are only prompted to provide authentication credentials once across the domain and any inter-app
|
|
8
|
+
interactions (e.g javascript Cross-Origin requests) can be easily secured.
|
|
9
|
+
|
|
10
|
+
## What's provided
|
|
11
|
+
|
|
12
|
+
The main [pan-domain-authentication](https://github.com/guardian/pan-domain-authentication) repository provides the
|
|
13
|
+
functionality for signing and verifying login cookies in Scala.
|
|
14
|
+
|
|
15
|
+
The `pan-domain-node` library provides an implementation of *verification only* for node apps.
|
|
16
|
+
|
|
17
|
+
## Grace period
|
|
18
|
+
We continue to consider the request authenticated for a period of time after the cookie expiry.
|
|
19
|
+
|
|
20
|
+
This is to allow API requests which cannot directly send the user for re-auth to indicate to the user that they must take some action to refresh their credentials (usually, refreshing the page).
|
|
21
|
+
|
|
22
|
+
When the cookie is expired but we're still within this grace period, `shouldRefreshCredentials` will be `true`, which means:
|
|
23
|
+
- Endpoints that can refresh credentials (e.g. page endpoints that can redirect) should do so
|
|
24
|
+
- Endpoints that cannot refresh credentials (e.g. API endpoints) should tell the user to take some action to refresh credentials
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
Panda cookie: issued expires `mustRefreshByEpochTimeMillis`
|
|
28
|
+
| | |
|
|
29
|
+
|--1 hour--| |
|
|
30
|
+
Grace period: [------------- 24 hours ------]
|
|
31
|
+
|
|
32
|
+
`success`: --false-][-true-----------------------------------][-false-------->
|
|
33
|
+
`shouldRefreshCredentials` [-false---][-true------------------------]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Example usage
|
|
37
|
+
### Installation
|
|
38
|
+
[](https://badge.fury.io/js/%40guardian%2Fpan-domain-node)
|
|
39
|
+
```
|
|
40
|
+
npm install --save-dev @guardian/pan-domain-node
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Initialisation
|
|
44
|
+
```typescript
|
|
45
|
+
import { PanDomainAuthentication, AuthenticationStatus, User, guardianValidation } from '@guardian/pan-domain-node';
|
|
46
|
+
|
|
47
|
+
const panda = new PanDomainAuthentication(
|
|
48
|
+
"gutoolsAuth-assym", // cookie name
|
|
49
|
+
"eu-west-1", // AWS region
|
|
50
|
+
"pan-domain-auth-settings", // Settings bucket
|
|
51
|
+
"local.dev-gutools.co.uk.settings.public", // Settings file
|
|
52
|
+
guardianValidation
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// alternatively customise the validation function and pass at construction
|
|
56
|
+
function customValidation(user: User): boolean {
|
|
57
|
+
const isInCorrectDomain = user.email.indexOf('test.com') !== -1;
|
|
58
|
+
return isInCorrectDomain && user.multifactor;
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Verification: page endpoints
|
|
63
|
+
This is for endpoints that **can** refresh credentials, e.g. a page endpoint that can redirect to an auth flow:
|
|
64
|
+
```typescript
|
|
65
|
+
const authenticationResult = await panda.verify(headers.cookie);
|
|
66
|
+
if (authenticationResult.success) {
|
|
67
|
+
if (authenticationResult.shouldRefreshCredentials) {
|
|
68
|
+
// Send for auth
|
|
69
|
+
} else {
|
|
70
|
+
// Can perform action with user
|
|
71
|
+
return authenticationResult.user;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Verification: API endpoints
|
|
77
|
+
This is for endpoints that **cannot** refresh credentials, e.g. API endpoints:
|
|
78
|
+
```typescript
|
|
79
|
+
const authenticationResult = await panda.verify(headers.cookie);
|
|
80
|
+
if (authenticationResult.success) {
|
|
81
|
+
const user = authenticationResult.user;
|
|
82
|
+
// Handle request
|
|
83
|
+
// When returning response:
|
|
84
|
+
if (authenticationResult.shouldRefreshCredentials) {
|
|
85
|
+
const mustRefreshByEpochTimeMillis = authenticationResult.mustRefreshByEpochTimeMillis;
|
|
86
|
+
const remainingTime = mustRefreshByEpochTimeMillis - Date.now();
|
|
87
|
+
console.warn(`Stale Panda auth, will expire in ${remainingTime} milliseconds`);
|
|
88
|
+
// Can still return 200, but depending on the type of API,
|
|
89
|
+
// we may want to return some extra information so the client
|
|
90
|
+
// can warn the user they need to refresh their session.
|
|
91
|
+
} else {
|
|
92
|
+
// It's a fresh session. Nothing to worry about!
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
```
|
package/dist/src/api.d.ts
CHANGED
|
@@ -1,10 +1,35 @@
|
|
|
1
1
|
export { PanDomainAuthentication } from './panda';
|
|
2
|
-
export declare
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
NOT_AUTHORISED = "Not Authorised",
|
|
6
|
-
AUTHORISED = "Authorised"
|
|
2
|
+
export declare const gracePeriodInMillis: number;
|
|
3
|
+
interface Result {
|
|
4
|
+
success: boolean;
|
|
7
5
|
}
|
|
6
|
+
interface Success extends Result {
|
|
7
|
+
success: true;
|
|
8
|
+
shouldRefreshCredentials: boolean;
|
|
9
|
+
user: User;
|
|
10
|
+
}
|
|
11
|
+
interface Failure extends Result {
|
|
12
|
+
success: false;
|
|
13
|
+
reason: string;
|
|
14
|
+
}
|
|
15
|
+
export interface FreshSuccess extends Success {
|
|
16
|
+
shouldRefreshCredentials: false;
|
|
17
|
+
}
|
|
18
|
+
export interface StaleSuccess extends Success {
|
|
19
|
+
shouldRefreshCredentials: true;
|
|
20
|
+
mustRefreshByEpochTimeMillis: number;
|
|
21
|
+
}
|
|
22
|
+
export interface UserValidationFailure extends Failure {
|
|
23
|
+
reason: 'invalid-user';
|
|
24
|
+
user: User;
|
|
25
|
+
}
|
|
26
|
+
export interface CookieFailure extends Failure {
|
|
27
|
+
reason: 'no-cookie' | 'invalid-cookie' | 'expired-cookie';
|
|
28
|
+
}
|
|
29
|
+
export interface UnknownFailure extends Failure {
|
|
30
|
+
reason: 'unknown';
|
|
31
|
+
}
|
|
32
|
+
export declare type AuthenticationResult = FreshSuccess | StaleSuccess | CookieFailure | UserValidationFailure | UnknownFailure;
|
|
8
33
|
export interface User {
|
|
9
34
|
firstName: string;
|
|
10
35
|
lastName: string;
|
|
@@ -15,9 +40,5 @@ export interface User {
|
|
|
15
40
|
expires: number;
|
|
16
41
|
multifactor: boolean;
|
|
17
42
|
}
|
|
18
|
-
export interface AuthenticationResult {
|
|
19
|
-
status: AuthenticationStatus;
|
|
20
|
-
user?: User;
|
|
21
|
-
}
|
|
22
43
|
export declare type ValidateUserFn = (user: User) => boolean;
|
|
23
44
|
export declare function guardianValidation(user: User): boolean;
|
package/dist/src/api.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.guardianValidation = exports.
|
|
3
|
+
exports.guardianValidation = exports.gracePeriodInMillis = void 0;
|
|
4
4
|
var panda_1 = require("./panda");
|
|
5
5
|
Object.defineProperty(exports, "PanDomainAuthentication", { enumerable: true, get: function () { return panda_1.PanDomainAuthentication; } });
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
// We continue to consider the request authenticated for
|
|
7
|
+
// a period of time after the cookie expiry. This is to allow
|
|
8
|
+
// API requests which cannot directly send the user for re-auth to
|
|
9
|
+
// indicate to the user that they must take some action to refresh their
|
|
10
|
+
// credentials (usually, refreshing the page).
|
|
11
|
+
// Panda cookie: issued expires
|
|
12
|
+
// | |
|
|
13
|
+
// |--1 hour--|
|
|
14
|
+
// Grace period: [------------- 24 hours ------]
|
|
15
|
+
// `success`: --false-][-true-----------------------------------][-false-------->
|
|
16
|
+
// `shouldRefreshCredentials` [-false---][-true------------------------]
|
|
17
|
+
exports.gracePeriodInMillis = 24 * 60 * 60 * 1000;
|
|
13
18
|
function guardianValidation(user) {
|
|
14
19
|
const isGuardianUser = user.email.indexOf('guardian.co.uk') !== -1;
|
|
15
20
|
return isGuardianUser && user.multifactor;
|
package/dist/src/panda.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare class PanDomainAuthentication {
|
|
|
10
10
|
keyFile: string;
|
|
11
11
|
validateUser: ValidateUserFn;
|
|
12
12
|
publicKey: Promise<PublicKeyHolder>;
|
|
13
|
-
|
|
13
|
+
keyCacheTimeInMillis: number;
|
|
14
14
|
keyUpdateTimer?: NodeJS.Timeout;
|
|
15
15
|
constructor(cookieName: string, region: string, bucket: string, keyFile: string, validateUser: ValidateUserFn);
|
|
16
16
|
stop(): void;
|