Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Implementation of basic API authentication #1228

Merged
merged 7 commits into from
Jun 6, 2018
Merged

Conversation

AndreaLanfranchi
Copy link
Collaborator

Will help protect exposure of API port to the Web

Copy link
Contributor

@chfast chfast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By quick check it looks good, but I will first merge 0.15 changes back to master branch.

@@ -817,6 +822,7 @@ class MinerCLI

#if API_CORE
int m_api_port = 0;
string m_api_password = "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The = "" is not needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@@ -36,7 +37,7 @@ void ApiServer::start()
return;
}

cnote << "Api server listening for connections on port " + to_string(m_acceptor.local_endpoint().port());
cnote << "Api server listening for connections on port " + to_string(m_acceptor.local_endpoint().port()) << (m_password.empty() ? "" : " Authentication needed");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a . before Authentication needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a sophistication ... that line will be quickly scrolled outside the boundaries of the screen.
Anyway ... ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove "for connections" part of the message.

@@ -142,63 +143,101 @@ void ApiConnection::processRequest(Json::Value& requestObject)
std::string _method = requestObject.get("method", "").asString();
jRes["id"] = requestObject.get("id", 0).asInt();

// Check authentication
if (!m_is_authenticated) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried integrate clang-format to your editor. It would make it easier to have consistent coding style.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for formatting.

else {
Json::Value jPrm = requestObject["params"];
if (!jPrm.isMember("password") || jPrm["password"].empty() || !jPrm["password"].isString()) {
jRes["error"]["code"] = -32602;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error code -32602 is only for "Invalid params". We should assign our own error code here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And in fact here we have invalid params (the "password" member is missing).
Maybe you refer to validation of password ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right. Sorry.

jRes["result"] = true;
m_farm.shuffle();
if (!requestObject.isMember("params") || requestObject["params"].empty() || !requestObject["params"].isObject()) {
jRes["error"]["code"] = -32602;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be -32600 error code "Invalid Request". http://www.jsonrpc.org/specification#error_object


// Replies back to (check for liveness)
jRes["result"] = "pong";
jRes["error"]["code"] = -32601;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong error code.

m_is_authenticated = true;
}
else {
jRes["error"]["code"] = -32602;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong error code.

Farm& m_farm;

bool m_is_authenticated = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we always init that to false but require authentication only if the password is not empty?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And in fact it is like that. It's m_is_authenticated, not m_requires_auth

@StefanOberhumer
Copy link
Collaborator

I know - Claymore is not the standard.
So (just) as info: Claymore uses psw as direct API parameter for the password within 'miner_getstat1', miner_getstat2'....
Should we use/support the same parameter name ?
(Not because 'psw' is a good name - just because supporting different existing monitoring tools?)

@AndreaLanfranchi
Copy link
Collaborator Author

@StefanOberhumer you're driving me crazy with that Claymore's ... :)

Accomplished.

@AndreaLanfranchi
Copy link
Collaborator Author

@StefanOberhumer does rubbishmore implement also a specific method name ?

@AndreaLanfranchi
Copy link
Collaborator Author

@StefanOberhumer
Please note also that password authentication for us is session wide: which means you do not have to pass the password at every request (which is slightly more secure in case of man-in-the-middle attack)

}
else
{
jRes["error"]["code"] = -32001;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error codes from and including -32768 to -32000 are reserved
This can be any value, e.g. 1.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will use HTTP like range
-401 Unauthorized

}
else
{
jRes["error"]["code"] = -32601;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will use HTTP like range
-403 Forbidden

@AndreaLanfranchi AndreaLanfranchi merged commit a1cfec2 into master Jun 6, 2018
@AndreaLanfranchi AndreaLanfranchi deleted the api-password branch June 6, 2018 15:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants