@civic/auth 0.0.1-beta.23 → 0.0.1-beta.25
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/dist/AuthProvider-BBetpl_s.d.mts +21 -0
- package/dist/AuthProvider-BYZ8w92b.d.mts +15 -0
- package/dist/AuthProvider-BgOwv9h8.d.ts +15 -0
- package/dist/AuthProvider-D_kReUi0.d.ts +21 -0
- package/dist/UserProvider-BA2uflVB.d.ts +16 -0
- package/dist/UserProvider-Bl3j1PUO.d.mts +16 -0
- package/dist/chunk-2OZJONNO.js +172 -0
- package/dist/chunk-2OZJONNO.js.map +1 -0
- package/dist/chunk-2TDB4XWE.js +277 -0
- package/dist/chunk-2TDB4XWE.js.map +1 -0
- package/dist/chunk-4KSQPSLG.mjs +168 -0
- package/dist/chunk-4KSQPSLG.mjs.map +1 -0
- package/dist/chunk-63YGK3A7.mjs +223 -0
- package/dist/chunk-63YGK3A7.mjs.map +1 -0
- package/dist/chunk-75242WAX.js +711 -0
- package/dist/chunk-75242WAX.js.map +1 -0
- package/dist/chunk-ADCTONP6.js +709 -0
- package/dist/chunk-ADCTONP6.js.map +1 -0
- package/dist/chunk-AM2Y662I.js +601 -0
- package/dist/chunk-AM2Y662I.js.map +1 -0
- package/dist/chunk-BFJSBJHA.js +709 -0
- package/dist/chunk-BFJSBJHA.js.map +1 -0
- package/dist/chunk-BLLLGPVG.mjs +277 -0
- package/dist/chunk-BLLLGPVG.mjs.map +1 -0
- package/dist/chunk-EKLYHP2D.mjs +711 -0
- package/dist/chunk-EKLYHP2D.mjs.map +1 -0
- package/dist/chunk-FZ4INHWE.mjs +707 -0
- package/dist/chunk-FZ4INHWE.mjs.map +1 -0
- package/dist/chunk-G7PH56KW.mjs +274 -0
- package/dist/chunk-G7PH56KW.mjs.map +1 -0
- package/dist/chunk-GB3H3I47.js +711 -0
- package/dist/chunk-GB3H3I47.js.map +1 -0
- package/dist/chunk-HMPKCLIJ.mjs +709 -0
- package/dist/chunk-HMPKCLIJ.mjs.map +1 -0
- package/dist/chunk-IENACY5A.js +116 -0
- package/dist/chunk-IENACY5A.js.map +1 -0
- package/dist/chunk-J5KMPZIV.mjs +708 -0
- package/dist/chunk-J5KMPZIV.mjs.map +1 -0
- package/dist/chunk-J7FWSTAL.js +711 -0
- package/dist/chunk-J7FWSTAL.js.map +1 -0
- package/dist/chunk-JEOPLLWO.js +223 -0
- package/dist/chunk-JEOPLLWO.js.map +1 -0
- package/dist/chunk-KBHDXIAM.js +711 -0
- package/dist/chunk-KBHDXIAM.js.map +1 -0
- package/dist/chunk-M7QA57W3.mjs +711 -0
- package/dist/chunk-M7QA57W3.mjs.map +1 -0
- package/dist/chunk-MXAJ6OFR.js +168 -0
- package/dist/chunk-MXAJ6OFR.js.map +1 -0
- package/dist/chunk-NSAO2ERW.js +708 -0
- package/dist/chunk-NSAO2ERW.js.map +1 -0
- package/dist/chunk-NXGNAFNY.js +708 -0
- package/dist/chunk-NXGNAFNY.js.map +1 -0
- package/dist/chunk-ON4OH5OM.mjs +172 -0
- package/dist/chunk-ON4OH5OM.mjs.map +1 -0
- package/dist/chunk-OXXUQ36U.mjs +283 -0
- package/dist/chunk-OXXUQ36U.mjs.map +1 -0
- package/dist/chunk-PS5WST7W.mjs +711 -0
- package/dist/chunk-PS5WST7W.mjs.map +1 -0
- package/dist/chunk-Q7DSPTUG.mjs +601 -0
- package/dist/chunk-Q7DSPTUG.mjs.map +1 -0
- package/dist/chunk-RCFPLIWS.js +707 -0
- package/dist/chunk-RCFPLIWS.js.map +1 -0
- package/dist/chunk-RIHMMI3P.mjs +116 -0
- package/dist/chunk-RIHMMI3P.mjs.map +1 -0
- package/dist/chunk-RMN6R4VP.mjs +708 -0
- package/dist/chunk-RMN6R4VP.mjs.map +1 -0
- package/dist/chunk-SJ6NSD2E.js +709 -0
- package/dist/chunk-SJ6NSD2E.js.map +1 -0
- package/dist/chunk-SYJZGEFV.mjs +709 -0
- package/dist/chunk-SYJZGEFV.mjs.map +1 -0
- package/dist/chunk-TH6FI2XI.js +283 -0
- package/dist/chunk-TH6FI2XI.js.map +1 -0
- package/dist/chunk-UBO6RIOZ.mjs +709 -0
- package/dist/chunk-UBO6RIOZ.mjs.map +1 -0
- package/dist/chunk-UVRXIVK3.mjs +173 -0
- package/dist/chunk-UVRXIVK3.mjs.map +1 -0
- package/dist/chunk-WC2OMEHO.mjs +711 -0
- package/dist/chunk-WC2OMEHO.mjs.map +1 -0
- package/dist/chunk-WVG3PNQ6.js +708 -0
- package/dist/chunk-WVG3PNQ6.js.map +1 -0
- package/dist/chunk-WYA7Q4IM.mjs +708 -0
- package/dist/chunk-WYA7Q4IM.mjs.map +1 -0
- package/dist/chunk-X7YY6SHZ.js +274 -0
- package/dist/chunk-X7YY6SHZ.js.map +1 -0
- package/dist/chunk-ZXNMEKUE.js +173 -0
- package/dist/chunk-ZXNMEKUE.js.map +1 -0
- package/dist/index.css +10 -2
- package/dist/index.css.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/nextjs/client.css +10 -2
- package/dist/nextjs/client.css.map +1 -1
- package/dist/nextjs/client.d.mts +3 -5
- package/dist/nextjs/client.d.ts +3 -5
- package/dist/nextjs/client.js +25 -19
- package/dist/nextjs/client.js.map +1 -1
- package/dist/nextjs/client.mjs +21 -15
- package/dist/nextjs/client.mjs.map +1 -1
- package/dist/nextjs.d.mts +5 -9
- package/dist/nextjs.d.ts +5 -9
- package/dist/nextjs.js +63 -55
- package/dist/nextjs.js.map +1 -1
- package/dist/nextjs.mjs +40 -32
- package/dist/nextjs.mjs.map +1 -1
- package/dist/react.d.mts +5 -9
- package/dist/react.d.ts +5 -9
- package/dist/react.js +11 -23
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +2 -14
- package/dist/react.mjs.map +1 -1
- package/dist/server.d.mts +2 -2
- package/dist/server.d.ts +2 -2
- package/dist/server.js +3 -3
- package/dist/server.mjs +2 -2
- package/dist/storage-ANmRwpZ3.d.ts +25 -0
- package/dist/storage-BJyqsZwC.d.mts +25 -0
- package/dist/types-BxAubCqO.d.mts +58 -0
- package/dist/types-BxAubCqO.d.ts +58 -0
- package/dist/types-DOfl9w7j.d.mts +23 -0
- package/dist/types-DOfl9w7j.d.ts +23 -0
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { C as Config, S as SessionData } from './types-BxAubCqO.mjs';
|
|
3
|
+
|
|
4
|
+
interface PKCEConsumer {
|
|
5
|
+
getCodeChallenge(): Promise<string>;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
type AuthProviderProps = {
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
clientId: string;
|
|
11
|
+
redirectUrl?: string;
|
|
12
|
+
nonce?: string;
|
|
13
|
+
config?: Config;
|
|
14
|
+
onSignIn?: (error?: Error) => void;
|
|
15
|
+
onSignOut?: () => Promise<void>;
|
|
16
|
+
pkceConsumer?: PKCEConsumer;
|
|
17
|
+
modalIframe?: boolean;
|
|
18
|
+
sessionData?: SessionData;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type { AuthProviderProps as A };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { C as Config } from './types-BxAubCqO.mjs';
|
|
3
|
+
|
|
4
|
+
type AuthProviderProps = {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
clientId: string;
|
|
7
|
+
nonce?: string;
|
|
8
|
+
onSignIn?: (error?: Error) => void;
|
|
9
|
+
onSignOut?: () => Promise<void>;
|
|
10
|
+
modalIframe?: boolean;
|
|
11
|
+
config?: Config;
|
|
12
|
+
redirectUrl?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type { AuthProviderProps as A };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { C as Config } from './types-BxAubCqO.js';
|
|
3
|
+
|
|
4
|
+
type AuthProviderProps = {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
clientId: string;
|
|
7
|
+
nonce?: string;
|
|
8
|
+
onSignIn?: (error?: Error) => void;
|
|
9
|
+
onSignOut?: () => Promise<void>;
|
|
10
|
+
modalIframe?: boolean;
|
|
11
|
+
config?: Config;
|
|
12
|
+
redirectUrl?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type { AuthProviderProps as A };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { C as Config, S as SessionData } from './types-BxAubCqO.js';
|
|
3
|
+
|
|
4
|
+
interface PKCEConsumer {
|
|
5
|
+
getCodeChallenge(): Promise<string>;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
type AuthProviderProps = {
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
clientId: string;
|
|
11
|
+
redirectUrl?: string;
|
|
12
|
+
nonce?: string;
|
|
13
|
+
config?: Config;
|
|
14
|
+
onSignIn?: (error?: Error) => void;
|
|
15
|
+
onSignOut?: () => Promise<void>;
|
|
16
|
+
pkceConsumer?: PKCEConsumer;
|
|
17
|
+
modalIframe?: boolean;
|
|
18
|
+
sessionData?: SessionData;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type { AuthProviderProps as A };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { JWT } from 'oslo/jwt';
|
|
2
|
+
import { D as DisplayMode, U as User } from './types-BxAubCqO.js';
|
|
3
|
+
|
|
4
|
+
type AuthContextType = {
|
|
5
|
+
signIn: (displayMode?: DisplayMode) => Promise<void>;
|
|
6
|
+
isAuthenticated: boolean;
|
|
7
|
+
isLoading: boolean;
|
|
8
|
+
error: Error | null;
|
|
9
|
+
signOut: () => Promise<void>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type UserContextType<T extends Record<string, unknown> & JWT["payload"] = Record<string, unknown> & JWT["payload"]> = {
|
|
13
|
+
user: User<T> | null;
|
|
14
|
+
} & Omit<AuthContextType, "isAuthenticated">;
|
|
15
|
+
|
|
16
|
+
export type { AuthContextType as A, UserContextType as U };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { JWT } from 'oslo/jwt';
|
|
2
|
+
import { D as DisplayMode, U as User } from './types-BxAubCqO.mjs';
|
|
3
|
+
|
|
4
|
+
type AuthContextType = {
|
|
5
|
+
signIn: (displayMode?: DisplayMode) => Promise<void>;
|
|
6
|
+
isAuthenticated: boolean;
|
|
7
|
+
isLoading: boolean;
|
|
8
|
+
error: Error | null;
|
|
9
|
+
signOut: () => Promise<void>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type UserContextType<T extends Record<string, unknown> & JWT["payload"] = Record<string, unknown> & JWT["payload"]> = {
|
|
13
|
+
user: User<T> | null;
|
|
14
|
+
} & Omit<AuthContextType, "isAuthenticated">;
|
|
15
|
+
|
|
16
|
+
export type { AuthContextType as A, UserContextType as U };
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunk7K3QN2ATjs = require('./chunk-7K3QN2AT.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
var _chunkCRTRMMJ7js = require('./chunk-CRTRMMJ7.js');
|
|
9
|
+
|
|
10
|
+
// src/lib/logger.ts
|
|
11
|
+
var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug);
|
|
12
|
+
var PACKAGE_NAME = "@civic/auth";
|
|
13
|
+
var DebugLogger = class {
|
|
14
|
+
constructor(namespace) {
|
|
15
|
+
this.debugLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:debug`);
|
|
16
|
+
this.infoLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:info`);
|
|
17
|
+
this.warnLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:warn`);
|
|
18
|
+
this.errorLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:error`);
|
|
19
|
+
this.debugLogger.color = "4";
|
|
20
|
+
this.infoLogger.color = "2";
|
|
21
|
+
this.warnLogger.color = "3";
|
|
22
|
+
this.errorLogger.color = "1";
|
|
23
|
+
}
|
|
24
|
+
debug(message, ...args) {
|
|
25
|
+
this.debugLogger(message, ...args);
|
|
26
|
+
}
|
|
27
|
+
info(message, ...args) {
|
|
28
|
+
this.infoLogger(message, ...args);
|
|
29
|
+
}
|
|
30
|
+
warn(message, ...args) {
|
|
31
|
+
this.warnLogger(message, ...args);
|
|
32
|
+
}
|
|
33
|
+
error(message, ...args) {
|
|
34
|
+
this.errorLogger(message, ...args);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var createLogger = (namespace) => new DebugLogger(namespace);
|
|
38
|
+
var loggers = {
|
|
39
|
+
// Next.js specific loggers
|
|
40
|
+
nextjs: {
|
|
41
|
+
routes: createLogger("api:routes"),
|
|
42
|
+
middleware: createLogger("api:middleware"),
|
|
43
|
+
handlers: {
|
|
44
|
+
auth: createLogger("api:handlers:auth")
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
// React specific loggers
|
|
48
|
+
react: {
|
|
49
|
+
components: createLogger("react:components"),
|
|
50
|
+
hooks: createLogger("react:hooks"),
|
|
51
|
+
context: createLogger("react:context")
|
|
52
|
+
},
|
|
53
|
+
// Shared utilities loggers
|
|
54
|
+
services: {
|
|
55
|
+
validation: createLogger("utils:validation"),
|
|
56
|
+
network: createLogger("utils:network")
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/nextjs/config.ts
|
|
61
|
+
var logger = loggers.nextjs.handlers.auth;
|
|
62
|
+
var _a;
|
|
63
|
+
var appUrl = typeof process !== "undefined" ? ((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NEXT_PUBLIC_APP_URL) || "" : "";
|
|
64
|
+
var defaultServerSecure = !appUrl.startsWith("http://localhost");
|
|
65
|
+
var defaultAuthConfig = {
|
|
66
|
+
oauthServer: _chunk7K3QN2ATjs.DEFAULT_AUTH_SERVER,
|
|
67
|
+
callbackUrl: "/api/auth/callback",
|
|
68
|
+
challengeUrl: "/api/auth/challenge",
|
|
69
|
+
logoutUrl: "/api/auth/logout",
|
|
70
|
+
loginUrl: "/",
|
|
71
|
+
include: ["/*"],
|
|
72
|
+
exclude: [],
|
|
73
|
+
cookies: {
|
|
74
|
+
tokens: {
|
|
75
|
+
["id_token" /* ID_TOKEN */]: {
|
|
76
|
+
secure: defaultServerSecure,
|
|
77
|
+
httpOnly: true,
|
|
78
|
+
sameSite: "strict",
|
|
79
|
+
path: "/"
|
|
80
|
+
},
|
|
81
|
+
["access_token" /* ACCESS_TOKEN */]: {
|
|
82
|
+
secure: defaultServerSecure,
|
|
83
|
+
httpOnly: true,
|
|
84
|
+
sameSite: "strict",
|
|
85
|
+
path: "/"
|
|
86
|
+
},
|
|
87
|
+
["refresh_token" /* REFRESH_TOKEN */]: {
|
|
88
|
+
secure: defaultServerSecure,
|
|
89
|
+
httpOnly: true,
|
|
90
|
+
sameSite: "strict",
|
|
91
|
+
path: "/"
|
|
92
|
+
},
|
|
93
|
+
["code_verifier" /* COOKIE_NAME */]: {
|
|
94
|
+
secure: defaultServerSecure,
|
|
95
|
+
httpOnly: true,
|
|
96
|
+
sameSite: "strict",
|
|
97
|
+
path: "/"
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
user: {
|
|
101
|
+
secure: defaultServerSecure,
|
|
102
|
+
httpOnly: false,
|
|
103
|
+
sameSite: "strict",
|
|
104
|
+
path: "/",
|
|
105
|
+
maxAge: 60 * 60
|
|
106
|
+
// 1 hour
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var resolveAuthConfig = (config = {}) => {
|
|
111
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
112
|
+
const configFromEnv = _chunk7K3QN2ATjs.withoutUndefined.call(void 0, {
|
|
113
|
+
clientId: process.env._civic_auth_client_id,
|
|
114
|
+
oauthServer: process.env._civic_oauth_server,
|
|
115
|
+
callbackUrl: process.env._civic_auth_callback_url,
|
|
116
|
+
challengeUrl: process.env._civic_auth_challenge_url,
|
|
117
|
+
loginUrl: process.env._civic_auth_login_url,
|
|
118
|
+
appUrl: process.env._civic_auth_app_url,
|
|
119
|
+
logoutUrl: process.env._civic_auth_logout_url,
|
|
120
|
+
include: (_a2 = process.env._civic_auth_includes) == null ? void 0 : _a2.split(","),
|
|
121
|
+
exclude: (_b = process.env._civic_auth_excludes) == null ? void 0 : _b.split(","),
|
|
122
|
+
cookies: process.env._civic_auth_cookie_config ? JSON.parse(process.env._civic_auth_cookie_config) : void 0
|
|
123
|
+
});
|
|
124
|
+
const mergedConfig = _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig), configFromEnv), config), {
|
|
125
|
+
// Override with directly passed config
|
|
126
|
+
cookies: {
|
|
127
|
+
tokens: _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig.cookies.tokens), ((_c = configFromEnv == null ? void 0 : configFromEnv.cookies) == null ? void 0 : _c.tokens) || {}), ((_d = config.cookies) == null ? void 0 : _d.tokens) || {}),
|
|
128
|
+
user: _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig.cookies.user), ((_e = configFromEnv == null ? void 0 : configFromEnv.cookies) == null ? void 0 : _e.user) || {}), ((_f = config.cookies) == null ? void 0 : _f.user) || {})
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
logger.debug(
|
|
132
|
+
"Config from environment:",
|
|
133
|
+
JSON.stringify(configFromEnv, null, 2)
|
|
134
|
+
);
|
|
135
|
+
logger.debug("Resolved config:", JSON.stringify(mergedConfig, null, 2));
|
|
136
|
+
if (mergedConfig.clientId === void 0) {
|
|
137
|
+
throw new Error("Civic Auth client ID is required");
|
|
138
|
+
}
|
|
139
|
+
return mergedConfig;
|
|
140
|
+
};
|
|
141
|
+
var createCivicAuthPlugin = (authConfig) => {
|
|
142
|
+
return (nextConfig) => {
|
|
143
|
+
logger.debug(
|
|
144
|
+
"createCivicAuthPlugin nextConfig",
|
|
145
|
+
JSON.stringify(nextConfig, null, 2)
|
|
146
|
+
);
|
|
147
|
+
const resolvedConfig = resolveAuthConfig(_chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, authConfig));
|
|
148
|
+
return _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, nextConfig), {
|
|
149
|
+
env: _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, nextConfig == null ? void 0 : nextConfig.env), {
|
|
150
|
+
// Internal environment variables - do not set these manually
|
|
151
|
+
_civic_auth_client_id: resolvedConfig.clientId,
|
|
152
|
+
_civic_oauth_server: resolvedConfig.oauthServer,
|
|
153
|
+
_civic_auth_callback_url: resolvedConfig.callbackUrl,
|
|
154
|
+
_civic_auth_challenge_url: resolvedConfig.challengeUrl,
|
|
155
|
+
_civic_auth_login_url: resolvedConfig.loginUrl,
|
|
156
|
+
_civic_auth_logout_url: resolvedConfig.logoutUrl,
|
|
157
|
+
_civic_auth_app_url: resolvedConfig.appUrl,
|
|
158
|
+
_civic_auth_includes: resolvedConfig.include.join(","),
|
|
159
|
+
_civic_auth_excludes: resolvedConfig.exclude.join(","),
|
|
160
|
+
_civic_auth_cookie_config: JSON.stringify(resolvedConfig.cookies)
|
|
161
|
+
})
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
exports.loggers = loggers; exports.defaultAuthConfig = defaultAuthConfig; exports.resolveAuthConfig = resolveAuthConfig; exports.createCivicAuthPlugin = createCivicAuthPlugin;
|
|
172
|
+
//# sourceMappingURL=chunk-2OZJONNO.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/lucas/dev/civic/civic-auth/packages/civic-auth-client/dist/chunk-2OZJONNO.js","../src/lib/logger.ts","../src/nextjs/config.ts"],"names":[],"mappings":"AAAA;AACE;AACA;AACF,sDAA4B;AAC5B;AACE;AACA;AACF,sDAA4B;AAC5B;AACA;ACTA,4EAAkB;AAElB,IAAM,aAAA,EAAe,aAAA;AASrB,IAAM,YAAA,EAAN,MAAoC;AAAA,EAMlC,WAAA,CAAY,SAAA,EAAmB;AAE7B,IAAA,IAAA,CAAK,YAAA,EAAc,6BAAA,CAAM,EAAA;AACD,IAAA;AACA,IAAA;AACC,IAAA;AAEA,IAAA;AACD,IAAA;AACA,IAAA;AACC,IAAA;AAC3B,EAAA;AAEiD,EAAA;AACrB,IAAA;AAC5B,EAAA;AAEgD,EAAA;AACrB,IAAA;AAC3B,EAAA;AAEgD,EAAA;AACrB,IAAA;AAC3B,EAAA;AAEiD,EAAA;AACrB,IAAA;AAC5B,EAAA;AACF;AAE6B;AAIN;AAAA;AAEb,EAAA;AACe,IAAA;AACI,IAAA;AACf,IAAA;AACW,MAAA;AACrB,IAAA;AACF,EAAA;AAAA;AAEO,EAAA;AACoB,IAAA;AACL,IAAA;AACE,IAAA;AACxB,EAAA;AAAA;AAEU,EAAA;AACiB,IAAA;AACH,IAAA;AACxB,EAAA;AACF;ADb8B;AACA;AE/CA;AAZ9B;AAqCS;AACoB;AAK8C;AAC5D,EAAA;AACA,EAAA;AACC,EAAA;AACH,EAAA;AACD,EAAA;AACI,EAAA;AACJ,EAAA;AACD,EAAA;AACC,IAAA;AACN,MAAA;AACU,QAAA;AACE,QAAA;AACA,QAAA;AACJ,QAAA;AACR,MAAA;AACA,MAAA;AACU,QAAA;AACE,QAAA;AACA,QAAA;AACJ,QAAA;AACR,MAAA;AACA,MAAA;AACU,QAAA;AACE,QAAA;AACA,QAAA;AACJ,QAAA;AACR,MAAA;AACA,MAAA;AACU,QAAA;AACE,QAAA;AACA,QAAA;AACJ,QAAA;AACR,MAAA;AACF,IAAA;AACM,IAAA;AACI,MAAA;AACE,MAAA;AACA,MAAA;AACJ,MAAA;AACO,MAAA;AAAA;AACf,IAAA;AACF,EAAA;AACF;AAoBE;AA1GF,EAAA;AA6GwB,EAAA;AACE,IAAA;AACG,IAAA;AACA,IAAA;AACC,IAAA;AACJ,IAAA;AACF,IAAA;AACG,IAAA;AACN,IAAA;AACA,IAAA;AACI,IAAA;AAGtB,EAAA;AACoB,EAAA;AAAA;AAIV,IAAA;AACC,MAAA;AAKF,MAAA;AAKR,IAAA;AACF,EAAA;AAEO,EAAA;AACL,IAAA;AACe,IAAA;AACjB,EAAA;AACa,EAAA;AACI,EAAA;AACC,IAAA;AAClB,EAAA;AACO,EAAA;AACT;AA0BE;AAEoC,EAAA;AAC3B,IAAA;AACL,MAAA;AACe,MAAA;AACjB,IAAA;AACuB,IAAA;AAChB,IAAA;AAEA,MAAA;AAAA;AAGH,QAAA;AACqB,QAAA;AACrB,QAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACqB,QAAA;AACC,QAAA;AACA,QAAA;AACtB,QAAA;AACF,MAAA;AACF,IAAA;AACF,EAAA;AACF;AFtC8B;AACA;AACA;AACA;AACA;AACA;AACA","file":"/Users/lucas/dev/civic/civic-auth/packages/civic-auth-client/dist/chunk-2OZJONNO.js","sourcesContent":[null,"import debug from \"debug\";\n\nconst PACKAGE_NAME = \"@civic/auth\";\n\nexport interface Logger {\n debug(message: string, ...args: unknown[]): void;\n info(message: string, ...args: unknown[]): void;\n warn(message: string, ...args: unknown[]): void;\n error(message: string, ...args: unknown[]): void;\n}\n\nclass DebugLogger implements Logger {\n private debugLogger: debug.Debugger;\n private infoLogger: debug.Debugger;\n private warnLogger: debug.Debugger;\n private errorLogger: debug.Debugger;\n\n constructor(namespace: string) {\n // Format: @org/package:library:component:level\n this.debugLogger = debug(`${PACKAGE_NAME}:${namespace}:debug`);\n this.infoLogger = debug(`${PACKAGE_NAME}:${namespace}:info`);\n this.warnLogger = debug(`${PACKAGE_NAME}:${namespace}:warn`);\n this.errorLogger = debug(`${PACKAGE_NAME}:${namespace}:error`);\n\n this.debugLogger.color = \"4\";\n this.infoLogger.color = \"2\";\n this.warnLogger.color = \"3\";\n this.errorLogger.color = \"1\";\n }\n\n debug(message: string, ...args: unknown[]): void {\n this.debugLogger(message, ...args);\n }\n\n info(message: string, ...args: unknown[]): void {\n this.infoLogger(message, ...args);\n }\n\n warn(message: string, ...args: unknown[]): void {\n this.warnLogger(message, ...args);\n }\n\n error(message: string, ...args: unknown[]): void {\n this.errorLogger(message, ...args);\n }\n}\n\nexport const createLogger = (namespace: string): Logger =>\n new DebugLogger(namespace);\n\n// Pre-configured loggers for different parts of your package\nexport const loggers = {\n // Next.js specific loggers\n nextjs: {\n routes: createLogger(\"api:routes\"),\n middleware: createLogger(\"api:middleware\"),\n handlers: {\n auth: createLogger(\"api:handlers:auth\"),\n },\n },\n // React specific loggers\n react: {\n components: createLogger(\"react:components\"),\n hooks: createLogger(\"react:hooks\"),\n context: createLogger(\"react:context\"),\n },\n // Shared utilities loggers\n services: {\n validation: createLogger(\"utils:validation\"),\n network: createLogger(\"utils:network\"),\n },\n} as const;\n","/* eslint-disable turbo/no-undeclared-env-vars */\nimport { NextConfig } from \"next\";\nimport { loggers } from \"@/lib/logger\";\nimport { withoutUndefined } from \"@/utils\";\nimport {\n CodeVerifier,\n CookieConfig,\n OAuthTokens,\n TokensCookieConfig,\n} from \"@/shared/types\";\nimport { DEFAULT_AUTH_SERVER } from \"@/constants\";\n\nconst logger = loggers.nextjs.handlers.auth;\n\nexport type CookiesConfigObject = {\n tokens: TokensCookieConfig;\n user: CookieConfig;\n};\n\nexport type AuthConfigWithDefaults = {\n clientId: string;\n oauthServer: string;\n callbackUrl: string;\n loginUrl: string;\n logoutUrl: string;\n appUrl?: string;\n challengeUrl: string;\n include: string[];\n exclude: string[];\n cookies: CookiesConfigObject;\n};\n\nexport type AuthConfig = Partial<AuthConfigWithDefaults>;\n\nexport type DefinedAuthConfig = AuthConfigWithDefaults;\n\nconst appUrl =\n typeof process !== \"undefined\" ? process?.env?.NEXT_PUBLIC_APP_URL || \"\" : \"\";\nconst defaultServerSecure = !appUrl.startsWith(\"http://localhost\");\n\n/**\n * Default configuration values that will be used if not overridden\n */\nexport const defaultAuthConfig: Omit<AuthConfigWithDefaults, \"clientId\"> = {\n oauthServer: DEFAULT_AUTH_SERVER,\n callbackUrl: \"/api/auth/callback\",\n challengeUrl: \"/api/auth/challenge\",\n logoutUrl: \"/api/auth/logout\",\n loginUrl: \"/\",\n include: [\"/*\"],\n exclude: [],\n cookies: {\n tokens: {\n [OAuthTokens.ID_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [OAuthTokens.ACCESS_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [OAuthTokens.REFRESH_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [CodeVerifier.COOKIE_NAME]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n },\n user: {\n secure: defaultServerSecure,\n httpOnly: false,\n sameSite: \"strict\",\n path: \"/\",\n maxAge: 60 * 60, // 1 hour\n },\n },\n};\n\n/**\n * Resolves the authentication configuration by combining:\n * 1. Default values\n * 2. Environment variables (set internally by the plugin)\n * 3. Explicitly passed configuration\n *\n * Note: Developers should not set _civic_auth_* environment variables directly.\n * Instead, pass configuration to the createCivicAuthPlugin in next.config.js:\n *\n * @example\n * ```js\n * // next.config.js\n * export default createCivicAuthPlugin({\n * callbackUrl: '/custom/callback',\n * })\n * ```\n */\nexport const resolveAuthConfig = (\n config: AuthConfig = {},\n): AuthConfigWithDefaults => {\n // Read configuration that was set by the plugin via environment variables\n const configFromEnv = withoutUndefined({\n clientId: process.env._civic_auth_client_id,\n oauthServer: process.env._civic_oauth_server,\n callbackUrl: process.env._civic_auth_callback_url,\n challengeUrl: process.env._civic_auth_challenge_url,\n loginUrl: process.env._civic_auth_login_url,\n appUrl: process.env._civic_auth_app_url,\n logoutUrl: process.env._civic_auth_logout_url,\n include: process.env._civic_auth_includes?.split(\",\"),\n exclude: process.env._civic_auth_excludes?.split(\",\"),\n cookies: process.env._civic_auth_cookie_config\n ? JSON.parse(process.env._civic_auth_cookie_config)\n : undefined,\n }) as AuthConfig;\n const mergedConfig = {\n ...defaultAuthConfig,\n ...configFromEnv, // Apply plugin-set config\n ...config, // Override with directly passed config\n cookies: {\n tokens: {\n ...defaultAuthConfig.cookies.tokens,\n ...(configFromEnv?.cookies?.tokens || {}),\n ...(config.cookies?.tokens || {}),\n },\n user: {\n ...defaultAuthConfig.cookies.user,\n ...(configFromEnv?.cookies?.user || {}),\n ...(config.cookies?.user || {}),\n },\n },\n };\n\n logger.debug(\n \"Config from environment:\",\n JSON.stringify(configFromEnv, null, 2),\n );\n logger.debug(\"Resolved config:\", JSON.stringify(mergedConfig, null, 2));\n if (mergedConfig.clientId === undefined) {\n throw new Error(\"Civic Auth client ID is required\");\n }\n return mergedConfig as AuthConfigWithDefaults & { clientId: string };\n};\n\n/**\n * Creates a Next.js plugin that handles auth configuration.\n *\n * This is the main configuration point for the auth system.\n * Do not set _civic_auth_* environment variables directly - instead,\n * pass your configuration here:\n *\n * @example\n * ```js\n * // next.config.js\n * export default createCivicAuthPlugin({\n * clientId: 'my-client-id',\n * callbackUrl: '/custom/callback',\n * loginUrl: '/custom/login',\n * logoutUrl: '/custom/logout',\n * include: ['/protected/*'],\n * exclude: ['/public/*']\n * })\n * ```\n *\n * The plugin sets internal environment variables that are used by\n * the auth system. These variables should not be set manually.\n */\nexport const createCivicAuthPlugin = (\n authConfig: AuthConfig & Pick<Required<AuthConfig>, \"clientId\">,\n) => {\n return (nextConfig?: NextConfig) => {\n logger.debug(\n \"createCivicAuthPlugin nextConfig\",\n JSON.stringify(nextConfig, null, 2),\n );\n const resolvedConfig = resolveAuthConfig({ ...authConfig });\n return {\n ...nextConfig,\n env: {\n ...nextConfig?.env,\n // Internal environment variables - do not set these manually\n _civic_auth_client_id: resolvedConfig.clientId,\n _civic_oauth_server: resolvedConfig.oauthServer,\n _civic_auth_callback_url: resolvedConfig.callbackUrl,\n _civic_auth_challenge_url: resolvedConfig.challengeUrl,\n _civic_auth_login_url: resolvedConfig.loginUrl,\n _civic_auth_logout_url: resolvedConfig.logoutUrl,\n _civic_auth_app_url: resolvedConfig.appUrl,\n _civic_auth_includes: resolvedConfig.include.join(\",\"),\n _civic_auth_excludes: resolvedConfig.exclude.join(\",\"),\n _civic_auth_cookie_config: JSON.stringify(resolvedConfig.cookies),\n },\n };\n };\n};\n"]}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
|
+
|
|
3
|
+
var _chunk6RFRDWIPjs = require('./chunk-6RFRDWIP.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
var _chunk7K3QN2ATjs = require('./chunk-7K3QN2AT.js');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
var _chunkCRTRMMJ7js = require('./chunk-CRTRMMJ7.js');
|
|
15
|
+
|
|
16
|
+
// src/lib/logger.ts
|
|
17
|
+
var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug);
|
|
18
|
+
var PACKAGE_NAME = "@civic/auth";
|
|
19
|
+
var DebugLogger = class {
|
|
20
|
+
constructor(namespace) {
|
|
21
|
+
this.debugLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:debug`);
|
|
22
|
+
this.infoLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:info`);
|
|
23
|
+
this.warnLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:warn`);
|
|
24
|
+
this.errorLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:error`);
|
|
25
|
+
this.debugLogger.color = "4";
|
|
26
|
+
this.infoLogger.color = "2";
|
|
27
|
+
this.warnLogger.color = "3";
|
|
28
|
+
this.errorLogger.color = "1";
|
|
29
|
+
}
|
|
30
|
+
debug(message, ...args) {
|
|
31
|
+
this.debugLogger(message, ...args);
|
|
32
|
+
}
|
|
33
|
+
info(message, ...args) {
|
|
34
|
+
this.infoLogger(message, ...args);
|
|
35
|
+
}
|
|
36
|
+
warn(message, ...args) {
|
|
37
|
+
this.warnLogger(message, ...args);
|
|
38
|
+
}
|
|
39
|
+
error(message, ...args) {
|
|
40
|
+
this.errorLogger(message, ...args);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
var createLogger = (namespace) => new DebugLogger(namespace);
|
|
44
|
+
var loggers = {
|
|
45
|
+
// Next.js specific loggers
|
|
46
|
+
nextjs: {
|
|
47
|
+
routes: createLogger("api:routes"),
|
|
48
|
+
middleware: createLogger("api:middleware"),
|
|
49
|
+
handlers: {
|
|
50
|
+
auth: createLogger("api:handlers:auth")
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
// React specific loggers
|
|
54
|
+
react: {
|
|
55
|
+
components: createLogger("react:components"),
|
|
56
|
+
hooks: createLogger("react:hooks"),
|
|
57
|
+
context: createLogger("react:context")
|
|
58
|
+
},
|
|
59
|
+
// Shared utilities loggers
|
|
60
|
+
services: {
|
|
61
|
+
validation: createLogger("utils:validation"),
|
|
62
|
+
network: createLogger("utils:network")
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/nextjs/config.ts
|
|
67
|
+
var logger = loggers.nextjs.handlers.auth;
|
|
68
|
+
console.log(`process.env.NODE_ENV: ${process.env.NODE_ENV}`);
|
|
69
|
+
var isDevelopment = process.env.NODE_ENV === "development";
|
|
70
|
+
var defaultServerSecure = isDevelopment ? false : true;
|
|
71
|
+
console.log(`defaultServerSecure: ${defaultServerSecure}`);
|
|
72
|
+
var defaultAuthConfig = {
|
|
73
|
+
oauthServer: _chunk7K3QN2ATjs.DEFAULT_AUTH_SERVER,
|
|
74
|
+
callbackUrl: "/api/auth/callback",
|
|
75
|
+
challengeUrl: "/api/auth/challenge",
|
|
76
|
+
logoutUrl: "/api/auth/logout",
|
|
77
|
+
loginUrl: "/",
|
|
78
|
+
include: ["/*"],
|
|
79
|
+
exclude: [],
|
|
80
|
+
cookies: {
|
|
81
|
+
tokens: {
|
|
82
|
+
["id_token" /* ID_TOKEN */]: {
|
|
83
|
+
secure: defaultServerSecure,
|
|
84
|
+
httpOnly: true,
|
|
85
|
+
sameSite: "strict",
|
|
86
|
+
path: "/"
|
|
87
|
+
},
|
|
88
|
+
["access_token" /* ACCESS_TOKEN */]: {
|
|
89
|
+
secure: defaultServerSecure,
|
|
90
|
+
httpOnly: true,
|
|
91
|
+
sameSite: "strict",
|
|
92
|
+
path: "/"
|
|
93
|
+
},
|
|
94
|
+
["refresh_token" /* REFRESH_TOKEN */]: {
|
|
95
|
+
secure: defaultServerSecure,
|
|
96
|
+
httpOnly: true,
|
|
97
|
+
sameSite: "strict",
|
|
98
|
+
path: "/"
|
|
99
|
+
},
|
|
100
|
+
["code_verifier" /* COOKIE_NAME */]: {
|
|
101
|
+
secure: defaultServerSecure,
|
|
102
|
+
httpOnly: true,
|
|
103
|
+
sameSite: "strict",
|
|
104
|
+
path: "/"
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
user: {
|
|
108
|
+
secure: defaultServerSecure,
|
|
109
|
+
httpOnly: false,
|
|
110
|
+
sameSite: "strict",
|
|
111
|
+
path: "/",
|
|
112
|
+
maxAge: 60 * 60
|
|
113
|
+
// 1 hour
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
var resolveAuthConfig = (config = {}) => {
|
|
118
|
+
var _a, _b, _c, _d, _e, _f;
|
|
119
|
+
const configFromEnv = _chunk7K3QN2ATjs.withoutUndefined.call(void 0, {
|
|
120
|
+
clientId: process.env._civic_auth_client_id,
|
|
121
|
+
oauthServer: process.env._civic_oauth_server,
|
|
122
|
+
callbackUrl: process.env._civic_auth_callback_url,
|
|
123
|
+
challengeUrl: process.env._civic_auth_challenge_url,
|
|
124
|
+
loginUrl: process.env._civic_auth_login_url,
|
|
125
|
+
logoutUrl: process.env._civic_auth_logout_url,
|
|
126
|
+
include: (_a = process.env._civic_auth_includes) == null ? void 0 : _a.split(","),
|
|
127
|
+
exclude: (_b = process.env._civic_auth_excludes) == null ? void 0 : _b.split(","),
|
|
128
|
+
cookies: process.env._civic_auth_cookie_config ? JSON.parse(process.env._civic_auth_cookie_config) : void 0
|
|
129
|
+
});
|
|
130
|
+
const mergedConfig = _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig), configFromEnv), config), {
|
|
131
|
+
// Override with directly passed config
|
|
132
|
+
cookies: {
|
|
133
|
+
tokens: _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig.cookies.tokens), ((_c = configFromEnv == null ? void 0 : configFromEnv.cookies) == null ? void 0 : _c.tokens) || {}), ((_d = config.cookies) == null ? void 0 : _d.tokens) || {}),
|
|
134
|
+
user: _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig.cookies.user), ((_e = configFromEnv == null ? void 0 : configFromEnv.cookies) == null ? void 0 : _e.user) || {}), ((_f = config.cookies) == null ? void 0 : _f.user) || {})
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
logger.debug(
|
|
138
|
+
"Config from environment:",
|
|
139
|
+
JSON.stringify(configFromEnv, null, 2)
|
|
140
|
+
);
|
|
141
|
+
logger.debug("Resolved config:", JSON.stringify(mergedConfig, null, 2));
|
|
142
|
+
if (mergedConfig.clientId === void 0) {
|
|
143
|
+
throw new Error("Civic Auth client ID is required");
|
|
144
|
+
}
|
|
145
|
+
return mergedConfig;
|
|
146
|
+
};
|
|
147
|
+
var createCivicAuthPlugin = (authConfig) => {
|
|
148
|
+
return (nextConfig) => {
|
|
149
|
+
logger.debug(
|
|
150
|
+
"createCivicAuthPlugin nextConfig",
|
|
151
|
+
JSON.stringify(nextConfig, null, 2)
|
|
152
|
+
);
|
|
153
|
+
const resolvedConfig = resolveAuthConfig(_chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, authConfig));
|
|
154
|
+
return _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, nextConfig), {
|
|
155
|
+
env: _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, nextConfig == null ? void 0 : nextConfig.env), {
|
|
156
|
+
// Internal environment variables - do not set these manually
|
|
157
|
+
_civic_auth_client_id: resolvedConfig.clientId,
|
|
158
|
+
_civic_oauth_server: resolvedConfig.oauthServer,
|
|
159
|
+
_civic_auth_callback_url: resolvedConfig.callbackUrl,
|
|
160
|
+
_civic_auth_challenge_url: resolvedConfig.challengeUrl,
|
|
161
|
+
_civic_auth_login_url: resolvedConfig.loginUrl,
|
|
162
|
+
_civic_auth_logout_url: resolvedConfig.logoutUrl,
|
|
163
|
+
_civic_auth_includes: resolvedConfig.include.join(","),
|
|
164
|
+
_civic_auth_excludes: resolvedConfig.exclude.join(","),
|
|
165
|
+
_civic_auth_cookie_config: JSON.stringify(resolvedConfig.cookies)
|
|
166
|
+
})
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// src/nextjs/cookies.ts
|
|
172
|
+
var _headersjs = require('next/headers.js');
|
|
173
|
+
var createTokenCookies = (response, sessionData, config) => {
|
|
174
|
+
var _a, _b;
|
|
175
|
+
const maxAge = (_a = sessionData.expiresIn) != null ? _a : 3600;
|
|
176
|
+
const cookieOptions = _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, (_b = config.cookies) == null ? void 0 : _b.tokens), {
|
|
177
|
+
maxAge
|
|
178
|
+
});
|
|
179
|
+
if (sessionData.accessToken) {
|
|
180
|
+
response.cookies.set("access_token", sessionData.accessToken, _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, cookieOptions), {
|
|
181
|
+
httpOnly: true
|
|
182
|
+
}));
|
|
183
|
+
}
|
|
184
|
+
if (sessionData.idToken) {
|
|
185
|
+
response.cookies.set("id_token", sessionData.idToken, _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, cookieOptions), {
|
|
186
|
+
httpOnly: true
|
|
187
|
+
}));
|
|
188
|
+
}
|
|
189
|
+
if (sessionData.refreshToken) {
|
|
190
|
+
response.cookies.set("refresh_token", sessionData.refreshToken, _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, cookieOptions), {
|
|
191
|
+
httpOnly: true
|
|
192
|
+
}));
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
var createUserInfoCookie = (response, user, sessionData, config) => {
|
|
196
|
+
var _a, _b, _c;
|
|
197
|
+
if (!user) {
|
|
198
|
+
response.cookies.set("user", "", _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, (_a = config.cookies) == null ? void 0 : _a.user), {
|
|
199
|
+
maxAge: 0
|
|
200
|
+
}));
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const maxAge = (_b = sessionData.expiresIn) != null ? _b : 3600;
|
|
204
|
+
const frontendUser = _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, user);
|
|
205
|
+
response.cookies.set("user", JSON.stringify(frontendUser), _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, (_c = config.cookies) == null ? void 0 : _c.user), {
|
|
206
|
+
maxAge
|
|
207
|
+
}));
|
|
208
|
+
};
|
|
209
|
+
var clearAuthCookies = (config) => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () {
|
|
210
|
+
var _a;
|
|
211
|
+
const cookieStorage = new NextjsCookieStorage((_a = config.cookies) == null ? void 0 : _a.tokens);
|
|
212
|
+
_chunk7K3QN2ATjs.clearTokens.call(void 0, cookieStorage);
|
|
213
|
+
const clientStorage = new NextjsClientStorage();
|
|
214
|
+
const userSession = new (0, _chunk7K3QN2ATjs.GenericUserSession)(clientStorage);
|
|
215
|
+
userSession.set(null);
|
|
216
|
+
});
|
|
217
|
+
var NextjsCookieStorage = class extends _chunk6RFRDWIPjs.CookieStorage {
|
|
218
|
+
constructor(config = {}) {
|
|
219
|
+
super({
|
|
220
|
+
secure: true,
|
|
221
|
+
httpOnly: true
|
|
222
|
+
});
|
|
223
|
+
this.config = config;
|
|
224
|
+
}
|
|
225
|
+
get(key) {
|
|
226
|
+
var _a;
|
|
227
|
+
return ((_a = _headersjs.cookies.call(void 0, ).get(key)) == null ? void 0 : _a.value) || null;
|
|
228
|
+
}
|
|
229
|
+
set(key, value) {
|
|
230
|
+
var _a;
|
|
231
|
+
const cookieSettings = ((_a = this.config) == null ? void 0 : _a[key]) || _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, this.settings);
|
|
232
|
+
console.log(
|
|
233
|
+
"NextjsCookieStorage.set",
|
|
234
|
+
JSON.stringify(
|
|
235
|
+
{ key, value, config: this.config, cookieSettings },
|
|
236
|
+
null,
|
|
237
|
+
2
|
|
238
|
+
)
|
|
239
|
+
);
|
|
240
|
+
_headersjs.cookies.call(void 0, ).set(key, value, cookieSettings);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
var NextjsClientStorage = class extends _chunk6RFRDWIPjs.CookieStorage {
|
|
244
|
+
constructor(config = {}) {
|
|
245
|
+
super(_chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, config), {
|
|
246
|
+
secure: false,
|
|
247
|
+
httpOnly: false
|
|
248
|
+
}));
|
|
249
|
+
}
|
|
250
|
+
get(key) {
|
|
251
|
+
var _a;
|
|
252
|
+
return ((_a = _headersjs.cookies.call(void 0, ).get(key)) == null ? void 0 : _a.value) || null;
|
|
253
|
+
}
|
|
254
|
+
set(key, value) {
|
|
255
|
+
_headersjs.cookies.call(void 0, ).set(key, value, this.settings);
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
// src/nextjs/utils.ts
|
|
260
|
+
var resolveCallbackUrl = (config, baseUrl) => {
|
|
261
|
+
const callbackUrl = new URL(config == null ? void 0 : config.callbackUrl, baseUrl).toString();
|
|
262
|
+
return callbackUrl.toString();
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
exports.loggers = loggers; exports.defaultAuthConfig = defaultAuthConfig; exports.resolveAuthConfig = resolveAuthConfig; exports.createCivicAuthPlugin = createCivicAuthPlugin; exports.createTokenCookies = createTokenCookies; exports.createUserInfoCookie = createUserInfoCookie; exports.clearAuthCookies = clearAuthCookies; exports.NextjsCookieStorage = NextjsCookieStorage; exports.NextjsClientStorage = NextjsClientStorage; exports.resolveCallbackUrl = resolveCallbackUrl;
|
|
277
|
+
//# sourceMappingURL=chunk-2TDB4XWE.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/lucas/dev/civic/civic-auth/packages/civic-auth-client/dist/chunk-2TDB4XWE.js","../src/lib/logger.ts","../src/nextjs/config.ts","../src/nextjs/cookies.ts","../src/nextjs/utils.ts"],"names":[],"mappings":"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","file":"/Users/lucas/dev/civic/civic-auth/packages/civic-auth-client/dist/chunk-2TDB4XWE.js","sourcesContent":[null,"import debug from \"debug\";\n\nconst PACKAGE_NAME = \"@civic/auth\";\n\nexport interface Logger {\n debug(message: string, ...args: unknown[]): void;\n info(message: string, ...args: unknown[]): void;\n warn(message: string, ...args: unknown[]): void;\n error(message: string, ...args: unknown[]): void;\n}\n\nclass DebugLogger implements Logger {\n private debugLogger: debug.Debugger;\n private infoLogger: debug.Debugger;\n private warnLogger: debug.Debugger;\n private errorLogger: debug.Debugger;\n\n constructor(namespace: string) {\n // Format: @org/package:library:component:level\n this.debugLogger = debug(`${PACKAGE_NAME}:${namespace}:debug`);\n this.infoLogger = debug(`${PACKAGE_NAME}:${namespace}:info`);\n this.warnLogger = debug(`${PACKAGE_NAME}:${namespace}:warn`);\n this.errorLogger = debug(`${PACKAGE_NAME}:${namespace}:error`);\n\n this.debugLogger.color = \"4\";\n this.infoLogger.color = \"2\";\n this.warnLogger.color = \"3\";\n this.errorLogger.color = \"1\";\n }\n\n debug(message: string, ...args: unknown[]): void {\n this.debugLogger(message, ...args);\n }\n\n info(message: string, ...args: unknown[]): void {\n this.infoLogger(message, ...args);\n }\n\n warn(message: string, ...args: unknown[]): void {\n this.warnLogger(message, ...args);\n }\n\n error(message: string, ...args: unknown[]): void {\n this.errorLogger(message, ...args);\n }\n}\n\nexport const createLogger = (namespace: string): Logger =>\n new DebugLogger(namespace);\n\n// Pre-configured loggers for different parts of your package\nexport const loggers = {\n // Next.js specific loggers\n nextjs: {\n routes: createLogger(\"api:routes\"),\n middleware: createLogger(\"api:middleware\"),\n handlers: {\n auth: createLogger(\"api:handlers:auth\"),\n },\n },\n // React specific loggers\n react: {\n components: createLogger(\"react:components\"),\n hooks: createLogger(\"react:hooks\"),\n context: createLogger(\"react:context\"),\n },\n // Shared utilities loggers\n services: {\n validation: createLogger(\"utils:validation\"),\n network: createLogger(\"utils:network\"),\n },\n} as const;\n","/* eslint-disable turbo/no-undeclared-env-vars */\nimport { NextConfig } from \"next\";\nimport { loggers } from \"@/lib/logger\";\nimport { withoutUndefined } from \"@/utils\";\nimport {\n CodeVerifier,\n CookieConfig,\n OAuthTokens,\n TokensCookieConfig,\n} from \"@/shared/types\";\nimport { DEFAULT_AUTH_SERVER } from \"@/constants\";\n\nconst logger = loggers.nextjs.handlers.auth;\n\nexport type CookiesConfigObject = {\n tokens: TokensCookieConfig;\n user: CookieConfig;\n};\n\nexport type AuthConfigWithDefaults = {\n clientId: string;\n oauthServer: string;\n callbackUrl: string;\n loginUrl: string;\n logoutUrl: string;\n challengeUrl: string;\n include: string[];\n exclude: string[];\n cookies: CookiesConfigObject;\n};\n\nexport type AuthConfig = Partial<AuthConfigWithDefaults>;\n\nexport type DefinedAuthConfig = AuthConfigWithDefaults;\n\nconsole.log(`process.env.NODE_ENV: ${process.env.NODE_ENV}`);\nconst isDevelopment = process.env.NODE_ENV === \"development\";\n\nconst defaultServerSecure = isDevelopment ? false : true;\nconsole.log(`defaultServerSecure: ${defaultServerSecure}`);\n/**\n * Default configuration values that will be used if not overridden\n */\nexport const defaultAuthConfig: Omit<AuthConfigWithDefaults, \"clientId\"> = {\n oauthServer: DEFAULT_AUTH_SERVER,\n callbackUrl: \"/api/auth/callback\",\n challengeUrl: \"/api/auth/challenge\",\n logoutUrl: \"/api/auth/logout\",\n loginUrl: \"/\",\n include: [\"/*\"],\n exclude: [],\n cookies: {\n tokens: {\n [OAuthTokens.ID_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [OAuthTokens.ACCESS_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [OAuthTokens.REFRESH_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [CodeVerifier.COOKIE_NAME]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n },\n user: {\n secure: defaultServerSecure,\n httpOnly: false,\n sameSite: \"strict\",\n path: \"/\",\n maxAge: 60 * 60, // 1 hour\n },\n },\n};\n\n/**\n * Resolves the authentication configuration by combining:\n * 1. Default values\n * 2. Environment variables (set internally by the plugin)\n * 3. Explicitly passed configuration\n *\n * Note: Developers should not set _civic_auth_* environment variables directly.\n * Instead, pass configuration to the createCivicAuthPlugin in next.config.js:\n *\n * @example\n * ```js\n * // next.config.js\n * export default createCivicAuthPlugin({\n * callbackUrl: '/custom/callback',\n * })\n * ```\n */\nexport const resolveAuthConfig = (\n config: AuthConfig = {},\n): AuthConfigWithDefaults => {\n // Read configuration that was set by the plugin via environment variables\n const configFromEnv = withoutUndefined({\n clientId: process.env._civic_auth_client_id,\n oauthServer: process.env._civic_oauth_server,\n callbackUrl: process.env._civic_auth_callback_url,\n challengeUrl: process.env._civic_auth_challenge_url,\n loginUrl: process.env._civic_auth_login_url,\n logoutUrl: process.env._civic_auth_logout_url,\n include: process.env._civic_auth_includes?.split(\",\"),\n exclude: process.env._civic_auth_excludes?.split(\",\"),\n cookies: process.env._civic_auth_cookie_config\n ? JSON.parse(process.env._civic_auth_cookie_config)\n : undefined,\n }) as AuthConfig;\n const mergedConfig = {\n ...defaultAuthConfig,\n ...configFromEnv, // Apply plugin-set config\n ...config, // Override with directly passed config\n cookies: {\n tokens: {\n ...defaultAuthConfig.cookies.tokens,\n ...(configFromEnv?.cookies?.tokens || {}),\n ...(config.cookies?.tokens || {}),\n },\n user: {\n ...defaultAuthConfig.cookies.user,\n ...(configFromEnv?.cookies?.user || {}),\n ...(config.cookies?.user || {}),\n },\n },\n };\n\n logger.debug(\n \"Config from environment:\",\n JSON.stringify(configFromEnv, null, 2),\n );\n logger.debug(\"Resolved config:\", JSON.stringify(mergedConfig, null, 2));\n if (mergedConfig.clientId === undefined) {\n throw new Error(\"Civic Auth client ID is required\");\n }\n return mergedConfig as AuthConfigWithDefaults & { clientId: string };\n};\n\n/**\n * Creates a Next.js plugin that handles auth configuration.\n *\n * This is the main configuration point for the auth system.\n * Do not set _civic_auth_* environment variables directly - instead,\n * pass your configuration here:\n *\n * @example\n * ```js\n * // next.config.js\n * export default createCivicAuthPlugin({\n * clientId: 'my-client-id',\n * callbackUrl: '/custom/callback',\n * loginUrl: '/custom/login',\n * logoutUrl: '/custom/logout',\n * include: ['/protected/*'],\n * exclude: ['/public/*']\n * })\n * ```\n *\n * The plugin sets internal environment variables that are used by\n * the auth system. These variables should not be set manually.\n */\nexport const createCivicAuthPlugin = (\n authConfig: AuthConfig & Pick<Required<AuthConfig>, \"clientId\">,\n) => {\n return (nextConfig?: NextConfig) => {\n logger.debug(\n \"createCivicAuthPlugin nextConfig\",\n JSON.stringify(nextConfig, null, 2),\n );\n const resolvedConfig = resolveAuthConfig({ ...authConfig });\n return {\n ...nextConfig,\n env: {\n ...nextConfig?.env,\n // Internal environment variables - do not set these manually\n _civic_auth_client_id: resolvedConfig.clientId,\n _civic_oauth_server: resolvedConfig.oauthServer,\n _civic_auth_callback_url: resolvedConfig.callbackUrl,\n _civic_auth_challenge_url: resolvedConfig.challengeUrl,\n _civic_auth_login_url: resolvedConfig.loginUrl,\n _civic_auth_logout_url: resolvedConfig.logoutUrl,\n _civic_auth_includes: resolvedConfig.include.join(\",\"),\n _civic_auth_excludes: resolvedConfig.exclude.join(\",\"),\n _civic_auth_cookie_config: JSON.stringify(resolvedConfig.cookies),\n },\n };\n };\n};\n","import { SessionData, UnknownObject, User } from \"@/types\";\nimport { NextResponse } from \"next/server\";\nimport { AuthConfig } from \"@/nextjs/config\";\nimport { CookieStorage, CookieStorageSettings } from \"@/server\";\nimport { cookies } from \"next/headers.js\";\nimport { GenericUserSession } from \"@/shared/UserSession\";\nimport { clearTokens } from \"@/shared/util\";\nimport { CodeVerifier, OAuthTokens, TokensCookieConfig } from \"@/shared/types\";\n\n/**\n * Creates HTTP-only cookies for authentication tokens\n */\nconst createTokenCookies = (\n response: NextResponse,\n sessionData: SessionData,\n config: AuthConfig,\n) => {\n const maxAge = sessionData.expiresIn ?? 3600;\n const cookieOptions = {\n ...config.cookies?.tokens,\n maxAge,\n };\n\n if (sessionData.accessToken) {\n response.cookies.set(\"access_token\", sessionData.accessToken, {\n ...cookieOptions,\n httpOnly: true,\n });\n }\n\n if (sessionData.idToken) {\n response.cookies.set(\"id_token\", sessionData.idToken, {\n ...cookieOptions,\n httpOnly: true,\n });\n }\n\n if (sessionData.refreshToken) {\n response.cookies.set(\"refresh_token\", sessionData.refreshToken, {\n ...cookieOptions,\n httpOnly: true,\n });\n }\n};\n\n/**\n * Creates a client-readable cookie with user info\n */\nconst createUserInfoCookie = (\n response: NextResponse,\n user: User<UnknownObject> | null,\n sessionData: SessionData,\n config: AuthConfig,\n) => {\n if (!user) {\n response.cookies.set(\"user\", \"\", {\n ...config.cookies?.user,\n maxAge: 0,\n });\n return;\n }\n const maxAge = sessionData.expiresIn ?? 3600;\n\n // TODO select fields to include in the user cookie\n const frontendUser = {\n ...user,\n };\n\n // TODO make call to get user info from the\n // auth server /userinfo endpoint when it's available\n // then add to the default claims above\n\n response.cookies.set(\"user\", JSON.stringify(frontendUser), {\n ...config.cookies?.user,\n maxAge,\n });\n};\n\n/**\n * Clears all authentication cookies\n */\nconst clearAuthCookies = async (config: AuthConfig) => {\n // clear session, and tokens\n const cookieStorage = new NextjsCookieStorage(config.cookies?.tokens);\n clearTokens(cookieStorage);\n\n // clear user\n const clientStorage = new NextjsClientStorage();\n const userSession = new GenericUserSession(clientStorage);\n userSession.set(null);\n};\n\ntype KeySetter = OAuthTokens | CodeVerifier;\nclass NextjsCookieStorage extends CookieStorage {\n constructor(readonly config: Partial<TokensCookieConfig> = {}) {\n super({\n secure: true,\n httpOnly: true,\n });\n }\n\n get(key: string): string | null {\n return cookies().get(key)?.value || null;\n }\n\n set(key: KeySetter, value: string): void {\n const cookieSettings = this.config?.[key as KeySetter] || {\n ...this.settings,\n };\n console.log(\n \"NextjsCookieStorage.set\",\n JSON.stringify(\n { key, value, config: this.config, cookieSettings },\n null,\n 2,\n ),\n );\n cookies().set(key, value, cookieSettings);\n }\n}\n\nclass NextjsClientStorage extends CookieStorage {\n constructor(config: Partial<CookieStorageSettings> = {}) {\n super({\n ...config,\n secure: false,\n httpOnly: false,\n });\n }\n\n get(key: string): string | null {\n return cookies().get(key)?.value || null;\n }\n\n set(key: string, value: string): void {\n cookies().set(key, value, this.settings);\n }\n}\n\nexport {\n createTokenCookies,\n createUserInfoCookie,\n clearAuthCookies,\n NextjsCookieStorage,\n NextjsClientStorage,\n};\n","import { AuthConfigWithDefaults } from \"@/nextjs/config\";\n\nexport const resolveCallbackUrl = (\n config: AuthConfigWithDefaults,\n baseUrl?: string,\n): string => {\n const callbackUrl = new URL(config?.callbackUrl, baseUrl).toString();\n return callbackUrl.toString();\n};\n"]}
|