-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SK-243 Device registration api (#125)
* SK-243 created an encryption controller, added a device registry method and a test for it * SK-243 added delete method and test for it * SK-243 added device list method and test for it * SK-243 added migration for encryption colection * SK-243 replaced collection name * SK-243 added request_keys method and test for it * SK-243 updated API.md
- Loading branch information
1 parent
2fe1e56
commit 1094bb5
Showing
22 changed files
with
777 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import BaseJSONController from "./base.js" | ||
|
||
import ServiceLocatorContainer from "@sama/common/ServiceLocatorContainer.js" | ||
|
||
import Response from "@sama/networking/models/Response.js" | ||
|
||
class EncryptionController extends BaseJSONController { | ||
async register(ws, data) { | ||
const { id: requestId } = data | ||
|
||
const encryptionRegisterOperation = ServiceLocatorContainer.use("EncryptionRegisterOperation") | ||
await encryptionRegisterOperation.perform(ws, data.device_register) | ||
|
||
return new Response().addBackMessage({ response: { id: requestId, success: true } }) | ||
} | ||
|
||
async list(ws, data) { | ||
const { id: requestId } = data | ||
|
||
const encryptionListOperation = ServiceLocatorContainer.use("EncryptionListOperation") | ||
const deviceList = await encryptionListOperation.perform(ws) | ||
|
||
return new Response().addBackMessage({ response: { id: requestId, devices: deviceList } }) | ||
} | ||
|
||
async request_keys(ws, data) { | ||
const { id: requestId } = data | ||
|
||
const encryptionRequestKeysOperation = ServiceLocatorContainer.use("EncryptionRequestKeysOperation") | ||
const deviceList = await encryptionRequestKeysOperation.perform(ws, data.request_keys) | ||
|
||
return new Response().addBackMessage({ response: { id: requestId, devices: deviceList } }) | ||
} | ||
|
||
async delete(ws, data) { | ||
const { id: requestId } = data | ||
|
||
const encryptionDeleteOperation = ServiceLocatorContainer.use("EncryptionDeleteOperation") | ||
await encryptionDeleteOperation.perform(ws, data.device_delete) | ||
|
||
return new Response().addBackMessage({ response: { id: requestId, success: true } }) | ||
} | ||
} | ||
|
||
export default new EncryptionController() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import Joi from "joi" | ||
import { ERROR_STATUES } from "@sama/constants/errors.js" | ||
|
||
export const encryptionSchemaValidation = { | ||
device_register: Joi.object({ | ||
identity_key: Joi.string() | ||
.max(255) | ||
.required() | ||
.error( | ||
new Error(ERROR_STATUES.INCORRECT_IDENTITY_KEY.message, { | ||
cause: ERROR_STATUES.INCORRECT_IDENTITY_KEY, | ||
}) | ||
), | ||
signed_key: Joi.string() | ||
.max(255) | ||
.required() | ||
.error( | ||
new Error(ERROR_STATUES.INCORRECT_SIGNED_KEY.message, { | ||
cause: ERROR_STATUES.INCORRECT_SIGNED_KEY, | ||
}) | ||
), | ||
one_time_pre_keys: Joi.array() | ||
.items( | ||
Joi.string() | ||
.max(255) | ||
.error( | ||
new Error(ERROR_STATUES.INCORRECT_ONE_TIME_PRE_KEYS.message, { | ||
cause: ERROR_STATUES.INCORRECT_ONE_TIME_PRE_KEYS, | ||
}) | ||
) | ||
) | ||
.max(100) | ||
.required() | ||
.error( | ||
new Error(ERROR_STATUES.INCORRECT_ONE_TIME_PRE_KEYS.message, { | ||
cause: ERROR_STATUES.INCORRECT_ONE_TIME_PRE_KEYS, | ||
}) | ||
), | ||
}), | ||
device_list: Joi.object({}), | ||
request_keys: Joi.object({ | ||
user_ids: Joi.array() | ||
.items( | ||
Joi.string().error( | ||
new Error(ERROR_STATUES.INCORRECT_USER_ID.message, { | ||
cause: ERROR_STATUES.INCORRECT_USER_ID, | ||
}) | ||
) | ||
) | ||
.max(50) | ||
.required() | ||
.error( | ||
new Error(ERROR_STATUES.INCORRECT_USERS_ARRAY.message, { | ||
cause: ERROR_STATUES.INCORRECT_USERS_ARRAY, | ||
}) | ||
), | ||
}), | ||
device_delete: Joi.object({ | ||
key: Joi.string() | ||
.max(255) | ||
.required() | ||
.error( | ||
new Error(ERROR_STATUES.INCORRECT_IDENTITY_KEY.message, { | ||
cause: ERROR_STATUES.INCORRECT_IDENTITY_KEY, | ||
}) | ||
), | ||
}), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import BaseModel from "./base.js" | ||
|
||
class EncryptedDevice extends BaseModel { | ||
static get collection() { | ||
return "encrypted_devices" | ||
} | ||
|
||
static get visibleFields() { | ||
return ["_id", "identity_key", "signed_key", "one_time_pre_keys"] | ||
} | ||
} | ||
|
||
export default EncryptedDevice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import RegisterProvider from "../../../../common/RegisterProvider.js" | ||
import EncryptionDeleteOperation from "./index.js" | ||
|
||
const name = "EncryptionDeleteOperation" | ||
|
||
class EncryptionDeleteOperationRegisterProvider extends RegisterProvider { | ||
register(slc) { | ||
const encryptionService = slc.use("EncryptionService") | ||
const sessionService = slc.use("SessionService") | ||
const helpers = slc.use("Helpers") | ||
|
||
return new EncryptionDeleteOperation(encryptionService, sessionService, helpers) | ||
} | ||
} | ||
|
||
export default new EncryptionDeleteOperationRegisterProvider({ | ||
name, | ||
implementationName: EncryptionDeleteOperation.name, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ERROR_STATUES } from "../../../../constants/errors.js" | ||
|
||
class EncryptionDeleteOperation { | ||
constructor(encryptionService, sessionService, helpers) { | ||
this.encryptionService = encryptionService | ||
this.sessionService = sessionService | ||
this.helpers = helpers | ||
} | ||
|
||
async perform(ws, deleteParams) { | ||
const device = await this.encryptionService.encryptionRepo.findByIdentityKey(deleteParams.key) | ||
|
||
const userId = this.sessionService.getSessionUserId(ws) | ||
|
||
if (!this.helpers.isEqualsNativeIds(device.user_id, userId)) { | ||
throw new Error(ERROR_STATUES.FORBIDDEN.message, { | ||
cause: ERROR_STATUES.FORBIDDEN, | ||
}) | ||
} | ||
|
||
await this.encryptionService.encryptionRepo.deleteById(device._id) | ||
} | ||
} | ||
|
||
export default EncryptionDeleteOperation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import RegisterProvider from "../../../../common/RegisterProvider.js" | ||
import EncryptionListOperation from "./index.js" | ||
|
||
const name = "EncryptionListOperation" | ||
|
||
class EncryptionListOperationRegisterProvider extends RegisterProvider { | ||
register(slc) { | ||
const encryptionService = slc.use("EncryptionService") | ||
const sessionService = slc.use("SessionService") | ||
|
||
return new EncryptionListOperation(encryptionService, sessionService) | ||
} | ||
} | ||
|
||
export default new EncryptionListOperationRegisterProvider({ | ||
name, | ||
implementationName: EncryptionListOperation.name, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class EncryptionListOperation { | ||
constructor(encryptionService, sessionService) { | ||
this.encryptionService = encryptionService | ||
this.sessionService = sessionService | ||
} | ||
|
||
async perform(ws) { | ||
const userId = this.sessionService.getSessionUserId(ws) | ||
|
||
const deviceList = await this.encryptionService.encryptionRepo.findAll({ user_id: userId }) | ||
return deviceList.map((device) => ({ identity_key: device.identity_key, signed_key: device.signed_key })) | ||
} | ||
} | ||
|
||
export default EncryptionListOperation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import RegisterProvider from "../../../../common/RegisterProvider.js" | ||
import EncryptionRegisterOperation from "./index.js" | ||
|
||
const name = "EncryptionRegisterOperation" | ||
|
||
class EncryptionRegisterOperationProvider extends RegisterProvider { | ||
register(slc) { | ||
const encryptionService = slc.use("EncryptionService") | ||
const sessionService = slc.use("SessionService") | ||
|
||
return new EncryptionRegisterOperation(encryptionService, sessionService) | ||
} | ||
} | ||
|
||
export default new EncryptionRegisterOperationProvider({ | ||
name, | ||
implementationName: EncryptionRegisterOperation.name, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class EncryptionRegisterOperation { | ||
constructor(encryptionService, sessionService) { | ||
this.encryptionService = encryptionService | ||
this.sessionService = sessionService | ||
} | ||
|
||
async perform(ws, registerDeviceParams) { | ||
const existingDevice = await this.encryptionService.encryptionRepo.findByIdentityKey( | ||
registerDeviceParams.identity_key | ||
) | ||
|
||
if (existingDevice) { | ||
await this.encryptionService.update(existingDevice, registerDeviceParams) | ||
} else { | ||
const currentuserId = this.sessionService.getSessionUserId(ws) | ||
await this.encryptionService.encryptionRepo.create({ user_id: currentuserId, ...registerDeviceParams }) | ||
} | ||
} | ||
} | ||
|
||
export default EncryptionRegisterOperation |
18 changes: 18 additions & 0 deletions
18
app/providers/operations/encryption/request_keys/Provider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import RegisterProvider from "../../../../common/RegisterProvider.js" | ||
import EncryptionRequestKeysOperation from "./index.js" | ||
|
||
const name = "EncryptionRequestKeysOperation" | ||
|
||
class EncryptionRequestKeysOperationProvider extends RegisterProvider { | ||
register(slc) { | ||
const userRepo = slc.use("UserRepository") | ||
const encryptionService = slc.use("EncryptionService") | ||
|
||
return new EncryptionRequestKeysOperation(encryptionService, userRepo) | ||
} | ||
} | ||
|
||
export default new EncryptionRequestKeysOperationProvider({ | ||
name, | ||
implementationName: EncryptionRequestKeysOperation.name, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class EncryptionRequestKeysOperation { | ||
constructor(encryptionService, userRepo) { | ||
this.encryptionService = encryptionService | ||
this.userRepo = userRepo | ||
} | ||
|
||
async perform(ws, listParams) { | ||
const userIds = listParams.user_ids | ||
|
||
const existUserIds = await this.userRepo.retrieveExistedIds(userIds) | ||
|
||
const deviceList = await this.encryptionService.encryptionRepo.getAllUserDevicesByIds(existUserIds) | ||
|
||
await this.encryptionService.removeFirstOneTimeKey(userIds) | ||
|
||
return deviceList | ||
} | ||
} | ||
|
||
export default EncryptionRequestKeysOperation |
Oops, something went wrong.