@eaccess/auth 0.1.20 → 0.1.21

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.d.cts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Request, Response, NextFunction } from 'express';
2
2
  import { Pool } from 'pg';
3
+ import { IncomingMessage } from 'http';
3
4
 
4
5
  interface OAuthProviderConfig {
5
6
  clientId: string;
@@ -119,6 +120,10 @@ interface AuthRemember {
119
120
  token: string;
120
121
  expires: Date;
121
122
  }
123
+ interface AuthenticateRequestResult {
124
+ account: AuthAccount | null;
125
+ source: "session" | "remember" | null;
126
+ }
122
127
  interface AuthReset {
123
128
  id: number;
124
129
  account_id: number;
@@ -599,6 +604,7 @@ interface AuthContext {
599
604
  }
600
605
  declare function createAuthContext(config: AuthConfig): AuthContext;
601
606
 
607
+ declare function authenticateRequest(config: AuthConfig, req: IncomingMessage, sessionMiddleware?: (req: any, res: any, next: () => void) => void): Promise<AuthenticateRequestResult>;
602
608
  declare function createUser(config: AuthConfig, credentials: {
603
609
  email: string;
604
610
  password: string;
@@ -654,6 +660,7 @@ declare function forceLogoutForUserBy(config: AuthConfig, identifier: {
654
660
  }>;
655
661
 
656
662
  declare const authFunctions_addRoleForUserBy: typeof addRoleForUserBy;
663
+ declare const authFunctions_authenticateRequest: typeof authenticateRequest;
657
664
  declare const authFunctions_changePasswordForUserBy: typeof changePasswordForUserBy;
658
665
  declare const authFunctions_confirmResetPassword: typeof confirmResetPassword;
659
666
  declare const authFunctions_createUser: typeof createUser;
@@ -667,7 +674,7 @@ declare const authFunctions_resetPassword: typeof resetPassword;
667
674
  declare const authFunctions_setStatusForUserBy: typeof setStatusForUserBy;
668
675
  declare const authFunctions_userExistsByEmail: typeof userExistsByEmail;
669
676
  declare namespace authFunctions {
670
- export { authFunctions_addRoleForUserBy as addRoleForUserBy, authFunctions_changePasswordForUserBy as changePasswordForUserBy, authFunctions_confirmResetPassword as confirmResetPassword, authFunctions_createUser as createUser, authFunctions_deleteUserBy as deleteUserBy, authFunctions_forceLogoutForUserBy as forceLogoutForUserBy, authFunctions_hasRoleForUserBy as hasRoleForUserBy, authFunctions_initiatePasswordResetForUserBy as initiatePasswordResetForUserBy, authFunctions_register as register, authFunctions_removeRoleForUserBy as removeRoleForUserBy, authFunctions_resetPassword as resetPassword, authFunctions_setStatusForUserBy as setStatusForUserBy, authFunctions_userExistsByEmail as userExistsByEmail };
677
+ export { authFunctions_addRoleForUserBy as addRoleForUserBy, authFunctions_authenticateRequest as authenticateRequest, authFunctions_changePasswordForUserBy as changePasswordForUserBy, authFunctions_confirmResetPassword as confirmResetPassword, authFunctions_createUser as createUser, authFunctions_deleteUserBy as deleteUserBy, authFunctions_forceLogoutForUserBy as forceLogoutForUserBy, authFunctions_hasRoleForUserBy as hasRoleForUserBy, authFunctions_initiatePasswordResetForUserBy as initiatePasswordResetForUserBy, authFunctions_register as register, authFunctions_removeRoleForUserBy as removeRoleForUserBy, authFunctions_resetPassword as resetPassword, authFunctions_setStatusForUserBy as setStatusForUserBy, authFunctions_userExistsByEmail as userExistsByEmail };
671
678
  }
672
679
 
673
680
  type UserIdentifier = {
@@ -1193,4 +1200,4 @@ declare class AzureProvider extends BaseOAuthProvider {
1193
1200
  protected exchangeCodeForToken(code: string, tokenUrl: string): Promise<string>;
1194
1201
  }
1195
1202
 
1196
- export { ActivityLogger, type AuthAccount, type AuthActivity, AuthActivityAction, type AuthActivityActionType, type AuthConfig, type AuthConfirmation, type AuthContext, AuthError, type AuthManager$1 as AuthManager, type AuthProvider, type AuthRemember, type AuthReset, AuthRole, type AuthSession, AuthStatus, AzureProvider, type AzureProviderConfig, BaseOAuthProvider, ConfirmationExpiredError, ConfirmationNotFoundError, EmailNotVerifiedError, EmailTakenError, GitHubProvider, type GitHubProviderConfig, GoogleProvider, type GoogleProviderConfig, InvalidBackupCodeError, InvalidEmailError, InvalidPasswordError, InvalidTokenError, InvalidTwoFactorCodeError, type OAuthCallbackResult, type OAuthProvider, type OAuthProviderConfig, type OAuthUserData, OtpProvider, ResetDisabledError, ResetExpiredError, ResetNotFoundError, SecondFactorRequiredError, type TokenCallback, TooManyResetsError, TotpProvider, TwoFactorAlreadyEnabledError, type TwoFactorChallenge, TwoFactorExpiredError, TwoFactorManager, TwoFactorMechanism, type TwoFactorMethod, TwoFactorNotSetupError, TwoFactorSetupIncompleteError, type TwoFactorSetupResult, type TwoFactorToken, type UserIdentifier, UserInactiveError, UserNotFoundError, UserNotLoggedInError, addRoleForUserBy, addRoleToUser, authFunctions, changePasswordForUserBy, cleanupExpiredTokens, confirmResetPassword, createAuthContext, createAuthMiddleware, createAuthTables, createUser, deleteUserBy, dropAuthTables, forceLogoutForUserBy, getAuthTableStats, getUserRoles, hasRoleForUserBy, initiatePasswordResetForUserBy, isValidEmail, register, removeRoleForUserBy, removeRoleFromUser, resetPassword, setStatusForUserBy, setUserRoles, userExistsByEmail, validateEmail };
1203
+ export { ActivityLogger, type AuthAccount, type AuthActivity, AuthActivityAction, type AuthActivityActionType, type AuthConfig, type AuthConfirmation, type AuthContext, AuthError, type AuthManager$1 as AuthManager, type AuthProvider, type AuthRemember, type AuthReset, AuthRole, type AuthSession, AuthStatus, type AuthenticateRequestResult, AzureProvider, type AzureProviderConfig, BaseOAuthProvider, ConfirmationExpiredError, ConfirmationNotFoundError, EmailNotVerifiedError, EmailTakenError, GitHubProvider, type GitHubProviderConfig, GoogleProvider, type GoogleProviderConfig, InvalidBackupCodeError, InvalidEmailError, InvalidPasswordError, InvalidTokenError, InvalidTwoFactorCodeError, type OAuthCallbackResult, type OAuthProvider, type OAuthProviderConfig, type OAuthUserData, OtpProvider, ResetDisabledError, ResetExpiredError, ResetNotFoundError, SecondFactorRequiredError, type TokenCallback, TooManyResetsError, TotpProvider, TwoFactorAlreadyEnabledError, type TwoFactorChallenge, TwoFactorExpiredError, TwoFactorManager, TwoFactorMechanism, type TwoFactorMethod, TwoFactorNotSetupError, TwoFactorSetupIncompleteError, type TwoFactorSetupResult, type TwoFactorToken, type UserIdentifier, UserInactiveError, UserNotFoundError, UserNotLoggedInError, addRoleForUserBy, addRoleToUser, authFunctions, authenticateRequest, changePasswordForUserBy, cleanupExpiredTokens, confirmResetPassword, createAuthContext, createAuthMiddleware, createAuthTables, createUser, deleteUserBy, dropAuthTables, forceLogoutForUserBy, getAuthTableStats, getUserRoles, hasRoleForUserBy, initiatePasswordResetForUserBy, isValidEmail, register, removeRoleForUserBy, removeRoleFromUser, resetPassword, setStatusForUserBy, setUserRoles, userExistsByEmail, validateEmail };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Request, Response, NextFunction } from 'express';
2
2
  import { Pool } from 'pg';
3
+ import { IncomingMessage } from 'http';
3
4
 
4
5
  interface OAuthProviderConfig {
5
6
  clientId: string;
@@ -119,6 +120,10 @@ interface AuthRemember {
119
120
  token: string;
120
121
  expires: Date;
121
122
  }
123
+ interface AuthenticateRequestResult {
124
+ account: AuthAccount | null;
125
+ source: "session" | "remember" | null;
126
+ }
122
127
  interface AuthReset {
123
128
  id: number;
124
129
  account_id: number;
@@ -599,6 +604,7 @@ interface AuthContext {
599
604
  }
600
605
  declare function createAuthContext(config: AuthConfig): AuthContext;
601
606
 
607
+ declare function authenticateRequest(config: AuthConfig, req: IncomingMessage, sessionMiddleware?: (req: any, res: any, next: () => void) => void): Promise<AuthenticateRequestResult>;
602
608
  declare function createUser(config: AuthConfig, credentials: {
603
609
  email: string;
604
610
  password: string;
@@ -654,6 +660,7 @@ declare function forceLogoutForUserBy(config: AuthConfig, identifier: {
654
660
  }>;
655
661
 
656
662
  declare const authFunctions_addRoleForUserBy: typeof addRoleForUserBy;
663
+ declare const authFunctions_authenticateRequest: typeof authenticateRequest;
657
664
  declare const authFunctions_changePasswordForUserBy: typeof changePasswordForUserBy;
658
665
  declare const authFunctions_confirmResetPassword: typeof confirmResetPassword;
659
666
  declare const authFunctions_createUser: typeof createUser;
@@ -667,7 +674,7 @@ declare const authFunctions_resetPassword: typeof resetPassword;
667
674
  declare const authFunctions_setStatusForUserBy: typeof setStatusForUserBy;
668
675
  declare const authFunctions_userExistsByEmail: typeof userExistsByEmail;
669
676
  declare namespace authFunctions {
670
- export { authFunctions_addRoleForUserBy as addRoleForUserBy, authFunctions_changePasswordForUserBy as changePasswordForUserBy, authFunctions_confirmResetPassword as confirmResetPassword, authFunctions_createUser as createUser, authFunctions_deleteUserBy as deleteUserBy, authFunctions_forceLogoutForUserBy as forceLogoutForUserBy, authFunctions_hasRoleForUserBy as hasRoleForUserBy, authFunctions_initiatePasswordResetForUserBy as initiatePasswordResetForUserBy, authFunctions_register as register, authFunctions_removeRoleForUserBy as removeRoleForUserBy, authFunctions_resetPassword as resetPassword, authFunctions_setStatusForUserBy as setStatusForUserBy, authFunctions_userExistsByEmail as userExistsByEmail };
677
+ export { authFunctions_addRoleForUserBy as addRoleForUserBy, authFunctions_authenticateRequest as authenticateRequest, authFunctions_changePasswordForUserBy as changePasswordForUserBy, authFunctions_confirmResetPassword as confirmResetPassword, authFunctions_createUser as createUser, authFunctions_deleteUserBy as deleteUserBy, authFunctions_forceLogoutForUserBy as forceLogoutForUserBy, authFunctions_hasRoleForUserBy as hasRoleForUserBy, authFunctions_initiatePasswordResetForUserBy as initiatePasswordResetForUserBy, authFunctions_register as register, authFunctions_removeRoleForUserBy as removeRoleForUserBy, authFunctions_resetPassword as resetPassword, authFunctions_setStatusForUserBy as setStatusForUserBy, authFunctions_userExistsByEmail as userExistsByEmail };
671
678
  }
672
679
 
673
680
  type UserIdentifier = {
@@ -1193,4 +1200,4 @@ declare class AzureProvider extends BaseOAuthProvider {
1193
1200
  protected exchangeCodeForToken(code: string, tokenUrl: string): Promise<string>;
1194
1201
  }
1195
1202
 
1196
- export { ActivityLogger, type AuthAccount, type AuthActivity, AuthActivityAction, type AuthActivityActionType, type AuthConfig, type AuthConfirmation, type AuthContext, AuthError, type AuthManager$1 as AuthManager, type AuthProvider, type AuthRemember, type AuthReset, AuthRole, type AuthSession, AuthStatus, AzureProvider, type AzureProviderConfig, BaseOAuthProvider, ConfirmationExpiredError, ConfirmationNotFoundError, EmailNotVerifiedError, EmailTakenError, GitHubProvider, type GitHubProviderConfig, GoogleProvider, type GoogleProviderConfig, InvalidBackupCodeError, InvalidEmailError, InvalidPasswordError, InvalidTokenError, InvalidTwoFactorCodeError, type OAuthCallbackResult, type OAuthProvider, type OAuthProviderConfig, type OAuthUserData, OtpProvider, ResetDisabledError, ResetExpiredError, ResetNotFoundError, SecondFactorRequiredError, type TokenCallback, TooManyResetsError, TotpProvider, TwoFactorAlreadyEnabledError, type TwoFactorChallenge, TwoFactorExpiredError, TwoFactorManager, TwoFactorMechanism, type TwoFactorMethod, TwoFactorNotSetupError, TwoFactorSetupIncompleteError, type TwoFactorSetupResult, type TwoFactorToken, type UserIdentifier, UserInactiveError, UserNotFoundError, UserNotLoggedInError, addRoleForUserBy, addRoleToUser, authFunctions, changePasswordForUserBy, cleanupExpiredTokens, confirmResetPassword, createAuthContext, createAuthMiddleware, createAuthTables, createUser, deleteUserBy, dropAuthTables, forceLogoutForUserBy, getAuthTableStats, getUserRoles, hasRoleForUserBy, initiatePasswordResetForUserBy, isValidEmail, register, removeRoleForUserBy, removeRoleFromUser, resetPassword, setStatusForUserBy, setUserRoles, userExistsByEmail, validateEmail };
1203
+ export { ActivityLogger, type AuthAccount, type AuthActivity, AuthActivityAction, type AuthActivityActionType, type AuthConfig, type AuthConfirmation, type AuthContext, AuthError, type AuthManager$1 as AuthManager, type AuthProvider, type AuthRemember, type AuthReset, AuthRole, type AuthSession, AuthStatus, type AuthenticateRequestResult, AzureProvider, type AzureProviderConfig, BaseOAuthProvider, ConfirmationExpiredError, ConfirmationNotFoundError, EmailNotVerifiedError, EmailTakenError, GitHubProvider, type GitHubProviderConfig, GoogleProvider, type GoogleProviderConfig, InvalidBackupCodeError, InvalidEmailError, InvalidPasswordError, InvalidTokenError, InvalidTwoFactorCodeError, type OAuthCallbackResult, type OAuthProvider, type OAuthProviderConfig, type OAuthUserData, OtpProvider, ResetDisabledError, ResetExpiredError, ResetNotFoundError, SecondFactorRequiredError, type TokenCallback, TooManyResetsError, TotpProvider, TwoFactorAlreadyEnabledError, type TwoFactorChallenge, TwoFactorExpiredError, TwoFactorManager, TwoFactorMechanism, type TwoFactorMethod, TwoFactorNotSetupError, TwoFactorSetupIncompleteError, type TwoFactorSetupResult, type TwoFactorToken, type UserIdentifier, UserInactiveError, UserNotFoundError, UserNotLoggedInError, addRoleForUserBy, addRoleToUser, authFunctions, authenticateRequest, changePasswordForUserBy, cleanupExpiredTokens, confirmResetPassword, createAuthContext, createAuthMiddleware, createAuthTables, createUser, deleteUserBy, dropAuthTables, forceLogoutForUserBy, getAuthTableStats, getUserRoles, hasRoleForUserBy, initiatePasswordResetForUserBy, isValidEmail, register, removeRoleForUserBy, removeRoleFromUser, resetPassword, setStatusForUserBy, setUserRoles, userExistsByEmail, validateEmail };
package/dist/index.js CHANGED
@@ -1382,6 +1382,7 @@ var TwoFactorManager = class {
1382
1382
  var auth_functions_exports = {};
1383
1383
  __export(auth_functions_exports, {
1384
1384
  addRoleForUserBy: () => addRoleForUserBy,
1385
+ authenticateRequest: () => authenticateRequest,
1385
1386
  changePasswordForUserBy: () => changePasswordForUserBy,
1386
1387
  confirmResetPassword: () => confirmResetPassword,
1387
1388
  createUser: () => createUser,
@@ -1397,6 +1398,48 @@ __export(auth_functions_exports, {
1397
1398
  });
1398
1399
  import { hash as hash3 } from "@prsm/hash";
1399
1400
  import ms2 from "@prsm/ms";
1401
+ function parseCookies(cookieHeader) {
1402
+ const cookies = {};
1403
+ if (!cookieHeader) return cookies;
1404
+ for (const pair of cookieHeader.split(";")) {
1405
+ const idx = pair.indexOf("=");
1406
+ if (idx === -1) continue;
1407
+ const key = pair.slice(0, idx).trim();
1408
+ const value = pair.slice(idx + 1).trim();
1409
+ if (key) cookies[key] = decodeURIComponent(value);
1410
+ }
1411
+ return cookies;
1412
+ }
1413
+ async function authenticateRequest(config, req, sessionMiddleware) {
1414
+ const queries = new AuthQueries(config);
1415
+ if (sessionMiddleware) {
1416
+ await new Promise((resolve) => {
1417
+ sessionMiddleware(req, {}, resolve);
1418
+ });
1419
+ }
1420
+ const session = req.session;
1421
+ if (session?.auth?.loggedIn && session.auth.accountId) {
1422
+ const account2 = await queries.findAccountById(session.auth.accountId);
1423
+ if (account2 && account2.status === AuthStatus.Normal) {
1424
+ return { account: account2, source: "session" };
1425
+ }
1426
+ }
1427
+ const cookies = parseCookies(req.headers.cookie || "");
1428
+ const cookieName = config.rememberCookieName || "remember_token";
1429
+ const token = cookies[cookieName];
1430
+ if (!token) {
1431
+ return { account: null, source: null };
1432
+ }
1433
+ const remember = await queries.findRememberToken(token);
1434
+ if (!remember || /* @__PURE__ */ new Date() > remember.expires) {
1435
+ return { account: null, source: null };
1436
+ }
1437
+ const account = await queries.findAccountById(remember.account_id);
1438
+ if (!account || account.status !== AuthStatus.Normal) {
1439
+ return { account: null, source: null };
1440
+ }
1441
+ return { account, source: "remember" };
1442
+ }
1400
1443
  function validatePassword(password, config) {
1401
1444
  const minLength = config.minPasswordLength || 8;
1402
1445
  const maxLength = config.maxPasswordLength || 64;
@@ -2715,6 +2758,7 @@ export {
2715
2758
  addRoleForUserBy,
2716
2759
  addRoleToUser,
2717
2760
  auth_functions_exports as authFunctions,
2761
+ authenticateRequest,
2718
2762
  changePasswordForUserBy,
2719
2763
  cleanupExpiredTokens,
2720
2764
  confirmResetPassword,