New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@accounts/server

Package Overview
Dependencies
Maintainers
3
Versions
204
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@accounts/server - npm Package Compare versions

Comparing version 0.33.1 to 1.0.0-alpha-20231022085748-0910b9e1

lib/types/AccountsCoreConfig.symbol.d.ts

23

lib/accounts-server.d.ts
import * as jwt from 'jsonwebtoken';
import Emittery from 'emittery';
import { User, LoginResult, Tokens, Session, ImpersonationUserIdentity, ImpersonationResult, HookListener, AuthenticationService, ConnectionInformations } from '@accounts/types';
import { User, LoginResult, Tokens, Session, ImpersonationUserIdentity, ImpersonationResult, HookListener, DatabaseInterface, ConnectionInformations, DatabaseInterfaceUser, DatabaseInterfaceSessions } from '@accounts/types';
import { AccountsServerOptions } from './types/accounts-server-options';
import { EmailTemplateType } from './types/email-template-type';
import { ExecutionContext } from 'graphql-modules';
import { AuthenticationServices } from './types/authentication-services';
declare const defaultOptions: {
micro: boolean;
ambiguousErrorMessages: boolean;

@@ -23,3 +26,3 @@ tokenSecret: string | {

siteUrl: string;
userObjectSanitizer: (user: User) => User;
userObjectSanitizer: <CustomUser extends User = User>(user: CustomUser) => CustomUser;
createNewSessionTokenOnRefresh: boolean;

@@ -30,12 +33,12 @@ useInternalUserObjectSanitizer: boolean;

export declare class AccountsServer<CustomUser extends User = User> {
services: AuthenticationServices<CustomUser>;
private db;
context: ExecutionContext;
options: AccountsServerOptions<CustomUser> & typeof defaultOptions;
private services;
private db;
private hooks;
constructor(options: AccountsServerOptions<CustomUser>, services: {
[key: string]: AuthenticationService<CustomUser>;
});
getServices(): {
[key: string]: AuthenticationService;
};
private micro;
private dbSessions;
constructor(options: AccountsServerOptions<CustomUser>, services: AuthenticationServices<CustomUser>, db: DatabaseInterface<CustomUser> | DatabaseInterfaceUser<CustomUser>, dbSessions?: DatabaseInterfaceSessions);
private getService;
getServices(): AuthenticationServices<CustomUser>;
getOptions(): AccountsServerOptions<CustomUser>;

@@ -42,0 +45,0 @@ getHooks(): Emittery;

@@ -14,3 +14,9 @@ "use strict";

const validation_1 = require("./utils/validation");
const graphql_modules_1 = require("graphql-modules");
const AccountsCoreConfig_symbol_1 = require("./types/AccountsCoreConfig.symbol");
const AuthenticationServices_symbol_1 = require("./types/AuthenticationServices.symbol");
const DatabaseInterfaceUser_symbol_1 = require("./types/DatabaseInterfaceUser.symbol");
const DatabaseInterfaceSessions_symbol_1 = require("./types/DatabaseInterfaceSessions.symbol");
const defaultOptions = {
micro: false,
ambiguousErrorMessages: true,

@@ -34,8 +40,9 @@ tokenSecret: 'secret',

};
class AccountsServer {
constructor(options, services) {
this.options = lodash_merge_1.default({ ...defaultOptions }, options);
if (!this.options.db) {
throw new Error('A database driver is required');
}
let AccountsServer = class AccountsServer {
constructor(options, services, db, dbSessions) {
this.services = services;
this.db = db;
this.options = (0, lodash_merge_1.default)({ ...defaultOptions }, options);
this.micro = this.options.micro;
this.dbSessions = dbSessions !== null && dbSessions !== void 0 ? dbSessions : db;
if (this.options.tokenSecret === defaultOptions.tokenSecret) {

@@ -51,7 +58,11 @@ console.log(`

this.services = services || {};
this.db = this.options.db;
// Set the db to all services
for (const service in this.services) {
this.services[service].setStore(this.db);
this.services[service].server = this;
for (const serviceName in this.services) {
const service = this.services[serviceName];
// We have an instance, thus no Dependency Injection is being used (REST)
// and we must manually set the db and server in all services
if (typeof service === 'object') {
service.setUserStore(this.db);
service.setSessionsStore(this.dbSessions);
service.server = this;
}
}

@@ -61,2 +72,13 @@ // Initialize hooks

}
getService(serviceName) {
const instanceOrCtor = this.services[serviceName];
// If it's a constructor we use dependency injection (GraphQL), otherwise we already have an instance (REST)
const service = typeof instanceOrCtor === 'function'
? this.context.injector.get(instanceOrCtor)
: instanceOrCtor;
if (!service) {
throw new accounts_error_1.AccountsJsError(`No service with the name ${serviceName} was registered.`, errors_1.AuthenticateWithServiceErrors.ServiceNotFound);
}
return service;
}
getServices() {

@@ -98,6 +120,3 @@ return this.services;

try {
if (!this.services[serviceName]) {
throw new accounts_error_1.AccountsJsError(`No service with the name ${serviceName} was registered.`, errors_1.AuthenticateWithServiceErrors.ServiceNotFound);
}
const user = await this.services[serviceName].authenticate(params);
const user = await this.getService(serviceName).authenticate(params);
hooksInfo.user = user;

@@ -131,6 +150,3 @@ if (!user) {

try {
if (!this.services[serviceName]) {
throw new accounts_error_1.AccountsJsError(`No service with the name ${serviceName} was registered.`, errors_1.LoginWithServiceErrors.ServiceNotFound);
}
const user = await this.services[serviceName].authenticate(params);
const user = await this.getService(serviceName).authenticate(params);
hooksInfo.user = user;

@@ -164,3 +180,3 @@ if (!user) {

const token = await this.createSessionToken(user);
const sessionId = await this.db.createSession(user.id, token, infos);
const sessionId = await this.dbSessions.createSession(user.id, token, infos);
const { accessToken, refreshToken } = await this.createTokens({

@@ -222,3 +238,3 @@ token,

const token = await this.createSessionToken(impersonatedUser);
const newSessionId = await this.db.createSession(impersonatedUser.id, token, infos, {
const newSessionId = await this.dbSessions.createSession(impersonatedUser.id, token, infos, {
impersonatorUserId: user.id,

@@ -258,3 +274,3 @@ });

try {
if (!validation_1.isString(accessToken) || !validation_1.isString(refreshToken)) {
if (!(0, validation_1.isString)(accessToken) || !(0, validation_1.isString)(refreshToken)) {
throw new accounts_error_1.AccountsJsError('An accessToken and refreshToken are required', errors_1.RefreshTokensErrors.InvalidTokens);

@@ -273,3 +289,3 @@ }

}
const session = await this.db.findSessionByToken(sessionToken);
const session = await this.dbSessions.findSessionByToken(sessionToken);
if (!session) {

@@ -288,3 +304,3 @@ throw new accounts_error_1.AccountsJsError('Session not found', errors_1.RefreshTokensErrors.SessionNotFound);

const tokens = await this.createTokens({ token: newToken || sessionToken, user });
await this.db.updateSession(session.id, infos, newToken);
await this.dbSessions.updateSession(session.id, infos, newToken);
const result = {

@@ -322,3 +338,3 @@ sessionId: session.id,

};
const accessToken = tokens_1.generateAccessToken({
const accessToken = (0, tokens_1.generateAccessToken)({
payload: await this.createJwtPayload(jwtData, user),

@@ -328,3 +344,3 @@ secret: this.getSecretOrPrivateKey(),

});
const refreshToken = tokens_1.generateRefreshToken({
const refreshToken = (0, tokens_1.generateRefreshToken)({
secret: this.getSecretOrPrivateKey(),

@@ -345,3 +361,3 @@ config: tokenConfigs.refreshToken,

if (session.valid) {
await this.db.invalidateSession(session.id);
await this.dbSessions.invalidateSession(session.id);
await this.hooks.emit(server_hooks_1.ServerHooks.LogoutSuccess, {

@@ -372,3 +388,3 @@ session,

try {
if (!validation_1.isString(accessToken)) {
if (!(0, validation_1.isString)(accessToken)) {
throw new accounts_error_1.AccountsJsError('An accessToken is required', errors_1.ResumeSessionErrors.InvalidToken);

@@ -386,6 +402,10 @@ }

}
if (this.micro) {
// We need to only verify the access tokens without any additional session logic
return { id: userId };
}
// If the session is stateful we check the validity of the token against the db
let session = null;
if (!this.options.useStatelessSession) {
session = await this.db.findSessionByToken(sessionToken);
session = await this.dbSessions.findSessionByToken(sessionToken);
if (!session) {

@@ -418,3 +438,3 @@ throw new accounts_error_1.AccountsJsError('Session not found', errors_1.ResumeSessionErrors.SessionNotFound);

async findSessionByAccessToken(accessToken) {
if (!validation_1.isString(accessToken)) {
if (!(0, validation_1.isString)(accessToken)) {
throw new accounts_error_1.AccountsJsError('An accessToken is required', errors_1.FindSessionByAccessTokenErrors.InvalidToken);

@@ -430,3 +450,3 @@ }

}
const session = await this.db.findSessionByToken(sessionToken);
const session = await this.dbSessions.findSessionByToken(sessionToken);
if (!session) {

@@ -498,3 +518,3 @@ throw new accounts_error_1.AccountsJsError('Session not found', errors_1.FindSessionByAccessTokenErrors.SessionNotFound);

? this.options.tokenCreator.createToken(user)
: tokens_1.generateRandomToken();
: (0, tokens_1.generateRandomToken)();
}

@@ -519,5 +539,19 @@ async createJwtPayload(data, user) {

}
}
};
exports.AccountsServer = AccountsServer;
tslib_1.__decorate([
(0, graphql_modules_1.ExecutionContext)(),
tslib_1.__metadata("design:type", Object)
], AccountsServer.prototype, "context", void 0);
exports.AccountsServer = AccountsServer = tslib_1.__decorate([
(0, graphql_modules_1.Injectable)({
global: true,
}),
tslib_1.__param(0, (0, graphql_modules_1.Inject)(AccountsCoreConfig_symbol_1.AccountsCoreConfigToken)),
tslib_1.__param(1, (0, graphql_modules_1.Inject)(AuthenticationServices_symbol_1.AuthenticationServicesToken)),
tslib_1.__param(2, (0, graphql_modules_1.Inject)(DatabaseInterfaceUser_symbol_1.DatabaseInterfaceUserToken)),
tslib_1.__param(3, (0, graphql_modules_1.Inject)(DatabaseInterfaceSessions_symbol_1.DatabaseInterfaceSessionsToken)),
tslib_1.__metadata("design:paramtypes", [Object, Object, Object, Object])
], AccountsServer);
exports.default = AccountsServer;
//# sourceMappingURL=accounts-server.js.map

@@ -18,3 +18,3 @@ "use strict";

AuthenticateWithServiceErrors["AuthenticationFailed"] = "AuthenticationFailed";
})(AuthenticateWithServiceErrors = exports.AuthenticateWithServiceErrors || (exports.AuthenticateWithServiceErrors = {}));
})(AuthenticateWithServiceErrors || (exports.AuthenticateWithServiceErrors = AuthenticateWithServiceErrors = {}));
var LoginWithServiceErrors;

@@ -34,3 +34,3 @@ (function (LoginWithServiceErrors) {

LoginWithServiceErrors["AuthenticationFailed"] = "AuthenticationFailed";
})(LoginWithServiceErrors = exports.LoginWithServiceErrors || (exports.LoginWithServiceErrors = {}));
})(LoginWithServiceErrors || (exports.LoginWithServiceErrors = LoginWithServiceErrors = {}));
var ImpersonateErrors;

@@ -64,3 +64,3 @@ (function (ImpersonateErrors) {

ImpersonateErrors["SessionNotFound"] = "SessionNotFound";
})(ImpersonateErrors = exports.ImpersonateErrors || (exports.ImpersonateErrors = {}));
})(ImpersonateErrors || (exports.ImpersonateErrors = ImpersonateErrors = {}));
var RefreshTokensErrors;

@@ -88,3 +88,3 @@ (function (RefreshTokensErrors) {

RefreshTokensErrors["InvalidSession"] = "InvalidSession";
})(RefreshTokensErrors = exports.RefreshTokensErrors || (exports.RefreshTokensErrors = {}));
})(RefreshTokensErrors || (exports.RefreshTokensErrors = RefreshTokensErrors = {}));
var LogoutErrors;

@@ -109,3 +109,3 @@ (function (LogoutErrors) {

LogoutErrors["SessionNotFound"] = "SessionNotFound";
})(LogoutErrors = exports.LogoutErrors || (exports.LogoutErrors = {}));
})(LogoutErrors || (exports.LogoutErrors = LogoutErrors = {}));
var FindSessionByAccessTokenErrors;

@@ -125,3 +125,3 @@ (function (FindSessionByAccessTokenErrors) {

FindSessionByAccessTokenErrors["SessionNotFound"] = "SessionNotFound";
})(FindSessionByAccessTokenErrors = exports.FindSessionByAccessTokenErrors || (exports.FindSessionByAccessTokenErrors = {}));
})(FindSessionByAccessTokenErrors || (exports.FindSessionByAccessTokenErrors = FindSessionByAccessTokenErrors = {}));
var ResumeSessionErrors;

@@ -150,3 +150,3 @@ (function (ResumeSessionErrors) {

ResumeSessionErrors["SessionNotFound"] = "SessionNotFound";
})(ResumeSessionErrors = exports.ResumeSessionErrors || (exports.ResumeSessionErrors = {}));
})(ResumeSessionErrors || (exports.ResumeSessionErrors = ResumeSessionErrors = {}));
//# sourceMappingURL=errors.js.map

@@ -7,4 +7,9 @@ import { AccountsServer } from './accounts-server';

export { AccountsJsError } from './utils/accounts-error';
export { DatabaseInterfaceUserToken } from './types/DatabaseInterfaceUser.symbol';
export { DatabaseInterfaceSessionsToken } from './types/DatabaseInterfaceSessions.symbol';
export { AccountsCoreConfigToken } from './types/AccountsCoreConfig.symbol';
export { AuthenticationServicesToken } from './types/AuthenticationServices.symbol';
export { AuthenticationServices } from './types/authentication-services';
export { AuthenticateWithServiceErrors, LoginWithServiceErrors, ImpersonateErrors, FindSessionByAccessTokenErrors, RefreshTokensErrors, LogoutErrors, ResumeSessionErrors, } from './errors';
export default AccountsServer;
export { AccountsServer };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccountsServer = exports.ResumeSessionErrors = exports.LogoutErrors = exports.RefreshTokensErrors = exports.FindSessionByAccessTokenErrors = exports.ImpersonateErrors = exports.LoginWithServiceErrors = exports.AuthenticateWithServiceErrors = exports.AccountsJsError = exports.ServerHooks = exports.getFirstUserEmail = exports.generateRandomToken = void 0;
exports.AccountsServer = exports.ResumeSessionErrors = exports.LogoutErrors = exports.RefreshTokensErrors = exports.FindSessionByAccessTokenErrors = exports.ImpersonateErrors = exports.LoginWithServiceErrors = exports.AuthenticateWithServiceErrors = exports.AuthenticationServicesToken = exports.AccountsCoreConfigToken = exports.DatabaseInterfaceSessionsToken = exports.DatabaseInterfaceUserToken = exports.AccountsJsError = exports.ServerHooks = exports.getFirstUserEmail = exports.generateRandomToken = void 0;
const accounts_server_1 = require("./accounts-server");

@@ -14,2 +14,10 @@ Object.defineProperty(exports, "AccountsServer", { enumerable: true, get: function () { return accounts_server_1.AccountsServer; } });

Object.defineProperty(exports, "AccountsJsError", { enumerable: true, get: function () { return accounts_error_1.AccountsJsError; } });
var DatabaseInterfaceUser_symbol_1 = require("./types/DatabaseInterfaceUser.symbol");
Object.defineProperty(exports, "DatabaseInterfaceUserToken", { enumerable: true, get: function () { return DatabaseInterfaceUser_symbol_1.DatabaseInterfaceUserToken; } });
var DatabaseInterfaceSessions_symbol_1 = require("./types/DatabaseInterfaceSessions.symbol");
Object.defineProperty(exports, "DatabaseInterfaceSessionsToken", { enumerable: true, get: function () { return DatabaseInterfaceSessions_symbol_1.DatabaseInterfaceSessionsToken; } });
var AccountsCoreConfig_symbol_1 = require("./types/AccountsCoreConfig.symbol");
Object.defineProperty(exports, "AccountsCoreConfigToken", { enumerable: true, get: function () { return AccountsCoreConfig_symbol_1.AccountsCoreConfigToken; } });
var AuthenticationServices_symbol_1 = require("./types/AuthenticationServices.symbol");
Object.defineProperty(exports, "AuthenticationServicesToken", { enumerable: true, get: function () { return AuthenticationServices_symbol_1.AuthenticationServicesToken; } });
var errors_1 = require("./errors");

@@ -16,0 +24,0 @@ Object.defineProperty(exports, "AuthenticateWithServiceErrors", { enumerable: true, get: function () { return errors_1.AuthenticateWithServiceErrors; } });

import * as jwt from 'jsonwebtoken';
import { User, DatabaseInterface, Session } from '@accounts/types';
import { User, Session } from '@accounts/types';
import { EmailTemplatesType } from './email-templates-type';

@@ -9,2 +9,3 @@ import { PrepareMailFunction } from './prepare-mail-function';

export interface AccountsServerOptions<CustomUser extends User = User> {
micro?: boolean;
/**

@@ -14,3 +15,2 @@ * Return ambiguous error messages from login failures to prevent user enumeration. Defaults to true.

ambiguousErrorMessages?: boolean;
db?: DatabaseInterface<CustomUser>;
tokenSecret: string | {

@@ -17,0 +17,0 @@ publicKey: jwt.Secret;

import { EmailTemplateType } from './email-template-type';
export declare type EmailType = EmailTemplateType & {
export type EmailType = EmailTemplateType & {
to: string;
};
import { User } from '@accounts/types';
import { EmailTemplateType } from './email-template-type';
export declare type PrepareMailFunction = (to: string, token: string, user: User, pathFragment: string, emailTemplate: EmailTemplateType, from: string) => object;
export type PrepareMailFunction = (to: string, token: string, user: User, pathFragment: string, emailTemplate: EmailTemplateType, from: string) => object;

@@ -1,1 +0,1 @@

export declare type SendMailType = (mail: any) => Promise<void>;
export type SendMailType = (mail: any) => Promise<void>;
import { EmailTemplatesType } from '../types/email-templates-type';
export declare const emailTemplates: EmailTemplatesType;
export declare type SendMailType = (mail: object) => Promise<void>;
export type SendMailType = (mail: object) => Promise<void>;
export declare const sendMail: (mail: object) => Promise<void>;

@@ -10,3 +10,3 @@ "use strict";

*/
const generateRandomToken = (length = 43) => crypto_1.randomBytes(length).toString('hex');
const generateRandomToken = (length = 43) => (0, crypto_1.randomBytes)(length).toString('hex');
exports.generateRandomToken = generateRandomToken;

@@ -13,0 +13,0 @@ const generateAccessToken = ({ secret, payload = {}, config, }) => jwt.sign(payload, secret, config);

{
"name": "@accounts/server",
"version": "0.33.1",
"version": "1.0.0-alpha-20231022085748-0910b9e1",
"description": "Fullstack authentication and accounts-management",

@@ -10,2 +10,14 @@ "main": "lib/index.js",

},
"scripts": {
"clean": "yarn run -T rimraf lib",
"start": "yarn run -T tsc --watch",
"precompile": "yarn run clean",
"compile": "yarn run -T tsc",
"prepublishOnly": "yarn run compile",
"test": "yarn run testonly",
"test-ci": "yarn run lint && yarn run coverage",
"testonly": "yarn run -T jest",
"test:watch": "yarn run -T jest --watch",
"coverage": "yarn run testonly --coverage"
},
"files": [

@@ -35,30 +47,21 @@ "src",

"license": "MIT",
"peerDependencies": {
"graphql": "^16.0.0",
"graphql-modules": "^3.0.0"
},
"dependencies": {
"@accounts/types": "^0.33.1",
"@types/jsonwebtoken": "8.3.9",
"emittery": "0.8.1",
"jsonwebtoken": "8.5.1",
"@accounts/types": "1.0.0-alpha-20231022085748-0910b9e1",
"@types/jsonwebtoken": "9.0.4",
"emittery": "0.13.1",
"jsonwebtoken": "9.0.2",
"jwt-decode": "3.1.2",
"lodash.merge": "4.6.2",
"tslib": "2.3.0"
"tslib": "2.6.2"
},
"devDependencies": {
"@types/jest": "26.0.24",
"@types/lodash.merge": "4.6.6",
"@types/node": "16.4.13",
"jest": "27.0.6",
"rimraf": "3.0.2",
"ts-jest": "27.0.4"
},
"scripts": {
"clean": "rimraf lib",
"start": "tsc --watch",
"precompile": "pnpm run clean",
"compile": "tsc",
"test": "npm run testonly",
"test-ci": "npm lint && npm coverage",
"testonly": "jest",
"test:watch": "jest --watch",
"coverage": "npm run testonly -- --coverage"
"@types/lodash.merge": "4.6.8",
"graphql": "16.8.1",
"graphql-modules": "3.0.0-alpha-20231018163544-36199b68",
"reflect-metadata": "0.1.13"
}
}
}

@@ -13,4 +13,5 @@ import merge from 'lodash.merge';

DatabaseInterface,
AuthenticationService,
ConnectionInformations,
DatabaseInterfaceUser,
DatabaseInterfaceSessions,
} from '@accounts/types';

@@ -38,4 +39,11 @@

import { isString } from './utils/validation';
import { ExecutionContext, Inject, Injectable } from 'graphql-modules';
import { AccountsCoreConfigToken } from './types/AccountsCoreConfig.symbol';
import { AuthenticationServicesToken } from './types/AuthenticationServices.symbol';
import { AuthenticationServices } from './types/authentication-services';
import { DatabaseInterfaceUserToken } from './types/DatabaseInterfaceUser.symbol';
import { DatabaseInterfaceSessionsToken } from './types/DatabaseInterfaceSessions.symbol';
const defaultOptions = {
micro: false,
ambiguousErrorMessages: true,

@@ -59,3 +67,3 @@ tokenSecret: 'secret' as

siteUrl: 'http://localhost:3000',
userObjectSanitizer: (user: User) => user,
userObjectSanitizer: <CustomUser extends User = User>(user: CustomUser) => user,
createNewSessionTokenOnRefresh: false,

@@ -66,16 +74,22 @@ useInternalUserObjectSanitizer: true,

@Injectable({
global: true,
})
export class AccountsServer<CustomUser extends User = User> {
@ExecutionContext() public context!: ExecutionContext;
public options: AccountsServerOptions<CustomUser> & typeof defaultOptions;
private services: { [key: string]: AuthenticationService<CustomUser> };
private db: DatabaseInterface<CustomUser>;
private hooks: Emittery;
private micro: boolean;
private dbSessions: DatabaseInterfaceSessions;
constructor(
options: AccountsServerOptions<CustomUser>,
services: { [key: string]: AuthenticationService<CustomUser> }
@Inject(AccountsCoreConfigToken) options: AccountsServerOptions<CustomUser>,
@Inject(AuthenticationServicesToken) public services: AuthenticationServices<CustomUser>,
@Inject(DatabaseInterfaceUserToken)
private db: DatabaseInterface<CustomUser> | DatabaseInterfaceUser<CustomUser>,
@Inject(DatabaseInterfaceSessionsToken) dbSessions?: DatabaseInterfaceSessions
) {
this.options = merge({ ...defaultOptions }, options);
if (!this.options.db) {
throw new Error('A database driver is required');
}
this.micro = this.options.micro;
this.dbSessions = dbSessions ?? (db as DatabaseInterfaceSessions);
if (this.options.tokenSecret === defaultOptions.tokenSecret) {

@@ -94,8 +108,12 @@ console.log(`

this.services = services || {};
this.db = this.options.db;
// Set the db to all services
for (const service in this.services) {
this.services[service].setStore(this.db);
this.services[service].server = this;
for (const serviceName in this.services) {
const service = this.services[serviceName];
// We have an instance, thus no Dependency Injection is being used (REST)
// and we must manually set the db and server in all services
if (typeof service === 'object') {
service.setUserStore(this.db);
service.setSessionsStore(this.dbSessions);
service.server = this;
}
}

@@ -107,3 +125,19 @@

public getServices(): { [key: string]: AuthenticationService } {
private getService(serviceName: string) {
const instanceOrCtor = this.services[serviceName];
// If it's a constructor we use dependency injection (GraphQL), otherwise we already have an instance (REST)
const service =
typeof instanceOrCtor === 'function'
? this.context.injector.get(instanceOrCtor)
: instanceOrCtor;
if (!service) {
throw new AccountsJsError(
`No service with the name ${serviceName} was registered.`,
AuthenticateWithServiceErrors.ServiceNotFound
);
}
return service;
}
public getServices(): AuthenticationServices<CustomUser> {
return this.services;

@@ -153,10 +187,3 @@ }

try {
if (!this.services[serviceName]) {
throw new AccountsJsError(
`No service with the name ${serviceName} was registered.`,
AuthenticateWithServiceErrors.ServiceNotFound
);
}
const user: CustomUser | null = await this.services[serviceName].authenticate(params);
const user: CustomUser | null = await this.getService(serviceName).authenticate(params);
hooksInfo.user = user;

@@ -201,10 +228,3 @@ if (!user) {

try {
if (!this.services[serviceName]) {
throw new AccountsJsError(
`No service with the name ${serviceName} was registered.`,
LoginWithServiceErrors.ServiceNotFound
);
}
const user: CustomUser | null = await this.services[serviceName].authenticate(params);
const user: CustomUser | null = await this.getService(serviceName).authenticate(params);
hooksInfo.user = user;

@@ -248,3 +268,3 @@ if (!user) {

const token = await this.createSessionToken(user);
const sessionId = await this.db.createSession(user.id, token, infos);
const sessionId = await this.dbSessions.createSession(user.id, token, infos);

@@ -323,3 +343,3 @@ const { accessToken, refreshToken } = await this.createTokens({

const token = await this.createSessionToken(impersonatedUser);
const newSessionId = await this.db.createSession(impersonatedUser.id, token, infos, {
const newSessionId = await this.dbSessions.createSession(impersonatedUser.id, token, infos, {
impersonatorUserId: user.id,

@@ -388,3 +408,3 @@ });

const session: Session | null = await this.db.findSessionByToken(sessionToken);
const session: Session | null = await this.dbSessions.findSessionByToken(sessionToken);
if (!session) {

@@ -406,3 +426,3 @@ throw new AccountsJsError('Session not found', RefreshTokensErrors.SessionNotFound);

const tokens = await this.createTokens({ token: newToken || sessionToken, user });
await this.db.updateSession(session.id, infos, newToken);
await this.dbSessions.updateSession(session.id, infos, newToken);

@@ -475,3 +495,3 @@ const result = {

if (session.valid) {
await this.db.invalidateSession(session.id);
await this.dbSessions.invalidateSession(session.id);
await this.hooks.emit(ServerHooks.LogoutSuccess, {

@@ -520,6 +540,11 @@ session,

if (this.micro) {
// We need to only verify the access tokens without any additional session logic
return { id: userId } as CustomUser;
}
// If the session is stateful we check the validity of the token against the db
let session: Session | null = null;
if (!this.options.useStatelessSession) {
session = await this.db.findSessionByToken(sessionToken);
session = await this.dbSessions.findSessionByToken(sessionToken);
if (!session) {

@@ -576,3 +601,3 @@ throw new AccountsJsError('Session not found', ResumeSessionErrors.SessionNotFound);

const session: Session | null = await this.db.findSessionByToken(sessionToken);
const session: Session | null = await this.dbSessions.findSessionByToken(sessionToken);
if (!session) {

@@ -579,0 +604,0 @@ throw new AccountsJsError(

@@ -7,2 +7,7 @@ import { AccountsServer } from './accounts-server';

export { AccountsJsError } from './utils/accounts-error';
export { DatabaseInterfaceUserToken } from './types/DatabaseInterfaceUser.symbol';
export { DatabaseInterfaceSessionsToken } from './types/DatabaseInterfaceSessions.symbol';
export { AccountsCoreConfigToken } from './types/AccountsCoreConfig.symbol';
export { AuthenticationServicesToken } from './types/AuthenticationServices.symbol';
export { AuthenticationServices } from './types/authentication-services';
export {

@@ -9,0 +14,0 @@ AuthenticateWithServiceErrors,

import * as jwt from 'jsonwebtoken';
import { User, DatabaseInterface, Session } from '@accounts/types';
import { User, Session } from '@accounts/types';
import { EmailTemplatesType } from './email-templates-type';

@@ -10,2 +10,3 @@ import { PrepareMailFunction } from './prepare-mail-function';

export interface AccountsServerOptions<CustomUser extends User = User> {
micro?: boolean;
/**

@@ -15,3 +16,3 @@ * Return ambiguous error messages from login failures to prevent user enumeration. Defaults to true.

ambiguousErrorMessages?: boolean;
db?: DatabaseInterface<CustomUser>;
//db?: DatabaseInterface<CustomUser>;
tokenSecret:

@@ -18,0 +19,0 @@ | string

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc