-
Notifications
You must be signed in to change notification settings - Fork 1
/
steem-auth.js
82 lines (72 loc) · 2.96 KB
/
steem-auth.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
const steem = require('steem');
const config = require("./config.js");
// Define a custom Error class for authentication errors. The
// constructor implementation allows more specific errors to be
// derived as one-liners.
class AuthError extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name
}
}
class BadAuthDataError extends AuthError {}
class InvalidCredentialError extends AuthError {}
class AccessDeniedError extends AuthError {}
/**
* Tests if an username/private key pair is correct
* @param {String} username - username of the account
* @param {String} wif - Private key used for login
* @param {String} type - Type of the private key, can be "posting", "active" or "owner"
* @return {boolean} valid - True if the password is correct, false if not (or if the account doesn't exists)
*/
function login_using_wif(username, wif, type) {
steem.api.setOptions({url: 'https://rpc.buildteam.io'});
return new Promise(resolve => {
steem.api.getAccounts([username], function (err, result) {
// check if the account exists
if (result.length !== 0) {
// get the public posting key
let pubWif = "";
if (type === "posting")
pubWif = result[0].posting.key_auths[0][0];
else if (type === "active")
pubWif = result[0].active.key_auths[0][0];
else if (type === "owner")
pubWif = result[0].owner.key_auths[0][0];
else if (type === "memo")
pubWif = result[0].memo_key;
let valid = false;
try {
// Check if the private key matches the public one.
valid = steem.auth.wifIsValid(wif, pubWif)
} catch (e) {
}
return resolve(valid);
}
return resolve(false);
});
});
}
// Login function used for legacy API.
function login(data) {
return new Promise(async resolve => {
if (data['username'] && data['wif'] && data['type'] && data['username'] !== "" && data['wif'] !== "" && data['type'] !== "") {
if (config.admins.indexOf(data['username']) === -1)
return resolve({error: "You are not authorized to perform this action", ok : false});
if (data['type'] !== "posting" && data['type'] !== "memo")
return resolve({error: "Invalid key type"});
const valid = await login_using_wif(data['username'], data['wif'], data['type']);
if (valid === false)
return resolve({error: "Wrong username/wif/type combination", ok : false});
return resolve({ok : true});
} else {
return resolve({error: "Invalid login parameters", ok : false});
}
});
}
module.exports = {login,
login_using_wif,
AuthError,
BadAuthDataError,
InvalidCredentialError,
AccessDeniedError}