-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (94 loc) · 2.66 KB
/
index.js
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
const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const { promisify } = require('util')
const OAuth2Server = require('oauth2-server')
const Request = OAuth2Server.Request
const Response = OAuth2Server.Response;
const MithVaultSDK = require('./mith-vault-sdk.min.js')
const models = require('./model')
const userModel = require('./mongo/model/user');
const clientId = 'f435ce9da82d9960846192adb99d5a38'
const clientSecret = '1163f104e7d7ef2a01f62132c4b1494fc44e859318aaf494c379a97caaa2b34ca25854154ba29af80ee4e49adb12b1ebb99944da2fddf1ccce5fabba96c0c9a2'
const miningKey = 'mdp'
const userToken = '19e5abc521f14f2317712cb7725d8a9b0f670afe04ea61e091f1060e7845e16e55e300995cb79340782ce34ba683ec9e37e856ff95'
//sdk.getUserMiningAction({ token: userToken }).then(data => {
// console.log(data)
//}).catch(error => {
// console.log(error)
//})
const app = express()
app.use(bodyParser.json())
// add mongo connect
var mongoUri = 'mongodb://localhost/dapp'
mongoose.connect(mongoUri, {
useCreateIndex: true,
useNewUrlParser: true
}, function(err, res) {
if (err) {
return console.error('Error connecting to "%s":', mongoUri, err);
}
console.log('Connected successfully to "%s"', mongoUri);
});
app.oauth = new OAuth2Server({
model: require('./model.js'),
accessTokenLifetime: 60 * 60,
allowBearerTokensInQueryString: true
});
app.all('/oauth/grant/:id', obtainGrantCode);
app.post('/oauth/token', obtainToken);
app.get('/', function(req, res) {
res.send('hello world');
});
const startServer = async () => {
const port = process.env.SERVER_PORT || 8080
await promisify(app.listen).bind(app)(port)
console.log(`Listening on port ${port}`)
}
startServer()
function obtainToken(req, res) {
console.log(req.body);
var grantCode = req.body.grantCode;
var state = req.body.state;
var address = req.body.address;
const sdk = new MithVaultSDK({ clientId, clientSecret, miningKey })
sdk.getAccessToken({ grantCode, state }).then(data => {
console.log(data)
models.getUser(address)
.then(u => {
if (!u) {
user = new userModel({
username: address,
accessTokenMith: data.token
});
user.save()
} else {
u.accessTokenMith = data.token
u.save()
}
})
res.json({
token: data.token
});
}).catch(err => {
res.status(err.code || 500).json(err);
})
}
function obtainGrantCode(req, res) {
let id = req.params.id;
models.getUser(id)
.then(u => {
if (u) {
res.json({
uri: "",
token: u.accessTokenMith
}).send();
}
const sdk = new MithVaultSDK({ clientId, clientSecret, miningKey })
const uri = sdk.getBindURI()
res.json({
uri: uri,
token: ""
});
})
}