@multiplatform.one/keycloak 1.0.0 → 1.0.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/lib/index.js CHANGED
@@ -1,4 +1,5 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});require('./chunk-BUZONXAW.js');
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});require('./chunk-WS4IMGVL.js');
2
+ require('./chunk-BUZONXAW.js');
2
3
 
3
4
 
4
5
  var _chunk6APOOEW6js = require('./chunk-6APOOEW6.js');
@@ -9,7 +10,6 @@ var _chunkW4LGQZMFjs = require('./chunk-W4LGQZMF.js');
9
10
 
10
11
 
11
12
  var _chunkEGCPD3BXjs = require('./chunk-EGCPD3BX.js');
12
- require('./chunk-WS4IMGVL.js');
13
13
 
14
14
 
15
15
 
package/lib/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import "./chunk-OAUYHIWG.mjs";
1
2
  import "./chunk-UESWAK47.mjs";
2
3
  import {
3
4
  KeycloakProvider
@@ -9,7 +10,6 @@ import {
9
10
  import {
10
11
  AfterAuth
11
12
  } from "./chunk-IGSEESI3.mjs";
12
- import "./chunk-OAUYHIWG.mjs";
13
13
  import {
14
14
  Authenticated,
15
15
  withAuthenticated
package/lib/routes.d.mts CHANGED
@@ -5,14 +5,21 @@ import { JWT } from 'next-auth/jwt';
5
5
  import { OAuthUserConfig } from 'next-auth/providers';
6
6
  import 'keycloak-js';
7
7
 
8
- declare function createAuthHandler(options?: AuthOptions): any;
9
- declare function createLogoutHandler(options?: AuthOptions): {
8
+ declare function createAuthHandler(options: AuthHandlerOptions): any;
9
+ declare function createLogoutHandler(options: AuthHandlerOptions): {
10
10
  GET(): Promise<next_server.NextResponse<unknown>>;
11
11
  };
12
- declare function createNextAuthOptions(options?: CreateAuthHandlerOptions): AuthOptions;
13
- interface CreateAuthHandlerOptions {
14
- keycloakProvider?: Partial<OAuthUserConfig<any>>;
15
- nextAuth?: AuthOptions;
12
+ declare function createNextAuthOptions(options: AuthHandlerOptions): AuthOptions;
13
+ interface AuthHandlerOptions {
14
+ baseUrl: string;
15
+ keycloak: KeycloakOptions;
16
+ nextAuth?: Partial<AuthOptions>;
17
+ }
18
+ interface KeycloakOptions extends OAuthUserConfig<any> {
19
+ adminPassword?: string;
20
+ adminUsername?: string;
21
+ baseUrl?: string;
22
+ realm?: string;
16
23
  }
17
24
  interface NextToken extends JWT {
18
25
  accessToken?: string;
@@ -24,4 +31,4 @@ interface NextToken extends JWT {
24
31
  roles: string[];
25
32
  }
26
33
 
27
- export { type CreateAuthHandlerOptions, type NextToken, createAuthHandler, createLogoutHandler, createNextAuthOptions };
34
+ export { type AuthHandlerOptions, type KeycloakOptions, type NextToken, createAuthHandler, createLogoutHandler, createNextAuthOptions };
package/lib/routes.d.ts CHANGED
@@ -5,14 +5,21 @@ import { JWT } from 'next-auth/jwt';
5
5
  import { OAuthUserConfig } from 'next-auth/providers';
6
6
  import 'keycloak-js';
7
7
 
8
- declare function createAuthHandler(options?: AuthOptions): any;
9
- declare function createLogoutHandler(options?: AuthOptions): {
8
+ declare function createAuthHandler(options: AuthHandlerOptions): any;
9
+ declare function createLogoutHandler(options: AuthHandlerOptions): {
10
10
  GET(): Promise<next_server.NextResponse<unknown>>;
11
11
  };
12
- declare function createNextAuthOptions(options?: CreateAuthHandlerOptions): AuthOptions;
13
- interface CreateAuthHandlerOptions {
14
- keycloakProvider?: Partial<OAuthUserConfig<any>>;
15
- nextAuth?: AuthOptions;
12
+ declare function createNextAuthOptions(options: AuthHandlerOptions): AuthOptions;
13
+ interface AuthHandlerOptions {
14
+ baseUrl: string;
15
+ keycloak: KeycloakOptions;
16
+ nextAuth?: Partial<AuthOptions>;
17
+ }
18
+ interface KeycloakOptions extends OAuthUserConfig<any> {
19
+ adminPassword?: string;
20
+ adminUsername?: string;
21
+ baseUrl?: string;
22
+ realm?: string;
16
23
  }
17
24
  interface NextToken extends JWT {
18
25
  accessToken?: string;
@@ -24,4 +31,4 @@ interface NextToken extends JWT {
24
31
  roles: string[];
25
32
  }
26
33
 
27
- export { type CreateAuthHandlerOptions, type NextToken, createAuthHandler, createLogoutHandler, createNextAuthOptions };
34
+ export { type AuthHandlerOptions, type KeycloakOptions, type NextToken, createAuthHandler, createLogoutHandler, createNextAuthOptions };
package/lib/routes.js CHANGED
@@ -10,18 +10,24 @@ var _nextAuth;
10
10
  var NextAuth = _chunkP57PW2IIjs.__require.call(void 0, "next-auth").default;
11
11
  var NextResponse = _chunkP57PW2IIjs.__require.call(void 0, "next/server").NextResponse;
12
12
  var logger = console;
13
- function createAuthHandler(options = createNextAuthOptions()) {
14
- return NextAuth(options);
13
+ function createAuthHandler(options) {
14
+ return NextAuth(createNextAuthOptions(options));
15
15
  }
16
- function createLogoutHandler(options = createNextAuthOptions()) {
16
+ var defaults = {
17
+ keycloak: {
18
+ baseUrl: "http://localhost:8080",
19
+ realm: "master"
20
+ }
21
+ };
22
+ function createLogoutHandler(options) {
17
23
  return {
18
24
  async GET() {
19
- const idToken = await _asyncOptionalChain([(await _nextauth.getServerSession.call(void 0, options)), 'optionalAccess', async _ => _.idToken]);
25
+ const idToken = await _asyncOptionalChain([(await _nextauth.getServerSession.call(void 0, createNextAuthOptions(options))), 'optionalAccess', async _ => _.idToken]);
20
26
  if (idToken) {
21
27
  try {
22
28
  await fetch(
23
- `${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM || "master"}/protocol/openid-connect/logout?id_token_hint=${idToken}&post_logout_redirect_uri=${encodeURIComponent(
24
- process.env.NEXTAUTH_URL || ""
29
+ `${_optionalChain([options, 'access', _2 => _2.keycloak, 'optionalAccess', _3 => _3.baseUrl]) || defaults.keycloak.baseUrl}/realms/${_optionalChain([options, 'access', _4 => _4.keycloak, 'optionalAccess', _5 => _5.realm]) || defaults.keycloak.realm}/protocol/openid-connect/logout?id_token_hint=${idToken}&post_logout_redirect_uri=${encodeURIComponent(
30
+ options.baseUrl
25
31
  )}`,
26
32
  { method: "GET" }
27
33
  );
@@ -34,25 +40,23 @@ function createLogoutHandler(options = createNextAuthOptions()) {
34
40
  }
35
41
  };
36
42
  }
37
- function createNextAuthOptions(options = {}) {
43
+ function createNextAuthOptions(options) {
38
44
  if (_nextAuth)
39
45
  return _nextAuth;
40
46
  _nextAuth = {
41
47
  ...options.nextAuth,
42
48
  providers: [
43
49
  KeycloakProvider({
44
- clientId: process.env.KEYCLOAK_CLIENT_ID || "",
45
- clientSecret: process.env.KEYCLOAK_CLIENT_SECRET || "",
46
- issuer: `${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM || "master"}`,
47
- ...options.keycloakProvider
50
+ issuer: `${_optionalChain([options, 'access', _6 => _6.keycloak, 'optionalAccess', _7 => _7.baseUrl]) || defaults.keycloak.baseUrl}/realms/${_optionalChain([options, 'access', _8 => _8.keycloak, 'optionalAccess', _9 => _9.realm]) || defaults.keycloak.realm}`,
51
+ ...options.keycloak
48
52
  }),
49
- ..._optionalChain([options, 'access', _2 => _2.nextAuth, 'optionalAccess', _3 => _3.providers]) || []
53
+ ..._optionalChain([options, 'access', _10 => _10.nextAuth, 'optionalAccess', _11 => _11.providers]) || []
50
54
  ],
51
55
  callbacks: {
52
- ..._optionalChain([options, 'access', _4 => _4.nextAuth, 'optionalAccess', _5 => _5.callbacks]),
56
+ ..._optionalChain([options, 'access', _12 => _12.nextAuth, 'optionalAccess', _13 => _13.callbacks]),
53
57
  async jwt(params) {
54
- if (typeof _optionalChain([options, 'access', _6 => _6.nextAuth, 'optionalAccess', _7 => _7.callbacks, 'optionalAccess', _8 => _8.jwt]) === "function") {
55
- return _optionalChain([options, 'access', _9 => _9.nextAuth, 'optionalAccess', _10 => _10.callbacks, 'access', _11 => _11.jwt, 'call', _12 => _12(params)]);
58
+ if (typeof _optionalChain([options, 'access', _14 => _14.nextAuth, 'optionalAccess', _15 => _15.callbacks, 'optionalAccess', _16 => _16.jwt]) === "function") {
59
+ return _optionalChain([options, 'access', _17 => _17.nextAuth, 'optionalAccess', _18 => _18.callbacks, 'access', _19 => _19.jwt, 'call', _20 => _20(params)]);
56
60
  }
57
61
  const { token, account } = params;
58
62
  if (account) {
@@ -62,20 +66,20 @@ function createNextAuthOptions(options = {}) {
62
66
  token.idToken = account.id_token;
63
67
  token.expiresAt = account.expires_at;
64
68
  token.refreshToken = account.refresh_token;
65
- token.roles = _optionalChain([decoded, 'access', _13 => _13.realm_access, 'optionalAccess', _14 => _14.roles]);
69
+ token.roles = _optionalChain([decoded, 'access', _21 => _21.realm_access, 'optionalAccess', _22 => _22.roles]);
66
70
  return token;
67
71
  } else if (Math.floor(Date.now() / 1e3) < Number(token.expiresAt)) {
68
72
  return token;
69
73
  } else {
70
74
  try {
71
- return refreshAccessToken(token);
75
+ return refreshAccessToken(options, token);
72
76
  } catch (err) {
73
77
  return { ...token, err };
74
78
  }
75
79
  }
76
80
  },
77
81
  async session(params) {
78
- if (typeof _optionalChain([options, 'access', _15 => _15.nextAuth, 'optionalAccess', _16 => _16.callbacks, 'optionalAccess', _17 => _17.session]) === "function") {
82
+ if (typeof _optionalChain([options, 'access', _23 => _23.nextAuth, 'optionalAccess', _24 => _24.callbacks, 'optionalAccess', _25 => _25.session]) === "function") {
79
83
  return options.nextAuth.callbacks.session(params);
80
84
  }
81
85
  const { session: nextSession, token: nextToken } = params;
@@ -92,17 +96,17 @@ function createNextAuthOptions(options = {}) {
92
96
  };
93
97
  return _nextAuth;
94
98
  }
95
- async function refreshAccessToken(nextToken) {
99
+ async function refreshAccessToken(options, nextToken) {
96
100
  const res = await fetch(
97
- `${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM || "master"}/protocol/openid-connect/token`,
101
+ `${_optionalChain([options, 'access', _26 => _26.keycloak, 'optionalAccess', _27 => _27.baseUrl]) || defaults.keycloak.baseUrl}/realms/${_optionalChain([options, 'access', _28 => _28.keycloak, 'optionalAccess', _29 => _29.realm]) || defaults.keycloak.realm}/protocol/openid-connect/token`,
98
102
  {
99
103
  method: "POST",
100
104
  headers: {
101
105
  "Content-Type": "application/x-www-form-urlencoded"
102
106
  },
103
107
  body: new URLSearchParams({
104
- client_id: process.env.KEYCLOAK_CLIENT_ID || "",
105
- client_secret: process.env.KEYCLOAK_CLIENT_SECRET || "",
108
+ client_id: options.keycloak.clientId,
109
+ client_secret: options.keycloak.clientSecret,
106
110
  grant_type: "refresh_token",
107
111
  refresh_token: nextToken.refreshToken
108
112
  })
package/lib/routes.mjs CHANGED
@@ -10,18 +10,24 @@ var _nextAuth;
10
10
  var NextAuth = __require("next-auth").default;
11
11
  var NextResponse = __require("next/server").NextResponse;
12
12
  var logger = console;
13
- function createAuthHandler(options = createNextAuthOptions()) {
14
- return NextAuth(options);
13
+ function createAuthHandler(options) {
14
+ return NextAuth(createNextAuthOptions(options));
15
15
  }
16
- function createLogoutHandler(options = createNextAuthOptions()) {
16
+ var defaults = {
17
+ keycloak: {
18
+ baseUrl: "http://localhost:8080",
19
+ realm: "master"
20
+ }
21
+ };
22
+ function createLogoutHandler(options) {
17
23
  return {
18
24
  async GET() {
19
- const idToken = (await getServerSession(options))?.idToken;
25
+ const idToken = (await getServerSession(createNextAuthOptions(options)))?.idToken;
20
26
  if (idToken) {
21
27
  try {
22
28
  await fetch(
23
- `${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM || "master"}/protocol/openid-connect/logout?id_token_hint=${idToken}&post_logout_redirect_uri=${encodeURIComponent(
24
- process.env.NEXTAUTH_URL || ""
29
+ `${options.keycloak?.baseUrl || defaults.keycloak.baseUrl}/realms/${options.keycloak?.realm || defaults.keycloak.realm}/protocol/openid-connect/logout?id_token_hint=${idToken}&post_logout_redirect_uri=${encodeURIComponent(
30
+ options.baseUrl
25
31
  )}`,
26
32
  { method: "GET" }
27
33
  );
@@ -34,17 +40,15 @@ function createLogoutHandler(options = createNextAuthOptions()) {
34
40
  }
35
41
  };
36
42
  }
37
- function createNextAuthOptions(options = {}) {
43
+ function createNextAuthOptions(options) {
38
44
  if (_nextAuth)
39
45
  return _nextAuth;
40
46
  _nextAuth = {
41
47
  ...options.nextAuth,
42
48
  providers: [
43
49
  KeycloakProvider({
44
- clientId: process.env.KEYCLOAK_CLIENT_ID || "",
45
- clientSecret: process.env.KEYCLOAK_CLIENT_SECRET || "",
46
- issuer: `${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM || "master"}`,
47
- ...options.keycloakProvider
50
+ issuer: `${options.keycloak?.baseUrl || defaults.keycloak.baseUrl}/realms/${options.keycloak?.realm || defaults.keycloak.realm}`,
51
+ ...options.keycloak
48
52
  }),
49
53
  ...options.nextAuth?.providers || []
50
54
  ],
@@ -68,7 +72,7 @@ function createNextAuthOptions(options = {}) {
68
72
  return token;
69
73
  } else {
70
74
  try {
71
- return refreshAccessToken(token);
75
+ return refreshAccessToken(options, token);
72
76
  } catch (err) {
73
77
  return { ...token, err };
74
78
  }
@@ -92,17 +96,17 @@ function createNextAuthOptions(options = {}) {
92
96
  };
93
97
  return _nextAuth;
94
98
  }
95
- async function refreshAccessToken(nextToken) {
99
+ async function refreshAccessToken(options, nextToken) {
96
100
  const res = await fetch(
97
- `${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM || "master"}/protocol/openid-connect/token`,
101
+ `${options.keycloak?.baseUrl || defaults.keycloak.baseUrl}/realms/${options.keycloak?.realm || defaults.keycloak.realm}/protocol/openid-connect/token`,
98
102
  {
99
103
  method: "POST",
100
104
  headers: {
101
105
  "Content-Type": "application/x-www-form-urlencoded"
102
106
  },
103
107
  body: new URLSearchParams({
104
- client_id: process.env.KEYCLOAK_CLIENT_ID || "",
105
- client_secret: process.env.KEYCLOAK_CLIENT_SECRET || "",
108
+ client_id: options.keycloak.clientId,
109
+ client_secret: options.keycloak.clientSecret,
106
110
  grant_type: "refresh_token",
107
111
  refresh_token: nextToken.refreshToken
108
112
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@multiplatform.one/keycloak",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "keycloak client for multiplatform.one ecosystem",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.mjs",
@@ -42,7 +42,7 @@
42
42
  "peerDependencies": {
43
43
  "@react-native-async-storage/async-storage": "1.19.3",
44
44
  "expo-keycloak-auth": "1.0.14",
45
- "multiplatform.one": "^1.0.1",
45
+ "multiplatform.one": "^1.0.2",
46
46
  "next": "^13.0.5",
47
47
  "react": "^18.2.0",
48
48
  "react-native": "~0.73.1"
@@ -61,7 +61,7 @@
61
61
  "expo-keycloak-auth": "1.0.14",
62
62
  "jest": "^29.7.0",
63
63
  "jest-sonar-reporter": "^2.0.0",
64
- "multiplatform.one": "^1.0.1",
64
+ "multiplatform.one": "^1.0.2",
65
65
  "next": "^14.0.4",
66
66
  "prettier": "^3.2.4",
67
67
  "react": "^18.2.0",
package/src/routes.ts CHANGED
@@ -37,21 +37,28 @@ const NextResponse = require('next/server').NextResponse as typeof import('next/
37
37
 
38
38
  const logger = console;
39
39
 
40
- export function createAuthHandler(options = createNextAuthOptions()) {
41
- return NextAuth(options);
40
+ export function createAuthHandler(options: AuthHandlerOptions) {
41
+ return NextAuth(createNextAuthOptions(options));
42
42
  }
43
43
 
44
- export function createLogoutHandler(options = createNextAuthOptions()) {
44
+ const defaults = {
45
+ keycloak: {
46
+ baseUrl: 'http://localhost:8080',
47
+ realm: 'master',
48
+ },
49
+ };
50
+
51
+ export function createLogoutHandler(options: AuthHandlerOptions) {
45
52
  return {
46
53
  async GET() {
47
- const idToken = ((await getServerSession(options)) as Session)?.idToken;
54
+ const idToken = ((await getServerSession(createNextAuthOptions(options))) as Session)?.idToken;
48
55
  if (idToken) {
49
56
  try {
50
57
  await fetch(
51
- `${process.env.KEYCLOAK_BASE_URL}/realms/${
52
- process.env.KEYCLOAK_REALM || 'master'
58
+ `${options.keycloak?.baseUrl || defaults.keycloak.baseUrl}/realms/${
59
+ options.keycloak?.realm || defaults.keycloak.realm
53
60
  }/protocol/openid-connect/logout?id_token_hint=${idToken}&post_logout_redirect_uri=${encodeURIComponent(
54
- process.env.NEXTAUTH_URL || '',
61
+ options.baseUrl,
55
62
  )}`,
56
63
  { method: 'GET' },
57
64
  );
@@ -65,16 +72,14 @@ export function createLogoutHandler(options = createNextAuthOptions()) {
65
72
  };
66
73
  }
67
74
 
68
- export function createNextAuthOptions(options: CreateAuthHandlerOptions = {}) {
75
+ export function createNextAuthOptions(options: AuthHandlerOptions) {
69
76
  if (_nextAuth) return _nextAuth;
70
77
  _nextAuth = {
71
78
  ...options.nextAuth,
72
79
  providers: [
73
80
  KeycloakProvider({
74
- clientId: process.env.KEYCLOAK_CLIENT_ID || '',
75
- clientSecret: process.env.KEYCLOAK_CLIENT_SECRET || '',
76
- issuer: `${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM || 'master'}`,
77
- ...options.keycloakProvider,
81
+ issuer: `${options.keycloak?.baseUrl || defaults.keycloak.baseUrl}/realms/${options.keycloak?.realm || defaults.keycloak.realm}`,
82
+ ...options.keycloak,
78
83
  }),
79
84
  ...(options.nextAuth?.providers || []),
80
85
  ],
@@ -98,7 +103,7 @@ export function createNextAuthOptions(options: CreateAuthHandlerOptions = {}) {
98
103
  return token;
99
104
  } else {
100
105
  try {
101
- return refreshAccessToken(token as NextToken);
106
+ return refreshAccessToken(options, token as NextToken);
102
107
  } catch (err) {
103
108
  return { ...token, err };
104
109
  }
@@ -121,9 +126,17 @@ export function createNextAuthOptions(options: CreateAuthHandlerOptions = {}) {
121
126
  return _nextAuth;
122
127
  }
123
128
 
124
- export interface CreateAuthHandlerOptions {
125
- keycloakProvider?: Partial<OAuthUserConfig<any>>;
126
- nextAuth?: AuthOptions;
129
+ export interface AuthHandlerOptions {
130
+ baseUrl: string;
131
+ keycloak: KeycloakOptions;
132
+ nextAuth?: Partial<AuthOptions>;
133
+ }
134
+
135
+ export interface KeycloakOptions extends OAuthUserConfig<any> {
136
+ adminPassword?: string;
137
+ adminUsername?: string;
138
+ baseUrl?: string;
139
+ realm?: string;
127
140
  }
128
141
 
129
142
  export interface NextToken extends JWT {
@@ -136,17 +149,19 @@ export interface NextToken extends JWT {
136
149
  roles: string[];
137
150
  }
138
151
 
139
- async function refreshAccessToken(nextToken: NextToken) {
152
+ async function refreshAccessToken(options: AuthHandlerOptions, nextToken: NextToken) {
140
153
  const res = await fetch(
141
- `${process.env.KEYCLOAK_BASE_URL}/realms/${process.env.KEYCLOAK_REALM || 'master'}/protocol/openid-connect/token`,
154
+ `${options.keycloak?.baseUrl || defaults.keycloak.baseUrl}/realms/${
155
+ options.keycloak?.realm || defaults.keycloak.realm
156
+ }/protocol/openid-connect/token`,
142
157
  {
143
158
  method: 'POST',
144
159
  headers: {
145
160
  'Content-Type': 'application/x-www-form-urlencoded',
146
161
  },
147
162
  body: new URLSearchParams({
148
- client_id: process.env.KEYCLOAK_CLIENT_ID || '',
149
- client_secret: process.env.KEYCLOAK_CLIENT_SECRET || '',
163
+ client_id: options.keycloak.clientId,
164
+ client_secret: options.keycloak.clientSecret,
150
165
  grant_type: 'refresh_token',
151
166
  refresh_token: nextToken.refreshToken,
152
167
  }),