fixes required by review of pr1700

This commit is contained in:
csoler 2019-11-25 22:16:32 +01:00
parent 5223ff751a
commit 7aa51423a4
No known key found for this signature in database
GPG key ID: 7BCA522266C0804C
13 changed files with 98 additions and 154 deletions

View file

@ -117,15 +117,6 @@ JsonApiServer::corsOptionsHeaders =
return false;
}
static bool is_alphanumeric(char c) { return (c>='0' && c<'9') || (c>='a' && c<='z') || (c>='A' && c<='Z') ;}
static bool is_alphanumeric(const std::string& s)
{
for(uint32_t i=0;i<s.size();++i)
if(!is_alphanumeric(s[i]))
return false;
return true;
}
bool RsJsonAPI::parseToken(const std::string& clear_token,std::string& user,std::string& passwd)
{
uint32_t last_index=0;
@ -137,7 +128,7 @@ bool RsJsonAPI::parseToken(const std::string& clear_token,std::string& user,std:
++nb_colons;
last_index = i;
}
else if(!is_alphanumeric(clear_token[i]))
else if(!librs::util::is_alphanumeric(clear_token[i]))
return false;
if(nb_colons != 1)
@ -424,7 +415,7 @@ void JsonApiServer::registerHandler(
else session->close(rb::UNAUTHORIZED);
} );
_resources.push_back(resource);
mResources.push_back(resource);
}
void JsonApiServer::setNewAccessRequestCallback( const std::function<bool (const std::string&,std::string&)>& callback )
@ -501,8 +492,11 @@ void JsonApiServer::connectToConfigManager(p3ConfigMgr *cfgmgr)
bool JsonApiServer::authorizeUser(const std::string& user,const std::string& passwd)
{
if(!is_alphanumeric(user) || !is_alphanumeric(passwd))
if(!librs::util::is_alphanumeric(user) || !librs::util::is_alphanumeric(passwd))
{
RsErr() << "Password or username for jsonapi token is not alphanumeric." << std::endl;
return false;
}
RS_STACK_MUTEX(configMutex);
@ -516,26 +510,6 @@ bool JsonApiServer::authorizeUser(const std::string& user,const std::string& pas
return true;
}
// bool JsonApiServer::authorizeToken(const std::string& token)
// {
// std::string user,password;
//
// if(!parseToken(token,user,password))
// return false;
//
// RS_STACK_MUTEX(configMutex);
//
// std::string& p(mAuthTokenStorage.mAuthorizedTokens[user]);
//
// if(p != password)
// {
// p = password;
// IndicateConfigChanged();
// }
// return true;
// }
/*static*/ std::string JsonApiServer::decodeToken(const std::string& radix64_token)
{
std::vector<uint8_t> decodedVect(Radix64::decode(radix64_token));
@ -601,32 +575,24 @@ void JsonApiServer::handleCorsOptions(
const std::shared_ptr<restbed::Session> session )
{ session->close(rb::NO_CONTENT, corsOptionsHeaders); }
int JsonApiServer::status()
{
if(RestbedService::isRunning())
return JSONAPI_STATUS_RUNNING;
else
return JSONAPI_STATUS_NOT_RUNNING;
}
void JsonApiServer::registerResourceProvider(const JsonApiResourceProvider *rp)
{
_resource_providers.insert(rp);
mResourceProviders.insert(rp);
}
void JsonApiServer::unregisterResourceProvider(const JsonApiResourceProvider *rp)
{
_resource_providers.erase(rp);
mResourceProviders.erase(rp);
}
bool JsonApiServer::hasResourceProvider(const JsonApiResourceProvider *rp)
{
return _resource_providers.find(rp) != _resource_providers.end();
return mResourceProviders.find(rp) != mResourceProviders.end();
}
std::vector<std::shared_ptr<rb::Resource> > JsonApiServer::getResources() const
{
auto tab = _resources;
auto tab = mResources;
for(auto& rp: _resource_providers)
for(auto& rp: mResourceProviders)
for(auto r: rp->getResources())
tab.push_back(r);

View file

@ -68,8 +68,8 @@ public:
// RsJsonAPI public API
bool restart() override { return RestbedService::restart(); }
bool stop() override { return RestbedService::stop();}
int status() override;
bool stop() override { return RestbedService::fullstop();}
bool isRunning() override { return RestbedService::isRunning(); }
void setListeningPort(uint16_t port) override { return RestbedService::setListeningPort(port); }
void setBindingAddress(const std::string& bind_address) override { return RestbedService::setBindAddress(bind_address); }
@ -185,7 +185,7 @@ private:
return checkRsServicePtrReady( serviceInstance.get(), serviceName, ctx, session );
}
std::vector<std::shared_ptr<rb::Resource> > _resources;
std::set<const JsonApiResourceProvider *> _resource_providers;
std::vector<std::shared_ptr<rb::Resource> > mResources;
std::set<const JsonApiResourceProvider *> mResourceProviders;
};

View file

@ -6,7 +6,7 @@
* Copyright 2019-2019 Cyril Soler *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
@ -15,31 +15,32 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include "retroshare/rsjsonapi.h"
#include "util/rsthreads.h"
#include "util/rsdebug.h"
#include "restbedservice.h"
RestbedService::RestbedService()
: mService(std::make_shared<restbed::Service>()), // this is a place holder, in case we request some internal values.
mListeningPort(RsJsonAPI::DEFAULT_PORT),
mBindingAddress(RsJsonAPI::DEFAULT_BINDING_ADDRESS)
{
mService = std::make_shared<restbed::Service>(); // this is a place holder, in case we request some internal values.
mListeningPort = 9092;
mBindingAddress = "127.0.0.1";
}
bool RestbedService::restart()
{
stop();
fullstop();
start("Restbed Service");
return true;
}
bool RestbedService::stop()
bool RestbedService::fullstop()
{
if(!mService->is_up())
return true;
@ -69,7 +70,7 @@ void RestbedService::runloop()
if(mService->is_up())
{
std::cerr << "(II) WebUI is already running. Killing it." << std::endl;
RsWarn() << "(II) WebUI is already running. Killing it." << std::endl;
mService->stop();
}

View file

@ -6,16 +6,16 @@
* Copyright 2019-2019 Cyril Soler *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
@ -34,7 +34,7 @@ public:
virtual ~RestbedService() = default;
bool restart();
bool stop();
bool fullstop();
void setListeningPort(uint16_t port) ;
void setBindAddress(const std::string& bind_address);