mirror of
https://github.com/standardnotes/server
synced 2026-08-03 06:15:59 -04:00
* fix(api-gateway): reduce exports * wip controllers * fix: imports of controllers * fix(api-gateway): rename http service interface to proxy interface * wip: self-registering services and controllers * wip: add registering controller method bindings and services in container * feat: merge two services together * wip: resolving endpoints to direct code calls * wip: bind controller container to a singleton * fix: controller binding to instantiate and self-register on controller container * fix: move signout endpoint to auth controller * wip: define inversify controllers in the controller container * fix(auth): bind inversify controllers to controller container * fix(auth): linter issues * fix(auth): specs * fix(auth): inversify controllers bindings * wip: endpoint resolving * wip: add endpoint for more auth controllers * wip: add sessions controller endpoint resolvings * wip: add subscription invites endpoint resolvings * wip: add subscription tokens endpoint resolvings * wip: add all binding for auth server controllers * wip: fix migrations path * fix: configure default env vars and ci setup
113 lines
3.3 KiB
TypeScript
113 lines
3.3 KiB
TypeScript
import { TimerInterface } from '@standardnotes/time'
|
|
import * as crypto from 'crypto'
|
|
import { inject, injectable } from 'inversify'
|
|
|
|
import TYPES from '../../Bootstrap/Types'
|
|
import { AuthenticationMethodResolverInterface } from '../Auth/AuthenticationMethodResolverInterface'
|
|
import { Session } from '../Session/Session'
|
|
|
|
import { AuthenticateUserDTO } from './AuthenticateUserDTO'
|
|
import { AuthenticateUserResponse } from './AuthenticateUserResponse'
|
|
import { UseCaseInterface } from './UseCaseInterface'
|
|
|
|
@injectable()
|
|
export class AuthenticateUser implements UseCaseInterface {
|
|
constructor(
|
|
@inject(TYPES.Auth_AuthenticationMethodResolver)
|
|
private authenticationMethodResolver: AuthenticationMethodResolverInterface,
|
|
@inject(TYPES.Auth_Timer) private timer: TimerInterface,
|
|
@inject(TYPES.Auth_ACCESS_TOKEN_AGE) private accessTokenAge: number,
|
|
) {}
|
|
|
|
async execute(dto: AuthenticateUserDTO): Promise<AuthenticateUserResponse> {
|
|
const authenticationMethod = await this.authenticationMethodResolver.resolve(dto.token)
|
|
if (!authenticationMethod) {
|
|
return {
|
|
success: false,
|
|
failureType: 'INVALID_AUTH',
|
|
}
|
|
}
|
|
|
|
if (authenticationMethod.type === 'revoked') {
|
|
return {
|
|
success: false,
|
|
failureType: 'REVOKED_SESSION',
|
|
}
|
|
}
|
|
|
|
const user = authenticationMethod.user
|
|
if (!user) {
|
|
return {
|
|
success: false,
|
|
failureType: 'INVALID_AUTH',
|
|
}
|
|
}
|
|
|
|
if (authenticationMethod.type == 'jwt' && user.supportsSessions()) {
|
|
return {
|
|
success: false,
|
|
failureType: 'INVALID_AUTH',
|
|
}
|
|
}
|
|
|
|
switch (authenticationMethod.type) {
|
|
case 'jwt': {
|
|
const pwHash = <string>(<Record<string, unknown>>authenticationMethod.claims).pw_hash
|
|
const encryptedPasswordDigest = crypto.createHash('sha256').update(user.encryptedPassword).digest('hex')
|
|
|
|
if (!pwHash || !crypto.timingSafeEqual(Buffer.from(pwHash), Buffer.from(encryptedPasswordDigest))) {
|
|
return {
|
|
success: false,
|
|
failureType: 'INVALID_AUTH',
|
|
}
|
|
}
|
|
break
|
|
}
|
|
case 'session_token': {
|
|
const session = authenticationMethod.session
|
|
if (!session) {
|
|
return {
|
|
success: false,
|
|
failureType: 'INVALID_AUTH',
|
|
}
|
|
}
|
|
|
|
if (session.refreshExpiration < this.timer.getUTCDate()) {
|
|
return {
|
|
success: false,
|
|
failureType: 'INVALID_AUTH',
|
|
}
|
|
}
|
|
|
|
if (this.sessionIsExpired(session)) {
|
|
return {
|
|
success: false,
|
|
failureType: 'EXPIRED_TOKEN',
|
|
}
|
|
}
|
|
|
|
break
|
|
}
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
user,
|
|
session: authenticationMethod.session,
|
|
}
|
|
}
|
|
|
|
private sessionIsExpired(session: Session): boolean {
|
|
const sessionIsExpired = session.accessExpiration < this.timer.getUTCDate()
|
|
|
|
const freshlyCreatedSessionSafetyBufferSeconds = 10
|
|
const currentConfigurationAccessTokenExpiration = this.timer.getUTCDateNSecondsAhead(
|
|
this.accessTokenAge + freshlyCreatedSessionSafetyBufferSeconds,
|
|
)
|
|
|
|
const sessionIsLongerThanCurrentConfiguration = session.accessExpiration > currentConfigurationAccessTokenExpiration
|
|
|
|
return sessionIsExpired || sessionIsLongerThanCurrentConfiguration
|
|
}
|
|
}
|