forked from DctrlVan/dctrl-fobtap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize.js
More file actions
113 lines (106 loc) · 3.57 KB
/
initialize.js
File metadata and controls
113 lines (106 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const colors = require('colors')
const prompt = require('prompt')
const request = require('superagent')
const uuidV1 = require('uuid/v1')
const uuidV4 = require('uuid/v4')
const fs = require('fs')
const cryptoUtils = require('./crypto')
const utils = require('./utils')
prompt.delimiter = colors.red("(>-_-<)")
prompt.start()
prompt.get([{
name: 'admin',
default: 'localhost:8003/',
description: colors.white( 'Provide the root ip of dctrl-admin.'),
type: 'string',
required: true,
},{
name: 'name',
default: '',
description: colors.white( 'Provide a name for the new resource.'),
type: 'string',
required: true,
}, {
name: 'charged',
default: 0,
description: colors.white( 'How much should be charged on tap? (CAD).'),
type: 'number',
required: true,
},{
name: 'reaction',
default: 'door',
description: colors.white( 'What type of pin response: currently available: door, bitpepsi.'),
type: 'string',
required: true,
}, {
name: 'trackStock',
default: 'rhodes',
default: 'true',
description: colors.white( 'Do you want to track stock?'),
type: 'boolean',
required: true
}, {
name: 'hackername',
default: 'rhodes',
description: colors.white( 'Your dctrl hackername'),
type: 'string',
required: true
}, {
name: 'secret',
default: '1235',
hidden: true,
description: colors.white( 'please type your password'),
type: 'string',
required: true,
}], function (err, promptData) {
utils.auth(promptData.admin, promptData.hackername, promptData.secret, (err, token)=>{
if (err) {
console.log('authentication failed, try again?')
return prompt.stop()
}
createResource(promptData.admin, token, promptData.name, promptData.charged, promptData.trackStock, (err, resourceInfo)=> {
if (err){
console.log('creation failed, odd...')
console.log(err)
return prompt.stop()
}
console.log(colors.yellow("Going to guess which keyboard is the reader"))
fs.readdir("/dev/input/by-id", function(err, items) {
console.log(colors.yellow("found input: ", items[0]))
console.log({promptData, resourceInfo})
let str = "module.exports = " + JSON.stringify({
brainLocation: promptData.admin,
resourceId: resourceInfo.resourceId,
secret: resourceInfo.rawSecret,
reaction: promptData.reaction,
fobReader: "/dev/input/by-id/" + items[0] // ls /dev/input/by-id
})
fs.writeFileSync(__dirname + '/configuration.js', str)
console.log(colors.green( 'SUCCESS!!@#$@#@!' ))
})
})
})
})
function createResource(admin, token, name, charged, trackStock, callback){
let resourceId = uuidV1()
let rawSecret = uuidV4()
let secret = cryptoUtils.createHash(rawSecret)
let newResource = {
type: 'resource-created',
resourceId,
name,
charged,
secret,
trackStock
}
console.log('attempting to create new resource:', name)
request
.post(admin + 'events')
.set('Authorization', token)
.send(newResource)
.end((err, res)=> {
if (err) return callback(err)
newResource.rawSecret = rawSecret
callback(null, newResource)
})
}