@hanzo/iam 0.4.1 → 0.5.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.
@@ -1,64 +0,0 @@
1
- /**
2
- * BetterAuth SSO provider configuration for 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 { iamProvider } from "@hanzo/iam/betterauth";
11
- *
12
- * export const auth = betterAuth({
13
- * socialProviders: [
14
- * iamProvider({
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
- * Create a BetterAuth-compatible social provider for IAM.
27
- *
28
- * Works with BetterAuth's SSO plugin or generic OAuth integration.
29
- * Uses standard OIDC endpoints.
30
- */
31
- export function iamProvider(config) {
32
- const baseUrl = config.serverUrl.replace(/\/+$/, "");
33
- return {
34
- id: "iam",
35
- name: "IAM",
36
- type: "oidc",
37
- issuer: baseUrl,
38
- clientId: config.clientId,
39
- clientSecret: config.clientSecret,
40
- authorization: {
41
- url: `${baseUrl}/login/oauth/authorize`,
42
- params: { scope: "openid profile email" },
43
- },
44
- token: { url: `${baseUrl}/api/login/oauth/access_token` },
45
- userinfo: { url: `${baseUrl}/api/userinfo` },
46
- profile(profile) {
47
- return {
48
- id: profile.sub ?? profile.id ?? "",
49
- name: profile.displayName ??
50
- profile.name ??
51
- profile.preferred_username ??
52
- "",
53
- email: profile.email ?? "",
54
- image: profile.avatar ?? profile.picture ?? null,
55
- };
56
- },
57
- };
58
- }
59
- // Backwards-compatible aliases
60
- /** @deprecated Use iamProvider instead */
61
- export { iamProvider as hanzoIamProvider };
62
- /** @deprecated Use iamProvider instead */
63
- export { iamProvider as hanzoIamSocialProvider };
64
- //# sourceMappingURL=betterauth.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"betterauth.js","sourceRoot":"","sources":["../src/betterauth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAsBH;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CACzB,MAA4C;IAE5C,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAErD,OAAO;QACL,EAAE,EAAE,KAAK;QACT,IAAI,EAAE,KAAK;QACX,IAAI,EAAE,MAAM;QACZ,MAAM,EAAE,OAAO;QACf,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,YAAY,EAAE,MAAM,CAAC,YAAY;QACjC,aAAa,EAAE;YACb,GAAG,EAAE,GAAG,OAAO,wBAAwB;YACvC,MAAM,EAAE,EAAE,KAAK,EAAE,sBAAsB,EAAE;SAC1C;QACD,KAAK,EAAE,EAAE,GAAG,EAAE,GAAG,OAAO,+BAA+B,EAAE;QACzD,QAAQ,EAAE,EAAE,GAAG,EAAE,GAAG,OAAO,eAAe,EAAE;QAC5C,OAAO,CAAC,OAAgC;YACtC,OAAO;gBACL,EAAE,EAAG,OAAO,CAAC,GAAc,IAAK,OAAO,CAAC,EAAa,IAAI,EAAE;gBAC3D,IAAI,EACD,OAAO,CAAC,WAAsB;oBAC9B,OAAO,CAAC,IAAe;oBACvB,OAAO,CAAC,kBAA6B;oBACtC,EAAE;gBACJ,KAAK,EAAG,OAAO,CAAC,KAAgB,IAAI,EAAE;gBACtC,KAAK,EAAG,OAAO,CAAC,MAAiB,IAAK,OAAO,CAAC,OAAkB,IAAI,IAAI;aACzE,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC;AAED,+BAA+B;AAC/B,0CAA0C;AAC1C,OAAO,EAAE,WAAW,IAAI,gBAAgB,EAAE,CAAC;AAC3C,0CAA0C;AAC1C,OAAO,EAAE,WAAW,IAAI,sBAAsB,EAAE,CAAC"}
@@ -1,44 +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
- import type { IamConfig } from "./types.js";
23
- export interface IamPassportConfig extends IamConfig {
24
- /** Full callback URL for OAuth2 redirect. */
25
- callbackUrl: string;
26
- /** OAuth2 scopes. Default: "openid profile email". */
27
- scope?: string;
28
- }
29
- export interface IamPassportUser {
30
- accessToken: string;
31
- refreshToken?: string;
32
- userinfo: Record<string, unknown>;
33
- }
34
- /**
35
- * Create a Passport OAuth2 strategy for Hanzo IAM.
36
- *
37
- * Requires `passport-oauth2` as a peer dependency.
38
- * Returns an OAuth2Strategy instance ready to pass to `passport.use()`.
39
- *
40
- * The verify callback fetches userinfo from the IAM server and passes
41
- * `{ accessToken, refreshToken, userinfo }` as the user object.
42
- */
43
- export declare function createIamPassportStrategy(config: IamPassportConfig): unknown;
44
- //# sourceMappingURL=passport.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"passport.d.ts","sourceRoot":"","sources":["../src/passport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C,MAAM,WAAW,iBAAkB,SAAQ,SAAS;IAClD,6CAA6C;IAC7C,WAAW,EAAE,MAAM,CAAC;IACpB,sDAAsD;IACtD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC;AAED;;;;;;;;GAQG;AACH,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,iBAAiB,GACxB,OAAO,CAgDT"}
package/dist/passport.js DELETED
@@ -1,67 +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
- * Create a Passport OAuth2 strategy for Hanzo IAM.
24
- *
25
- * Requires `passport-oauth2` as a peer dependency.
26
- * Returns an OAuth2Strategy instance ready to pass to `passport.use()`.
27
- *
28
- * The verify callback fetches userinfo from the IAM server and passes
29
- * `{ accessToken, refreshToken, userinfo }` as the user object.
30
- */
31
- export function createIamPassportStrategy(config) {
32
- // Dynamic import to keep passport-oauth2 as optional peer dep.
33
- // eslint-disable-next-line @typescript-eslint/no-require-imports
34
- const { Strategy: OAuth2Strategy } = require("passport-oauth2");
35
- const baseUrl = config.serverUrl.replace(/\/+$/, "");
36
- const verify = async (...args) => {
37
- // passReqToCallback=true: (req, accessToken, refreshToken, profile, done)
38
- const accessToken = args[1];
39
- const refreshToken = args[2];
40
- const done = args[4];
41
- try {
42
- const res = await fetch(`${baseUrl}/api/userinfo`, {
43
- headers: { Authorization: `Bearer ${accessToken}` },
44
- });
45
- if (!res.ok) {
46
- return done(new Error(`IAM userinfo failed: ${res.status}`));
47
- }
48
- const userinfo = (await res.json());
49
- done(null, { accessToken, refreshToken, userinfo });
50
- }
51
- catch (err) {
52
- done(err instanceof Error ? err : new Error(String(err)));
53
- }
54
- };
55
- return new OAuth2Strategy({
56
- authorizationURL: `${baseUrl}/login/oauth/authorize`,
57
- tokenURL: `${baseUrl}/api/login/oauth/access_token`,
58
- clientID: config.clientId,
59
- clientSecret: config.clientSecret ?? "",
60
- callbackURL: config.callbackUrl,
61
- scope: config.scope ?? "openid profile email",
62
- state: true,
63
- pkce: true,
64
- passReqToCallback: true,
65
- }, verify);
66
- }
67
- //# sourceMappingURL=passport.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"passport.js","sourceRoot":"","sources":["../src/passport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAiBH;;;;;;;;GAQG;AACH,MAAM,UAAU,yBAAyB,CACvC,MAAyB;IAEzB,+DAA+D;IAC/D,iEAAiE;IACjE,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,iBAAiB,CAK7D,CAAC;IAEF,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAErD,MAAM,MAAM,GAAG,KAAK,EAClB,GAAG,IAAe,EACH,EAAE;QACjB,0EAA0E;QAC1E,MAAM,WAAW,GAAG,IAAI,CAAC,CAAC,CAAW,CAAC;QACtC,MAAM,YAAY,GAAG,IAAI,CAAC,CAAC,CAAuB,CAAC;QACnD,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAwD,CAAC;QAE5E,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,GAAG,OAAO,eAAe,EAAE;gBACjD,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE;aACpD,CAAC,CAAC;YACH,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;gBACZ,OAAO,IAAI,CAAC,IAAI,KAAK,CAAC,wBAAwB,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;YAC/D,CAAC;YACD,MAAM,QAAQ,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAA4B,CAAC;YAC/D,IAAI,CAAC,IAAI,EAAE,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAE,CAAC,CAAC;QACtD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,CAAC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAC5D,CAAC;IACH,CAAC,CAAC;IAEF,OAAO,IAAI,cAAc,CACvB;QACE,gBAAgB,EAAE,GAAG,OAAO,wBAAwB;QACpD,QAAQ,EAAE,GAAG,OAAO,+BAA+B;QACnD,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,YAAY,EAAE,MAAM,CAAC,YAAY,IAAI,EAAE;QACvC,WAAW,EAAE,MAAM,CAAC,WAAW;QAC/B,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,sBAAsB;QAC7C,KAAK,EAAE,IAAI;QACX,IAAI,EAAE,IAAI;QACV,iBAAiB,EAAE,IAAI;KACxB,EACD,MAAM,CACP,CAAC;AACJ,CAAC"}
package/src/betterauth.ts DELETED
@@ -1,91 +0,0 @@
1
- /**
2
- * BetterAuth SSO provider configuration for 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 { iamProvider } from "@hanzo/iam/betterauth";
11
- *
12
- * export const auth = betterAuth({
13
- * socialProviders: [
14
- * iamProvider({
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 IamSocialProvider {
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 IAM.
48
- *
49
- * Works with BetterAuth's SSO plugin or generic OAuth integration.
50
- * Uses standard OIDC endpoints.
51
- */
52
- export function iamProvider(
53
- config: IamConfig & { redirectUri?: string },
54
- ): IamSocialProvider {
55
- const baseUrl = config.serverUrl.replace(/\/+$/, "");
56
-
57
- return {
58
- id: "iam",
59
- name: "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 aliases
86
- /** @deprecated Use iamProvider instead */
87
- export { iamProvider as hanzoIamProvider };
88
- /** @deprecated Use iamProvider instead */
89
- export { iamProvider as hanzoIamSocialProvider };
90
- /** @deprecated Use IamSocialProvider instead */
91
- export type { IamSocialProvider 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
- }