2013-07-29 16:51:26 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Intel Corporation
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) version 3.
|
|
|
|
*
|
|
|
|
* This library 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <syncevo/IdentityProvider.h>
|
|
|
|
#include <syncevo/SyncConfig.h>
|
2014-04-02 14:57:56 +02:00
|
|
|
#include <syncevo/Exception.h>
|
2013-07-29 16:51:26 +02:00
|
|
|
|
2013-07-30 13:19:02 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2013-07-29 16:51:26 +02:00
|
|
|
SE_BEGIN_CXX
|
|
|
|
|
|
|
|
const char USER_IDENTITY_PLAIN_TEXT[] = "user";
|
|
|
|
const char USER_IDENTITY_SYNC_CONFIG[] = "id";
|
|
|
|
|
|
|
|
Credentials IdentityProviderCredentials(const UserIdentity &identity,
|
|
|
|
const InitStateString &password)
|
|
|
|
{
|
|
|
|
Credentials cred;
|
|
|
|
|
|
|
|
if (identity.m_provider == USER_IDENTITY_PLAIN_TEXT) {
|
|
|
|
cred.m_username = identity.m_identity;
|
|
|
|
cred.m_password = password;
|
|
|
|
} else {
|
|
|
|
// We could use the gSSO password plugin to request
|
|
|
|
// username/password. But it is uncertain whether that is useful,
|
|
|
|
// therefore that is not implemented at the moment.
|
|
|
|
SE_THROW(StringPrintf("%s: need username+password as credentials", identity.toString().c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return cred;
|
|
|
|
}
|
|
|
|
|
2013-07-30 10:17:33 +02:00
|
|
|
class CredentialsProvider : public AuthProvider
|
|
|
|
{
|
|
|
|
Credentials m_creds;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CredentialsProvider(const std::string &username,
|
|
|
|
const std::string &password)
|
|
|
|
{
|
|
|
|
m_creds.m_username = username;
|
|
|
|
m_creds.m_password = password;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool wasConfigured() const { return !m_creds.m_username.empty() || !m_creds.m_password.empty(); }
|
|
|
|
virtual bool methodIsSupported(AuthMethod method) const { return method == AUTH_METHOD_CREDENTIALS; }
|
2014-11-28 16:46:40 +01:00
|
|
|
virtual Credentials getCredentials() { return m_creds; }
|
|
|
|
virtual std::string getOAuth2Bearer(const PasswordUpdateCallback &passwordUpdateCallback) { SE_THROW("OAuth2 not supported"); return ""; }
|
2013-07-30 10:17:33 +02:00
|
|
|
virtual std::string getUsername() const { return m_creds.m_username; }
|
|
|
|
};
|
|
|
|
|
2018-01-16 17:17:34 +01:00
|
|
|
std::shared_ptr<AuthProvider> AuthProvider::create(const UserIdentity &identity,
|
2013-07-30 10:17:33 +02:00
|
|
|
const InitStateString &password)
|
|
|
|
{
|
2018-01-16 17:17:34 +01:00
|
|
|
std::shared_ptr<AuthProvider> authProvider;
|
2013-07-30 10:17:33 +02:00
|
|
|
|
|
|
|
if (identity.m_provider == USER_IDENTITY_PLAIN_TEXT) {
|
2013-09-04 17:02:14 +02:00
|
|
|
SE_LOG_DEBUG(NULL, "using plain username/password for %s", identity.toString().c_str());
|
2013-07-30 10:17:33 +02:00
|
|
|
authProvider.reset(new CredentialsProvider(identity.m_identity, password));
|
|
|
|
} else {
|
2013-09-04 17:02:14 +02:00
|
|
|
SE_LOG_DEBUG(NULL, "looking for identity provider for %s", identity.toString().c_str());
|
2018-01-16 10:58:04 +01:00
|
|
|
for (IdentityProvider *idProvider: IdentityProvider::getRegistry()) {
|
2013-07-30 13:19:02 +02:00
|
|
|
if (boost::iequals(idProvider->m_key, identity.m_provider)) {
|
|
|
|
authProvider = idProvider->create(identity.m_identity, password);
|
|
|
|
if (!authProvider) {
|
|
|
|
SE_THROW(StringPrintf("identity provider for '%s' is disabled in this installation",
|
|
|
|
identity.m_provider.c_str()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!authProvider) {
|
|
|
|
SE_THROW(StringPrintf("unknown identity provider '%s' in '%s'",
|
|
|
|
identity.m_provider.c_str(),
|
|
|
|
identity.toString().c_str()));
|
|
|
|
}
|
2013-07-30 10:17:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return authProvider;
|
|
|
|
}
|
|
|
|
|
2013-07-30 13:19:02 +02:00
|
|
|
std::list<IdentityProvider *> &IdentityProvider::getRegistry()
|
|
|
|
{
|
|
|
|
static std::list<IdentityProvider *> providers;
|
|
|
|
return providers;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentityProvider::IdentityProvider(const std::string &key,
|
|
|
|
const std::string &descr) :
|
|
|
|
m_key(key),
|
|
|
|
m_descr(descr)
|
|
|
|
{
|
|
|
|
getRegistry().push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentityProvider::~IdentityProvider()
|
|
|
|
{
|
|
|
|
getRegistry().erase(std::find(getRegistry().begin(),
|
|
|
|
getRegistry().end(),
|
|
|
|
this));
|
|
|
|
}
|
|
|
|
|
2013-07-29 16:51:26 +02:00
|
|
|
SE_END_CXX
|