@hanzo/iam 0.4.0 → 0.4.2
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/browser.js +2 -2
- package/dist/browser.js.map +1 -1
- package/dist/client.d.ts +21 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +32 -0
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/react.d.ts +45 -1
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +131 -0
- package/dist/react.js.map +1 -1
- package/dist/types.d.ts +15 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -11
- package/src/browser.ts +2 -2
- package/src/client.ts +83 -0
- package/src/index.ts +2 -1
- package/src/react.ts +195 -1
- package/src/types.ts +20 -0
- package/dist/betterauth.d.ts +0 -62
- package/dist/betterauth.d.ts.map +0 -1
- package/dist/betterauth.js +0 -61
- package/dist/betterauth.js.map +0 -1
- package/dist/passport.d.ts +0 -44
- package/dist/passport.d.ts.map +0 -1
- package/dist/passport.js +0 -67
- package/dist/passport.js.map +0 -1
- package/src/betterauth.ts +0 -86
- package/src/passport.ts +0 -97
package/src/betterauth.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* BetterAuth SSO provider configuration for Hanzo IAM.
|
|
3
|
-
*
|
|
4
|
-
* Returns a provider config object compatible with BetterAuth's
|
|
5
|
-
* `socialProviders` or generic OAuth plugin.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* import { betterAuth } from "better-auth";
|
|
10
|
-
* import { hanzoIamProvider } from "@hanzo/iam/betterauth";
|
|
11
|
-
*
|
|
12
|
-
* export const auth = betterAuth({
|
|
13
|
-
* socialProviders: [
|
|
14
|
-
* hanzoIamProvider({
|
|
15
|
-
* serverUrl: process.env.IAM_SERVER_URL!,
|
|
16
|
-
* clientId: process.env.IAM_CLIENT_ID!,
|
|
17
|
-
* clientSecret: process.env.IAM_CLIENT_SECRET!,
|
|
18
|
-
* }),
|
|
19
|
-
* ],
|
|
20
|
-
* });
|
|
21
|
-
* ```
|
|
22
|
-
*
|
|
23
|
-
* @packageDocumentation
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
import type { IamConfig } from "./types.js";
|
|
27
|
-
|
|
28
|
-
export interface HanzoIamSocialProvider {
|
|
29
|
-
id: string;
|
|
30
|
-
name: string;
|
|
31
|
-
type: "oidc";
|
|
32
|
-
issuer: string;
|
|
33
|
-
clientId: string;
|
|
34
|
-
clientSecret?: string;
|
|
35
|
-
authorization: { url: string; params: { scope: string } };
|
|
36
|
-
token: { url: string };
|
|
37
|
-
userinfo: { url: string };
|
|
38
|
-
profile: (profile: Record<string, unknown>) => {
|
|
39
|
-
id: string;
|
|
40
|
-
name: string;
|
|
41
|
-
email: string;
|
|
42
|
-
image: string | null;
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Create a BetterAuth-compatible social provider for Hanzo IAM.
|
|
48
|
-
*
|
|
49
|
-
* Works with BetterAuth's SSO plugin or generic OAuth integration.
|
|
50
|
-
* Uses the standard Hanzo IAM / Casdoor OIDC endpoints.
|
|
51
|
-
*/
|
|
52
|
-
export function hanzoIamProvider(
|
|
53
|
-
config: IamConfig & { redirectUri?: string },
|
|
54
|
-
): HanzoIamSocialProvider {
|
|
55
|
-
const baseUrl = config.serverUrl.replace(/\/+$/, "");
|
|
56
|
-
|
|
57
|
-
return {
|
|
58
|
-
id: "hanzo-iam",
|
|
59
|
-
name: "Hanzo IAM",
|
|
60
|
-
type: "oidc",
|
|
61
|
-
issuer: baseUrl,
|
|
62
|
-
clientId: config.clientId,
|
|
63
|
-
clientSecret: config.clientSecret,
|
|
64
|
-
authorization: {
|
|
65
|
-
url: `${baseUrl}/login/oauth/authorize`,
|
|
66
|
-
params: { scope: "openid profile email" },
|
|
67
|
-
},
|
|
68
|
-
token: { url: `${baseUrl}/api/login/oauth/access_token` },
|
|
69
|
-
userinfo: { url: `${baseUrl}/api/userinfo` },
|
|
70
|
-
profile(profile: Record<string, unknown>) {
|
|
71
|
-
return {
|
|
72
|
-
id: (profile.sub as string) ?? (profile.id as string) ?? "",
|
|
73
|
-
name:
|
|
74
|
-
(profile.displayName as string) ??
|
|
75
|
-
(profile.name as string) ??
|
|
76
|
-
(profile.preferred_username as string) ??
|
|
77
|
-
"",
|
|
78
|
-
email: (profile.email as string) ?? "",
|
|
79
|
-
image: (profile.avatar as string) ?? (profile.picture as string) ?? null,
|
|
80
|
-
};
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Backwards-compatible alias
|
|
86
|
-
export { hanzoIamProvider as hanzoIamSocialProvider };
|
package/src/passport.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Passport.js OAuth2 strategy factory for Hanzo IAM.
|
|
3
|
-
*
|
|
4
|
-
* Creates a pre-configured passport-oauth2 strategy that authenticates
|
|
5
|
-
* against hanzo.id with PKCE and fetches user info on callback.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* import passport from "passport";
|
|
10
|
-
* import { createIamPassportStrategy } from "@hanzo/iam/passport";
|
|
11
|
-
*
|
|
12
|
-
* passport.use("iam", createIamPassportStrategy({
|
|
13
|
-
* serverUrl: "https://hanzo.id",
|
|
14
|
-
* clientId: "hanzo-kms-client-id",
|
|
15
|
-
* clientSecret: process.env.IAM_CLIENT_SECRET!,
|
|
16
|
-
* callbackUrl: "https://kms.hanzo.ai/api/v1/sso/oidc/callback",
|
|
17
|
-
* }));
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* @packageDocumentation
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import type { IamConfig } from "./types.js";
|
|
24
|
-
|
|
25
|
-
export interface IamPassportConfig extends IamConfig {
|
|
26
|
-
/** Full callback URL for OAuth2 redirect. */
|
|
27
|
-
callbackUrl: string;
|
|
28
|
-
/** OAuth2 scopes. Default: "openid profile email". */
|
|
29
|
-
scope?: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface IamPassportUser {
|
|
33
|
-
accessToken: string;
|
|
34
|
-
refreshToken?: string;
|
|
35
|
-
userinfo: Record<string, unknown>;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Create a Passport OAuth2 strategy for Hanzo IAM.
|
|
40
|
-
*
|
|
41
|
-
* Requires `passport-oauth2` as a peer dependency.
|
|
42
|
-
* Returns an OAuth2Strategy instance ready to pass to `passport.use()`.
|
|
43
|
-
*
|
|
44
|
-
* The verify callback fetches userinfo from the IAM server and passes
|
|
45
|
-
* `{ accessToken, refreshToken, userinfo }` as the user object.
|
|
46
|
-
*/
|
|
47
|
-
export function createIamPassportStrategy(
|
|
48
|
-
config: IamPassportConfig,
|
|
49
|
-
): unknown {
|
|
50
|
-
// Dynamic import to keep passport-oauth2 as optional peer dep.
|
|
51
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
52
|
-
const { Strategy: OAuth2Strategy } = require("passport-oauth2") as {
|
|
53
|
-
Strategy: new (
|
|
54
|
-
options: Record<string, unknown>,
|
|
55
|
-
verify: (...args: unknown[]) => void,
|
|
56
|
-
) => unknown;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const baseUrl = config.serverUrl.replace(/\/+$/, "");
|
|
60
|
-
|
|
61
|
-
const verify = async (
|
|
62
|
-
...args: unknown[]
|
|
63
|
-
): Promise<void> => {
|
|
64
|
-
// passReqToCallback=true: (req, accessToken, refreshToken, profile, done)
|
|
65
|
-
const accessToken = args[1] as string;
|
|
66
|
-
const refreshToken = args[2] as string | undefined;
|
|
67
|
-
const done = args[4] as (err: Error | null, user?: IamPassportUser) => void;
|
|
68
|
-
|
|
69
|
-
try {
|
|
70
|
-
const res = await fetch(`${baseUrl}/api/userinfo`, {
|
|
71
|
-
headers: { Authorization: `Bearer ${accessToken}` },
|
|
72
|
-
});
|
|
73
|
-
if (!res.ok) {
|
|
74
|
-
return done(new Error(`IAM userinfo failed: ${res.status}`));
|
|
75
|
-
}
|
|
76
|
-
const userinfo = (await res.json()) as Record<string, unknown>;
|
|
77
|
-
done(null, { accessToken, refreshToken, userinfo });
|
|
78
|
-
} catch (err) {
|
|
79
|
-
done(err instanceof Error ? err : new Error(String(err)));
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
return new OAuth2Strategy(
|
|
84
|
-
{
|
|
85
|
-
authorizationURL: `${baseUrl}/login/oauth/authorize`,
|
|
86
|
-
tokenURL: `${baseUrl}/api/login/oauth/access_token`,
|
|
87
|
-
clientID: config.clientId,
|
|
88
|
-
clientSecret: config.clientSecret ?? "",
|
|
89
|
-
callbackURL: config.callbackUrl,
|
|
90
|
-
scope: config.scope ?? "openid profile email",
|
|
91
|
-
state: true,
|
|
92
|
-
pkce: true,
|
|
93
|
-
passReqToCallback: true,
|
|
94
|
-
},
|
|
95
|
-
verify,
|
|
96
|
-
);
|
|
97
|
-
}
|