create-authenik8-app 2.4.3 → 2.4.4

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.
Files changed (37) hide show
  1. package/README.md +151 -4
  2. package/package.json +10 -8
  3. package/templates/THREAT_MODEL.md +138 -0
  4. package/templates/express-auth/README.md +196 -0
  5. package/templates/express-auth/docker-compose.yml +23 -0
  6. package/templates/express-auth/package.json +4 -2
  7. package/templates/express-auth/src/app.ts +1 -1
  8. package/templates/express-auth/src/server.ts +1 -1
  9. package/templates/express-auth+/README.md +247 -0
  10. package/templates/express-auth+/docker-compose.yml +23 -0
  11. package/templates/express-auth+/package.json +5 -3
  12. package/templates/express-auth+/src/auth/auth.ts +6 -16
  13. package/templates/express-auth+/src/auth/controllers/oauth.controller.ts +1 -0
  14. package/templates/express-auth+/src/auth/{password.controller.ts → controllers/password.controller.ts} +4 -4
  15. package/templates/express-auth+/src/auth/{protected.controller.ts → controllers/protected.controller.ts} +2 -2
  16. package/templates/express-auth+/src/auth/{auth.middleware.ts → middleware/auth.middleware.ts} +1 -1
  17. package/templates/express-auth+/src/auth/routes/oauth.routes.ts +5 -0
  18. package/templates/express-auth+/src/auth/{password.route.ts → routes/password.route.ts} +1 -1
  19. package/templates/express-auth+/src/auth/{protected.routes.ts → routes/protected.routes.ts} +2 -2
  20. package/templates/express-auth+/src/oauth-providers/github/src/auth/auth.ts +42 -0
  21. package/templates/express-auth+/src/oauth-providers/github/src/auth/oauth.controller.ts +37 -0
  22. package/templates/express-auth+/src/oauth-providers/github/src/auth/oauth.routes.ts +11 -0
  23. package/templates/express-auth+/src/oauth-providers/google/src/auth/auth.ts +42 -0
  24. package/templates/express-auth+/src/oauth-providers/google/src/auth/oauth.controller.ts +37 -0
  25. package/templates/express-auth+/src/oauth-providers/google/src/auth/oauth.routes.ts +11 -0
  26. package/templates/express-auth+/src/oauth-providers/google-github/src/auth/auth.ts +47 -0
  27. package/templates/express-auth+/src/oauth-providers/google-github/src/auth/oauth.controller.ts +57 -0
  28. package/templates/express-auth+/src/server.ts +3 -3
  29. package/templates/express-base/README.md +113 -0
  30. package/templates/express-base/app.ts +1 -1
  31. package/templates/express-base/docker-compose.yml +23 -0
  32. package/templates/express-base/package.json +4 -2
  33. package/templates/express-base/src/server.ts +1 -1
  34. package/templates/prisma/postgresql/.env.example +11 -0
  35. package/templates/prisma/sqlite/.env.example +11 -0
  36. package/templates/express-auth+/src/auth/oauth.controller.ts +0 -38
  37. package/templates/express-auth+/src/{auth → oauth-providers/google-github/src/auth}/oauth.routes.ts +1 -1
@@ -1,7 +1,7 @@
1
1
  import express from "express";
2
- import passwordRoutes from "./auth/password.route";
3
- import oauthRoutes from "./auth/oauth.routes";
4
- import protectedRoutes from "./auth/protected.routes";
2
+ import passwordRoutes from "./auth/routes/password.route";
3
+ import oauthRoutes from "./auth/routes/oauth.routes";
4
+ import protectedRoutes from "./auth/routes/protected.routes";
5
5
  import { getAuth, initAuth } from "./auth/auth";
6
6
 
