这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions test/test-cases/api-sync-mode-sqlite-test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ module.exports = (
} = params
const auth = { token: '' }

it('it should be successfully performed by the isStagingBfxApi method', async function () {
this.timeout(5000)

const res = await agent
.post(`${basePath}/json-rpc`)
.type('json')
.send({
method: 'isStagingBfxApi',
id: 5
})
.expect('Content-Type', /json/)
.expect(200)

assert.isObject(res.body)
assert.propertyVal(res.body, 'id', 5)
assert.isBoolean(res.body.result)
})

it('it should be successfully performed by the getPlatformStatus method', async function () {
this.timeout(5000)

Expand Down Expand Up @@ -184,6 +202,7 @@ module.exports = (
assert.isNull(res.body.result.authTokenTTLSec)
assert.isNull(res.body.result.localUsername)
assert.isNull(res.body.result.lastSyncMts)
assert.isBoolean(res.body.result.isStagingBfxApi)

auth.token = res.body.result.token
})
Expand Down Expand Up @@ -283,6 +302,7 @@ module.exports = (
assert.isNumber(res.body.result.authTokenTTLSec)
assert.isNull(res.body.result.localUsername)
assert.isNull(res.body.result.lastSyncMts)
assert.isBoolean(res.body.result.isStagingBfxApi)
})

