@dloizides/auth-client 2.1.0 → 3.2.1

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/index.js CHANGED
@@ -1059,6 +1059,179 @@ var AuthApiClient = class {
1059
1059
  }
1060
1060
  };
1061
1061
 
1062
+ // src/bff/BffAuthClient.ts
1063
+ var CSRF_HEADER = "X-BFF-Csrf";
1064
+ var CSRF_HEADER_VALUE = "1";
1065
+ var JSON_CONTENT_TYPE = "application/json";
1066
+ var ENDPOINTS = {
1067
+ login: "/bff/login",
1068
+ logout: "/bff/logout",
1069
+ me: "/bff/me",
1070
+ register: "/bff/register",
1071
+ forgotPassword: "/bff/forgot-password",
1072
+ resetPassword: "/bff/reset-password",
1073
+ otpRequest: "/bff/otp/request",
1074
+ otpVerify: "/bff/otp/verify",
1075
+ pinLogin: "/bff/pin/login"
1076
+ };
1077
+ function isRecord(value) {
1078
+ return typeof value === "object" && value !== null;
1079
+ }
1080
+ function extractUser(data) {
1081
+ if (!isRecord(data)) {
1082
+ return null;
1083
+ }
1084
+ const envelope = data;
1085
+ return isRecord(envelope.user) ? envelope.user : null;
1086
+ }
1087
+ function toOtpRequestResult(data) {
1088
+ if (!isRecord(data)) {
1089
+ return { success: true, expiresIn: 0, code: null };
1090
+ }
1091
+ return {
1092
+ success: typeof data.success === "boolean" ? data.success : true,
1093
+ expiresIn: typeof data.expiresIn === "number" ? data.expiresIn : 0,
1094
+ code: typeof data.code === "string" ? data.code : null
1095
+ };
1096
+ }
1097
+ var BffAuthClient = class {
1098
+ constructor(options) {
1099
+ this.http = options.http;
1100
+ this.baseUrl = (options.baseUrl ?? "").replace(/\/$/, "");
1101
+ }
1102
+ /**
1103
+ * `POST /bff/login` — the BFF does ROPC against Keycloak server-side, stores
1104
+ * the tokens in its Redis vault, and sets the httpOnly session cookie.
1105
+ * Returns the sanitised user. Throws on a non-2xx response.
1106
+ */
1107
+ async login(request) {
1108
+ const data = await this.postState(ENDPOINTS.login, request, "login");
1109
+ const user = extractUser(data);
1110
+ if (user === null) {
1111
+ throw new Error("login: BFF response missing user");
1112
+ }
1113
+ return user;
1114
+ }
1115
+ /**
1116
+ * `POST /bff/logout` — the BFF calls KC end-session, deletes the Redis
1117
+ * session, and clears the cookie. Non-fatal: a failed logout still leaves
1118
+ * the SPA logged out client-side. Throws only on a non-2xx response.
1119
+ */
1120
+ async logout() {
1121
+ await this.postState(ENDPOINTS.logout, void 0, "logout");
1122
+ }
1123
+ /**
1124
+ * `GET /bff/me` — the live session's sanitised user, or `null` when there is
1125
+ * no session (the BFF answers `401`). Used at app load to bootstrap auth
1126
+ * state in place of the old token-in-storage check.
1127
+ */
1128
+ async getCurrentUser() {
1129
+ const response = await this.http({
1130
+ url: `${this.baseUrl}${ENDPOINTS.me}`,
1131
+ method: "GET",
1132
+ headers: { Accept: JSON_CONTENT_TYPE },
1133
+ credentials: "include"
1134
+ });
1135
+ if (!response.ok) {
1136
+ return null;
1137
+ }
1138
+ return extractUser(response.data);
1139
+ }
1140
+ /**
1141
+ * `POST /bff/register` — the BFF proxies registration to TenantService and,
1142
+ * on success, establishes a session exactly like `login`. Returns the user.
1143
+ */
1144
+ async register(request) {
1145
+ const data = await this.postState(ENDPOINTS.register, request, "register");
1146
+ const user = extractUser(data);
1147
+ if (user === null) {
1148
+ throw new Error("register: BFF response missing user");
1149
+ }
1150
+ return user;
1151
+ }
1152
+ /**
1153
+ * `POST /bff/forgot-password` — proxied to TenantService. The backend
1154
+ * returns 200 unconditionally (no email enumeration); anything else throws.
1155
+ */
1156
+ async forgotPassword(request) {
1157
+ await this.postState(ENDPOINTS.forgotPassword, request, "forgot-password");
1158
+ }
1159
+ /**
1160
+ * `POST /bff/reset-password` — proxied to TenantService. Throws on a non-2xx
1161
+ * response (e.g. `400` for an invalid / expired token).
1162
+ */
1163
+ async resetPassword(request) {
1164
+ await this.postState(ENDPOINTS.resetPassword, request, "reset-password");
1165
+ }
1166
+ /**
1167
+ * `POST /bff/otp/request` — the BFF proxies to TenantService, which generates
1168
+ * a short-TTL code and emails it.
1169
+ *
1170
+ * The endpoint is anti-enumeration: a `200` is the normal path whether or not
1171
+ * the identifier is registered. This method therefore **returns** the relayed
1172
+ * `{ success, expiresIn, code }` body (so the UI can show the expiry) rather
1173
+ * than treating a 200 as opaque. It still throws on a non-2xx — a `501`
1174
+ * (OTP not enabled) or `502` (upstream down) is a real failure to surface.
1175
+ */
1176
+ async requestOtp(request) {
1177
+ const data = await this.postState(ENDPOINTS.otpRequest, request, "otp-request");
1178
+ return toOtpRequestResult(data);
1179
+ }
1180
+ /**
1181
+ * `POST /bff/otp/verify` — the BFF runs the OTP direct-grant against Keycloak
1182
+ * server-side, stores the tokens in its Redis vault, and sets the httpOnly
1183
+ * session cookie. Returns the sanitised user, exactly like `login`. Throws on
1184
+ * a non-2xx (e.g. `401` for a bad / expired code).
1185
+ */
1186
+ async verifyOtp(request) {
1187
+ const data = await this.postState(ENDPOINTS.otpVerify, request, "otp-verify");
1188
+ const user = extractUser(data);
1189
+ if (user === null) {
1190
+ throw new Error("otp-verify: BFF response missing user");
1191
+ }
1192
+ return user;
1193
+ }
1194
+ /**
1195
+ * `POST /bff/pin/login` — the BFF runs the event-scoped PIN direct-grant
1196
+ * against Keycloak server-side (the `(event, pin)` pair resolves to the
1197
+ * staff member's KC account + event-scoped role), stores the tokens in its
1198
+ * Redis vault, and sets the httpOnly session cookie. Returns the sanitised
1199
+ * user, exactly like `login` / `verifyOtp`. Throws on a non-2xx — `401` for
1200
+ * a bad / expired / locked-out PIN or an unknown event, `501` when PIN login
1201
+ * is not an enabled method for this BFF.
1202
+ */
1203
+ async pinLogin(request) {
1204
+ const data = await this.postState(ENDPOINTS.pinLogin, request, "pin-login");
1205
+ const user = extractUser(data);
1206
+ if (user === null) {
1207
+ throw new Error("pin-login: BFF response missing user");
1208
+ }
1209
+ return user;
1210
+ }
1211
+ /**
1212
+ * Shared POST for every state-changing `/bff/*` call: same-origin, cookie
1213
+ * included, `X-BFF-Csrf` header attached. Throws a labelled error on non-2xx.
1214
+ */
1215
+ async postState(path, body, label) {
1216
+ const headers = {
1217
+ "Content-Type": JSON_CONTENT_TYPE,
1218
+ Accept: JSON_CONTENT_TYPE,
1219
+ [CSRF_HEADER]: CSRF_HEADER_VALUE
1220
+ };
1221
+ const response = await this.http({
1222
+ url: `${this.baseUrl}${path}`,
1223
+ method: "POST",
1224
+ headers,
1225
+ body: body === void 0 ? void 0 : JSON.stringify(body),
1226
+ credentials: "include"
1227
+ });
1228
+ if (!response.ok) {
1229
+ throw new Error(`${label} failed with status ${String(response.status)}`);
1230
+ }
1231
+ return response.data;
1232
+ }
1233
+ };
1234
+
1062
1235
  // src/utils/normalizeKeycloakUser.ts
1063
1236
  function isNonEmptyString(value) {
1064
1237
  return typeof value === "string" && value !== "";
@@ -1172,6 +1345,7 @@ function decodeUtf8(binary) {
1172
1345
  exports.AuthApiClient = AuthApiClient;
1173
1346
  exports.AuthClient = AuthClient;
1174
1347
  exports.AuthEventEmitter = AuthEventEmitter;
1348
+ exports.BffAuthClient = BffAuthClient;
1175
1349
  exports.BiometricGate = BiometricGate;
1176
1350
  exports.BrowserStorageTokenStorage = BrowserStorageTokenStorage;
1177
1351
  exports.CookieTokenStorage = CookieTokenStorage;