Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Updated login-token hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
MoizAdnan committed Oct 5, 2023
1 parent 8952738 commit fa04c16
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
24 changes: 3 additions & 21 deletions packages/server-core/src/user/login-token/login-token.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ Ethereal Engine. All Rights Reserved.
*/

import type { Params } from '@feathersjs/feathers'
import type { KnexAdapterOptions } from '@feathersjs/knex'
import { KnexAdapter } from '@feathersjs/knex'
import crypto from 'crypto'
import moment from 'moment'
import config from '../../appconfig'
import { KnexService } from '@feathersjs/knex'

import {
LoginTokenData,
Expand All @@ -37,9 +33,7 @@ import {
LoginTokenType
} from '@etherealengine/engine/src/schemas/user/login-token.schema'

import { Application } from '../../../declarations'
import { RootParams } from '../../api/root-params'
import { toDateTimeSql } from '../../util/datetime-sql'

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface LoginTokenParams extends RootParams<LoginTokenQuery> {}
Expand All @@ -48,21 +42,9 @@ export interface LoginTokenParams extends RootParams<LoginTokenQuery> {}
* A class for LoginToken service
*/

export class LoginTokenService<T = LoginTokenType, ServiceParams extends Params = LoginTokenParams> extends KnexAdapter<
export class LoginTokenService<T = LoginTokenType, ServiceParams extends Params = LoginTokenParams> extends KnexService<
LoginTokenType,
LoginTokenData,
LoginTokenParams,
LoginTokenPatch
> {
app: Application

constructor(options: KnexAdapterOptions, app: Application) {
super(options)
this.app = app
}
async create(data: LoginTokenData, params?: LoginTokenParams) {
const token = crypto.randomBytes(config.authentication.bearerToken.numBytes).toString('hex')

return await super._create({ ...data, token, expiresAt: toDateTimeSql(moment().utc().add(2, 'days').toDate()) })
}
}
> {}
14 changes: 13 additions & 1 deletion packages/server-core/src/user/login-token/login-token.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ import {
loginTokenPatchValidator,
loginTokenQueryValidator
} from '@etherealengine/engine/src/schemas/user/login-token.schema'
import crypto from 'crypto'
import moment from 'moment'
import config from '../../appconfig'

import { HookContext } from '@feathersjs/feathers'
import { toDateTimeSql } from '../../util/datetime-sql'
import {
loginTokenDataResolver,
loginTokenExternalResolver,
Expand All @@ -40,6 +45,12 @@ import {
loginTokenResolver
} from './login-token.resolvers'

const generateToken = async (context: HookContext) => {
const token = crypto.randomBytes(config.authentication.bearerToken.numBytes).toString('hex')

context.data = { ...context.data, token, expiresAt: toDateTimeSql(moment().utc().add(2, 'days').toDate()) }
}

export default {
around: {
all: [schemaHooks.resolveExternal(loginTokenExternalResolver), schemaHooks.resolveResult(loginTokenResolver)]
Expand All @@ -52,7 +63,8 @@ export default {
create: [
disallow('external'),
() => schemaHooks.validateData(loginTokenDataValidator),
schemaHooks.resolveData(loginTokenDataResolver)
schemaHooks.resolveData(loginTokenDataResolver),
generateToken
],
update: [disallow('external')],
patch: [
Expand Down
2 changes: 1 addition & 1 deletion packages/server-core/src/user/login-token/login-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default (app: Application): void => {
multi: true
}

app.use(loginTokenPath, new LoginTokenService(options, app), {
app.use(loginTokenPath, new LoginTokenService(options), {
// A list of all methods this service exposes externally
methods: loginTokenMethods,
// You can add additional custom events to be sent to clients here
Expand Down
8 changes: 4 additions & 4 deletions packages/server-core/src/user/login/login.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Ethereal Engine. All Rights Reserved.
import { Id, Paginated, ServiceInterface } from '@feathersjs/feathers'

import { identityProviderPath } from '@etherealengine/engine/src/schemas/user/identity-provider.schema'
import { loginTokenPath } from '@etherealengine/engine/src/schemas/user/login-token.schema'
import { LoginTokenType, loginTokenPath } from '@etherealengine/engine/src/schemas/user/login-token.schema'
import { UserApiKeyType, userApiKeyPath } from '@etherealengine/engine/src/schemas/user/user-api-key.schema'
import { userPath } from '@etherealengine/engine/src/schemas/user/user.schema'
import { Application } from '../../../declarations'
Expand Down Expand Up @@ -62,11 +62,11 @@ export class LoginService implements ServiceInterface {
error: 'invalid login token id, cannot be null or undefined'
}
}
const result = await this.app.service(loginTokenPath)._find({
const result = (await this.app.service(loginTokenPath).find({
query: {
token: id.toString()
}
})
})) as Paginated<LoginTokenType>

if (result.data.length === 0) {
logger.info('Invalid login token')
Expand All @@ -92,7 +92,7 @@ export class LoginService implements ServiceInterface {
const token = await this.app
.service('authentication')
.createAccessToken({}, { subject: identityProvider.id.toString() })
await this.app.service(loginTokenPath)._remove(result.data[0].id)
await this.app.service(loginTokenPath).remove(result.data[0].id)
await this.app.service(userPath).patch(identityProvider.userId, {
isGuest: false
})
Expand Down

0 comments on commit fa04c16

Please sign in to comment.