@redmix/auth-auth0-api 0.0.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/LICENSE +21 -0
- package/README.md +148 -0
- package/dist/decoder.d.ts +25 -0
- package/dist/decoder.d.ts.map +1 -0
- package/dist/decoder.js +81 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/package.json +60 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Redmix
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
# Authentication
|
2
|
+
|
3
|
+
## Contributing
|
4
|
+
|
5
|
+
If you want to contribute a new auth provider integration we recommend you
|
6
|
+
start by implementing it as a custom auth provider in a Redwood App first. When
|
7
|
+
that works you can package it up as an npm package and publish it on your own.
|
8
|
+
You can then create a PR on this repo with support for your new auth provider
|
9
|
+
in our `yarn rw setup auth` cli command. The easiest option is probably to just
|
10
|
+
look at one of the existing auth providers in
|
11
|
+
`packages/cli/src/commands/setup/auth/providers` and the corresponding
|
12
|
+
templates in `../templates`.
|
13
|
+
|
14
|
+
If you need help setting up a custom auth provider there's more info in the
|
15
|
+
[auth docs](https://redwoodjs.com/docs/authentication).
|
16
|
+
|
17
|
+
### Contributing to the base auth implementation
|
18
|
+
|
19
|
+
If you want to contribute to our auth implementation, the interface towards
|
20
|
+
both auth service providers and RW apps we recommend you start looking in
|
21
|
+
`authFactory.ts` and then continue to `AuthProvider.tsx`. `AuthProvider.tsx`
|
22
|
+
has most of our implementation together with all the custom hooks it uses.
|
23
|
+
Another file to be accustomed with is `AuthContext.ts`. The interface in there
|
24
|
+
has pretty good code comments, and is what will be exposed to RW apps.
|
25
|
+
|
26
|
+
## getCurrentUser
|
27
|
+
|
28
|
+
`getCurrentUser` returns the user information together with
|
29
|
+
an optional collection of roles used by requireAuth() to check if the user is authenticated or has role-based access.
|
30
|
+
|
31
|
+
Use in conjunction with `requireAuth` in your services to check that a user is logged in, whether or not they are assigned a role, and optionally raise an error if they're not.
|
32
|
+
|
33
|
+
```js
|
34
|
+
@param decoded - The decoded access token containing user info and JWT claims like `sub`
|
35
|
+
@param { token, SupportedAuthTypes type } - The access token itself as well as the auth provider type
|
36
|
+
@param { APIGatewayEvent event, Context context } - An object which contains information from the invoker
|
37
|
+
such as headers and cookies, and the context information about the invocation such as IP Address
|
38
|
+
```
|
39
|
+
|
40
|
+
### Examples
|
41
|
+
|
42
|
+
#### Checks if currentUser is authenticated
|
43
|
+
|
44
|
+
This example is the standard use of `getCurrentUser`.
|
45
|
+
|
46
|
+
```js
|
47
|
+
export const getCurrentUser = async (
|
48
|
+
decoded,
|
49
|
+
{ _token, _type },
|
50
|
+
{ _event, _context },
|
51
|
+
) => {
|
52
|
+
return { ...decoded, roles: parseJWT({ decoded }).roles }
|
53
|
+
}
|
54
|
+
```
|
55
|
+
|
56
|
+
#### User details fetched via database query
|
57
|
+
|
58
|
+
```js
|
59
|
+
export const getCurrentUser = async (decoded) => {
|
60
|
+
return await db.user.findUnique({ where: { decoded.email } })
|
61
|
+
}
|
62
|
+
```
|
63
|
+
|
64
|
+
#### User info is decoded from the access token
|
65
|
+
|
66
|
+
```js
|
67
|
+
export const getCurrentUser = async (decoded) => {
|
68
|
+
return { ...decoded }
|
69
|
+
}
|
70
|
+
```
|
71
|
+
|
72
|
+
#### User info is contained in the decoded token and roles extracted
|
73
|
+
|
74
|
+
```js
|
75
|
+
export const getCurrentUser = async (decoded) => {
|
76
|
+
return { ...decoded, roles: parseJWT({ decoded }).roles }
|
77
|
+
}
|
78
|
+
```
|
79
|
+
|
80
|
+
#### User record query by email with namespaced app_metadata roles as Auth0 requires custom JWT claims to be namespaced
|
81
|
+
|
82
|
+
```js
|
83
|
+
export const getCurrentUser = async (decoded) => {
|
84
|
+
const currentUser = await db.user.findUnique({
|
85
|
+
where: { email: decoded.email },
|
86
|
+
})
|
87
|
+
|
88
|
+
return {
|
89
|
+
...currentUser,
|
90
|
+
roles: parseJWT({ decoded: decoded, namespace: NAMESPACE }).roles,
|
91
|
+
}
|
92
|
+
}
|
93
|
+
```
|
94
|
+
|
95
|
+
#### User record query by an identity with app_metadata roles
|
96
|
+
|
97
|
+
```js
|
98
|
+
const getCurrentUser = async (decoded) => {
|
99
|
+
const currentUser = await db.user.findUnique({
|
100
|
+
where: { userIdentity: decoded.sub },
|
101
|
+
})
|
102
|
+
return {
|
103
|
+
...currentUser,
|
104
|
+
roles: parseJWT({ decoded: decoded }).roles,
|
105
|
+
}
|
106
|
+
}
|
107
|
+
```
|
108
|
+
|
109
|
+
#### Cookies and other request information are available in the req parameter, just in case
|
110
|
+
|
111
|
+
```js
|
112
|
+
const getCurrentUser = async (_decoded, _raw, { event, _context }) => {
|
113
|
+
const cookies = cookie(event.headers.cookies)
|
114
|
+
const session = cookies['my.cookie.name']
|
115
|
+
const currentUser = await db.sessions.findUnique({ where: { id: session } })
|
116
|
+
return currentUser
|
117
|
+
}
|
118
|
+
```
|
119
|
+
|
120
|
+
## requireAuth
|
121
|
+
|
122
|
+
Use `requireAuth` in your services to check that a user is logged in, whether or not they are assigned a role, and optionally raise an error if they're not.
|
123
|
+
|
124
|
+
```js
|
125
|
+
@param {string=} roles - An optional role or list of roles
|
126
|
+
@param {string[]=} roles - An optional list of roles
|
127
|
+
|
128
|
+
@returns {boolean} - If the currentUser is authenticated (and assigned one of the given roles)
|
129
|
+
|
130
|
+
@throws {AuthenticationError} - If the currentUser is not authenticated
|
131
|
+
@throws {ForbiddenError} If the currentUser is not allowed due to role permissions
|
132
|
+
```
|
133
|
+
|
134
|
+
### Examples
|
135
|
+
|
136
|
+
#### Checks if currentUser is authenticated
|
137
|
+
|
138
|
+
```js
|
139
|
+
requireAuth()
|
140
|
+
```
|
141
|
+
|
142
|
+
#### Checks if currentUser is authenticated and assigned one of the given roles
|
143
|
+
|
144
|
+
```js
|
145
|
+
requireAuth({ role: 'admin' })
|
146
|
+
requireAuth({ role: ['editor', 'author'] })
|
147
|
+
requireAuth({ role: ['publisher'] })
|
148
|
+
```
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import type { Decoder } from '@redmix/api';
|
2
|
+
/**
|
3
|
+
* This takes an auth0 jwt and verifies it. It returns something like this:
|
4
|
+
* ```js
|
5
|
+
* {
|
6
|
+
* iss: 'https://<AUTH0_DOMAIN>/',
|
7
|
+
* sub: 'auth0|xxx',
|
8
|
+
* aud: [ 'api.billable', 'https://<AUTH0_DOMAIN>/userinfo' ],
|
9
|
+
* iat: 1588800141,
|
10
|
+
* exp: 1588886541,
|
11
|
+
* azp: 'QOsYIlHvCLqLzmfDU0Z3upFdu1znlkqK',
|
12
|
+
* scope: 'openid profile email'
|
13
|
+
* }
|
14
|
+
* ```
|
15
|
+
*
|
16
|
+
* You can use `sub` as a stable reference to your user, but if you want the email
|
17
|
+
* address you can set a context object[^0] in rules[^1]:
|
18
|
+
*
|
19
|
+
* ^0: https://auth0.com/docs/rules/references/context-object
|
20
|
+
* ^1: https://manage.auth0.com/#/rules/new
|
21
|
+
*
|
22
|
+
*/
|
23
|
+
export declare const verifyAuth0Token: (bearerToken: string) => Promise<null | Record<string, unknown>>;
|
24
|
+
export declare const authDecoder: Decoder;
|
25
|
+
//# sourceMappingURL=decoder.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"decoder.d.ts","sourceRoot":"","sources":["../src/decoder.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AAE1C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,gBAAgB,gBACd,MAAM,KAClB,OAAO,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAqCxC,CAAA;AAED,eAAO,MAAM,WAAW,EAAE,OAMzB,CAAA"}
|
package/dist/decoder.js
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
+
var __export = (target, all) => {
|
9
|
+
for (var name in all)
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
+
};
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
+
for (let key of __getOwnPropNames(from))
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
+
}
|
18
|
+
return to;
|
19
|
+
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
+
var decoder_exports = {};
|
30
|
+
__export(decoder_exports, {
|
31
|
+
authDecoder: () => authDecoder,
|
32
|
+
verifyAuth0Token: () => verifyAuth0Token
|
33
|
+
});
|
34
|
+
module.exports = __toCommonJS(decoder_exports);
|
35
|
+
var import_jsonwebtoken = __toESM(require("jsonwebtoken"));
|
36
|
+
var import_jwks_rsa = __toESM(require("jwks-rsa"));
|
37
|
+
const verifyAuth0Token = (bearerToken) => {
|
38
|
+
return new Promise((resolve, reject) => {
|
39
|
+
const { AUTH0_DOMAIN, AUTH0_AUDIENCE } = process.env;
|
40
|
+
if (!AUTH0_DOMAIN || !AUTH0_AUDIENCE) {
|
41
|
+
throw new Error(
|
42
|
+
"`AUTH0_DOMAIN` or `AUTH0_AUDIENCE` env vars are not set."
|
43
|
+
);
|
44
|
+
}
|
45
|
+
const client = (0, import_jwks_rsa.default)({
|
46
|
+
jwksUri: `https://${AUTH0_DOMAIN}/.well-known/jwks.json`
|
47
|
+
});
|
48
|
+
import_jsonwebtoken.default.verify(
|
49
|
+
bearerToken,
|
50
|
+
(header, callback) => {
|
51
|
+
client.getSigningKey(header.kid, (error, key) => {
|
52
|
+
callback(error, key?.getPublicKey());
|
53
|
+
});
|
54
|
+
},
|
55
|
+
{
|
56
|
+
audience: AUTH0_AUDIENCE,
|
57
|
+
issuer: `https://${AUTH0_DOMAIN}/`,
|
58
|
+
algorithms: ["RS256"]
|
59
|
+
},
|
60
|
+
(verifyError, decoded) => {
|
61
|
+
if (verifyError) {
|
62
|
+
return reject(verifyError);
|
63
|
+
}
|
64
|
+
resolve(
|
65
|
+
typeof decoded === "undefined" ? null : decoded
|
66
|
+
);
|
67
|
+
}
|
68
|
+
);
|
69
|
+
});
|
70
|
+
};
|
71
|
+
const authDecoder = async (token, type) => {
|
72
|
+
if (type !== "auth0") {
|
73
|
+
return null;
|
74
|
+
}
|
75
|
+
return verifyAuth0Token(token);
|
76
|
+
};
|
77
|
+
// Annotate the CommonJS export names for ESM import in node:
|
78
|
+
0 && (module.exports = {
|
79
|
+
authDecoder,
|
80
|
+
verifyAuth0Token
|
81
|
+
});
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA"}
|
package/dist/index.js
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
var index_exports = {};
|
20
|
+
__export(index_exports, {
|
21
|
+
authDecoder: () => import_decoder.authDecoder
|
22
|
+
});
|
23
|
+
module.exports = __toCommonJS(index_exports);
|
24
|
+
var import_decoder = require("./decoder");
|
25
|
+
// Annotate the CommonJS export names for ESM import in node:
|
26
|
+
0 && (module.exports = {
|
27
|
+
authDecoder
|
28
|
+
});
|
package/package.json
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
{
|
2
|
+
"name": "@redmix/auth-auth0-api",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"repository": {
|
5
|
+
"type": "git",
|
6
|
+
"url": "git+https://github.com/redmix-run/redmix.git",
|
7
|
+
"directory": "packages/auth-providers/auth0/api"
|
8
|
+
},
|
9
|
+
"license": "MIT",
|
10
|
+
"type": "commonjs",
|
11
|
+
"exports": {
|
12
|
+
".": {
|
13
|
+
"default": {
|
14
|
+
"types": "./dist/index.d.ts",
|
15
|
+
"default": "./dist/index.js"
|
16
|
+
}
|
17
|
+
},
|
18
|
+
"./dist/decoder": {
|
19
|
+
"default": {
|
20
|
+
"types": "./dist/decoder.d.ts",
|
21
|
+
"default": "./dist/decoder.js"
|
22
|
+
}
|
23
|
+
}
|
24
|
+
},
|
25
|
+
"main": "./dist/index.js",
|
26
|
+
"types": "./dist/index.d.ts",
|
27
|
+
"files": [
|
28
|
+
"dist"
|
29
|
+
],
|
30
|
+
"scripts": {
|
31
|
+
"build": "tsx ./build.mts && yarn build:types",
|
32
|
+
"build:pack": "yarn pack -o redmix-auth-auth0-api.tgz",
|
33
|
+
"build:types": "tsc --build --verbose ./tsconfig.json",
|
34
|
+
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx,template\" --ignore dist --exec \"yarn build\"",
|
35
|
+
"check:attw": "yarn attw -P",
|
36
|
+
"check:package": "concurrently npm:check:attw yarn:publint",
|
37
|
+
"prepublishOnly": "NODE_ENV=production yarn build",
|
38
|
+
"test": "vitest run",
|
39
|
+
"test:watch": "vitest watch"
|
40
|
+
},
|
41
|
+
"dependencies": {
|
42
|
+
"jsonwebtoken": "9.0.2",
|
43
|
+
"jwks-rsa": "3.1.0"
|
44
|
+
},
|
45
|
+
"devDependencies": {
|
46
|
+
"@arethetypeswrong/cli": "0.16.4",
|
47
|
+
"@redmix/api": "0.0.1",
|
48
|
+
"@redmix/framework-tools": "0.0.1",
|
49
|
+
"@types/jsonwebtoken": "9.0.8",
|
50
|
+
"concurrently": "8.2.2",
|
51
|
+
"publint": "0.3.11",
|
52
|
+
"tsx": "4.19.3",
|
53
|
+
"typescript": "5.6.2",
|
54
|
+
"vitest": "2.1.9"
|
55
|
+
},
|
56
|
+
"publishConfig": {
|
57
|
+
"access": "public"
|
58
|
+
},
|
59
|
+
"gitHead": "688027c97502c500ebbede9cdc7cc51545a8dcf3"
|
60
|
+
}
|