Skip to content

Commit

Permalink
v10.0.0 use meshblu-connector-service to generate OTP
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdemartini committed Feb 13, 2017
1 parent 263c2c8 commit d4fdda8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 81 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meshblu-connector-factory",
"version": "9.9.2",
"version": "10.0.0",
"description": "meshblu-connector-factory",
"main": "index.js",
"scripts": {
Expand Down
28 changes: 0 additions & 28 deletions src/helpers/connector-metadata.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ from 'lodash'
import { getAllLatestVersions } from '../services/go-version-service'

export function getConnectorName(connector) {
return connector.replace(/^meshblu-(connector-)/, '')
Expand All @@ -9,30 +8,3 @@ export function getFriendlyName(connector) {
const name = getConnectorName(connector)
return `Connector ${_.startCase(name)}`
}

export function getConnectorMetadata({ connector, githubSlug, version, octoblu, meshblu }, callback) {
const betterVersion = version.replace('v', '')
const tag = `v${betterVersion}`

getAllLatestVersions((error, versions) => {
if (error != null) return callback(error)
const {
ignitionVersion,
installerVersion,
} = versions

const defaults = {
meshblu: { domain: 'octoblu.com' },
}

callback(null, _.defaultsDeep({
connector: getConnectorName(connector),
tag,
githubSlug,
installerVersion,
ignitionVersion,
octoblu,
meshblu,
}, defaults))
})
}
49 changes: 26 additions & 23 deletions src/services/connector-creator.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
import _ from 'lodash'
import request from 'superagent'
import { getMeshbluConfig } from '../helpers/authentication'
import { getFriendlyName, getConnectorMetadata } from '../helpers/connector-metadata'
import { getFriendlyName } from '../helpers/connector-metadata'

import { CONNECTOR_SERVICE_URI } from '../constants/config'

import {
generateAndStoreToken,
getDevice,
} from '../services/device-service'

import { generateOtp } from '../services/otp-service'

function generateKeyWrapper({ registryItem, connector, version, octoblu }, callback) {
return (error, device) => {
if (error) return callback(error)
const { uuid } = device
const meshblu = _.get(device, 'connectorMetadata.meshblu')
generateAndStoreToken({ uuid }, (error, { token } = {}) => {
if (error != null) return callback(error)
const { githubSlug } = registryItem
const options = {
connector,
githubSlug,
version,
octoblu,
meshblu,
}
getConnectorMetadata(options, (error, metadata) => {
if (error != null) return callback(error)
generateOtp({ uuid, token, metadata }, (error, response) => {
if (error != null) return callback(error)
const { key } = response || {}
return callback(null, { key, uuid })
})
})
generateOtp({ uuid }, (error, response) => {
if (error) return callback(error)
const { key } = response || {}
return callback(null, { key, uuid })
})
}
}
Expand All @@ -59,6 +41,27 @@ function updateConnector({ uuid, properties }, callback) {
})
}

function generateOtp({ uuid }, callback) {
request
.post(`${CONNECTOR_SERVICE_URI}/connectors/${uuid}/otp`)
.auth(getMeshbluConfig().uuid, getMeshbluConfig().token)
.accept('application/json')
.set('Content-Type', 'application/json')
.end((error, response) => {
if (error) {
callback(error)
return
}
if (!response.ok) {
callback(new Error(_.get(response, 'body.error', 'Unable to update the connector')))
return
}
const key = _.get(response, 'body.key')
if (!key) return callback(new Error('Unable to generate OTP'))
callback(null, { key })
})
}

function createConnector({ properties }, callback) {
request
.post(`${CONNECTOR_SERVICE_URI}/users/${getMeshbluConfig().uuid}/connectors`)
Expand Down
16 changes: 0 additions & 16 deletions src/services/go-version-service.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/services/otp-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,3 @@ export function resolveOtp({ otp }, callback) {
return callback(null, response.body)
})
}

export function generateOtp({ uuid, token, metadata }, callback) {
return request
.post(`${OTP_SERVICE_URI}/generate`)
.auth(uuid, token)
.send(metadata)
.end((error, response) => {
if (error) return callback(error)
if (!response.ok) return callback(new Error('Invalid Response'))

return callback(null, response.body)
})
}

0 comments on commit d4fdda8

Please sign in to comment.