Compare commits

..

10 Commits

Author SHA1 Message Date
standardci
aa8bd1f8dc chore(release): publish new version
- @standardnotes/analytics@2.21.10
 - @standardnotes/api-gateway@1.49.13
 - @standardnotes/auth-server@1.103.0
 - @standardnotes/domain-core@1.14.0
 - @standardnotes/files-server@1.10.14
 - @standardnotes/revisions-server@1.13.1
 - @standardnotes/scheduler-server@1.17.14
 - @standardnotes/settings@1.21.1
 - @standardnotes/syncing-server@1.34.1
 - @standardnotes/websockets-server@1.6.15
2023-05-02 09:57:55 +00:00
Karol Sójko
c71f7ff8ad feat: extract cache entry model to domain-core (#581)
* feat: extract cache entry model to domain-core

* fix(auth): rename cache table to be auth specific
2023-05-02 11:43:50 +02:00
standardci
fe18420913 chore(release): publish new version
- @standardnotes/auth-server@1.102.0
2023-05-01 13:07:13 +00:00
Karol Sójko
97124928df feat(auth): add sqlite driver repositories (#580)
* feat(auth): add pkce sqlite repository for home server

* feat(auth): add sqlite subscription token repository for home server
2023-05-01 14:50:36 +02:00
standardci
c108bfb12f chore(release): publish new version
- @standardnotes/auth-server@1.101.0
2023-05-01 12:05:53 +00:00
Karol Sójko
5fe6ed1462 feat(auth): add sqlite offline subscription token repository for home server (#579) 2023-05-01 13:50:24 +02:00
standardci
df5fcce769 chore(release): publish new version
- @standardnotes/auth-server@1.100.0
2023-05-01 11:26:33 +00:00
Karol Sójko
8f57ece7b8 feat(auth): add sqlite ephemeral session repository for home server (#578) 2023-05-01 13:09:47 +02:00
standardci
8a10d201c5 chore(release): publish new version
- @standardnotes/auth-server@1.99.0
2023-05-01 10:16:44 +00:00
Karol Sójko
9d7e63a7a7 feat(auth): add sqlite lock cache for home server (#577)
* feat(auth): add sqlite lock cache for home server

* fix(auth): lock repository binding
2023-05-01 12:02:52 +02:00
37 changed files with 561 additions and 33 deletions

View File

@@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [2.21.10](https://github.com/standardnotes/server/compare/@standardnotes/analytics@2.21.9...@standardnotes/analytics@2.21.10) (2023-05-02)
**Note:** Version bump only for package @standardnotes/analytics
## [2.21.9](https://github.com/standardnotes/server/compare/@standardnotes/analytics@2.21.8...@standardnotes/analytics@2.21.9) (2023-04-27)
**Note:** Version bump only for package @standardnotes/analytics

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/analytics",
"version": "2.21.9",
"version": "2.21.10",
"engines": {
"node": ">=18.0.0 <19.0.0"
},

View File

@@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.49.13](https://github.com/standardnotes/api-gateway/compare/@standardnotes/api-gateway@1.49.12...@standardnotes/api-gateway@1.49.13) (2023-05-02)
**Note:** Version bump only for package @standardnotes/api-gateway
## [1.49.12](https://github.com/standardnotes/api-gateway/compare/@standardnotes/api-gateway@1.49.11...@standardnotes/api-gateway@1.49.12) (2023-04-27)
**Note:** Version bump only for package @standardnotes/api-gateway

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/api-gateway",
"version": "1.49.12",
"version": "1.49.13",
"engines": {
"node": ">=18.0.0 <19.0.0"
},

View File

@@ -3,6 +3,36 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [1.103.0](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.102.0...@standardnotes/auth-server@1.103.0) (2023-05-02)
### Features
* extract cache entry model to domain-core ([#581](https://github.com/standardnotes/server/issues/581)) ([c71f7ff](https://github.com/standardnotes/server/commit/c71f7ff8ad4ffbd7151e8397b5816e383b178eb4))
# [1.102.0](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.101.0...@standardnotes/auth-server@1.102.0) (2023-05-01)
### Features
* **auth:** add sqlite driver repositories ([#580](https://github.com/standardnotes/server/issues/580)) ([9712492](https://github.com/standardnotes/server/commit/97124928df6298368408ee74cda71e2678d279dc))
# [1.101.0](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.100.0...@standardnotes/auth-server@1.101.0) (2023-05-01)
### Features
* **auth:** add sqlite offline subscription token repository for home server ([#579](https://github.com/standardnotes/server/issues/579)) ([5fe6ed1](https://github.com/standardnotes/server/commit/5fe6ed1462da3dcd1f40a10babf906fd522a3617))
# [1.100.0](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.99.0...@standardnotes/auth-server@1.100.0) (2023-05-01)
### Features
* **auth:** add sqlite ephemeral session repository for home server ([#578](https://github.com/standardnotes/server/issues/578)) ([8f57ece](https://github.com/standardnotes/server/commit/8f57ece7b88f7961eaf49144c4fdd72fbd07979b))
# [1.99.0](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.98.0...@standardnotes/auth-server@1.99.0) (2023-05-01)
### Features
* **auth:** add sqlite lock cache for home server ([#577](https://github.com/standardnotes/server/issues/577)) ([9d7e63a](https://github.com/standardnotes/server/commit/9d7e63a7a78adcb9817084e460a01189012bc403))
# [1.98.0](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.97.0...@standardnotes/auth-server@1.98.0) (2023-05-01)
### Features

View File

@@ -0,0 +1,13 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
export class changeCacheTableName1683017908845 implements MigrationInterface {
name = 'changeCacheTableName1683017908845'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('RENAME TABLE `cache_entries` TO `auth_cache_entries`')
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('RENAME TABLE `auth_cache_entries` TO `cache_entries`')
}
}

View File

@@ -0,0 +1,13 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
export class changeCacheTableName1683017671034 implements MigrationInterface {
name = 'changeCacheTableName1683017671034'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('ALTER TABLE "cache_entries" RENAME TO "auth_cache_entries"')
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('ALTER TABLE "auth_cache_entries" RENAME TO "cache_entries"')
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/auth-server",
"version": "1.98.0",
"version": "1.103.0",
"engines": {
"node": ">=18.0.0 <19.0.0"
},

View File

@@ -171,6 +171,7 @@ import { SubscriptionSettingProjector } from '../Projection/SubscriptionSettingP
import { SubscriptionSettingsAssociationService } from '../Domain/Setting/SubscriptionSettingsAssociationService'
import { SubscriptionSettingsAssociationServiceInterface } from '../Domain/Setting/SubscriptionSettingsAssociationServiceInterface'
import { PKCERepositoryInterface } from '../Domain/User/PKCERepositoryInterface'
import { LockRepositoryInterface } from '../Domain/User/LockRepositoryInterface'
import { RedisPKCERepository } from '../Infra/Redis/RedisPKCERepository'
import { RoleRepositoryInterface } from '../Domain/Role/RoleRepositoryInterface'
import { RevokedSessionRepositoryInterface } from '../Domain/Session/RevokedSessionRepositoryInterface'
@@ -186,7 +187,7 @@ import { UserRequestsController } from '../Controller/UserRequestsController'
import { EmailSubscriptionUnsubscribedEventHandler } from '../Domain/Handler/EmailSubscriptionUnsubscribedEventHandler'
import { SessionTraceRepositoryInterface } from '../Domain/Session/SessionTraceRepositoryInterface'
import { TypeORMSessionTraceRepository } from '../Infra/TypeORM/TypeORMSessionTraceRepository'
import { MapperInterface } from '@standardnotes/domain-core'
import { CacheEntry, CacheEntryRepositoryInterface, MapperInterface } from '@standardnotes/domain-core'
import { SessionTracePersistenceMapper } from '../Mapping/SessionTracePersistenceMapper'
import { SessionTrace } from '../Domain/Session/SessionTrace'
import { TypeORMSessionTrace } from '../Infra/TypeORM/TypeORMSessionTrace'
@@ -216,6 +217,15 @@ import { GenerateRecoveryCodes } from '../Domain/UseCase/GenerateRecoveryCodes/G
import { SignInWithRecoveryCodes } from '../Domain/UseCase/SignInWithRecoveryCodes/SignInWithRecoveryCodes'
import { GetUserKeyParamsRecovery } from '../Domain/UseCase/GetUserKeyParamsRecovery/GetUserKeyParamsRecovery'
import { CleanupExpiredSessions } from '../Domain/UseCase/CleanupExpiredSessions/CleanupExpiredSessions'
import { TypeORMCacheEntry } from '../Infra/TypeORM/TypeORMCacheEntry'
import { TypeORMCacheEntryRepository } from '../Infra/TypeORM/TypeORMCacheEntryRepository'
import { CacheEntryPersistenceMapper } from '../Mapping/CacheEntryPersistenceMapper'
import { TypeORMLockRepository } from '../Infra/TypeORM/TypeORMLockRepository'
import { EphemeralSessionRepositoryInterface } from '../Domain/Session/EphemeralSessionRepositoryInterface'
import { TypeORMEphemeralSessionRepository } from '../Infra/TypeORM/TypeORMEphemeralSessionRepository'
import { TypeORMOfflineSubscriptionTokenRepository } from '../Infra/TypeORM/TypeORMOfflineSubscriptionTokenRepository'
import { TypeORMPKCERepository } from '../Infra/TypeORM/TypeORMPKCERepository'
import { TypeORMSubscriptionTokenRepository } from '../Infra/TypeORM/TypeORMSubscriptionTokenRepository'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const newrelicFormatter = require('@newrelic/winston-enricher')
@@ -229,6 +239,8 @@ export class ContainerConfigLoader {
await AppDataSource.initialize()
const isConfiguredForHomeServer = env.get('DB_TYPE') === 'sqlite'
const redisUrl = env.get('REDIS_URL')
const isRedisInClusterMode = redisUrl.indexOf(',') > 0
let redis
@@ -298,6 +310,9 @@ export class ContainerConfigLoader {
TYPES.AuthenticatorChallengePersistenceMapper,
)
.toConstantValue(new AuthenticatorChallengePersistenceMapper())
container
.bind<MapperInterface<CacheEntry, TypeORMCacheEntry>>(TYPES.CacheEntryPersistenceMapper)
.toConstantValue(new CacheEntryPersistenceMapper())
// ORM
container
@@ -335,6 +350,9 @@ export class ContainerConfigLoader {
container
.bind<Repository<TypeORMAuthenticatorChallenge>>(TYPES.ORMAuthenticatorChallengeRepository)
.toConstantValue(AppDataSource.getRepository(TypeORMAuthenticatorChallenge))
container
.bind<Repository<TypeORMCacheEntry>>(TYPES.ORMCacheEntryRepository)
.toConstantValue(AppDataSource.getRepository(TypeORMCacheEntry))
// Repositories
container.bind<SessionRepositoryInterface>(TYPES.SessionRepository).to(TypeORMSessionRepository)
@@ -356,20 +374,9 @@ export class ContainerConfigLoader {
container
.bind<OfflineUserSubscriptionRepositoryInterface>(TYPES.OfflineUserSubscriptionRepository)
.to(TypeORMOfflineUserSubscriptionRepository)
container
.bind<RedisEphemeralSessionRepository>(TYPES.EphemeralSessionRepository)
.to(RedisEphemeralSessionRepository)
container.bind<LockRepository>(TYPES.LockRepository).to(LockRepository)
container
.bind<SubscriptionTokenRepositoryInterface>(TYPES.SubscriptionTokenRepository)
.to(RedisSubscriptionTokenRepository)
container
.bind<OfflineSubscriptionTokenRepositoryInterface>(TYPES.OfflineSubscriptionTokenRepository)
.to(RedisOfflineSubscriptionTokenRepository)
container
.bind<SharedSubscriptionInvitationRepositoryInterface>(TYPES.SharedSubscriptionInvitationRepository)
.to(TypeORMSharedSubscriptionInvitationRepository)
container.bind<PKCERepositoryInterface>(TYPES.PKCERepository).to(RedisPKCERepository)
container
.bind<SessionTraceRepositoryInterface>(TYPES.SessionTraceRepository)
.toConstantValue(
@@ -394,6 +401,14 @@ export class ContainerConfigLoader {
container.get(TYPES.AuthenticatorChallengePersistenceMapper),
),
)
container
.bind<CacheEntryRepositoryInterface>(TYPES.CacheEntryRepository)
.toConstantValue(
new TypeORMCacheEntryRepository(
container.get(TYPES.ORMCacheEntryRepository),
container.get(TYPES.CacheEntryPersistenceMapper),
),
)
// Middleware
container.bind<AuthMiddleware>(TYPES.AuthMiddleware).to(AuthMiddleware)
@@ -471,6 +486,62 @@ export class ContainerConfigLoader {
.bind(TYPES.READONLY_USERS)
.toConstantValue(env.get('READONLY_USERS', true) ? env.get('READONLY_USERS', true).split(',') : [])
if (isConfiguredForHomeServer) {
container
.bind<LockRepositoryInterface>(TYPES.LockRepository)
.toConstantValue(
new TypeORMLockRepository(
container.get(TYPES.CacheEntryRepository),
container.get(TYPES.Timer),
container.get(TYPES.MAX_LOGIN_ATTEMPTS),
container.get(TYPES.FAILED_LOGIN_LOCKOUT),
),
)
container
.bind<EphemeralSessionRepositoryInterface>(TYPES.EphemeralSessionRepository)
.toConstantValue(
new TypeORMEphemeralSessionRepository(
container.get(TYPES.CacheEntryRepository),
container.get(TYPES.EPHEMERAL_SESSION_AGE),
container.get(TYPES.Timer),
),
)
container
.bind<OfflineSubscriptionTokenRepositoryInterface>(TYPES.OfflineSubscriptionTokenRepository)
.toConstantValue(
new TypeORMOfflineSubscriptionTokenRepository(
container.get(TYPES.CacheEntryRepository),
container.get(TYPES.Timer),
),
)
container
.bind<PKCERepositoryInterface>(TYPES.PKCERepository)
.toConstantValue(
new TypeORMPKCERepository(
container.get(TYPES.CacheEntryRepository),
container.get(TYPES.Logger),
container.get(TYPES.Timer),
),
)
container
.bind<SubscriptionTokenRepositoryInterface>(TYPES.SubscriptionTokenRepository)
.toConstantValue(
new TypeORMSubscriptionTokenRepository(container.get(TYPES.CacheEntryRepository), container.get(TYPES.Timer)),
)
} else {
container.bind<PKCERepositoryInterface>(TYPES.PKCERepository).to(RedisPKCERepository)
container.bind<LockRepositoryInterface>(TYPES.LockRepository).to(LockRepository)
container
.bind<EphemeralSessionRepositoryInterface>(TYPES.EphemeralSessionRepository)
.to(RedisEphemeralSessionRepository)
container
.bind<OfflineSubscriptionTokenRepositoryInterface>(TYPES.OfflineSubscriptionTokenRepository)
.to(RedisOfflineSubscriptionTokenRepository)
container
.bind<SubscriptionTokenRepositoryInterface>(TYPES.SubscriptionTokenRepository)
.to(RedisSubscriptionTokenRepository)
}
// Services
container.bind<UAParser>(TYPES.DeviceDetector).toConstantValue(new UAParser())
container.bind<SessionService>(TYPES.SessionService).to(SessionService)

View File

@@ -8,6 +8,7 @@ const TYPES = {
AuthenticatorChallengePersistenceMapper: Symbol.for('AuthenticatorChallengePersistenceMapper'),
AuthenticatorPersistenceMapper: Symbol.for('AuthenticatorPersistenceMapper'),
AuthenticatorHttpMapper: Symbol.for('AuthenticatorHttpMapper'),
CacheEntryPersistenceMapper: Symbol.for('CacheEntryPersistenceMapper'),
// Controller
AuthController: Symbol.for('AuthController'),
AuthenticatorsController: Symbol.for('AuthenticatorsController'),
@@ -32,6 +33,7 @@ const TYPES = {
SessionTraceRepository: Symbol.for('SessionTraceRepository'),
AuthenticatorRepository: Symbol.for('AuthenticatorRepository'),
AuthenticatorChallengeRepository: Symbol.for('AuthenticatorChallengeRepository'),
CacheEntryRepository: Symbol.for('CacheEntryRepository'),
// ORM
ORMOfflineSettingRepository: Symbol.for('ORMOfflineSettingRepository'),
ORMOfflineUserSubscriptionRepository: Symbol.for('ORMOfflineUserSubscriptionRepository'),
@@ -46,6 +48,7 @@ const TYPES = {
ORMSessionTraceRepository: Symbol.for('ORMSessionTraceRepository'),
ORMAuthenticatorRepository: Symbol.for('ORMAuthenticatorRepository'),
ORMAuthenticatorChallengeRepository: Symbol.for('ORMAuthenticatorChallengeRepository'),
ORMCacheEntryRepository: Symbol.for('ORMCacheEntryRepository'),
// Middleware
AuthMiddleware: Symbol.for('AuthMiddleware'),
ApiGatewayAuthMiddleware: Symbol.for('ApiGatewayAuthMiddleware'),

View File

@@ -1,6 +0,0 @@
import { CacheEntry } from './CacheEntry'
export interface CacheEntryRepositoryInterface {
save(cacheEntry: CacheEntry): Promise<CacheEntry>
findOneByKey(key: string): Promise<CacheEntry | null>
}

View File

@@ -1,6 +1,6 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
@Entity({ name: 'cache_entries' })
@Entity({ name: 'auth_cache_entries' })
export class TypeORMCacheEntry {
@PrimaryGeneratedColumn('uuid')
declare uuid: string

View File

@@ -0,0 +1,39 @@
import { CacheEntry, CacheEntryRepositoryInterface, MapperInterface } from '@standardnotes/domain-core'
import { Repository } from 'typeorm'
import { TypeORMCacheEntry } from './TypeORMCacheEntry'
export class TypeORMCacheEntryRepository implements CacheEntryRepositoryInterface {
constructor(
private ormRepository: Repository<TypeORMCacheEntry>,
private mapper: MapperInterface<CacheEntry, TypeORMCacheEntry>,
) {}
async save(cacheEntry: CacheEntry): Promise<void> {
const persistence = this.mapper.toProjection(cacheEntry)
await this.ormRepository.save(persistence)
}
async findUnexpiredOneByKey(key: string): Promise<CacheEntry | null> {
const persistence = await this.ormRepository
.createQueryBuilder('cache')
.where('cache.key = :key', {
key,
})
.andWhere('cache.expires_at > :now', {
now: new Date(),
})
.getOne()
if (persistence === null) {
return null
}
return this.mapper.toDomain(persistence)
}
async removeByKey(key: string): Promise<void> {
await this.ormRepository.createQueryBuilder().delete().where('key = :key', { key }).execute()
}
}

View File

@@ -0,0 +1,134 @@
import { CacheEntryRepositoryInterface, CacheEntry } from '@standardnotes/domain-core'
import { TimerInterface } from '@standardnotes/time'
import { EphemeralSession } from '../../Domain/Session/EphemeralSession'
import { EphemeralSessionRepositoryInterface } from '../../Domain/Session/EphemeralSessionRepositoryInterface'
export class TypeORMEphemeralSessionRepository implements EphemeralSessionRepositoryInterface {
private readonly PREFIX = 'session'
private readonly USER_SESSIONS_PREFIX = 'user-sessions'
constructor(
private cacheEntryRepository: CacheEntryRepositoryInterface,
private ephemeralSessionAge: number,
private timer: TimerInterface,
) {}
async deleteOne(uuid: string, userUuid: string): Promise<void> {
await this.cacheEntryRepository.removeByKey(`${this.PREFIX}:${uuid}`)
await this.cacheEntryRepository.removeByKey(`${this.PREFIX}:${uuid}:${userUuid}`)
const userSessionsJSON = await this.cacheEntryRepository.findUnexpiredOneByKey(
`${this.USER_SESSIONS_PREFIX}:${userUuid}`,
)
if (userSessionsJSON) {
const userSessions = JSON.parse(userSessionsJSON.props.value)
const updatedUserSessions = userSessions.filter((sessionUuid: string) => sessionUuid !== uuid)
userSessionsJSON.props.value = JSON.stringify(updatedUserSessions)
await this.cacheEntryRepository.save(userSessionsJSON)
}
}
async updateTokensAndExpirationDates(
uuid: string,
hashedAccessToken: string,
hashedRefreshToken: string,
accessExpiration: Date,
refreshExpiration: Date,
): Promise<void> {
const session = await this.findOneByUuid(uuid)
if (!session) {
return
}
session.hashedAccessToken = hashedAccessToken
session.hashedRefreshToken = hashedRefreshToken
session.accessExpiration = accessExpiration
session.refreshExpiration = refreshExpiration
await this.save(session)
}
async findAllByUserUuid(userUuid: string): Promise<Array<EphemeralSession>> {
const ephemeralSessionUuidsJSON = await this.cacheEntryRepository.findUnexpiredOneByKey(
`${this.USER_SESSIONS_PREFIX}:${userUuid}`,
)
if (!ephemeralSessionUuidsJSON) {
return []
}
const ephemeralSessionUuids = JSON.parse(ephemeralSessionUuidsJSON.props.value)
const sessions = []
for (const ephemeralSessionUuid of ephemeralSessionUuids) {
const stringifiedSession = await this.cacheEntryRepository.findUnexpiredOneByKey(
`${this.PREFIX}:${ephemeralSessionUuid}`,
)
if (stringifiedSession !== null) {
sessions.push(JSON.parse(stringifiedSession.props.value))
}
}
return sessions
}
async findOneByUuid(uuid: string): Promise<EphemeralSession | null> {
const stringifiedSession = await this.cacheEntryRepository.findUnexpiredOneByKey(`${this.PREFIX}:${uuid}`)
if (!stringifiedSession) {
return null
}
return JSON.parse(stringifiedSession.props.value)
}
async findOneByUuidAndUserUuid(uuid: string, userUuid: string): Promise<EphemeralSession | null> {
const stringifiedSession = await this.cacheEntryRepository.findUnexpiredOneByKey(
`${this.PREFIX}:${uuid}:${userUuid}`,
)
if (!stringifiedSession) {
return null
}
return JSON.parse(stringifiedSession.props.value)
}
async save(ephemeralSession: EphemeralSession): Promise<void> {
const ttl = this.ephemeralSessionAge
const stringifiedSession = JSON.stringify(ephemeralSession)
await this.cacheEntryRepository.save(
CacheEntry.create({
key: `${this.PREFIX}:${ephemeralSession.uuid}:${ephemeralSession.userUuid}`,
value: stringifiedSession,
expiresAt: this.timer.getUTCDateNSecondsAhead(ttl),
}).getValue(),
)
await this.cacheEntryRepository.save(
CacheEntry.create({
key: `${this.PREFIX}:${ephemeralSession.uuid}`,
value: stringifiedSession,
expiresAt: this.timer.getUTCDateNSecondsAhead(ttl),
}).getValue(),
)
const ephemeralSessionUuidsJSON = await this.cacheEntryRepository.findUnexpiredOneByKey(
`${this.USER_SESSIONS_PREFIX}:${ephemeralSession.userUuid}`,
)
if (!ephemeralSessionUuidsJSON) {
await this.cacheEntryRepository.save(
CacheEntry.create({
key: `${this.USER_SESSIONS_PREFIX}:${ephemeralSession.userUuid}`,
value: JSON.stringify([ephemeralSession.uuid]),
expiresAt: this.timer.getUTCDateNSecondsAhead(ttl),
}).getValue(),
)
} else {
const ephemeralSessionUuids = JSON.parse(ephemeralSessionUuidsJSON.props.value)
ephemeralSessionUuids.push(ephemeralSession.uuid)
ephemeralSessionUuidsJSON.props.value = JSON.stringify(ephemeralSessionUuids)
ephemeralSessionUuidsJSON.props.expiresAt = this.timer.getUTCDateNSecondsAhead(ttl)
await this.cacheEntryRepository.save(ephemeralSessionUuidsJSON)
}
}
}

View File

@@ -0,0 +1,83 @@
import { CacheEntryRepositoryInterface, CacheEntry } from '@standardnotes/domain-core'
import { TimerInterface } from '@standardnotes/time'
import { LockRepositoryInterface } from '../../Domain/User/LockRepositoryInterface'
export class TypeORMLockRepository implements LockRepositoryInterface {
private readonly PREFIX = 'lock'
private readonly OTP_PREFIX = 'otp-lock'
constructor(
private cacheEntryRepository: CacheEntryRepositoryInterface,
private timer: TimerInterface,
private maxLoginAttempts: number,
private failedLoginLockout: number,
) {}
async lockSuccessfullOTP(userIdentifier: string, otp: string): Promise<void> {
const cacheEntryOrError = CacheEntry.create({
key: `${this.OTP_PREFIX}:${userIdentifier}`,
value: otp,
expiresAt: this.timer.getUTCDateNSecondsAhead(60),
})
if (cacheEntryOrError.isFailed()) {
throw new Error('Could not create cache entry')
}
await this.cacheEntryRepository.save(cacheEntryOrError.getValue())
}
async isOTPLocked(userIdentifier: string, otp: string): Promise<boolean> {
const lock = await this.cacheEntryRepository.findUnexpiredOneByKey(`${this.OTP_PREFIX}:${userIdentifier}`)
if (!lock) {
return false
}
return lock.props.value === otp
}
async resetLockCounter(userIdentifier: string): Promise<void> {
await this.cacheEntryRepository.removeByKey(`${this.PREFIX}:${userIdentifier}`)
}
async updateLockCounter(userIdentifier: string, counter: number): Promise<void> {
let cacheEntry = await this.cacheEntryRepository.findUnexpiredOneByKey(`${this.PREFIX}:${userIdentifier}`)
if (!cacheEntry) {
cacheEntry = CacheEntry.create({
key: `${this.PREFIX}:${userIdentifier}`,
value: counter.toString(),
expiresAt: this.timer.getUTCDateNSecondsAhead(this.failedLoginLockout),
}).getValue()
} else {
cacheEntry.props.value = counter.toString()
cacheEntry.props.expiresAt = this.timer.getUTCDateNSecondsAhead(this.failedLoginLockout)
}
await this.cacheEntryRepository.save(cacheEntry)
}
async getLockCounter(userIdentifier: string): Promise<number> {
const counter = await this.cacheEntryRepository.findUnexpiredOneByKey(`${this.PREFIX}:${userIdentifier}`)
if (!counter) {
return 0
}
return +counter.props.value
}
async lockUser(userIdentifier: string): Promise<void> {
const cacheEntry = await this.cacheEntryRepository.findUnexpiredOneByKey(`${this.PREFIX}:${userIdentifier}`)
if (cacheEntry !== null) {
cacheEntry.props.expiresAt = this.timer.getUTCDateNSecondsAhead(this.failedLoginLockout)
await this.cacheEntryRepository.save(cacheEntry)
}
}
async isUserLocked(userIdentifier: string): Promise<boolean> {
const counter = await this.getLockCounter(userIdentifier)
return counter >= this.maxLoginAttempts
}
}

View File

@@ -0,0 +1,32 @@
import { CacheEntryRepositoryInterface, CacheEntry } from '@standardnotes/domain-core'
import { TimerInterface } from '@standardnotes/time'
import { OfflineSubscriptionToken } from '../../Domain/Auth/OfflineSubscriptionToken'
import { OfflineSubscriptionTokenRepositoryInterface } from '../../Domain/Auth/OfflineSubscriptionTokenRepositoryInterface'
export class TypeORMOfflineSubscriptionTokenRepository implements OfflineSubscriptionTokenRepositoryInterface {
private readonly PREFIX = 'offline-subscription-token'
constructor(private cacheEntryRepository: CacheEntryRepositoryInterface, private timer: TimerInterface) {}
async getUserEmailByToken(token: string): Promise<string | undefined> {
const userUuid = await this.cacheEntryRepository.findUnexpiredOneByKey(`${this.PREFIX}:${token}`)
if (!userUuid) {
return undefined
}
return userUuid.props.value
}
async save(offlineSubscriptionToken: OfflineSubscriptionToken): Promise<void> {
const key = `${this.PREFIX}:${offlineSubscriptionToken.token}`
await this.cacheEntryRepository.save(
CacheEntry.create({
key,
value: offlineSubscriptionToken.userEmail,
expiresAt: this.timer.convertMicrosecondsToDate(offlineSubscriptionToken.expiresAt),
}).getValue(),
)
}
}

View File

@@ -0,0 +1,33 @@
import { CacheEntry, CacheEntryRepositoryInterface } from '@standardnotes/domain-core'
import { TimerInterface } from '@standardnotes/time'
import { Logger } from 'winston'
import { PKCERepositoryInterface } from '../../Domain/User/PKCERepositoryInterface'
export class TypeORMPKCERepository implements PKCERepositoryInterface {
private readonly PREFIX = 'pkce'
constructor(
private cacheEntryRepository: CacheEntryRepositoryInterface,
private logger: Logger,
private timer: TimerInterface,
) {}
async storeCodeChallenge(codeChallenge: string): Promise<void> {
this.logger.debug(`Storing code challenge: ${codeChallenge}`)
await this.cacheEntryRepository.save(
CacheEntry.create({
key: `${this.PREFIX}:${codeChallenge}`,
value: codeChallenge,
expiresAt: this.timer.getUTCDateNSecondsAhead(3600),
}).getValue(),
)
}
async removeCodeChallenge(codeChallenge: string): Promise<boolean> {
await this.cacheEntryRepository.removeByKey(`${this.PREFIX}:${codeChallenge}`)
return true
}
}

View File

@@ -0,0 +1,34 @@
import { CacheEntryRepositoryInterface, CacheEntry } from '@standardnotes/domain-core'
import { TimerInterface } from '@standardnotes/time'
import { SubscriptionToken } from '../../Domain/Subscription/SubscriptionToken'
import { SubscriptionTokenRepositoryInterface } from '../../Domain/Subscription/SubscriptionTokenRepositoryInterface'
export class TypeORMSubscriptionTokenRepository implements SubscriptionTokenRepositoryInterface {
private readonly PREFIX = 'subscription-token'
constructor(private cacheEntryRepository: CacheEntryRepositoryInterface, private timer: TimerInterface) {}
async getUserUuidByToken(token: string): Promise<string | undefined> {
const userUuid = await this.cacheEntryRepository.findUnexpiredOneByKey(`${this.PREFIX}:${token}`)
if (!userUuid) {
return undefined
}
return userUuid.props.value
}
async save(subscriptionToken: SubscriptionToken): Promise<boolean> {
const key = `${this.PREFIX}:${subscriptionToken.token}`
await this.cacheEntryRepository.save(
CacheEntry.create({
key,
value: subscriptionToken.userUuid,
expiresAt: this.timer.convertMicrosecondsToDate(subscriptionToken.expiresAt),
}).getValue(),
)
return true
}
}

View File

@@ -1,6 +1,5 @@
import { MapperInterface, UniqueEntityId } from '@standardnotes/domain-core'
import { CacheEntry, MapperInterface, UniqueEntityId } from '@standardnotes/domain-core'
import { CacheEntry } from '../Domain/Cache/CacheEntry'
import { TypeORMCacheEntry } from '../Infra/TypeORM/TypeORMCacheEntry'
export class CacheEntryPersistenceMapper implements MapperInterface<CacheEntry, TypeORMCacheEntry> {

View File

@@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [1.14.0](https://github.com/standardnotes/server/compare/@standardnotes/domain-core@1.13.0...@standardnotes/domain-core@1.14.0) (2023-05-02)
### Features
* extract cache entry model to domain-core ([#581](https://github.com/standardnotes/server/issues/581)) ([c71f7ff](https://github.com/standardnotes/server/commit/c71f7ff8ad4ffbd7151e8397b5816e383b178eb4))
# [1.13.0](https://github.com/standardnotes/server/compare/@standardnotes/domain-core@1.12.0...@standardnotes/domain-core@1.13.0) (2023-04-27)
### Features

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/domain-core",
"version": "1.13.0",
"version": "1.14.0",
"engines": {
"node": ">=18.0.0 <19.0.0"
},

View File

@@ -1,5 +1,6 @@
import { Entity, Result, UniqueEntityId } from '@standardnotes/domain-core'
import { Entity } from '../Core/Entity'
import { Result } from '../Core/Result'
import { UniqueEntityId } from '../Core/UniqueEntityId'
import { CacheEntryProps } from './CacheEntryProps'
export class CacheEntry extends Entity<CacheEntryProps> {

View File

@@ -0,0 +1,7 @@
import { CacheEntry } from './CacheEntry'
export interface CacheEntryRepositoryInterface {
save(cacheEntry: CacheEntry): Promise<void>
findUnexpiredOneByKey(key: string): Promise<CacheEntry | null>
removeByKey(key: string): Promise<void>
}

View File

@@ -5,6 +5,10 @@ export * from './Auth/SessionProps'
export * from './Auth/SessionToken'
export * from './Auth/SessionTokenProps'
export * from './Cache/CacheEntry'
export * from './Cache/CacheEntryProps'
export * from './Cache/CacheEntryRepositoryInterface'
export * from './Common/Dates'
export * from './Common/DatesProps'
export * from './Common/Email'

View File

@@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.10.14](https://github.com/standardnotes/files/compare/@standardnotes/files-server@1.10.13...@standardnotes/files-server@1.10.14) (2023-05-02)
**Note:** Version bump only for package @standardnotes/files-server
## [1.10.13](https://github.com/standardnotes/files/compare/@standardnotes/files-server@1.10.12...@standardnotes/files-server@1.10.13) (2023-04-27)
**Note:** Version bump only for package @standardnotes/files-server

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/files-server",
"version": "1.10.13",
"version": "1.10.14",
"engines": {
"node": ">=18.0.0 <19.0.0"
},

View File

@@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.13.1](https://github.com/standardnotes/server/compare/@standardnotes/revisions-server@1.13.0...@standardnotes/revisions-server@1.13.1) (2023-05-02)
**Note:** Version bump only for package @standardnotes/revisions-server
# [1.13.0](https://github.com/standardnotes/server/compare/@standardnotes/revisions-server@1.12.16...@standardnotes/revisions-server@1.13.0) (2023-04-28)
### Features

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/revisions-server",
"version": "1.13.0",
"version": "1.13.1",
"engines": {
"node": ">=18.0.0 <19.0.0"
},

View File

@@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.17.14](https://github.com/standardnotes/server/compare/@standardnotes/scheduler-server@1.17.13...@standardnotes/scheduler-server@1.17.14) (2023-05-02)
**Note:** Version bump only for package @standardnotes/scheduler-server
## [1.17.13](https://github.com/standardnotes/server/compare/@standardnotes/scheduler-server@1.17.12...@standardnotes/scheduler-server@1.17.13) (2023-04-27)
**Note:** Version bump only for package @standardnotes/scheduler-server

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/scheduler-server",
"version": "1.17.13",
"version": "1.17.14",
"engines": {
"node": ">=18.0.0 <19.0.0"
},

View File

@@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.21.1](https://github.com/standardnotes/server/compare/@standardnotes/settings@1.21.0...@standardnotes/settings@1.21.1) (2023-05-02)
**Note:** Version bump only for package @standardnotes/settings
# [1.21.0](https://github.com/standardnotes/server/compare/@standardnotes/settings@1.20.2...@standardnotes/settings@1.21.0) (2023-04-27)
### Features

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/settings",
"version": "1.21.0",
"version": "1.21.1",
"engines": {
"node": ">=18.0.0 <19.0.0"
},

View File

@@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.34.1](https://github.com/standardnotes/syncing-server-js/compare/@standardnotes/syncing-server@1.34.0...@standardnotes/syncing-server@1.34.1) (2023-05-02)
**Note:** Version bump only for package @standardnotes/syncing-server
# [1.34.0](https://github.com/standardnotes/syncing-server-js/compare/@standardnotes/syncing-server@1.33.0...@standardnotes/syncing-server@1.34.0) (2023-04-27)
### Features

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/syncing-server",
"version": "1.34.0",
"version": "1.34.1",
"engines": {
"node": ">=18.0.0 <19.0.0"
},

View File

@@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.6.15](https://github.com/standardnotes/server/compare/@standardnotes/websockets-server@1.6.14...@standardnotes/websockets-server@1.6.15) (2023-05-02)
**Note:** Version bump only for package @standardnotes/websockets-server
## [1.6.14](https://github.com/standardnotes/server/compare/@standardnotes/websockets-server@1.6.13...@standardnotes/websockets-server@1.6.14) (2023-04-27)
**Note:** Version bump only for package @standardnotes/websockets-server

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/websockets-server",
"version": "1.6.14",
"version": "1.6.15",
"engines": {
"node": ">=18.0.0 <19.0.0"
},