Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:Brewskey/spark-server into dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/repository/DeviceRepository.js
  • Loading branch information
jlkalberer committed Jan 11, 2017
2 parents 3b45556 + 594ad09 commit 413508e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 68 deletions.
19 changes: 7 additions & 12 deletions src/controllers/DeviceClaimsController.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
// @flow

import type {
Device,
DeviceRepository,
UserRepository,
} from '../types';
import type { Device, DeviceRepository } from '../types';
import type { ClaimCodeManager } from 'spark-protocol';

import Controller from './Controller';
import httpVerb from '../decorators/httpVerb';
import route from '../decorators/route';

class DeviceClaimsController extends Controller {
_deviceRepository: DeviceRepository;
_userRepository: UserRepository;

_claimCodeManager: ClaimCodeManager;

constructor(
deviceRepository: DeviceRepository,
userRepository: UserRepository,
claimCodeManager: ClaimCodeManager,
) {
super();

this._deviceRepository = deviceRepository;
this._userRepository = userRepository;
this._claimCodeManager = claimCodeManager;
}

@httpVerb('post')
@route('/v1/device_claims')
async generateClaimCode(): Promise<*> {
const claimCode = await this._deviceRepository.generateClaimCode(
async createClaimCode(): Promise<*> {
const claimCode = this._claimCodeManager.createClaimCode(
this.user.id,
);

await this._userRepository.addClaimCode(this.user.id, claimCode);
const devices = await this._deviceRepository.getAll(this.user.id);
const deviceIDs = devices.map(
(device: Device): string => device.deviceID,
Expand Down
2 changes: 1 addition & 1 deletion src/defaultBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default (container: Container) => {
DeviceClaimsController,
Transient.with([
'DeviceRepository',
'UserRepository',
'ClaimCodeManager',
]),
);
container.bindClass(
Expand Down
11 changes: 1 addition & 10 deletions src/repository/DeviceRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import type {
import type DeviceFirmwareRepository from './DeviceFirmwareFileRepository';

import fs from 'fs';
import crypto from 'crypto';
import Moniker from 'moniker';
import ursa from 'ursa';
import HttpError from '../lib/HttpError';
import FirmwareManager from '../managers/FirmwareManager';
import settings from '../settings';

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

class DeviceRepository {
_deviceAttributeRepository: DeviceAttributeRepository;
Expand Down Expand Up @@ -59,12 +57,6 @@ class DeviceRepository {
return await this._deviceAttributeRepository.update(attributesToSave);
};

generateClaimCode = (): string =>
crypto
.randomBytes(CLAIM_CODE_LENGTH)
.toString('base64')
.substring(0, CLAIM_CODE_LENGTH);

unclaimDevice = async (
deviceID: string,
userID: string,
Expand All @@ -78,7 +70,6 @@ class DeviceRepository {

const attributesToSave = {
...deviceAttributes,
claimCode: null,
ownerID: null,
};
return await this._deviceAttributeRepository.update(attributesToSave);
Expand Down Expand Up @@ -209,7 +200,7 @@ class DeviceRepository {

// TODO make FirmwareManager stateless
const firmwareManager = new FirmwareManager(device.getSystemInformation());
const otaUpdateConfig = firmwareManager.getOtaUpdateConfig();
const otaUpdateConfig = null; // firmwareManager.getOtaUpdateConfig();

console.log(otaUpdateConfig);

Expand Down
40 changes: 0 additions & 40 deletions src/repository/UserFileRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,6 @@ class UserFileRepository {
this._fileManager = new JSONFileManager(path);
}

addClaimCode = async (
userID: string,
claimCode: string,
): Promise<User> => {
const user = await this.getById(userID);
if (!user) {
throw new Error('no user found');
}
return await this.update({
...user,
claimCodes: [...user.claimCodes, claimCode],
});
};

removeClaimCode = async (
userID: string,
claimCode: string,
): Promise<?User> => {
const user = await this.getById(userID);
if (!user) {
return null;
}
return await this.update({
...user,
claimCodes: user.claimCodes.filter(
(code: string): boolean =>
code !== claimCode,
),
});
};

createWithCredentials = async (
userCredentials: UserCredentials,
): Promise<User> => {
Expand All @@ -59,7 +28,6 @@ class UserFileRepository {

const modelToSave = {
accessTokens: [],
claimCodes: [],
created_at: new Date(),
created_by: null,
id,
Expand Down Expand Up @@ -87,14 +55,6 @@ class UserFileRepository {
getById = async (id: string): Promise<?User> =>
this._fileManager.getFile(`${id}.json`);

getByClaimCode = async (claimCode: string): Promise<?User> =>
(await this.getAll()).find(
(user: User): boolean =>
user.claimCodes.some((code: string): boolean =>
code === claimCode,
),
);

getByUsername = async (username: string): Promise<?User> =>
(await this.getAll()).find(
(user: User): boolean => user.username === username,
Expand Down
5 changes: 0 additions & 5 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export type TokenObject = {

export type User = {
accessTokens: Array<TokenObject>,
claimCodes: Array<string>,
created_at: Date,
id: string,
passwordHash: string,
Expand Down Expand Up @@ -104,14 +103,11 @@ export type Repository<TModel> = {
};

export type UserRepository = Repository<User> & {
addClaimCode(userID: string, claimCode: string): Promise<User>,
createWithCredentials(credentials: UserCredentials): Promise<User>,
deleteAccessToken(user: User, accessToken: string): Promise<void>,
getByAccessToken(accessToken: string): Promise<?User>,
getByClaimCode(claimCode: string): Promise<?User>,
getByUsername(username: string): Promise<?User>,
isUserNameInUse(username: string): Promise<boolean>,
removeClaimCode(userID: string, claimCode: string): Promise<?User>,
saveAccessToken(userId: string, tokenObject: TokenObject): Promise<void>,
validateLogin(username: string, password: string): Promise<User>,
};
Expand Down Expand Up @@ -146,7 +142,6 @@ export type DeviceRepository = {
functionArguments: Object,
): Promise<*>,
claimDevice(deviceID: string, userID: string): Promise<DeviceAttributes>,
generateClaimCode(userID: string): Promise<string>,
flashBinary(deviceID: string, files: File): Promise<*>,
flashKnownApp(deviceID: string, userID: string, app: string): Promise<*>,
getAll(userID: string): Promise<Array<Device>>,
Expand Down

0 comments on commit 413508e

Please sign in to comment.