it('it should not be successfully performed by the signIn method', async function () {
Expand Down Expand Up @@ -483,6 +503,7 @@ module.exports = (
assert.isBoolean(user.isRestrictedToBeAddedToSubAccount)
assert.isBoolean(user.isApiKeysAuth)
assert.isArray(user.subUsers)
assert.isBoolean(user.isStagingBfxApi)

user.subUsers.forEach((subUser) => {
assert.isString(subUser.email)
Expand Down
15 changes: 12 additions & 3 deletions workers/loc.api/service.report.framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ class FrameworkReportService extends ReportService {
}, 'verifyOnBFX', args, cb)
}

isStagingBfxApi (space, args, cb) {
return this._responder(() => {
return this._authenticator.isStagingBfxApi()
}, 'isStagingBfxApi', args, cb)
}

signUp (space, args, cb) {
return this._responder(() => {
return this._authenticator.signUp(args)
Expand All @@ -114,7 +120,8 @@ class FrameworkReportService extends ReportService {
shouldNotSyncOnStartupAfterUpdate,
isSyncOnStartupRequired,
authTokenTTLSec,
localUsername
localUsername,
isStagingBfxApi
} = await this._authenticator.signIn(
args,
{ isReturnedUser: true }
Expand Down Expand Up @@ -148,7 +155,8 @@ class FrameworkReportService extends ReportService {
shouldNotSyncOnStartupAfterUpdate,
authTokenTTLSec,
localUsername,
lastSyncMts: lastFinishedSyncQueueJob?.updatedAt ?? null
lastSyncMts: lastFinishedSyncQueueJob?.updatedAt ?? null,
isStagingBfxApi
}
}, 'signIn', args, cb)
}
Expand Down Expand Up @@ -208,7 +216,8 @@ class FrameworkReportService extends ReportService {
'isNotProtected',
'subUsers',
'isRestrictedToBeAddedToSubAccount',
'isApiKeysAuth'
'isApiKeysAuth',
'isStagingBfxApi'
]
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = (session, isReturnedPassword) => {
'shouldNotSyncOnStartupAfterUpdate',
'isSyncOnStartupRequired',
'authTokenTTLSec',
'isStagingBfxApi',
...passwordProp
]
const { subUsers: reqSubUsers } = { ...session }
Expand Down
37 changes: 33 additions & 4 deletions workers/loc.api/sync/authenticator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const depsTypes = (TYPES) => [
TYPES.Crypto,
TYPES.SyncFactory,
TYPES.WSEventEmitterFactory,
TYPES.Logger
TYPES.Logger,
TYPES.CONF
]
class Authenticator {
constructor (
Expand All @@ -50,7 +51,8 @@ class Authenticator {
crypto,
syncFactory,
wsEventEmitterFactory,
logger
logger,
conf
) {
this.dao = dao
this.TABLES_NAMES = TABLES_NAMES
Expand All @@ -60,6 +62,7 @@ class Authenticator {
this.syncFactory = syncFactory
this.wsEventEmitterFactory = wsEventEmitterFactory
this.logger = logger
this.conf = conf

/**
* It may only work for one grenache worker instance
Expand All @@ -80,6 +83,12 @@ class Authenticator {
this.authTokenInvalidateIntervalsSec = 10 * 60
}

isStagingBfxApi () {
const bfxApiUrl = this.conf?.restUrl ?? ''

return /staging/gi.test(bfxApiUrl)
}

async signUp (args, opts) {
const { auth, params } = args ?? {}
const {
Expand Down Expand Up @@ -216,7 +225,8 @@ class Authenticator {
shouldNotSyncOnStartupAfterUpdate: 0,
isSyncOnStartupRequired: 0,
authTokenTTLSec,
localUsername
localUsername,
isStagingBfxApi: this.isStagingBfxApi()
},
{ isNotInTrans, withWorkerThreads, doNotQueueQuery }
)
Expand Down Expand Up @@ -332,17 +342,21 @@ class Authenticator {
userData = await this.rService._checkAuthInApi({
auth: { authToken: newAuthToken, apiKey, apiSecret }
})
userData.isStagingBfxApi = this.isStagingBfxApi()
} catch (err) {
if (!isENetError(err)) {
throw err
}

this.logger.debug(err)
}

const {
id,
timezone,
username: uName,
email: emailFromApi
email: emailFromApi,
isStagingBfxApi
} = userData ?? {}
const username = generateSubUserName(
{ username: uName },
Expand All @@ -360,6 +374,7 @@ class Authenticator {
isDataFromDb: isDataFromDb === null
? user.isDataFromDb
: isDataFromDb,
isStagingBfxApi,
...newAuthToken
? { authToken: newAuthToken }
: null
Expand All @@ -376,6 +391,20 @@ class Authenticator {
{ withWorkerThreads, doNotQueueQuery }
)

// Need to update `isStagingBfxApi` flag for sub-users if `isSubAccount`
if (
isSubAccount &&
Array.isArray(user?.subUsers) &&
user.subUsers.length > 0
) {
await this.dao.updateCollBy(
this.TABLES_NAMES.USERS,
{ $in: { _id: user.subUsers.map(({ _id }) => (_id)) } },
{ isStagingBfxApi },
{ withWorkerThreads, doNotQueueQuery }
)
}

if (res && res.changes < 1) {
throw new AuthError()
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict'

/*
* CREATED_AT: 2023-12-07T11:57:58.668Z
* VERSION: v39
*/

const {
ID_PRIMARY_KEY
} = require('./helpers/const')
const {
getSqlArrToModifyColumns
} = require('./helpers')

const AbstractMigration = require('./abstract.migration')

class MigrationV39 extends AbstractMigration {
/**
* @override
*/
up () {
const sqlArr = [
'ALTER TABLE users ADD COLUMN isStagingBfxApi INT'
]

this.addSql(sqlArr)
}

/**
* @override
*/
down () {
const sqlArr = [
...getSqlArrToModifyColumns(
'users',
{
_id: ID_PRIMARY_KEY,
id: 'BIGINT',
email: 'VARCHAR(255)',
apiKey: 'VARCHAR(255)',
apiSecret: 'VARCHAR(255)',
authToken: 'VARCHAR(255)',
active: 'INT',
isDataFromDb: 'INT',
timezone: 'VARCHAR(255)',
username: 'VARCHAR(255)',
localUsername: 'VARCHAR(255)',
passwordHash: 'VARCHAR(255)',
isNotProtected: 'INT',
isSubAccount: 'INT',
isSubUser: 'INT',
shouldNotSyncOnStartupAfterUpdate: 'INT',
isSyncOnStartupRequired: 'INT',
authTokenTTLSec: 'INT',
createdAt: 'BIGINT',
updatedAt: 'BIGINT'
}
)
]

this.addSql(sqlArr)
}

/**
* @override
*/
beforeDown () { return this.dao.disableForeignKeys() }

/**
* @override
*/
afterDown () { return this.dao.enableForeignKeys() }
}

module.exports = MigrationV39
6 changes: 4 additions & 2 deletions workers/loc.api/sync/dao/helpers/users/normalize-user-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const _normalize = (userData) => {
haveSubUsers,
isNotProtected,
shouldNotSyncOnStartupAfterUpdate,
isSyncOnStartupRequired
isSyncOnStartupRequired,
isStagingBfxApi
} = userData ?? {}

return {
Expand All @@ -21,7 +22,8 @@ const _normalize = (userData) => {
haveSubUsers: !!haveSubUsers,
isNotProtected: !!isNotProtected,
shouldNotSyncOnStartupAfterUpdate: !!shouldNotSyncOnStartupAfterUpdate,
isSyncOnStartupRequired: !!isSyncOnStartupRequired
isSyncOnStartupRequired: !!isSyncOnStartupRequired,
isStagingBfxApi: !!isStagingBfxApi
}
}

Expand Down
3 changes: 2 additions & 1 deletion workers/loc.api/sync/schema/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* e.g. `migration.v1.js`, where `v1` is `SUPPORTED_DB_VERSION`
*/

const SUPPORTED_DB_VERSION = 38
const SUPPORTED_DB_VERSION = 39

const TABLES_NAMES = require('./tables-names')
const {
Expand Down Expand Up @@ -67,6 +67,7 @@ const _models = new Map([
shouldNotSyncOnStartupAfterUpdate: 'INT',
isSyncOnStartupRequired: 'INT',
authTokenTTLSec: 'INT',
isStagingBfxApi: 'INT',
createdAt: 'BIGINT',
updatedAt: 'BIGINT',

Expand Down