From f9f71436818a5c2e18eab6de9117c6a611eb6ecc Mon Sep 17 00:00:00 2001 From: Egor Nemtsev Date: Fri, 6 Sep 2019 19:27:23 +0300 Subject: Authorization rework - move widget-dependent web page interaction to qml part,so this doesn't require special build for appman - add captcha processing - use Neptune-style controls in AuthView Task-number: AUTOSUITE-1197 Change-Id: If52552eee29abc370b0aefe5ceb1edb11e9b024c Reviewed-by: Bramastyo Harimukti Santoso --- plugins/alexaauth/alexaauth.cpp | 317 +++++++++------------------------ plugins/alexaauth/alexaauth.h | 130 ++++++-------- plugins/alexaauth/alexaauth.pro | 16 +- plugins/alexaauth/alexaauth_plugin.cpp | 2 +- 4 files changed, 146 insertions(+), 319 deletions(-) (limited to 'plugins') diff --git a/plugins/alexaauth/alexaauth.cpp b/plugins/alexaauth/alexaauth.cpp index b45b9ab..b76e495 100644 --- a/plugins/alexaauth/alexaauth.cpp +++ b/plugins/alexaauth/alexaauth.cpp @@ -31,129 +31,71 @@ #include "alexaauth.h" -#ifdef ALEXA_QT_WEBENGINE -#include -#include -#include -#include #include -#endif #include #include +#include +#include AlexaAuth::AlexaAuth(QObject *parent) : QObject(parent) { -#ifdef ALEXA_QT_WEBENGINE - m_authPage.profile()->clearHttpCache(); - m_authPage.profile()->cookieStore()->deleteAllCookies(); - m_authPage.profile()->setHttpAcceptLanguage("en-US,en;q=0.9"); - m_httpUserAgent = m_authPage.profile()->httpUserAgent(); - m_error = ErrorState::None; -#else - qDebug() << "QWebEngine not available, cannot authorize automatically."; - m_error = AlexaAuth::WebEngineNotAvailable; -#endif -} - -void AlexaAuth::setIsAuthorizing(bool isAuthorizing) -{ - if (m_isAuthorizing == isAuthorizing) - return; - - m_isAuthorizing = isAuthorizing; - emit isAuthorizingChanged(m_isAuthorizing); -} - -void AlexaAuth::setAuthCode(QString authCode) -{ - qDebug() << Q_FUNC_INFO << " " << authCode; - if (m_authCode == authCode) - return; - - m_authCode = authCode; - emit authCodeChanged(m_authCode); -} - -void AlexaAuth::setAuthUrl(QUrl authUrl) -{ - qDebug() << Q_FUNC_INFO << " " << authUrl; - if (m_authUrl == authUrl) - return; - - m_authUrl = authUrl; - emit authUrlChanged(m_authUrl); -} - -void AlexaAuth::setError(AlexaAuth::ErrorState error) -{ - if (m_error == error) - return; - - if (error != AlexaAuth::None) { - setIsAuthorizing(false); + QtWebEngine::initialize(); + QQuickWebEngineProfile::defaultProfile()->cookieStore()->deleteAllCookies(); +} + +QString AlexaAuth::getJSString(AlexaAuth::JSAuthString id, const QString &value) const +{ + QString result; + switch (id) { + case SignIn: + result = QString("document.getElementsByClassName('") + TAG_ALERT_HEADING_ID + "')[0].textContent"; + break; + case CaptchaSrc: + result = QString("document.getElementById('") + TAG_CAPTCHA_IMAGE_ID +"').src"; + break; + case GetCaptchaInput: + result = QString("document.getElementById('") + TAG_CAPTCHA_GUESS_ID + "')"; + break; + case SetCaptcha: + result = QString("document.getElementById('") + TAG_CAPTCHA_GUESS_ID + "').value='" + value + "'"; + break; + case GetEmailInput: + result = QString("document.getElementById('") + TAG_EMAIL_ID +"')"; + break; + case SetEmail: + result = QString("document.getElementById('") + TAG_EMAIL_ID + "').value='" + value+ "'"; + break; + case GetPasswordInput: + result = QString("document.getElementById('") + TAG_PASSWORD_ID +"')"; + break; + case SetPassword: + result = QString("document.getElementById('") + TAG_PASSWORD_ID + "').value='" + value + "'"; + break; + case GetClickSignIn: + result = QString("document.getElementById('") + TAG_SIGN_IN_SUBMIT_ID + "')"; + break; + case RegisterDeviceTitle: + result = QString("document.getElementById('") + TAG_SUCCESS_TITLE_ID + "').textContent"; + break; + case GetInputCode: + result = QString("document.getElementById('") + TAG_REGISTRATION_FIELD_ID + "')"; + break; + case SetInputCode: + result = QString("document.getElementById('") + TAG_REGISTRATION_FIELD_ID + "').value='" + value + "'"; + break; + case GetContinue: + result = QString("document.getElementById('") + TAG_CONTINUE_BUTTON_ID + "')"; + break; + case ClickElement: + result = value + ".click()"; + break; } - m_error = error; -#ifdef ALEXA_QT_WEBENGINE - QObject::disconnect( &m_authPage, &QWebEnginePage::loadFinished, this, &AlexaAuth::authPageLoaded); -#endif - emit errorChanged(m_error); + return result; } -void AlexaAuth::setHttpUserAgent(QString httpUserAgent) +bool AlexaAuth::parseJson() const { - qDebug() << Q_FUNC_INFO << httpUserAgent; - if (m_httpUserAgent == httpUserAgent) - return; - - m_httpUserAgent = httpUserAgent; -#ifdef ALEXA_QT_WEBENGINE - m_authPage.profile()->setHttpUserAgent(m_httpUserAgent); -#endif - emit httpUserAgentChanged(m_httpUserAgent); -} - -void AlexaAuth::setAuthorizationSucceed(bool authorizationSucceed) -{ - if (m_authorizationSucceed == authorizationSucceed) - return; - - m_authorizationSucceed = authorizationSucceed; - emit authorizationSucceedChanged(m_authorizationSucceed); -} - -void AlexaAuth::setEmail(QString email) -{ - if (m_email == email) - return; - m_email = email; - emit emailChanged(m_email); -} - -void AlexaAuth::setPassword(QString password) -{ - if (m_password == password) - return; - m_password = password; - emit passwordChanged(m_password); -} - -void AlexaAuth::authorize() -{ - qDebug() << Q_FUNC_INFO << " " << m_authUrl; -#ifdef ALEXA_QT_WEBENGINE - if (parseJson()) { - QObject::connect( &m_authPage, &QWebEnginePage::loadFinished, this, &AlexaAuth::authPageLoaded); - setIsAuthorizing(true); - m_authPage.load(m_authUrl); - } -#endif -} - -#ifdef ALEXA_QT_WEBENGINE -bool AlexaAuth::parseJson() -{ - qDebug() << Q_FUNC_INFO; if (qEnvironmentVariableIsSet("ALEXA_SDK_CONFIG_FILE")) { QFile file(qEnvironmentVariable("ALEXA_SDK_CONFIG_FILE")); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { @@ -167,9 +109,9 @@ bool AlexaAuth::parseJson() line = in.readLine(); indx = line.indexOf("//"); if ( indx >= 0 ) { - lines += line.mid(0, indx); + lines += line.mid(0, indx); } else { - lines += line + "\n"; + lines += line + "\n"; } } file.close(); @@ -178,144 +120,55 @@ bool AlexaAuth::parseJson() if (jsonError.error != QJsonParseError::NoError){ qDebug() << "Cannot parse AlexaClientSDKConfig.json: " << jsonError.errorString(); - setError(AlexaAuth::ConfigFileFailure); return false; } return true; } else { qWarning() << "Couldn't open the config file AlexaClientSDKConfig.json"; - setError(AlexaAuth::ConfigFileFailure); return false; } } else { qWarning() << "Couldn't read the environment variable ALEXA_SDK_CONFIG_FILE"; - setError(AlexaAuth::ConfigFileFailure); return false; } } -void AlexaAuth::authPageLoaded(bool ok) +AlexaAuth::AuthStage AlexaAuth::getAuthStage(const QString &title) { - qDebug() << Q_FUNC_INFO << " " << ok << " " << m_authPage.title(); - if (ok) { - if (m_authPage.title() == HTML_TITLE_FIRST) { - QTimer::singleShot(2000, this, &AlexaAuth::signinToAmazon); - - } else if (m_authPage.title() == HTML_TITLE_SECOND) { - QTimer::singleShot(1000, this, &AlexaAuth::registerDevice); - - } else { - qWarning() << "Unknown HTML title " << m_authPage.title(); - setError(AlexaAuth::AutomaticAuthFailed); - } - } else { - qWarning() << "Something went wrong to load the auth page"; - setError(AlexaAuth::AutomaticAuthFailed); + if (title == HTML_TITLE_FIRST) { + return AuthSignIn; + } else if (title == HTML_TITLE_SECOND) { + return AuthRegisterDevice; } + qWarning() << "Unknown HTML title " << title; + return AuthError; } -void AlexaAuth::signinToAmazon() -{ - qDebug() << Q_FUNC_INFO; - m_authPage.runJavaScript(QString("document.getElementsByClassName('") + TAG_ALERT_HEADING_ID + "')[0].textContent", [this](const QVariant &cb) { - if (cb.isNull() || cb.toString() == "" || cb.toString() == HTML_ENABLE_COOKIES) { - inputEMail(); - } else if (cb.toString() == HTML_IMPORTANT_MESSAGE) { - qWarning() << "Image capture detected! Cannot proceed automatically. Please, authorize manually on " << m_authUrl; - setError(AlexaAuth::ImageRecognizionRequired); - } else { - qDebug() << "Something went wrong in " << Q_FUNC_INFO << " " << cb.toString(); - setError(AlexaAuth::AutomaticAuthFailed); - } - }); -} - -void AlexaAuth::inputEMail() +AlexaAuth::SignInResult AlexaAuth::signinToAmazonResult(const QVariant &cb) { - qDebug() << Q_FUNC_INFO; - m_authPage.runJavaScript(QString("document.getElementById('") + TAG_EMAIL_ID +"')", [this](const QVariant &cb) { - if (cb.isNull()) { - qWarning() << "Email field doesn't exist on the page."; - setError(AlexaAuth::HtmlItemNotFound); - } else { - m_authPage.runJavaScript(QString("document.getElementById('") + TAG_EMAIL_ID + "').value='" + m_email + "'"); - QTimer::singleShot(2000, this, &AlexaAuth::inputPassword); - } - }); -} - -void AlexaAuth::inputPassword() -{ - qDebug() << Q_FUNC_INFO; - m_authPage.runJavaScript(QString("document.getElementById('") + TAG_PASSWORD_ID + "')", [this](const QVariant &cb){ - if (cb.isNull()) { - qWarning() << "Password field doesn't exist on the page"; - setError(AlexaAuth::HtmlItemNotFound); - } else { - m_authPage.runJavaScript(QString("document.getElementById('") + TAG_PASSWORD_ID + "').value='" + m_password + "'"); - QTimer::singleShot(2000, this, &AlexaAuth::clickSignIn); - } - }); -} - -void AlexaAuth::clickSignIn() -{ - qDebug() << Q_FUNC_INFO; - m_authPage.runJavaScript(QString("document.getElementById('") + TAG_SIGN_IN_SUBMIT_ID + "')", [this](const QVariant &cb){ - if (cb.isNull()) { - qWarning() << "Sign in button doesn't exist on the page"; - setError(AlexaAuth::HtmlItemNotFound); - } else { - m_authPage.runJavaScript(QString("document.getElementById('") + TAG_SIGN_IN_SUBMIT_ID + "').click()"); - } - }); -} - -void AlexaAuth::registerDevice() -{ - qDebug() << Q_FUNC_INFO; - m_authPage.runJavaScript(QString("document.getElementById('") + TAG_SUCCESS_TITLE_ID + "').textContent", [this](const QVariant &cb) { - if (cb.toString() == HTML_REGISTER_DEVICE) { - inputCode(); - } else if (cb.toString() == HTML_SUCCESS) { - qDebug() << "Automatic authorization completed successfully."; - setIsAuthorizing(false); - setAuthorizationSucceed(true); - } else { - qDebug() << "Something went wrong in " << Q_FUNC_INFO << " " << cb.toString(); - setError(AlexaAuth::AutomaticAuthFailed); - } - }); -} + if (cb.isNull() || cb.toString() == "" || cb.toString() == HTML_ENABLE_COOKIES) { + return SignInInputEmail; + } else if (cb.toString() == HTML_IMPORTANT_MESSAGE) { + qWarning() << "Image capture detected!"; + return SignInCaptcha; + } else if (cb.toString() == HTML_TITLE_ERROR_CAPTCHA) { + qWarning() << "Image capture detected!, wrong captcha"; + return SignInCaptcha; + } -void AlexaAuth::inputCode() -{ - qDebug() << Q_FUNC_INFO; - m_authPage.runJavaScript(QString("document.getElementById('") + TAG_REGISTRATION_FIELD_ID + "')" , [this] (const QVariant &cb) { - if (cb.isNull()) { - qWarning() << "No field for authorization code!"; - setError(AlexaAuth::HtmlItemNotFound); - } else if (m_authCode.length() > 0) { - m_authPage.runJavaScript(QString("document.getElementById('") + TAG_REGISTRATION_FIELD_ID + "').value='" + m_authCode + "'"); - QTimer::singleShot(2000, this, &AlexaAuth::clickContinue); - } else { - qDebug() << "Authorization code was empty"; - setError(AlexaAuth::AutomaticAuthFailed); - } - }); + qDebug() << "Something went wrong in " << Q_FUNC_INFO << " " << cb.toString(); + return SignInError; } -void AlexaAuth::clickContinue() +AlexaAuth::RegisterDeviceResult AlexaAuth::registerDeviceResult(const QVariant &cb) { - qDebug() << Q_FUNC_INFO; - m_authPage.runJavaScript(QString("document.getElementById('") + TAG_CONTINUE_BUTTON_ID + "')", [this] (const QVariant &cb) { - if (cb.isNull()) { - qWarning() << "Not found 'continue' button"; - setError(AlexaAuth::HtmlItemNotFound); - } else { - m_authPage.runJavaScript(QString("document.getElementById('") + TAG_CONTINUE_BUTTON_ID + "').click()"); - } - }); - // todo: check what happens if the code was wrong + if (cb.toString() == HTML_REGISTER_DEVICE) { + return AlexaAuth::RegisterDevice; + } else if (cb.toString() == HTML_SUCCESS) { + qDebug() << "Automatic authorization completed successfully."; + return AlexaAuth::RegisterDeviceSuccess; + } else { + qDebug() << "Something went wrong in " << Q_FUNC_INFO << " " << cb.toString(); + return AlexaAuth::RegisterDeviceError; + } } -#endif diff --git a/plugins/alexaauth/alexaauth.h b/plugins/alexaauth/alexaauth.h index be81e79..b61efac 100644 --- a/plugins/alexaauth/alexaauth.h +++ b/plugins/alexaauth/alexaauth.h @@ -35,9 +35,8 @@ #include #include #include -#ifdef ALEXA_QT_WEBENGINE -#include -#include +#include + // ## Step 1, login to amazon.developer.com #define HTML_TITLE_FIRST "Amazon Sign-In" @@ -47,9 +46,12 @@ #define TAG_EMAIL_ID "ap_email" #define TAG_PASSWORD_ID "ap_password" #define TAG_SIGN_IN_SUBMIT_ID "signInSubmit" +#define TAG_CAPTCHA_IMAGE_ID "auth-captcha-image" +#define TAG_CAPTCHA_GUESS_ID "auth-captcha-guess" // ## Step 2, give authorization code #define HTML_TITLE_SECOND "Amazon Two-Step Verification" +#define HTML_TITLE_ERROR_CAPTCHA "There was a problem" #define TAG_REGISTRATION_FIELD_ID "cbl-registration-field" #define TAG_CONTINUE_BUTTON_ID "cbl-continue-button" @@ -57,25 +59,14 @@ #define HTML_REGISTER_DEVICE "Register Your Device" #define HTML_SUCCESS "Success!" #define TAG_SUCCESS_TITLE_ID "cbl-page-title" -#endif class AlexaAuth : public QObject { Q_OBJECT - Q_PROPERTY(bool isAuthorizing READ isAuthorizing NOTIFY isAuthorizingChanged) - Q_PROPERTY(QString authCode READ authCode WRITE setAuthCode NOTIFY authCodeChanged) - Q_PROPERTY(QUrl authUrl READ authUrl WRITE setAuthUrl NOTIFY authUrlChanged) - Q_PROPERTY(ErrorState error READ error NOTIFY errorChanged) - Q_PROPERTY(QString httpUserAgent READ httpUserAgent WRITE setHttpUserAgent NOTIFY httpUserAgentChanged) - Q_PROPERTY(bool authorizationSucceed READ authorizationSucceed NOTIFY authorizationSucceedChanged) - Q_PROPERTY(QString email READ email WRITE setEmail NOTIFY emailChanged) - Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged) - public: - enum ErrorState { - None, + NoError, WebEngineNotAvailable, ConfigFileFailure, HtmlItemNotFound, @@ -84,64 +75,61 @@ public: }; Q_ENUM(ErrorState) + enum AuthStage { + AuthSignIn, + AuthRegisterDevice, + AuthError + }; + Q_ENUM(AuthStage) + + enum SignInResult { + SignInInputEmail, + SignInCaptcha, + SignInError + }; + Q_ENUM(SignInResult) + + enum RegisterDeviceResult { + RegisterDevice, + RegisterDeviceSuccess, + RegisterDeviceError + }; + Q_ENUM(RegisterDeviceResult) + + enum JSAuthString { + SignIn, + CaptchaSrc, + GetCaptchaInput, + SetCaptcha, + GetEmailInput, + SetEmail, + GetPasswordInput, + SetPassword, + GetClickSignIn, + RegisterDeviceTitle, + GetInputCode, + SetInputCode, + GetContinue, + ClickElement + }; + Q_ENUM(JSAuthString) + explicit AlexaAuth(QObject *parent = nullptr); - Q_INVOKABLE void authorize(); - - bool isAuthorizing() const { return m_isAuthorizing; } - QString authCode() const { return m_authCode; } - QUrl authUrl() const { return m_authUrl; } - ErrorState error() const { return m_error; } - QString httpUserAgent() const { return m_httpUserAgent; } - bool authorizationSucceed() const { return m_authorizationSucceed; } - QString email() const { return m_email; } - QString password() const { return m_password; } - - void setIsAuthorizing(bool isAuthorizing); - void setAuthCode(QString authCode); - void setAuthUrl(QUrl authUrl); - void setError(AlexaAuth::ErrorState error); - void setHttpUserAgent(QString httpUserAgent); - void setAuthorizationSucceed(bool authorizationSucceed); - void setEmail(QString email); - void setPassword(QString password); - - -signals: - void isAuthorizingChanged(bool isAuthorizing); - void authCodeChanged(QString authCode); - void authUrlChanged(QUrl authUrl); - void errorChanged(AlexaAuth::ErrorState error); - void httpUserAgentChanged(QString httpUserAgent); - void authorizationSucceedChanged(bool authorizationSucceed); - void emailChanged(QString email); - void passwordChanged(QString password); - -public slots: - - -private: -#ifdef ALEXA_QT_WEBENGINE - bool parseJson(); - void authPageLoaded(bool ok); - void signinToAmazon(); - void inputEMail(); - void inputPassword(); - void clickSignIn(); - void registerDevice(); - void inputCode(); - void clickContinue(); - - QWebEnginePage m_authPage; -#endif - QString m_authCode; - bool m_isAuthorizing = false; - QUrl m_authUrl; - ErrorState m_error = ErrorState::None; - QString m_httpUserAgent; - bool m_authorizationSucceed = false; - QString m_email; - QString m_password; + Q_INVOKABLE bool parseJson() const; + Q_INVOKABLE AlexaAuth::AuthStage getAuthStage(const QString &title); + Q_INVOKABLE QString getJSString(AlexaAuth::JSAuthString id, const QString &value = "") const; + Q_INVOKABLE AlexaAuth::SignInResult signinToAmazonResult(const QVariant &cb); + Q_INVOKABLE AlexaAuth::RegisterDeviceResult registerDeviceResult(const QVariant &cb); }; +static QObject *alexaAuthSingletonProvider(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + + AlexaAuth *singletonObject = new AlexaAuth(); + return singletonObject; +} + #endif // ALEXAAUTH_H diff --git a/plugins/alexaauth/alexaauth.pro b/plugins/alexaauth/alexaauth.pro index fdd2c14..f075d0f 100644 --- a/plugins/alexaauth/alexaauth.pro +++ b/plugins/alexaauth/alexaauth.pro @@ -1,20 +1,6 @@ TEMPLATE = lib TARGET = alexaauth -QT += qml quick - -# Is Qt Application manager compiled with 'enable-widgets' configuration. -# Do not change without recompiling the Qt Application Manger. -QAPPMAN_ENABLES_WIDGETS = 0 - -equals(QAPPMAN_ENABLES_WIDGETS, 1) { - qtHaveModule(webenginewidgets) { - QT += webenginewidgets - DEFINES += ALEXA_QT_WEBENGINE - } - else { - message("Qt module webenginewidgets is not available.") - } -} +QT += qml quick webengine CONFIG += plugin c++14 diff --git a/plugins/alexaauth/alexaauth_plugin.cpp b/plugins/alexaauth/alexaauth_plugin.cpp index 03f31a8..94e8728 100644 --- a/plugins/alexaauth/alexaauth_plugin.cpp +++ b/plugins/alexaauth/alexaauth_plugin.cpp @@ -36,6 +36,6 @@ void AlexaAuthPlugin::registerTypes(const char *uri) { - qmlRegisterType(uri, 1, 0, "AlexaAuth"); + qmlRegisterSingletonType(uri, 1, 0, "AlexaAuth", alexaAuthSingletonProvider); } -- cgit v1.2.3