login with email or username

This commit is contained in:
einhornimmond 2021-06-02 13:18:27 +02:00
parent 9e47ae36a4
commit b08a9ff059
4 changed files with 39 additions and 7 deletions

View File

@ -38,9 +38,12 @@ with:
```json ```json
{ {
"email": "max.musterman@gmail.de", "email": "max.musterman@gmail.de",
"username": "Maxilein",
"password": "123abcDE&" "password": "123abcDE&"
} }
``` ```
`username` or `email` must be present!
If booth present, `email` will be used.
### Response ### Response
In case of success returns: In case of success returns:

View File

@ -21,6 +21,7 @@ Poco::JSON::Object* JsonUnsecureLogin::handle(Poco::Dynamic::Var params)
// incoming // incoming
std::string email; std::string email;
std::string username;
std::string password; std::string password;
// if is json object // if is json object
@ -32,8 +33,17 @@ Poco::JSON::Object* JsonUnsecureLogin::handle(Poco::Dynamic::Var params)
/// not available for the given type. /// not available for the given type.
/// Throws InvalidAccessException if Var is empty. /// Throws InvalidAccessException if Var is empty.
try { try {
paramJsonObject->get("email").convert(email); //paramJsonObject->get("email").convert(email);
paramJsonObject->get("password").convert(password); paramJsonObject->get("password").convert(password);
auto email_obj = paramJsonObject->get("email");
auto username_obj = paramJsonObject->get("username");
if (!email_obj.isEmpty()) {
email_obj.convert(email);
}
if (!username_obj.isEmpty()) {
username_obj.convert(username);
}
} }
catch (Poco::Exception& ex) { catch (Poco::Exception& ex) {
return stateError("json exception", ex.displayText()); return stateError("json exception", ex.displayText());
@ -43,13 +53,24 @@ Poco::JSON::Object* JsonUnsecureLogin::handle(Poco::Dynamic::Var params)
return stateError("parameter format unknown"); return stateError("parameter format unknown");
} }
if (!email.size() && !username.size()) {
if (!email.size() || !sm->isValid(email, VALIDATE_EMAIL)) { return stateError("no email or username given");
return stateError("invalid or empty email");
} }
auto user = controller::User::create(); auto user = controller::User::create();
if (1 != user->load(email)) { if (email.size()) {
return stateError("user with email not found", email); if (!sm->isValid(email, VALIDATE_EMAIL)) {
return stateError("invalid email");
}
if (1 != user->load(email)) {
return stateError("user with email not found", email);
}
}
else if (username.size() > 0) {
if (1 != user->load(username)) {
return stateError("user with username not found", username);
}
email = user->getModel()->getEmail();
} }
NotificationList pwd_errors; NotificationList pwd_errors;

View File

@ -122,6 +122,14 @@ namespace controller {
Poco::Data::BLOB email_hash(*emailHash, crypto_generichash_BYTES); Poco::Data::BLOB email_hash(*emailHash, crypto_generichash_BYTES);
return getModel()->loadFromDB("email_hash", email_hash); return getModel()->loadFromDB("email_hash", email_hash);
} }
size_t User::load(const std::string& emailOrUsername)
{
auto model = getModel();
if (1 == model->loadFromDB("email", emailOrUsername)) {
return 1;
}
return model->loadFromDB("username", emailOrUsername);
}
Poco::AutoPtr<User> User::sload(int user_id) Poco::AutoPtr<User> User::sload(int user_id)
{ {
auto db = new model::table::User(); auto db = new model::table::User();

View File

@ -70,7 +70,7 @@ namespace controller {
//! \return 0 matching entry found //! \return 0 matching entry found
int tryLoadPassphraseUserBackup(KeyPairEd25519** createdKeyPair = nullptr); int tryLoadPassphraseUserBackup(KeyPairEd25519** createdKeyPair = nullptr);
inline size_t load(const std::string& email) { return getModel()->loadFromDB("email", email); } size_t load(const std::string& emailOrUsername);
//! \brief try to load user from db via user_id //! \brief try to load user from db via user_id
//! \return count of found rows, should be 1 or 0 //! \return count of found rows, should be 1 or 0
inline size_t load(int user_id) { return getModel()->loadFromDB("id", user_id); } inline size_t load(int user_id) { return getModel()->loadFromDB("id", user_id); }