@adobe/spacecat-shared-http-utils 1.6.7 → 1.6.9
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/CHANGELOG.md +14 -0
- package/README.md +69 -2
- package/package.json +8 -4
- package/src/auth/auth-wrapper.js +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [@adobe/spacecat-shared-http-utils-v1.6.9](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-http-utils-v1.6.8...@adobe/spacecat-shared-http-utils-v1.6.9) (2024-09-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **deps:** update external fixes ([#369](https://github.com/adobe/spacecat-shared/issues/369)) ([5412d7b](https://github.com/adobe/spacecat-shared/commit/5412d7be554b9940d43b39b18f2913146e866846))
|
|
7
|
+
|
|
8
|
+
# [@adobe/spacecat-shared-http-utils-v1.6.8](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-http-utils-v1.6.7...@adobe/spacecat-shared-http-utils-v1.6.8) (2024-08-22)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* add Scoped API Key docs ([#342](https://github.com/adobe/spacecat-shared/issues/342)) ([2fbf707](https://github.com/adobe/spacecat-shared/commit/2fbf707dfeff914dc47ae1f9860629873927e03e))
|
|
14
|
+
|
|
1
15
|
# [@adobe/spacecat-shared-http-utils-v1.6.7](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-http-utils-v1.6.6...@adobe/spacecat-shared-http-utils-v1.6.7) (2024-08-19)
|
|
2
16
|
|
|
3
17
|
|
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Spacecat Shared - HTTP Utilities
|
|
2
2
|
|
|
3
|
-
A set of TypeScript functions for creating HTTP responses with standardized formats
|
|
3
|
+
A set of TypeScript functions for creating HTTP responses with standardized formats, and classes for dealing with
|
|
4
|
+
authenticating HTTP requests.
|
|
4
5
|
|
|
5
6
|
## Table of Contents
|
|
6
7
|
|
|
@@ -76,6 +77,72 @@ Creates a response for a not found scenario with an error message and optional h
|
|
|
76
77
|
|
|
77
78
|
Creates a response for an internal server error with an error message and optional headers.
|
|
78
79
|
|
|
80
|
+
## Authentication
|
|
81
|
+
|
|
82
|
+
This package includes classes for dealing with authenticating HTTP requests.
|
|
83
|
+
|
|
84
|
+
### ScopedApiKeyHandler
|
|
85
|
+
|
|
86
|
+
Scoped API keys are defined in the datalayer and can be used to authenticate requests to the Spacecat API. They employ
|
|
87
|
+
"scopes" to enable fine-grained access to resources. An example API key entity looks like this (`id` omitted):
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
{
|
|
91
|
+
"name": "Example API Key",
|
|
92
|
+
"hashedApiKey": "4c806362b613f7496abf284146efd31da90e4b16169fe001841ca17290f427c4",
|
|
93
|
+
"createdAt": "2024-08-21T19:00:00.000Z",
|
|
94
|
+
"expiresAt": "2024-12-21T19:00:00.000Z",
|
|
95
|
+
"scopes": [
|
|
96
|
+
{ "name": "imports.write" },
|
|
97
|
+
{ "name": "imports.read" }
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Key points on the above:
|
|
103
|
+
- `hashedApiKey` is the SHA-256 hash of the actual API key ("test-api-key" above)
|
|
104
|
+
- `scopes` are the permissions granted to the API key
|
|
105
|
+
- Each `scope` object can contain additional data, but the `name` field is required
|
|
106
|
+
|
|
107
|
+
The `ScopedApiKeyHandler` class is used to authenticate requests using scoped API keys. To support the existing
|
|
108
|
+
Legacy API keys, it should be ordered after the `LegacyApiKeyHandler` in the `authHandlers` array. This enables requests
|
|
109
|
+
with the existing API keys to be authenticated quickly without requiring a database lookup.
|
|
110
|
+
|
|
111
|
+
#### Checking for scope access
|
|
112
|
+
|
|
113
|
+
To enable a new scope, first refer to the `scopeNames` array in the ApiKey model (/packages/spacecat-shared-data-access/src/models/api-key.js).
|
|
114
|
+
If the scope you need is not listed here, please add it. Note the convention for scope names is `resource.action`,
|
|
115
|
+
e.g. `imports.write` or `sites.read_all`. The `_all` action suffix indicates access beyond resources created (or
|
|
116
|
+
jobs initiated by) the current API key.
|
|
117
|
+
|
|
118
|
+
Next, you will want to check that the API used to make the request has access to the required scope(s) from your
|
|
119
|
+
controller. The `authWrapper` adds an `auth` helper to the context which makes this easy. Here's an example of how to
|
|
120
|
+
check for scope access from a controller:
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
// This route requires the 'imports.write' scope
|
|
124
|
+
function protectedRoute(context) {
|
|
125
|
+
const { auth } = context;
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
auth.checkScopes(['imports.write']);
|
|
129
|
+
} catch (error) {
|
|
130
|
+
throw new ErrorWithStatusCode('Missing required scopes', 401);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return ok('You have access to this resource');
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Need additional details from the API key entity object? The `authWrapper` places the authenticated `authInfo` object
|
|
138
|
+
into the context at `context.attributes.authInfo`, with the API key entity available in its `profile` property.
|
|
139
|
+
|
|
140
|
+
#### Creating a new API key
|
|
141
|
+
|
|
142
|
+
This is currently a manual process, and involves duplicating an existing API key entity in the datalayer and updating
|
|
143
|
+
its properties. For the table to update, refer to the `TABLE_NAME_API_KEYS` constant (which will be overridden on prod).
|
|
144
|
+
|
|
145
|
+
In the future we are planning to support a way for clients to request their own API key, given a valid IMS token.
|
|
79
146
|
|
|
80
147
|
## Contributing
|
|
81
148
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/spacecat-shared-http-utils",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.9",
|
|
4
4
|
"description": "Shared modules of the Spacecat Services - HTTP Utils",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": "^20.0.0 <21.0.0",
|
|
8
|
+
"npm": "^10.0.0 <11.0.0"
|
|
9
|
+
},
|
|
6
10
|
"main": "src/index.js",
|
|
7
11
|
"types": "src/index.d.ts",
|
|
8
12
|
"scripts": {
|
|
@@ -32,12 +36,12 @@
|
|
|
32
36
|
"@adobe/fetch": "4.1.8",
|
|
33
37
|
"@adobe/spacecat-shared-utils": "1.19.1",
|
|
34
38
|
"@adobe/spacecat-shared-data-access": "1.41.3",
|
|
35
|
-
"jose": "5.
|
|
39
|
+
"jose": "5.9.2"
|
|
36
40
|
},
|
|
37
41
|
"devDependencies": {
|
|
38
42
|
"@adobe/helix-shared-wrap": "2.0.2",
|
|
39
|
-
"chai": "
|
|
43
|
+
"chai": "5.1.1",
|
|
40
44
|
"chai-as-promised": "8.0.0",
|
|
41
|
-
"sinon": "18.0.
|
|
45
|
+
"sinon": "18.0.1"
|
|
42
46
|
}
|
|
43
47
|
}
|