@svadmin/sso 0.9.0 → 0.10.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/package.json +2 -2
- package/src/auth-helpers.ts +114 -0
- package/src/index.ts +12 -0
- package/src/presets.ts +25 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@svadmin/sso",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "OIDC/OAuth2 SSO AuthProvider plugin for svadmin — supports Okta, Azure AD, Cognito, Keycloak, Google",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"!src/**/setupTest.*"
|
|
36
36
|
],
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@svadmin/core": "^0.
|
|
38
|
+
"@svadmin/core": "^0.26.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"typescript": "^5.7.0"
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 浏览器端 OIDC/OAuth2 认证辅助函数。
|
|
3
|
+
*
|
|
4
|
+
* 提供 PKCE 生成、OIDC Discovery 端点解析、重定向校验、
|
|
5
|
+
* 反向代理 origin 恢复等通用工具。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** 将 Uint8Array 编码为 base64url 格式 (纯 Web API) */
|
|
9
|
+
function base64url(array: Uint8Array): string {
|
|
10
|
+
let binary = "";
|
|
11
|
+
for (const byte of array) binary += String.fromCharCode(byte);
|
|
12
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** 生成随机的 OAuth state 值 */
|
|
16
|
+
export function generateState(): string {
|
|
17
|
+
const array = new Uint8Array(16);
|
|
18
|
+
crypto.getRandomValues(array);
|
|
19
|
+
return base64url(array);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 生成 PKCE code verifier */
|
|
23
|
+
export function generateVerifier(): string {
|
|
24
|
+
const array = new Uint8Array(32);
|
|
25
|
+
crypto.getRandomValues(array);
|
|
26
|
+
return base64url(array);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 基于 code verifier 算得 code challenge (S256) */
|
|
30
|
+
export async function generateChallenge(verifier: string): Promise<string> {
|
|
31
|
+
const encoder = new TextEncoder();
|
|
32
|
+
const data = encoder.encode(verifier);
|
|
33
|
+
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
34
|
+
return base64url(new Uint8Array(hash));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 校验 returnTo 重定向地址,防止开放重定向漏洞。
|
|
39
|
+
* 只允许同源相对路径(必须以 / 开头且非 // 开头)。
|
|
40
|
+
*/
|
|
41
|
+
export function isValidReturnTo(url: string): boolean {
|
|
42
|
+
if (!url) return false;
|
|
43
|
+
if (url.includes("\\")) return false;
|
|
44
|
+
return /^\/[^/].*/.test(url) || url === "/";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 从反向代理头恢复公网 origin */
|
|
48
|
+
export function getForwardedOrigin(req: Request, fallbackUrl: URL): string {
|
|
49
|
+
const forwardedProto = req.headers.get("x-forwarded-proto")?.split(",")[0]?.trim();
|
|
50
|
+
const forwardedHost = req.headers.get("x-forwarded-host")?.split(",")[0]?.trim();
|
|
51
|
+
const host = forwardedHost || req.headers.get("host") || fallbackUrl.host;
|
|
52
|
+
const protocol = (forwardedProto || fallbackUrl.protocol.replace(/:$/, "")).replace(/:$/, "");
|
|
53
|
+
return `${protocol}://${host}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface OidcDiscoveryDocument {
|
|
57
|
+
authorization_endpoint?: string;
|
|
58
|
+
token_endpoint?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 通过 OIDC Discovery 解析授权端点 URL */
|
|
62
|
+
export async function resolveAuthorizeUrl(
|
|
63
|
+
issuer: string,
|
|
64
|
+
overrideUrl = "",
|
|
65
|
+
fetcher: typeof fetch = fetch,
|
|
66
|
+
): Promise<string> {
|
|
67
|
+
const normalizedIssuer = issuer.replace(/\/$/, "");
|
|
68
|
+
const normalizedOverride = overrideUrl.trim();
|
|
69
|
+
|
|
70
|
+
if (normalizedOverride) return normalizedOverride;
|
|
71
|
+
|
|
72
|
+
if (normalizedIssuer) {
|
|
73
|
+
try {
|
|
74
|
+
const response = await fetcher(`${normalizedIssuer}/.well-known/openid-configuration`);
|
|
75
|
+
if (response.ok) {
|
|
76
|
+
const discovery = await response.json() as OidcDiscoveryDocument;
|
|
77
|
+
if (discovery.authorization_endpoint) return discovery.authorization_endpoint;
|
|
78
|
+
}
|
|
79
|
+
} catch {
|
|
80
|
+
// 忽略 discovery 失败,走 fallback
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (normalizedIssuer) return `${normalizedIssuer}/oauth/authorize`;
|
|
85
|
+
|
|
86
|
+
return "";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 通过 OIDC Discovery 解析 token 端点 URL */
|
|
90
|
+
export async function resolveTokenUrl(
|
|
91
|
+
issuer: string,
|
|
92
|
+
overrideUrlOrFetcher: string | typeof fetch = "",
|
|
93
|
+
fetcherOverride: typeof fetch = fetch,
|
|
94
|
+
): Promise<string> {
|
|
95
|
+
const normalizedIssuer = issuer.replace(/\/$/, "");
|
|
96
|
+
const fetcher = typeof overrideUrlOrFetcher === "function" ? overrideUrlOrFetcher : fetcherOverride;
|
|
97
|
+
const overrideUrl = typeof overrideUrlOrFetcher === "string" ? overrideUrlOrFetcher : "";
|
|
98
|
+
const normalizedOverride = overrideUrl.trim();
|
|
99
|
+
|
|
100
|
+
if (normalizedOverride) return normalizedOverride;
|
|
101
|
+
if (!normalizedIssuer) return "";
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
const response = await fetcher(`${normalizedIssuer}/.well-known/openid-configuration`);
|
|
105
|
+
if (response.ok) {
|
|
106
|
+
const discovery = await response.json() as OidcDiscoveryDocument;
|
|
107
|
+
if (discovery.token_endpoint) return discovery.token_endpoint;
|
|
108
|
+
}
|
|
109
|
+
} catch {
|
|
110
|
+
// 忽略 discovery 失败
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return `${normalizedIssuer}/oauth/token`;
|
|
114
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -18,5 +18,17 @@ export {
|
|
|
18
18
|
createGitLabAuth,
|
|
19
19
|
createKeycloakAuth,
|
|
20
20
|
createAuth0Auth,
|
|
21
|
+
createSupauthAuth,
|
|
21
22
|
} from './presets';
|
|
22
23
|
export type { PresetOptions } from './presets';
|
|
24
|
+
|
|
25
|
+
// Auth helpers (PKCE, OIDC discovery, redirect validation)
|
|
26
|
+
export {
|
|
27
|
+
generateState,
|
|
28
|
+
generateVerifier,
|
|
29
|
+
generateChallenge,
|
|
30
|
+
isValidReturnTo,
|
|
31
|
+
getForwardedOrigin,
|
|
32
|
+
resolveAuthorizeUrl,
|
|
33
|
+
resolveTokenUrl,
|
|
34
|
+
} from './auth-helpers';
|
package/src/presets.ts
CHANGED
|
@@ -177,3 +177,28 @@ export function createAuth0Auth(
|
|
|
177
177
|
// ─── Type exports ────────────────────────────────────────────
|
|
178
178
|
|
|
179
179
|
export type { PresetOptions };
|
|
180
|
+
|
|
181
|
+
// ─── Supauth / GoTrue ────────────────────────────────────────
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Supauth OIDC Sign-In (GoTrue-compatible identity provider).
|
|
185
|
+
*
|
|
186
|
+
* Compatible with SupaCloud, SupaOAuth, and any GoTrue-based auth backend.
|
|
187
|
+
* Uses standard OIDC Authorization Code Flow with PKCE.
|
|
188
|
+
*
|
|
189
|
+
* @param issuer - Supauth issuer URL (e.g., 'https://auth.example.com')
|
|
190
|
+
* @param clientId - OAuth2 Client ID
|
|
191
|
+
*/
|
|
192
|
+
export function createSupauthAuth(
|
|
193
|
+
clientId: string,
|
|
194
|
+
opts?: PresetOptions & { issuer?: string },
|
|
195
|
+
) {
|
|
196
|
+
const issuer = opts?.issuer ?? '';
|
|
197
|
+
return createSSOAuthProvider({
|
|
198
|
+
issuer,
|
|
199
|
+
clientId,
|
|
200
|
+
scopes: ['openid', 'profile', 'email'],
|
|
201
|
+
redirectUri: resolveRedirectUri(opts),
|
|
202
|
+
...opts,
|
|
203
|
+
});
|
|
204
|
+
}
|