7
7
  async function start(){
@@ -0,0 +1,113 @@
1
+ # Authenik8 Express API
2
+
3
+ Generated by `create-authenik8-app`.
4
+
5
+ ## Start
6
+
7
+ ```bash
8
+ npm install
9
+ npm run docker:up
10
+ npm run prisma:migrate
11
+ npm run dev
12
+ ```
13
+
14
+ For SQLite, Postgres in `docker-compose.yml` is optional. Redis is required for refresh-token security features.
15
+
16
+ ## Environment
17
+
18
+ Review `.env` before running. The generated secrets are development placeholders and must be replaced before deployment.
19
+
20
+ Required:
21
+
22
+ ```bash
23
+ DATABASE_URL=file:./dev.db
24
+ JWT_SECRET=dev-jwt-secret-change-before-production-123456
25
+ REFRESH_SECRET=dev-refresh-secret-change-before-production-123456
26
+ REDIS_HOST=127.0.0.1
27
+ REDIS_PORT=6379
28
+ ```
29
+
30
+ ## Routes
31
+
32
+ ```http
33
+ GET /public
34
+ GET /guest
35
+ GET /protected
36
+ POST /refresh
37
+ ```
38
+
39
+ Protected requests should send:
40
+
41
+ ```http
42
+ Authorization: Bearer <accessToken>
43
+ ```
44
+
45
+ ## 3-Minute Verification
46
+
47
+ Start the API in one terminal:
48
+
49
+ ```bash
50
+ npm run docker:up
51
+ npm run prisma:migrate
52
+ npm run dev
53
+ ```
54
+
55
+ In another terminal, check that public routing works:
56
+
57
+ ```bash
58
+ curl http://localhost:3000/public
59
+ ```
60
+
61
+ Then check the protected route without a token:
62
+
63
+ ```bash
64
+ curl -i http://localhost:3000/protected
65
+ ```
66
+
67
+ Expected result: the route should reject the request because no access token was sent. That means the auth middleware is active.
68
+
69
+ ## Environment Variables
70
+
71
+ - `DATABASE_URL`: Prisma database connection. SQLite uses `file:./dev.db`; Postgres uses a `postgresql://...` URL.
72
+ - `JWT_SECRET`: signs short-lived access tokens. Use a long random value in production.
73
+ - `REFRESH_SECRET`: signs refresh tokens. Use a different long random value in production.
74
+ - `REDIS_HOST`: Redis host for refresh-token/session security.
75
+ - `REDIS_PORT`: Redis port, usually `6379` locally.
76
+
77
+ ## Frontend Fetch Example
78
+
79
+ ```ts
80
+ export async function getProtected(accessToken: string) {
81
+ const response = await fetch("http://localhost:3000/protected", {
82
+ headers: {
83
+ Authorization: `Bearer ${accessToken}`,
84
+ },
85
+ });
86
+
87
+ if (!response.ok) {
88
+ throw new Error(`Request failed: ${response.status}`);
89
+ }
90
+
91
+ return response.json();
92
+ }
93
+ ```
94
+
95
+ ## Troubleshooting
96
+
97
+ `Redis connection refused`: run `npm run docker:up` or start Redis locally with `redis-server --daemonize yes`.
98
+
99
+ `Prisma Client did not initialize`: run `npm run prisma:migrate`, then restart `npm run dev`.
100
+
101
+ `JWT_SECRET must be set to at least 32 characters`: check `.env`; both token secrets must be long strings.
102
+
103
+ `Port 3000 already in use`: stop the other process or change the `app.listen(3000)` port in `src/server.ts`.
104
+
105
+ `DATABASE_URL is wrong`: for SQLite use `file:./dev.db`; for local Docker Postgres use `postgresql://postgres:postgres@localhost:5432/authenik8?schema=public`.
106
+
107
+ ## Threat Model
108
+
109
+ Read `THREAT_MODEL.md` before deploying. It explains what Authenik8 protects, what Redis-backed token state handles, and what remains your responsibility.
110
+
111
+ ## Deploy
112
+
113
+ Use `npm run build`, run `npx prisma migrate deploy` for production databases, set real secrets in your host, and point Redis/Postgres env vars at managed services.
@@ -1,4 +1,4 @@
1
- import express from "express";
1
+ import express from "express";
2
2
  import { createBaseRoutes } from "./routes/base.routes";
3
3
 
4
4
  export const createApp = (auth: any) => {
@@ -0,0 +1,23 @@
1
+ services:
2
+ redis:
3
+ image: redis:7-alpine
4
+ ports:
5
+ - "6379:6379"
6
+ command: ["redis-server", "--appendonly", "yes"]
7
+ volumes:
8
+ - redis-data:/data
9
+
10
+ postgres:
11
+ image: postgres:16-alpine
12
+ ports:
13
+ - "5432:5432"
14
+ environment:
15
+ POSTGRES_USER: postgres
16
+ POSTGRES_PASSWORD: postgres
17
+ POSTGRES_DB: authenik8
18
+ volumes:
19
+ - postgres-data:/var/lib/postgresql/data
20
+
21
+ volumes:
22
+ redis-data:
23
+ postgres-data:
@@ -4,10 +4,12 @@
4
4
  "dev": "ts-node-dev --respawn --transpile-only ./src/server.ts",
5
5
  "build": "tsc",
6
6
  "start": "node dist/server.js",
7
- "prisma:migrate": "prisma migrate"
7
+ "prisma:migrate": "prisma migrate",
8
+ "docker:up": "docker compose up -d",
9
+ "docker:down": "docker compose down"
8
10
  },
9
11
  "dependencies": {
10
- "authenik8-core": "^1.0.3",
12
+ "authenik8-core": "^1.0.33",
11
13
  "dotenv": "^16.0.0",
12
14
  "express": "^4.18.2",
13
15
  "@prisma/client": "5.22.0"
@@ -1,4 +1,4 @@
1
- import dotenv from "dotenv";
1
+ import dotenv from "dotenv";
2
2
  import { createAuthenik8 } from "authenik8-core";
3
3
  import { createApp } from "../app";
4
4
  import { requiredSecret } from "../utils/security";
@@ -0,0 +1,11 @@
1
+ DATABASE_URL="postgresql://postgres:postgres@localhost:5432/authenik8?schema=public"
2
+ JWT_SECRET="dev-jwt-secret-change-before-production-123456"
3
+ REFRESH_SECRET="dev-refresh-secret-change-before-production-123456"
4
+ REDIS_HOST="127.0.0.1"
5
+ REDIS_PORT="6379"
6
+ GOOGLE_CLIENT_ID="change-me-google-client-id"
7
+ GOOGLE_CLIENT_SECRET="change-me-google-client-secret"
8
+ GOOGLE_REDIRECT_URI="http://localhost:3000/auth/google/callback"
9
+ GITHUB_CLIENT_ID="change-me-github-client-id"
10
+ GITHUB_CLIENT_SECRET="change-me-github-client-secret"
11
+ GITHUB_REDIRECT_URI="http://localhost:3000/auth/github/callback"
@@ -0,0 +1,11 @@
1
+ DATABASE_URL="file:./dev.db"
2
+ JWT_SECRET="dev-jwt-secret-change-before-production-123456"
3
+ REFRESH_SECRET="dev-refresh-secret-change-before-production-123456"
4
+ REDIS_HOST="127.0.0.1"
5
+ REDIS_PORT="6379"
6
+ GOOGLE_CLIENT_ID="change-me-google-client-id"
7
+ GOOGLE_CLIENT_SECRET="change-me-google-client-secret"
8
+ GOOGLE_REDIRECT_URI="http://localhost:3000/auth/google/callback"
9
+ GITHUB_CLIENT_ID="change-me-github-client-id"
10
+ GITHUB_CLIENT_SECRET="change-me-github-client-secret"
11
+ GITHUB_REDIRECT_URI="http://localhost:3000/auth/github/callback"
@@ -1,38 +0,0 @@
1
- import { Request, Response } from "express";
2
- import { getAuth } from "./auth";
3
-
4
- export const oauthController = {
5
- googleRedirect(req: Request, res: Response) {
6
- getAuth().oauth?.google?.redirect(req, res);
7
- },
8
-
9
- async googleCallback(req: Request, res: Response) {
10
- const result = await getAuth().oauth?.google?.handleCallback(req);
11
-
12
- res.json({
13
- provider: "google",
14
- ...result,
15
- });
16
- },
17
-
18
- githubRedirect(req: Request, res: Response) {
19
- getAuth().oauth?.github?.redirect(req, res);
20
- },
21
-
22
- async githubCallback(req: Request, res: Response) {
23
- const result = await getAuth().oauth?.github?.handleCallback(req);
24
-
25
- res.json({
26
- provider: "github",
27
- ...result,
28
- });
29
- },
30
-
31
- googleLink(req: Request, res: Response) {
32
- getAuth().oauth?.google?.redirect(req, res, "link");
33
- },
34
-
35
- githubLink(req: Request, res: Response) {
36
- getAuth().oauth?.github?.redirect(req, res, "link");
37
- },
38
- };
@@ -6,9 +6,9 @@ const router = express.Router();
6
6
 
7
7
  router.get("/google", oauthController.googleRedirect);
8
8
  router.get("/google/callback", oauthController.googleCallback);
9
+ router.get("/google/link", authMiddleware, oauthController.googleLink);
9
10
  router.get("/github", oauthController.githubRedirect);
10
11
  router.get("/github/callback", oauthController.githubCallback);
11
- router.get("/google/link", authMiddleware, oauthController.googleLink);
12
12
  router.get("/github/link", authMiddleware, oauthController.githubLink);
13
13
 
14
14
  export default router;