@hellocoop/fastify 1.6.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/README.md +7 -0
- package/dist/auth.d.ts +18 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +59 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/middleware.d.ts +11 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +28 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +73 -0
- package/package.json +60 -0
package/README.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# Fastify package for Hellō
|
2
|
+
|
3
|
+
[Hellō](https://hello.dev) is an identity network that provides login and registration using the standard OpenID Connect protocol. Hellō offers your users choice between all popular social login providers.
|
4
|
+
|
5
|
+
This [Fastify](https://fastify.dev/) package provides an endpoint that handles all protocol interactions and sets an encrypted cookie with the logged in user's information. The cookie contents are decrypted and available as `req.getAuth()`.
|
6
|
+
|
7
|
+
See the [Fastify Quickstart documentation](https://www.hello.dev/docs/quickstarts/fastiify) for how to add Hellō to your Fastify app in minutes, and the [Fastify SDK documentation](https://www.hello.dev/docs/sdks/fastiify) for details.
|
package/dist/auth.d.ts
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
import { FastifyPluginAsync } from 'fastify';
|
2
|
+
import { FastifyPluginOptions } from 'fastify';
|
3
|
+
import { Auth } from '@hellocoop/types';
|
4
|
+
import { Config } from '@hellocoop/router';
|
5
|
+
declare module 'fastify' {
|
6
|
+
interface FastifyRequest {
|
7
|
+
auth?: Auth;
|
8
|
+
getAuth: () => Promise<Auth>;
|
9
|
+
}
|
10
|
+
interface FastifyReply {
|
11
|
+
clearAuth: () => void;
|
12
|
+
}
|
13
|
+
}
|
14
|
+
export interface HelloConfig extends FastifyPluginOptions, Config {
|
15
|
+
}
|
16
|
+
declare const _default: FastifyPluginAsync<HelloConfig>;
|
17
|
+
export default _default;
|
18
|
+
//# sourceMappingURL=auth.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAEA,OAAO,EACH,kBAAkB,EACpB,MAAM,SAAS,CAAA;AAGjB,OAAO,EAGH,oBAAoB,EACvB,MAAM,SAAS,CAAA;AAIhB,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACvC,OAAO,EAQH,MAAM,EACT,MAAO,mBAAmB,CAAA;AAqC3B,OAAO,QAAQ,SAAS,CAAC;IACrB,UAAU,cAAc;QACtB,IAAI,CAAC,EAAE,IAAI,CAAC;QACZ,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;KAC7B;IACD,UAAU,YAAY;QACpB,SAAS,EAAE,MAAM,IAAI,CAAC;KACvB;CACF;AAEH,MAAM,WAAW,WAAY,SAAQ,oBAAoB,EAAE,MAAM;CAAG;;AAwBpE,wBAAgC"}
|
package/dist/auth.js
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
"use strict";
|
2
|
+
// use Express for type information
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
5
|
+
};
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
7
|
+
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
|
8
|
+
const cookie_1 = require("cookie");
|
9
|
+
const router_1 = require("@hellocoop/router");
|
10
|
+
const convertToHelloRequest = (req) => {
|
11
|
+
return {
|
12
|
+
headers: () => { return req.headers; },
|
13
|
+
query: req.query,
|
14
|
+
path: req.routeOptions.url,
|
15
|
+
getAuth: () => { return req.auth; },
|
16
|
+
setAuth: (auth) => { req.auth = auth; },
|
17
|
+
};
|
18
|
+
};
|
19
|
+
const convertToHelloResponse = (res) => {
|
20
|
+
return {
|
21
|
+
clearAuth: () => {
|
22
|
+
const { name, value, options } = (0, router_1.clearAuthCookieParams)();
|
23
|
+
res.header('Set-Cookie', (0, cookie_1.serialize)(name, value, options));
|
24
|
+
},
|
25
|
+
send: (data) => res.type('text/html').send(data),
|
26
|
+
json: (data) => res.send(data),
|
27
|
+
redirect: (url) => res.redirect(url),
|
28
|
+
setCookie: (name, value, options) => {
|
29
|
+
res.header('Set-Cookie', (0, cookie_1.serialize)(name, value, options));
|
30
|
+
},
|
31
|
+
setHeader: (name, value) => res.header(name, value),
|
32
|
+
status: (statusCode) => {
|
33
|
+
res.code(statusCode);
|
34
|
+
return {
|
35
|
+
send: (data) => res.send(data)
|
36
|
+
};
|
37
|
+
},
|
38
|
+
};
|
39
|
+
};
|
40
|
+
const helloPlugin = async (instance, options) => {
|
41
|
+
if (!router_1.isConfigured)
|
42
|
+
(0, router_1.configure)(options);
|
43
|
+
instance.decorateRequest('auth', undefined);
|
44
|
+
instance.decorateRequest('getAuth', async function () {
|
45
|
+
const helloReq = convertToHelloRequest(this);
|
46
|
+
this.auth = await (0, router_1.getAuthfromCookies)(helloReq);
|
47
|
+
return this.auth;
|
48
|
+
});
|
49
|
+
instance.decorateReply('clearAuth', function () {
|
50
|
+
const { name, value, options } = (0, router_1.clearAuthCookieParams)();
|
51
|
+
this.header('Set-Cookie', (0, cookie_1.serialize)(name, value, options));
|
52
|
+
});
|
53
|
+
instance.get('/api/hellocoop', async (req, res) => {
|
54
|
+
const helloReq = convertToHelloRequest(req);
|
55
|
+
const helloRes = convertToHelloResponse(res);
|
56
|
+
return await (0, router_1.router)(helloReq, helloRes);
|
57
|
+
});
|
58
|
+
};
|
59
|
+
exports.default = (0, fastify_plugin_1.default)(helloPlugin);
|
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,OAAO,IAAI,IAAI,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAA;AACrD,eAAe,IAAI,CAAA;AACnB,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,WAAW,EAAE,CAAA"}
|
package/dist/index.js
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.helloAuth = void 0;
|
7
|
+
const auth_1 = __importDefault(require("./auth"));
|
8
|
+
Object.defineProperty(exports, "helloAuth", { enumerable: true, get: function () { return auth_1.default; } });
|
9
|
+
exports.default = auth_1.default;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { FastifyRequest, FastifyReply } from 'fastify';
|
2
|
+
import { Auth } from '@hellocoop/types';
|
3
|
+
declare module 'fastify' {
|
4
|
+
interface FastifyRequest {
|
5
|
+
getAuth: () => Promise<Auth>;
|
6
|
+
}
|
7
|
+
}
|
8
|
+
export declare const redirect: (target: string) => (request: FastifyRequest, reply: FastifyReply) => Promise<undefined>;
|
9
|
+
export declare const unauthorized: (request: FastifyRequest, reply: FastifyReply) => Promise<undefined>;
|
10
|
+
export declare const setAuth: (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
|
11
|
+
//# sourceMappingURL=middleware.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtD,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AAGvC,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAU,cAAc;QACtB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;KAC9B;CACF;AAED,eAAO,MAAM,QAAQ,WAAY,MAAM,eACd,cAAc,SAAS,YAAY,uBAQ3D,CAAC;AAEF,eAAO,MAAM,YAAY,YAAmB,cAAc,SAAS,YAAY,uBAM9E,CAAC;AAEF,eAAO,MAAM,OAAO,YAAmB,cAAc,SAAS,YAAY,kBAEzE,CAAC"}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.setAuth = exports.unauthorized = exports.redirect = void 0;
|
4
|
+
const router_1 = require("@hellocoop/router");
|
5
|
+
const redirect = (target) => {
|
6
|
+
return async (request, reply) => {
|
7
|
+
const auth = await request.getAuth();
|
8
|
+
if (auth.isLoggedIn) {
|
9
|
+
return; // equivalent to Express' next()
|
10
|
+
}
|
11
|
+
else {
|
12
|
+
return reply.redirect(target);
|
13
|
+
}
|
14
|
+
};
|
15
|
+
};
|
16
|
+
exports.redirect = redirect;
|
17
|
+
const unauthorized = async (request, reply) => {
|
18
|
+
const auth = await request.getAuth();
|
19
|
+
if (!auth.isLoggedIn) {
|
20
|
+
reply.header('WWW-Authenticate', `Hello ${router_1.configuration.clientId}`).status(401);
|
21
|
+
return reply.send();
|
22
|
+
}
|
23
|
+
};
|
24
|
+
exports.unauthorized = unauthorized;
|
25
|
+
const setAuth = async (request, reply) => {
|
26
|
+
await request.getAuth();
|
27
|
+
};
|
28
|
+
exports.setAuth = setAuth;
|
package/dist/server.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":""}
|
package/dist/server.js
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
"use strict";
|
2
|
+
// test server
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
5
|
+
};
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
7
|
+
// import fastly from 'fastify'
|
8
|
+
// import auth from './index'
|
9
|
+
// import { redirect, unauthorized, setAuth } from './middleware'
|
10
|
+
// const app = fastly();
|
11
|
+
// const port = 8080; // default port to listen
|
12
|
+
// app.register(auth,{client_id:'90804992-8d01-474e-8a0c-59cddeb5a1a3'})
|
13
|
+
// app.get( "/", async ( req, res ) => {
|
14
|
+
// res.send( await req.getAuth())
|
15
|
+
// } );
|
16
|
+
// app.get( "/redirect", redirect('/'), async ( req, res ) => {
|
17
|
+
// res.send( await req.getAuth())
|
18
|
+
// } );
|
19
|
+
// app.get( "/unauthorized", unauthorized, async ( req, res ) => {
|
20
|
+
// res.send( await req.getAuth())
|
21
|
+
// } );
|
22
|
+
// app.get( "/setAuth", setAuth, ( req, res ) => {
|
23
|
+
// res.send( req.auth)
|
24
|
+
// } );
|
25
|
+
// // start the Express server
|
26
|
+
// app.listen( port, () => {
|
27
|
+
// console.log( `server started at http://localhost:${ port }` );
|
28
|
+
// } );
|
29
|
+
const fastify_1 = __importDefault(require("fastify"));
|
30
|
+
const index_1 = require("./index");
|
31
|
+
const middleware_1 = require("./middleware");
|
32
|
+
const config = require('../hello.config.js');
|
33
|
+
const app = (0, fastify_1.default)();
|
34
|
+
const port = 8080;
|
35
|
+
// Register your auth plugin with Fastify
|
36
|
+
app.register(index_1.helloAuth, config);
|
37
|
+
// Define your routes, using preHandler hooks to replace Express middleware
|
38
|
+
app.get("/", async (request, reply) => {
|
39
|
+
const auth = await request.getAuth();
|
40
|
+
return reply.send(auth);
|
41
|
+
});
|
42
|
+
app.get("/redirect", {
|
43
|
+
preHandler: (0, middleware_1.redirect)('/'),
|
44
|
+
handler: async (request, reply) => {
|
45
|
+
const auth = await request.getAuth();
|
46
|
+
return reply.send(auth);
|
47
|
+
}
|
48
|
+
});
|
49
|
+
app.get("/unauthorized", {
|
50
|
+
preHandler: middleware_1.unauthorized,
|
51
|
+
handler: async (request, reply) => {
|
52
|
+
const auth = await request.getAuth();
|
53
|
+
return reply.send(auth);
|
54
|
+
}
|
55
|
+
});
|
56
|
+
app.get("/setAuth", {
|
57
|
+
preHandler: middleware_1.setAuth,
|
58
|
+
handler: async (request, reply) => {
|
59
|
+
// Assuming setAuth sets something like request.auth
|
60
|
+
return reply.send(request.auth);
|
61
|
+
}
|
62
|
+
});
|
63
|
+
const startServer = async () => {
|
64
|
+
try {
|
65
|
+
await app.listen({ port: port });
|
66
|
+
console.log(`Server started at http://localhost:${port}`);
|
67
|
+
}
|
68
|
+
catch (err) {
|
69
|
+
console.error(err);
|
70
|
+
process.exit(1);
|
71
|
+
}
|
72
|
+
};
|
73
|
+
startServer();
|
package/package.json
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
{
|
2
|
+
"name": "@hellocoop/fastify",
|
3
|
+
"version": "1.6.0",
|
4
|
+
"description": "Fastify SDK for Hellō https://hello.dev",
|
5
|
+
"repository": {
|
6
|
+
"type": "git",
|
7
|
+
"url": "git+https://github.com/hellocoop/packages.git"
|
8
|
+
},
|
9
|
+
"homepage": "https://www.hello.dev/docs/sdks/fastify",
|
10
|
+
"main": "./dist/index.js",
|
11
|
+
"types": "./dist/index.d.ts",
|
12
|
+
"exports": {
|
13
|
+
".": "./dist/index.js",
|
14
|
+
"./middleware": "./dist/middleware.js"
|
15
|
+
},
|
16
|
+
"files": [
|
17
|
+
"dist/"
|
18
|
+
],
|
19
|
+
"keywords": [
|
20
|
+
"fastify",
|
21
|
+
"nodejs",
|
22
|
+
"node.js",
|
23
|
+
"hello",
|
24
|
+
"openid",
|
25
|
+
"oidc",
|
26
|
+
"sso"
|
27
|
+
],
|
28
|
+
"author": {
|
29
|
+
"name": "Hello Identity Co-op",
|
30
|
+
"email": "contact@hello.coop",
|
31
|
+
"url": "https://hello.coop"
|
32
|
+
},
|
33
|
+
"license": "MIT",
|
34
|
+
"bugs": {
|
35
|
+
"url": "https://github.com/hellocoop/packages/issues"
|
36
|
+
},
|
37
|
+
"scripts": {
|
38
|
+
"watch": "tsc --watch --declaration",
|
39
|
+
"build": "npm run build:clean && npm run build:src",
|
40
|
+
"build:clean": "rimraf dist/",
|
41
|
+
"build:src": "tsc --declaration"
|
42
|
+
},
|
43
|
+
"peerDependencies": {
|
44
|
+
"fastify": "^4"
|
45
|
+
},
|
46
|
+
"devDependencies": {
|
47
|
+
"@tsconfig/node18": "^18.2.2",
|
48
|
+
"rimraf": "^5.0.1",
|
49
|
+
"typescript": "^5.2.2",
|
50
|
+
"fastify": "^4"
|
51
|
+
},
|
52
|
+
"engines": {
|
53
|
+
"node": ">=18"
|
54
|
+
},
|
55
|
+
"dependencies": {
|
56
|
+
"dotenv": "^16.3.1",
|
57
|
+
"@hellocoop/router": "latest",
|
58
|
+
"fastify-plugin": "^4.5.1"
|
59
|
+
}
|
60
|
+
}
|