Skip to content

Commit 594ad09

Browse files
authored
Merge pull request particle-iot#72 from AntonPuko/claimCodeManager
implement claimCodeManager, remove claimCodes from User;
2 parents b6d64ef + 00ba787 commit 594ad09

File tree

5 files changed

+8
-67
lines changed

5 files changed

+8
-67
lines changed

src/controllers/DeviceClaimsController.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,33 @@
11
// @flow
22

3-
import type {
4-
Device,
5-
DeviceRepository,
6-
UserRepository,
7-
} from '../types';
3+
import type { Device, DeviceRepository } from '../types';
4+
import type { ClaimCodeManager } from 'spark-protocol';
85

96
import Controller from './Controller';
107
import httpVerb from '../decorators/httpVerb';
118
import route from '../decorators/route';
129

1310
class DeviceClaimsController extends Controller {
1411
_deviceRepository: DeviceRepository;
15-
_userRepository: UserRepository;
16-
12+
_claimCodeManager: ClaimCodeManager;
1713

1814
constructor(
1915
deviceRepository: DeviceRepository,
20-
userRepository: UserRepository,
16+
claimCodeManager: ClaimCodeManager,
2117
) {
2218
super();
2319

2420
this._deviceRepository = deviceRepository;
25-
this._userRepository = userRepository;
21+
this._claimCodeManager = claimCodeManager;
2622
}
2723

2824
@httpVerb('post')
2925
@route('/v1/device_claims')
30-
async generateClaimCode(): Promise<*> {
31-
const claimCode = await this._deviceRepository.generateClaimCode(
26+
async createClaimCode(): Promise<*> {
27+
const claimCode = this._claimCodeManager.createClaimCode(
3228
this.user.id,
3329
);
3430

35-
await this._userRepository.addClaimCode(this.user.id, claimCode);
3631
const devices = await this._deviceRepository.getAll(this.user.id);
3732
const deviceIDs = devices.map(
3833
(device: Device): string => device.deviceID,

src/defaultBindings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default (container: Container) => {
3535
DeviceClaimsController,
3636
Transient.with([
3737
'DeviceRepository',
38-
'UserRepository',
38+
'ClaimCodeManager',
3939
]),
4040
);
4141
container.bindClass(

src/repository/DeviceRepository.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ import type {
1010
} from '../types';
1111
import type DeviceFirmwareRepository from './DeviceFirmwareFileRepository';
1212

13-
import crypto from 'crypto';
1413
import Moniker from 'moniker';
1514
import ursa from 'ursa';
1615
import HttpError from '../lib/HttpError';
1716

1817
const NAME_GENERATOR = Moniker.generator([Moniker.adjective, Moniker.noun]);
19-
const CLAIM_CODE_LENGTH = 63;
2018

2119
class DeviceRepository {
2220
_deviceAttributeRepository: DeviceAttributeRepository;
@@ -56,12 +54,6 @@ class DeviceRepository {
5654
return await this._deviceAttributeRepository.update(attributesToSave);
5755
};
5856

59-
generateClaimCode = (): string =>
60-
crypto
61-
.randomBytes(CLAIM_CODE_LENGTH)
62-
.toString('base64')
63-
.substring(0, CLAIM_CODE_LENGTH);
64-
6557
unclaimDevice = async (
6658
deviceID: string,
6759
userID: string,
@@ -75,7 +67,6 @@ class DeviceRepository {
7567

7668
const attributesToSave = {
7769
...deviceAttributes,
78-
claimCode: null,
7970
ownerID: null,
8071
};
8172
return await this._deviceAttributeRepository.update(attributesToSave);

src/repository/UserFileRepository.js

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,6 @@ class UserFileRepository {
1414
this._fileManager = new JSONFileManager(path);
1515
}
1616

17-
addClaimCode = async (
18-
userID: string,
19-
claimCode: string,
20-
): Promise<User> => {
21-
const user = await this.getById(userID);
22-
if (!user) {
23-
throw new Error('no user found');
24-
}
25-
return await this.update({
26-
...user,
27-
claimCodes: [...user.claimCodes, claimCode],
28-
});
29-
};
30-
31-
removeClaimCode = async (
32-
userID: string,
33-
claimCode: string,
34-
): Promise<?User> => {
35-
const user = await this.getById(userID);
36-
if (!user) {
37-
return null;
38-
}
39-
return await this.update({
40-
...user,
41-
claimCodes: user.claimCodes.filter(
42-
(code: string): boolean =>
43-
code !== claimCode,
44-
),
45-
});
46-
};
47-
4817
createWithCredentials = async (
4918
userCredentials: UserCredentials,
5019
): Promise<User> => {
@@ -59,7 +28,6 @@ class UserFileRepository {
5928

6029
const modelToSave = {
6130
accessTokens: [],
62-
claimCodes: [],
6331
created_at: new Date(),
6432
created_by: null,
6533
id,
@@ -87,14 +55,6 @@ class UserFileRepository {
8755
getById = async (id: string): Promise<?User> =>
8856
this._fileManager.getFile(`${id}.json`);
8957

90-
getByClaimCode = async (claimCode: string): Promise<?User> =>
91-
(await this.getAll()).find(
92-
(user: User): boolean =>
93-
user.claimCodes.some((code: string): boolean =>
94-
code === claimCode,
95-
),
96-
);
97-
9858
getByUsername = async (username: string): Promise<?User> =>
9959
(await this.getAll()).find(
10060
(user: User): boolean => user.username === username,

src/types.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ export type TokenObject = {
7474

7575
export type User = {
7676
accessTokens: Array<TokenObject>,
77-
claimCodes: Array<string>,
7877
created_at: Date,
7978
id: string,
8079
passwordHash: string,
@@ -104,14 +103,11 @@ export type Repository<TModel> = {
104103
};
105104

106105
export type UserRepository = Repository<User> & {
107-
addClaimCode(userID: string, claimCode: string): Promise<User>,
108106
createWithCredentials(credentials: UserCredentials): Promise<User>,
109107
deleteAccessToken(user: User, accessToken: string): Promise<void>,
110108
getByAccessToken(accessToken: string): Promise<?User>,
111-
getByClaimCode(claimCode: string): Promise<?User>,
112109
getByUsername(username: string): Promise<?User>,
113110
isUserNameInUse(username: string): Promise<boolean>,
114-
removeClaimCode(userID: string, claimCode: string): Promise<?User>,
115111
saveAccessToken(userId: string, tokenObject: TokenObject): Promise<void>,
116112
validateLogin(username: string, password: string): Promise<User>,
117113
};
@@ -146,7 +142,6 @@ export type DeviceRepository = {
146142
functionArguments: Object,
147143
): Promise<*>,
148144
claimDevice(deviceID: string, userID: string): Promise<DeviceAttributes>,
149-
generateClaimCode(userID: string): Promise<string>,
150145
flashBinary(deviceID: string, files: File): Promise<*>,
151146
flashKnownApp(deviceID: string, userID: string, app: string): Promise<*>,
152147
getAll(userID: string): Promise<Array<Device>>,

0 commit comments

Comments
 (0)