@guardian/pan-domain-node 0.4.2 → 0.5.1
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/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +3 -0
- package/.github/workflows/ci.yml +29 -0
- package/.github/workflows/node-release.yml +57 -0
- package/.github/workflows/snyk.yml +19 -0
- package/.nvmrc +1 -1
- package/CHANGELOG.md +13 -0
- package/CODEOWNERS +1 -0
- package/LICENSE +201 -0
- package/README.md +55 -0
- package/dist/src/api.js +2 -1
- package/dist/src/fetch-public-key.d.ts +5 -0
- package/dist/src/fetch-public-key.js +40 -0
- package/dist/src/panda.d.ts +2 -6
- package/dist/src/panda.js +24 -25
- package/dist/src/utils.js +15 -2
- package/dist/test/fixtures.js +1 -0
- package/dist/test/panda.test.js +77 -6
- package/jest.config.js +2 -1
- package/package.json +9 -8
- package/src/fetch-public-key.ts +27 -0
- package/src/panda.ts +9 -29
- package/test/panda.test.ts +102 -7
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Changesets
|
|
2
|
+
|
|
3
|
+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
|
|
4
|
+
with multi-package repos, or single-package repos to help you version and publish your code. You can
|
|
5
|
+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
|
|
6
|
+
|
|
7
|
+
We have a quick list of common questions to get you started engaging with this project in
|
|
8
|
+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://unpkg.com/@changesets/config@3.0.4/schema.json",
|
|
3
|
+
"changelog": "@changesets/cli/changelog",
|
|
4
|
+
"commit": false,
|
|
5
|
+
"fixed": [],
|
|
6
|
+
"linked": [],
|
|
7
|
+
"access": "public",
|
|
8
|
+
"baseBranch": "main",
|
|
9
|
+
"updateInternalDependencies": "patch",
|
|
10
|
+
"ignore": []
|
|
11
|
+
}
|
|
@@ -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 }}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
name: Snyk
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
workflow_dispatch:
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
security:
|
|
11
|
+
uses: guardian/.github/.github/workflows/sbt-node-snyk.yml@main
|
|
12
|
+
with:
|
|
13
|
+
DEBUG: true
|
|
14
|
+
ORG: guardian
|
|
15
|
+
SKIP_NODE: false
|
|
16
|
+
NODE_VERSION_FILE: .nvmrc
|
|
17
|
+
|
|
18
|
+
secrets:
|
|
19
|
+
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
|
package/.nvmrc
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
v20.18.1
|
package/CHANGELOG.md
ADDED
package/CODEOWNERS
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* @guardian/digital-cms
|
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,55 @@
|
|
|
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
|
+
## To verify login in NodeJS
|
|
18
|
+
|
|
19
|
+
[](https://badge.fury.io/js/%40guardian%2Fpan-domain-node)
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
npm install --save-dev @guardian/pan-domain-node
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { PanDomainAuthentication, AuthenticationStatus, User, guardianValidation } from '@guardian/pan-domain-node';
|
|
27
|
+
|
|
28
|
+
const panda = new PanDomainAuthentication(
|
|
29
|
+
"gutoolsAuth-assym", // cookie name
|
|
30
|
+
"eu-west-1", // AWS region
|
|
31
|
+
"pan-domain-auth-settings", // Settings bucket
|
|
32
|
+
"local.dev-gutools.co.uk.settings.public", // Settings file
|
|
33
|
+
guardianValidation
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
// alternatively customise the validation function and pass at construction
|
|
37
|
+
function customValidation(user: User): boolean {
|
|
38
|
+
const isInCorrectDomain = user.email.indexOf('test.com') !== -1;
|
|
39
|
+
return isInCorrectDomain && user.multifactor;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// when handling a request
|
|
43
|
+
function(request) {
|
|
44
|
+
// Pass the raw unparsed cookies
|
|
45
|
+
return panda.verify(request.headers['Cookie']).then(( { status, user }) => {
|
|
46
|
+
switch(status) {
|
|
47
|
+
case AuthenticationStatus.Authorised:
|
|
48
|
+
// Good user, handle the request!
|
|
49
|
+
|
|
50
|
+
default:
|
|
51
|
+
// Bad user. Return 4XX
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
```
|
package/dist/src/api.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.guardianValidation = exports.AuthenticationStatus = void 0;
|
|
3
4
|
var panda_1 = require("./panda");
|
|
4
|
-
exports
|
|
5
|
+
Object.defineProperty(exports, "PanDomainAuthentication", { enumerable: true, get: function () { return panda_1.PanDomainAuthentication; } });
|
|
5
6
|
var AuthenticationStatus;
|
|
6
7
|
(function (AuthenticationStatus) {
|
|
7
8
|
AuthenticationStatus["INVALID_COOKIE"] = "Invalid Cookie";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.fetchPublicKey = void 0;
|
|
23
|
+
const iniparser = __importStar(require("iniparser"));
|
|
24
|
+
const utils_1 = require("./utils");
|
|
25
|
+
function fetchPublicKey(region, bucket, keyFile) {
|
|
26
|
+
const path = `https://s3.${region}.amazonaws.com/${bucket}/${keyFile}`;
|
|
27
|
+
return utils_1.httpGet(path).then(response => {
|
|
28
|
+
const config = iniparser.parseString(response);
|
|
29
|
+
if (config.publicKey) {
|
|
30
|
+
return {
|
|
31
|
+
key: utils_1.base64ToPEM(config.publicKey, "PUBLIC"),
|
|
32
|
+
lastUpdated: new Date()
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
throw new Error("Missing publicKey setting from config");
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
exports.fetchPublicKey = fetchPublicKey;
|
package/dist/src/panda.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { User, AuthenticationResult, ValidateUserFn } from './api';
|
|
3
|
-
|
|
4
|
-
key: string;
|
|
5
|
-
lastUpdated: Date;
|
|
6
|
-
}
|
|
3
|
+
import { PublicKeyHolder } from './fetch-public-key';
|
|
7
4
|
export declare function createCookie(user: User, privateKey: string): string;
|
|
8
|
-
export declare function verifyUser(pandaCookie: string | undefined, publicKey: string,
|
|
5
|
+
export declare function verifyUser(pandaCookie: string | undefined, publicKey: string, currentTime: Date, validateUser: ValidateUserFn): AuthenticationResult;
|
|
9
6
|
export declare class PanDomainAuthentication {
|
|
10
7
|
cookieName: string;
|
|
11
8
|
region: string;
|
|
@@ -20,4 +17,3 @@ export declare class PanDomainAuthentication {
|
|
|
20
17
|
getPublicKey(): Promise<string>;
|
|
21
18
|
verify(requestCookies: string): Promise<AuthenticationResult>;
|
|
22
19
|
}
|
|
23
|
-
export {};
|
package/dist/src/panda.js
CHANGED
|
@@ -1,31 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
2
14
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
3
15
|
if (mod && mod.__esModule) return mod;
|
|
4
16
|
var result = {};
|
|
5
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result
|
|
6
|
-
result
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
7
19
|
return result;
|
|
8
20
|
};
|
|
9
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
|
|
22
|
+
exports.PanDomainAuthentication = exports.verifyUser = exports.createCookie = void 0;
|
|
11
23
|
const cookie = __importStar(require("cookie"));
|
|
12
24
|
const utils_1 = require("./utils");
|
|
13
25
|
const api_1 = require("./api");
|
|
14
|
-
|
|
15
|
-
const path = `https://s3.${region}.amazonaws.com/${bucket}/${keyFile}`;
|
|
16
|
-
return utils_1.httpGet(path).then(response => {
|
|
17
|
-
const config = iniparser.parseString(response);
|
|
18
|
-
if (config.publicKey) {
|
|
19
|
-
return {
|
|
20
|
-
key: utils_1.base64ToPEM(config.publicKey, "PUBLIC"),
|
|
21
|
-
lastUpdated: new Date()
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
throw new Error("Missing publicKey setting from config");
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
26
|
+
const fetch_public_key_1 = require("./fetch-public-key");
|
|
29
27
|
function createCookie(user, privateKey) {
|
|
30
28
|
let queryParams = [];
|
|
31
29
|
queryParams.push("firstName=" + user.firstName);
|
|
@@ -42,7 +40,7 @@ function createCookie(user, privateKey) {
|
|
|
42
40
|
return queryParamsString + "." + signature;
|
|
43
41
|
}
|
|
44
42
|
exports.createCookie = createCookie;
|
|
45
|
-
function verifyUser(pandaCookie, publicKey,
|
|
43
|
+
function verifyUser(pandaCookie, publicKey, currentTime, validateUser) {
|
|
46
44
|
if (!pandaCookie) {
|
|
47
45
|
return { status: api_1.AuthenticationStatus.INVALID_COOKIE };
|
|
48
46
|
}
|
|
@@ -50,9 +48,10 @@ function verifyUser(pandaCookie, publicKey, currentTimestamp, validateUser) {
|
|
|
50
48
|
if (!utils_1.verifySignature(data, signature, publicKey)) {
|
|
51
49
|
return { status: api_1.AuthenticationStatus.INVALID_COOKIE };
|
|
52
50
|
}
|
|
51
|
+
const currentTimestampInMilliseconds = currentTime.getTime();
|
|
53
52
|
try {
|
|
54
53
|
const user = utils_1.parseUser(data);
|
|
55
|
-
const isExpired = user.expires <
|
|
54
|
+
const isExpired = user.expires < currentTimestampInMilliseconds;
|
|
56
55
|
if (isExpired) {
|
|
57
56
|
return { status: api_1.AuthenticationStatus.EXPIRED, user };
|
|
58
57
|
}
|
|
@@ -75,20 +74,21 @@ class PanDomainAuthentication {
|
|
|
75
74
|
this.bucket = bucket;
|
|
76
75
|
this.keyFile = keyFile;
|
|
77
76
|
this.validateUser = validateUser;
|
|
78
|
-
this.publicKey = fetchPublicKey(region, bucket, keyFile);
|
|
77
|
+
this.publicKey = fetch_public_key_1.fetchPublicKey(region, bucket, keyFile);
|
|
79
78
|
this.keyUpdateTimer = setInterval(() => this.getPublicKey(), this.keyCacheTime);
|
|
80
79
|
}
|
|
81
80
|
stop() {
|
|
82
81
|
if (this.keyUpdateTimer) {
|
|
83
82
|
clearInterval(this.keyUpdateTimer);
|
|
83
|
+
this.keyUpdateTimer = undefined;
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
getPublicKey() {
|
|
87
87
|
return this.publicKey.then(({ key, lastUpdated }) => {
|
|
88
88
|
const now = new Date();
|
|
89
|
-
const diff = now.
|
|
89
|
+
const diff = now.getTime() - lastUpdated.getTime();
|
|
90
90
|
if (diff > this.keyCacheTime) {
|
|
91
|
-
this.publicKey = fetchPublicKey(this.region, this.bucket, this.keyFile);
|
|
91
|
+
this.publicKey = fetch_public_key_1.fetchPublicKey(this.region, this.bucket, this.keyFile);
|
|
92
92
|
return this.publicKey.then(({ key }) => key);
|
|
93
93
|
}
|
|
94
94
|
else {
|
|
@@ -100,8 +100,7 @@ class PanDomainAuthentication {
|
|
|
100
100
|
return this.getPublicKey().then(publicKey => {
|
|
101
101
|
const cookies = cookie.parse(requestCookies);
|
|
102
102
|
const pandaCookie = cookies[this.cookieName];
|
|
103
|
-
|
|
104
|
-
return verifyUser(pandaCookie, publicKey, now, this.validateUser);
|
|
103
|
+
return verifyUser(pandaCookie, publicKey, new Date(), this.validateUser);
|
|
105
104
|
});
|
|
106
105
|
}
|
|
107
106
|
}
|
package/dist/src/utils.js
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
2
14
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
3
15
|
if (mod && mod.__esModule) return mod;
|
|
4
16
|
var result = {};
|
|
5
|
-
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result
|
|
6
|
-
result
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
7
19
|
return result;
|
|
8
20
|
};
|
|
9
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.parseUser = exports.httpGet = exports.base64ToPEM = exports.sign = exports.verifySignature = exports.parseCookie = exports.decodeBase64 = void 0;
|
|
10
23
|
const crypto = __importStar(require("crypto"));
|
|
11
24
|
const https = __importStar(require("https"));
|
|
12
25
|
const url_1 = require("url");
|
package/dist/test/fixtures.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sampleNonGuardianCookie = exports.sampleCookieWithoutMultifactor = exports.sampleCookie = exports.publicKey = exports.encodedPublicKey = exports.privateKey = exports.encodedPrivateKey = void 0;
|
|
3
4
|
const utils_1 = require("../src/utils");
|
|
4
5
|
exports.encodedPrivateKey = "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";
|
|
5
6
|
exports.privateKey = utils_1.base64ToPEM(exports.encodedPrivateKey, "RSA PRIVATE");
|
package/dist/test/panda.test.js
CHANGED
|
@@ -1,28 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
const api_1 = require("../src/api");
|
|
4
13
|
const panda_1 = require("../src/panda");
|
|
14
|
+
const fetch_public_key_1 = require("../src/fetch-public-key");
|
|
5
15
|
const fixtures_1 = require("./fixtures");
|
|
6
16
|
const utils_1 = require("../src/utils");
|
|
17
|
+
jest.mock('../src/fetch-public-key');
|
|
18
|
+
jest.useFakeTimers('modern');
|
|
7
19
|
describe('verifyUser', function () {
|
|
8
20
|
test("return invalid cookie if missing", () => {
|
|
9
|
-
expect(panda_1.verifyUser(undefined, "", 0, api_1.guardianValidation).status).toBe(api_1.AuthenticationStatus.INVALID_COOKIE);
|
|
21
|
+
expect(panda_1.verifyUser(undefined, "", new Date(0), api_1.guardianValidation).status).toBe(api_1.AuthenticationStatus.INVALID_COOKIE);
|
|
10
22
|
});
|
|
11
23
|
test("return invalid cookie for a malformed signature", () => {
|
|
12
24
|
const [data, signature] = fixtures_1.sampleCookie.split(".");
|
|
13
25
|
const testCookie = data + ".1234";
|
|
14
|
-
expect(panda_1.verifyUser(testCookie, fixtures_1.publicKey, 0, api_1.guardianValidation).status).toBe(api_1.AuthenticationStatus.INVALID_COOKIE);
|
|
26
|
+
expect(panda_1.verifyUser(testCookie, fixtures_1.publicKey, new Date(0), api_1.guardianValidation).status).toBe(api_1.AuthenticationStatus.INVALID_COOKIE);
|
|
15
27
|
});
|
|
16
28
|
test("return expired", () => {
|
|
17
|
-
const someTimeInTheFuture = 5678;
|
|
29
|
+
const someTimeInTheFuture = new Date(5678);
|
|
30
|
+
expect(someTimeInTheFuture.getTime()).toBe(5678);
|
|
18
31
|
expect(panda_1.verifyUser(fixtures_1.sampleCookie, fixtures_1.publicKey, someTimeInTheFuture, api_1.guardianValidation).status).toBe(api_1.AuthenticationStatus.EXPIRED);
|
|
19
32
|
});
|
|
20
33
|
test("return not authenticated if user fails validation function", () => {
|
|
21
|
-
expect(panda_1.verifyUser(fixtures_1.sampleCookieWithoutMultifactor, fixtures_1.publicKey, 0, api_1.guardianValidation).status).toBe(api_1.AuthenticationStatus.NOT_AUTHORISED);
|
|
22
|
-
expect(panda_1.verifyUser(fixtures_1.sampleNonGuardianCookie, fixtures_1.publicKey, 0, api_1.guardianValidation).status).toBe(api_1.AuthenticationStatus.NOT_AUTHORISED);
|
|
34
|
+
expect(panda_1.verifyUser(fixtures_1.sampleCookieWithoutMultifactor, fixtures_1.publicKey, new Date(0), api_1.guardianValidation).status).toBe(api_1.AuthenticationStatus.NOT_AUTHORISED);
|
|
35
|
+
expect(panda_1.verifyUser(fixtures_1.sampleNonGuardianCookie, fixtures_1.publicKey, new Date(0), api_1.guardianValidation).status).toBe(api_1.AuthenticationStatus.NOT_AUTHORISED);
|
|
23
36
|
});
|
|
24
37
|
test("return authenticated", () => {
|
|
25
|
-
expect(panda_1.verifyUser(fixtures_1.sampleCookie, fixtures_1.publicKey, 0, api_1.guardianValidation).status).toBe(api_1.AuthenticationStatus.AUTHORISED);
|
|
38
|
+
expect(panda_1.verifyUser(fixtures_1.sampleCookie, fixtures_1.publicKey, new Date(0), api_1.guardianValidation).status).toBe(api_1.AuthenticationStatus.AUTHORISED);
|
|
26
39
|
});
|
|
27
40
|
});
|
|
28
41
|
describe('createCookie', function () {
|
|
@@ -41,3 +54,61 @@ describe('createCookie', function () {
|
|
|
41
54
|
expect(cookie).toEqual(fixtures_1.sampleCookie);
|
|
42
55
|
});
|
|
43
56
|
});
|
|
57
|
+
describe('panda class', function () {
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
fetch_public_key_1.fetchPublicKey.mockResolvedValue({ key: 'PUBLIC KEY', lastUpdated: new Date() });
|
|
60
|
+
});
|
|
61
|
+
describe('stop', () => {
|
|
62
|
+
it('stops auto refresh', () => {
|
|
63
|
+
const panda = new panda_1.PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', (u) => true);
|
|
64
|
+
expect(panda.keyUpdateTimer).not.toBeUndefined();
|
|
65
|
+
panda.stop();
|
|
66
|
+
expect(panda.keyUpdateTimer).toBeUndefined();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
describe('getPublicKey', () => {
|
|
70
|
+
it('getsPublicKey immediately when last fetch is within the cache time', () => __awaiter(this, void 0, void 0, function* () {
|
|
71
|
+
const panda = new panda_1.PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', (u) => true);
|
|
72
|
+
const fetchesBeforeGet = fetch_public_key_1.fetchPublicKey.mock.calls.length;
|
|
73
|
+
yield expect(panda.getPublicKey()).resolves.toEqual('PUBLIC KEY');
|
|
74
|
+
const fetchesAfterGet = fetch_public_key_1.fetchPublicKey.mock.calls.length;
|
|
75
|
+
expect(fetchesAfterGet).toEqual(fetchesBeforeGet);
|
|
76
|
+
}));
|
|
77
|
+
it('getsPublicKey after refetching when last fetch is outside the cache time', () => __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
// cache time is 1 min
|
|
79
|
+
const fiveMinsAgo = new Date();
|
|
80
|
+
fiveMinsAgo.setMinutes(fiveMinsAgo.getMinutes() - 5);
|
|
81
|
+
fetch_public_key_1.fetchPublicKey.mockResolvedValue({ key: 'PUBLIC KEY', lastUpdated: fiveMinsAgo });
|
|
82
|
+
const panda = new panda_1.PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', (u) => true);
|
|
83
|
+
const fetchesBefore = fetch_public_key_1.fetchPublicKey.mock.calls.length;
|
|
84
|
+
yield expect(panda.getPublicKey()).resolves.toEqual('PUBLIC KEY');
|
|
85
|
+
fetch_public_key_1.fetchPublicKey.mockResolvedValue({ key: 'PUBLIC KEY 2', lastUpdated: fiveMinsAgo });
|
|
86
|
+
const fetchesAfter = fetch_public_key_1.fetchPublicKey.mock.calls.length;
|
|
87
|
+
yield expect(panda.getPublicKey()).resolves.toEqual('PUBLIC KEY 2');
|
|
88
|
+
expect(fetchesAfter).toEqual(fetchesBefore + 1);
|
|
89
|
+
}));
|
|
90
|
+
});
|
|
91
|
+
describe('verify', () => {
|
|
92
|
+
beforeEach(() => {
|
|
93
|
+
fetch_public_key_1.fetchPublicKey.mockResolvedValue({ key: fixtures_1.publicKey, lastUpdated: new Date() });
|
|
94
|
+
});
|
|
95
|
+
it('should return authenticated if valid', () => __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
jest.setSystemTime(100);
|
|
97
|
+
const panda = new panda_1.PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', (u) => true);
|
|
98
|
+
const { status } = yield panda.verify(`cookiename=${fixtures_1.sampleCookie}`);
|
|
99
|
+
expect(status).toBe(api_1.AuthenticationStatus.AUTHORISED);
|
|
100
|
+
}));
|
|
101
|
+
it('should return expired if expired', () => __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
jest.setSystemTime(10000);
|
|
103
|
+
const panda = new panda_1.PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', (u) => true);
|
|
104
|
+
const { status } = yield panda.verify(`cookiename=${fixtures_1.sampleCookie}`);
|
|
105
|
+
expect(status).toBe(api_1.AuthenticationStatus.EXPIRED);
|
|
106
|
+
}));
|
|
107
|
+
it('should return not authenticated if validation fails', () => __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
jest.setSystemTime(100);
|
|
109
|
+
const panda = new panda_1.PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', api_1.guardianValidation);
|
|
110
|
+
const { status } = yield panda.verify(`cookiename=${fixtures_1.sampleNonGuardianCookie}`);
|
|
111
|
+
expect(status).toBe(api_1.AuthenticationStatus.NOT_AUTHORISED);
|
|
112
|
+
}));
|
|
113
|
+
});
|
|
114
|
+
});
|
package/jest.config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guardian/pan-domain-node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "NodeJs implementation of Guardian pan-domain auth verification",
|
|
5
5
|
"main": "dist/src/api.js",
|
|
6
6
|
"types": "dist/src/api.d.ts",
|
|
@@ -22,16 +22,17 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://github.com/guardian/pan-domain-authentication",
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@
|
|
25
|
+
"@changesets/cli": "^2.27.10",
|
|
26
|
+
"@types/cookie": "^0.4.0",
|
|
26
27
|
"@types/iniparser": "0.0.29",
|
|
27
|
-
"@types/jest": "^
|
|
28
|
-
"@types/node": "^
|
|
29
|
-
"jest": "^
|
|
30
|
-
"ts-jest": "^
|
|
31
|
-
"typescript": "^3.
|
|
28
|
+
"@types/jest": "^26.0.9",
|
|
29
|
+
"@types/node": "^14.0.27",
|
|
30
|
+
"jest": "^26.2.2",
|
|
31
|
+
"ts-jest": "^26.1.4",
|
|
32
|
+
"typescript": "^3.9.7"
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"cookie": "^0.
|
|
35
|
+
"cookie": "^0.4.1",
|
|
35
36
|
"iniparser": "^1.0.5"
|
|
36
37
|
},
|
|
37
38
|
"scripts": {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as iniparser from 'iniparser';
|
|
2
|
+
import {base64ToPEM, httpGet} from './utils';
|
|
3
|
+
|
|
4
|
+
export interface PublicKeyHolder {
|
|
5
|
+
key: string,
|
|
6
|
+
lastUpdated: Date
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export function fetchPublicKey(region: string, bucket: String, keyFile: String): Promise<PublicKeyHolder> {
|
|
11
|
+
const path = `https://s3.${region}.amazonaws.com/${bucket}/${keyFile}`;
|
|
12
|
+
|
|
13
|
+
return httpGet(path).then(response => {
|
|
14
|
+
const config: { publicKey?: string} = iniparser.parseString(response);
|
|
15
|
+
|
|
16
|
+
if(config.publicKey) {
|
|
17
|
+
return {
|
|
18
|
+
key: base64ToPEM(config.publicKey, "PUBLIC"),
|
|
19
|
+
lastUpdated: new Date()
|
|
20
|
+
};
|
|
21
|
+
} else {
|
|
22
|
+
throw new Error("Missing publicKey setting from config");
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
package/src/panda.ts
CHANGED
|
@@ -1,30 +1,8 @@
|
|
|
1
|
-
import * as iniparser from 'iniparser';
|
|
2
1
|
import * as cookie from 'cookie';
|
|
3
2
|
|
|
4
|
-
import {
|
|
3
|
+
import {parseCookie, parseUser, sign, verifySignature} from './utils';
|
|
5
4
|
import {AuthenticationStatus, User, AuthenticationResult, ValidateUserFn} from './api';
|
|
6
|
-
|
|
7
|
-
interface PublicKeyHolder {
|
|
8
|
-
key: string,
|
|
9
|
-
lastUpdated: Date
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function fetchPublicKey(region: string, bucket: String, keyFile: String): Promise<PublicKeyHolder> {
|
|
13
|
-
const path = `https://s3.${region}.amazonaws.com/${bucket}/${keyFile}`;
|
|
14
|
-
|
|
15
|
-
return httpGet(path).then(response => {
|
|
16
|
-
const config: { publicKey?: string} = iniparser.parseString(response);
|
|
17
|
-
|
|
18
|
-
if(config.publicKey) {
|
|
19
|
-
return {
|
|
20
|
-
key: base64ToPEM(config.publicKey, "PUBLIC"),
|
|
21
|
-
lastUpdated: new Date()
|
|
22
|
-
};
|
|
23
|
-
} else {
|
|
24
|
-
throw new Error("Missing publicKey setting from config");
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}
|
|
5
|
+
import { fetchPublicKey, PublicKeyHolder } from './fetch-public-key';
|
|
28
6
|
|
|
29
7
|
export function createCookie(user: User, privateKey: string): string {
|
|
30
8
|
let queryParams: string[] = [];
|
|
@@ -46,7 +24,7 @@ export function createCookie(user: User, privateKey: string): string {
|
|
|
46
24
|
return queryParamsString + "." + signature
|
|
47
25
|
}
|
|
48
26
|
|
|
49
|
-
export function verifyUser(pandaCookie: string | undefined, publicKey: string,
|
|
27
|
+
export function verifyUser(pandaCookie: string | undefined, publicKey: string, currentTime: Date, validateUser: ValidateUserFn): AuthenticationResult {
|
|
50
28
|
if(!pandaCookie) {
|
|
51
29
|
return { status: AuthenticationStatus.INVALID_COOKIE };
|
|
52
30
|
}
|
|
@@ -57,9 +35,11 @@ export function verifyUser(pandaCookie: string | undefined, publicKey: string, c
|
|
|
57
35
|
return { status: AuthenticationStatus.INVALID_COOKIE };
|
|
58
36
|
}
|
|
59
37
|
|
|
38
|
+
const currentTimestampInMilliseconds = currentTime.getTime();
|
|
39
|
+
|
|
60
40
|
try {
|
|
61
41
|
const user: User = parseUser(data);
|
|
62
|
-
const isExpired = user.expires <
|
|
42
|
+
const isExpired = user.expires < currentTimestampInMilliseconds;
|
|
63
43
|
|
|
64
44
|
if(isExpired) {
|
|
65
45
|
return { status: AuthenticationStatus.EXPIRED, user };
|
|
@@ -102,13 +82,14 @@ export class PanDomainAuthentication {
|
|
|
102
82
|
stop(): void {
|
|
103
83
|
if(this.keyUpdateTimer) {
|
|
104
84
|
clearInterval(this.keyUpdateTimer);
|
|
85
|
+
this.keyUpdateTimer = undefined;
|
|
105
86
|
}
|
|
106
87
|
}
|
|
107
88
|
|
|
108
89
|
getPublicKey(): Promise<string> {
|
|
109
90
|
return this.publicKey.then(({ key, lastUpdated }) => {
|
|
110
91
|
const now = new Date();
|
|
111
|
-
const diff = now.
|
|
92
|
+
const diff = now.getTime() - lastUpdated.getTime();
|
|
112
93
|
|
|
113
94
|
if(diff > this.keyCacheTime) {
|
|
114
95
|
this.publicKey = fetchPublicKey(this.region, this.bucket, this.keyFile);
|
|
@@ -124,8 +105,7 @@ export class PanDomainAuthentication {
|
|
|
124
105
|
const cookies = cookie.parse(requestCookies);
|
|
125
106
|
const pandaCookie = cookies[this.cookieName];
|
|
126
107
|
|
|
127
|
-
|
|
128
|
-
return verifyUser(pandaCookie, publicKey, now, this.validateUser);
|
|
108
|
+
return verifyUser(pandaCookie, publicKey, new Date(), this.validateUser);
|
|
129
109
|
});
|
|
130
110
|
}
|
|
131
111
|
}
|
package/test/panda.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {guardianValidation, AuthenticationStatus, User} from '../src/api';
|
|
2
|
-
import { verifyUser, createCookie } from '../src/panda';
|
|
2
|
+
import { verifyUser, createCookie, PanDomainAuthentication } from '../src/panda';
|
|
3
|
+
import { fetchPublicKey } from '../src/fetch-public-key';
|
|
3
4
|
|
|
4
5
|
import {
|
|
5
6
|
sampleCookie,
|
|
@@ -10,31 +11,35 @@ import {
|
|
|
10
11
|
} from './fixtures';
|
|
11
12
|
import {decodeBase64} from "../src/utils";
|
|
12
13
|
|
|
14
|
+
jest.mock('../src/fetch-public-key');
|
|
15
|
+
jest.useFakeTimers('modern');
|
|
16
|
+
|
|
13
17
|
describe('verifyUser', function () {
|
|
14
18
|
|
|
15
19
|
test("return invalid cookie if missing", () => {
|
|
16
|
-
expect(verifyUser(undefined, "", 0, guardianValidation).status).toBe(AuthenticationStatus.INVALID_COOKIE);
|
|
20
|
+
expect(verifyUser(undefined, "", new Date(0), guardianValidation).status).toBe(AuthenticationStatus.INVALID_COOKIE);
|
|
17
21
|
});
|
|
18
22
|
|
|
19
23
|
test("return invalid cookie for a malformed signature", () => {
|
|
20
24
|
const [data, signature] = sampleCookie.split(".");
|
|
21
25
|
const testCookie = data + ".1234";
|
|
22
26
|
|
|
23
|
-
expect(verifyUser(testCookie, publicKey, 0, guardianValidation).status).toBe(AuthenticationStatus.INVALID_COOKIE);
|
|
27
|
+
expect(verifyUser(testCookie, publicKey, new Date(0), guardianValidation).status).toBe(AuthenticationStatus.INVALID_COOKIE);
|
|
24
28
|
});
|
|
25
29
|
|
|
26
30
|
test("return expired", () => {
|
|
27
|
-
const someTimeInTheFuture = 5678;
|
|
31
|
+
const someTimeInTheFuture = new Date(5678);
|
|
32
|
+
expect(someTimeInTheFuture.getTime()).toBe(5678);
|
|
28
33
|
expect(verifyUser(sampleCookie, publicKey, someTimeInTheFuture, guardianValidation).status).toBe(AuthenticationStatus.EXPIRED);
|
|
29
34
|
});
|
|
30
35
|
|
|
31
36
|
test("return not authenticated if user fails validation function", () => {
|
|
32
|
-
expect(verifyUser(sampleCookieWithoutMultifactor, publicKey, 0, guardianValidation).status).toBe(AuthenticationStatus.NOT_AUTHORISED);
|
|
33
|
-
expect(verifyUser(sampleNonGuardianCookie, publicKey, 0, guardianValidation).status).toBe(AuthenticationStatus.NOT_AUTHORISED);
|
|
37
|
+
expect(verifyUser(sampleCookieWithoutMultifactor, publicKey, new Date(0), guardianValidation).status).toBe(AuthenticationStatus.NOT_AUTHORISED);
|
|
38
|
+
expect(verifyUser(sampleNonGuardianCookie, publicKey, new Date(0), guardianValidation).status).toBe(AuthenticationStatus.NOT_AUTHORISED);
|
|
34
39
|
});
|
|
35
40
|
|
|
36
41
|
test("return authenticated", () => {
|
|
37
|
-
expect(verifyUser(sampleCookie, publicKey, 0, guardianValidation).status).toBe(AuthenticationStatus.AUTHORISED);
|
|
42
|
+
expect(verifyUser(sampleCookie, publicKey, new Date(0), guardianValidation).status).toBe(AuthenticationStatus.AUTHORISED);
|
|
38
43
|
});
|
|
39
44
|
});
|
|
40
45
|
|
|
@@ -56,3 +61,93 @@ describe('createCookie', function () {
|
|
|
56
61
|
expect(cookie).toEqual(sampleCookie)
|
|
57
62
|
});
|
|
58
63
|
});
|
|
64
|
+
|
|
65
|
+
describe('panda class', function () {
|
|
66
|
+
beforeEach(() => {
|
|
67
|
+
(fetchPublicKey as jest.MockedFunction<typeof fetchPublicKey>).mockResolvedValue({ key: 'PUBLIC KEY', lastUpdated: new Date() });
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('stop', () => {
|
|
71
|
+
|
|
72
|
+
it('stops auto refresh', () => {
|
|
73
|
+
const panda = new PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', (u)=> true);
|
|
74
|
+
expect(panda.keyUpdateTimer).not.toBeUndefined();
|
|
75
|
+
panda.stop();
|
|
76
|
+
expect(panda.keyUpdateTimer).toBeUndefined();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('getPublicKey', () => {
|
|
82
|
+
|
|
83
|
+
it('getsPublicKey immediately when last fetch is within the cache time', async () => {
|
|
84
|
+
|
|
85
|
+
const panda = new PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', (u)=> true);
|
|
86
|
+
const fetchesBeforeGet = (fetchPublicKey as jest.MockedFunction<typeof fetchPublicKey>).mock.calls.length;
|
|
87
|
+
|
|
88
|
+
await expect(panda.getPublicKey()).resolves.toEqual('PUBLIC KEY');
|
|
89
|
+
const fetchesAfterGet = (fetchPublicKey as jest.MockedFunction<typeof fetchPublicKey>).mock.calls.length;
|
|
90
|
+
|
|
91
|
+
expect(fetchesAfterGet).toEqual(fetchesBeforeGet);
|
|
92
|
+
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('getsPublicKey after refetching when last fetch is outside the cache time', async () => {
|
|
96
|
+
// cache time is 1 min
|
|
97
|
+
const fiveMinsAgo = new Date();
|
|
98
|
+
fiveMinsAgo.setMinutes(fiveMinsAgo.getMinutes() - 5);
|
|
99
|
+
|
|
100
|
+
(fetchPublicKey as jest.MockedFunction<typeof fetchPublicKey>).mockResolvedValue({ key: 'PUBLIC KEY', lastUpdated: fiveMinsAgo });
|
|
101
|
+
|
|
102
|
+
const panda = new PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', (u)=> true);
|
|
103
|
+
|
|
104
|
+
const fetchesBefore = (fetchPublicKey as jest.MockedFunction<typeof fetchPublicKey>).mock.calls.length;
|
|
105
|
+
|
|
106
|
+
await expect(panda.getPublicKey()).resolves.toEqual('PUBLIC KEY');
|
|
107
|
+
|
|
108
|
+
(fetchPublicKey as jest.MockedFunction<typeof fetchPublicKey>).mockResolvedValue({ key: 'PUBLIC KEY 2', lastUpdated: fiveMinsAgo });
|
|
109
|
+
|
|
110
|
+
const fetchesAfter = (fetchPublicKey as jest.MockedFunction<typeof fetchPublicKey>).mock.calls.length;
|
|
111
|
+
|
|
112
|
+
await expect(panda.getPublicKey()).resolves.toEqual('PUBLIC KEY 2');
|
|
113
|
+
|
|
114
|
+
expect(fetchesAfter).toEqual(fetchesBefore + 1);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe('verify', () => {
|
|
120
|
+
|
|
121
|
+
beforeEach(() => {
|
|
122
|
+
(fetchPublicKey as jest.MockedFunction<typeof fetchPublicKey>).mockResolvedValue({ key: publicKey, lastUpdated: new Date() });
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should return authenticated if valid', async () => {
|
|
126
|
+
jest.setSystemTime(100);
|
|
127
|
+
const panda = new PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', (u)=> true);
|
|
128
|
+
const { status } = await panda.verify(`cookiename=${sampleCookie}`);
|
|
129
|
+
|
|
130
|
+
expect(status).toBe(AuthenticationStatus.AUTHORISED);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should return expired if expired', async () => {
|
|
134
|
+
jest.setSystemTime(10_000);
|
|
135
|
+
|
|
136
|
+
const panda = new PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', (u)=> true);
|
|
137
|
+
const { status } = await panda.verify(`cookiename=${sampleCookie}`);
|
|
138
|
+
|
|
139
|
+
expect(status).toBe(AuthenticationStatus.EXPIRED);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('should return not authenticated if validation fails', async () => {
|
|
143
|
+
jest.setSystemTime(100);
|
|
144
|
+
|
|
145
|
+
const panda = new PanDomainAuthentication('cookiename', 'region', 'bucket', 'keyfile', guardianValidation);
|
|
146
|
+
const { status } = await panda.verify(`cookiename=${sampleNonGuardianCookie}`);
|
|
147
|
+
|
|
148
|
+
expect(status).toBe(AuthenticationStatus.NOT_AUTHORISED);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
});
|