File indexing completed on 2024-12-01 04:35:31

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ddpauthenticationmanagerutils.h"
0008 #include "utils.h"
0009 
0010 #include <QJsonObject>
0011 
0012 QJsonArray DDPAuthenticationManagerUtils::loginResume(const QString &token)
0013 {
0014     QJsonArray array;
0015     QJsonObject resumeObject;
0016     resumeObject[QLatin1String("resume")] = token;
0017     array.append(resumeObject);
0018     return array;
0019 }
0020 
0021 QJsonArray DDPAuthenticationManagerUtils::loginLdap(const QString &user, const QString &password)
0022 {
0023     QJsonArray array;
0024     QJsonObject ldapObject;
0025     ldapObject[QLatin1String("ldap")] = true;
0026     ldapObject[QLatin1String("username")] = user;
0027 
0028     // Ldap seems to new supported hashPassword... To bad.
0029     ldapObject[QLatin1String("ldapPass")] = password;
0030     ldapObject[QLatin1String("ldapOptions")] = QJsonObject();
0031     array.append(ldapObject);
0032     return array;
0033 }
0034 
0035 QJsonArray DDPAuthenticationManagerUtils::loginOAuth(const QString &credentialToken, const QString &credentialSecret)
0036 {
0037     QJsonArray array;
0038     QJsonObject oauthObject;
0039 
0040     QJsonObject oauthCredentialObject;
0041     oauthCredentialObject[QLatin1String("credentialToken")] = credentialToken;
0042     oauthCredentialObject[QLatin1String("credentialSecret")] = credentialSecret;
0043     oauthObject[QLatin1String("oauth")] = oauthCredentialObject;
0044     array.append(oauthObject);
0045     return array;
0046 }
0047 
0048 QJsonObject DDPAuthenticationManagerUtils::hashPassword(const QString &password)
0049 {
0050     QJsonObject passwordObject;
0051     const QByteArray sha256pw = Utils::convertSha256Password(password);
0052     passwordObject[QLatin1String("algorithm")] = QStringLiteral("sha-256");
0053     passwordObject[QLatin1String("digest")] = QString::fromLatin1(sha256pw);
0054     return passwordObject;
0055 }
0056 
0057 QJsonArray DDPAuthenticationManagerUtils::login(const QString &user, const QString &password)
0058 {
0059     QJsonArray array;
0060     QJsonObject loginObject;
0061 
0062     QJsonObject userObject;
0063     if (user.contains(QLatin1Char('@'))) {
0064         userObject[QLatin1String("email")] = user;
0065     } else {
0066         userObject[QLatin1String("username")] = user;
0067     }
0068     loginObject[QLatin1String("user")] = userObject;
0069 
0070     const QJsonObject passwordObject = hashPassword(password);
0071     loginObject[QLatin1String("password")] = passwordObject;
0072 
0073     array.append(loginObject);
0074     return array;
0075 }
0076 
0077 QJsonArray DDPAuthenticationManagerUtils::sendOTP(const QString &otpCode, const QJsonObject &lastLoginPayload)
0078 {
0079     QJsonArray array;
0080     QJsonObject otp;
0081 
0082     QJsonObject totpObject;
0083     totpObject[QLatin1String("code")] = otpCode;
0084     totpObject[QLatin1String("login")] = lastLoginPayload;
0085     otp[QLatin1String("totp")] = totpObject;
0086     array.append(otp);
0087     return array;
0088 }