rcsession.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include <QInputDialog>
  2. // from WccApi
  3. #include "wccapi/wccfnlogin.h"
  4. #include "wccapi/wccgetrequest.h"
  5. #include "wccapi/wccnetworkaccessmanager.h"
  6. #include "wccapi/wccsigninrequest.h"
  7. // from this project
  8. #include "generatoroutputview.h"
  9. #include "rcsession.h"
  10. #include "settings.h"
  11. namespace
  12. {
  13. const char * const SERVER_URL = "https://build.forum.nokia.com";
  14. const char * const SSO_LOGIN_URL = "https://sso.forum.nokia.com/login?";
  15. const char * const SSO_LOGOUT_URL = "https://sso.forum.nokia.com/logout";
  16. }
  17. RcSession::RcSession(GeneratorOutputView & outputView,
  18. QObject * parent = NULL)
  19. : m_outputView(outputView)
  20. , m_networkAccessManager(NULL)
  21. , m_currentReqId(0)
  22. {
  23. setParent(parent);
  24. m_networkAccessManager = new WccApi::WccNetworkAccessManager(this);
  25. loadSettings();
  26. }
  27. RcSession::~RcSession()
  28. {
  29. ;
  30. }
  31. bool RcSession::isSignedOn() const
  32. {
  33. return m_sessionIdCookie.length() > 0;
  34. }
  35. void RcSession::signIn()
  36. {
  37. if (isSignedOn())
  38. {
  39. emit signInCompleted(true);
  40. return;
  41. }
  42. loadSettings();
  43. if (m_proxyConfig == Settings::PCK_UndefinedProxyConfig)
  44. {
  45. m_outputView.printOutput("Unconfigured proxy settings");
  46. emit signInCompleted(false);
  47. return;
  48. }
  49. int
  50. id = nextReqId();
  51. QString
  52. geturl = QString(SSO_LOGIN_URL)
  53. + "service="
  54. + QUrl::toPercentEncoding( QString(SERVER_URL)
  55. + "/api/sign-in?cas=fn");
  56. m_networkAccessManager->ssoFormForumNokia(id, geturl);
  57. WccApi::WccSsoFormForumNokia
  58. *req = qobject_cast<WccApi::WccSsoFormForumNokia*>(m_networkAccessManager->requestById(id));
  59. if (!req)
  60. {
  61. m_outputView.printOutput("Could not get response to Forum Nokia SSO request");
  62. emit signInCompleted(false);
  63. return;
  64. }
  65. connect(req,
  66. SIGNAL(finished()),
  67. this,
  68. SLOT(ssoFormReplySlot()));
  69. }
  70. void RcSession::signOut()
  71. {
  72. int
  73. id = nextReqId();
  74. m_networkAccessManager->get(id,
  75. SSO_LOGOUT_URL);
  76. m_sessionIdCookie = QByteArray();
  77. m_currentReqId = 0;
  78. }
  79. void RcSession::refreshRcProperties()
  80. {
  81. int
  82. id = nextReqId();
  83. m_networkAccessManager->get(id,
  84. makeRequestUrl("properties")); //?cl=nqsc&v=1"));
  85. WccApi::WccGetRequest
  86. *req = qobject_cast<WccApi::WccGetRequest*>(m_networkAccessManager->requestById(id));
  87. if (!req)
  88. {
  89. m_outputView.printOutput("Could not get remote build target properties");
  90. emit refreshRcPropertiesCompleted(false,
  91. false);
  92. return;
  93. }
  94. connect(req,
  95. SIGNAL(finished()),
  96. this,
  97. SLOT(propertiesReplySlot()));
  98. }
  99. QString RcSession::makeRequestUrl(const QString & operation)
  100. {
  101. return QString(SERVER_URL) + "/api/rpc/" + operation;
  102. }
  103. WccApi::WccNetworkAccessManager * RcSession::networkAccessManager()
  104. {
  105. return m_networkAccessManager;
  106. }
  107. int RcSession::nextReqId()
  108. {
  109. return ++m_currentReqId;
  110. }
  111. void RcSession::ssoFormReplySlot()
  112. {
  113. QObject *oreq = sender();
  114. WccApi::WccSsoFormForumNokia
  115. *req = qobject_cast<WccApi::WccSsoFormForumNokia*>(oreq);
  116. if (!req)
  117. {
  118. m_outputView.printOutput("Could not obtain Nokia Form SSO response");
  119. emit signInCompleted(false);
  120. return;
  121. }
  122. WccApi::WccResponse
  123. *resp = req->response();
  124. m_outputView.printOutput("SSO login form:");
  125. m_outputView.printOutput(resp->message());
  126. QString
  127. specid = req->m_forumNokiaSSOSpecialId;
  128. QString
  129. forumNokiaPassword = QInputDialog::getText(qobject_cast<QWidget*>(this->parent()),
  130. "Login to Remote Compiler (Forum Nokia)",
  131. "Forum Nokia password",
  132. QLineEdit::Password);
  133. if (forumNokiaPassword.length() == 0)
  134. {
  135. m_outputView.printOutput("No password given");
  136. emit signInCompleted(false);
  137. return;
  138. }
  139. QString
  140. srvurl = QString::fromAscii(QUrl::toPercentEncoding( QString(SERVER_URL)
  141. + "/api/sign-in?cas=fn"));
  142. QString reqargs = QString("service=")
  143. + srvurl
  144. + "&lt="
  145. + specid
  146. + "&_currentStateId=&_eventId=submit&username="
  147. + m_forumNokiaUserName
  148. + "&password="
  149. + forumNokiaPassword;
  150. int id = nextReqId();
  151. QString
  152. url = QString(SSO_LOGIN_URL) + reqargs;
  153. m_networkAccessManager->ssoAuthForumNokia(id,
  154. url,
  155. QString());
  156. WccApi::WccSsoAuthForumNokia
  157. *nreq = qobject_cast<WccApi::WccSsoAuthForumNokia*>(m_networkAccessManager->requestById(id));
  158. if (!nreq)
  159. {
  160. m_outputView.printOutput("Could not obtain Nokia Form SSO auth response");
  161. emit signInCompleted(false);
  162. return;
  163. }
  164. connect(nreq,
  165. SIGNAL(finished()),
  166. this,
  167. SLOT(ssoAuthReplySlot()));
  168. }
  169. void RcSession::ssoAuthReplySlot()
  170. {
  171. QObject
  172. *oreq = sender();
  173. WccApi::WccSsoAuthForumNokia
  174. *req = qobject_cast<WccApi::WccSsoAuthForumNokia*>(oreq);
  175. if (!req)
  176. {
  177. m_outputView.printOutput("Could not obtain Nokia Form SSO auth response");
  178. emit signInCompleted(false);
  179. return;
  180. }
  181. WccApi::WccResponse
  182. *resp = req->response();
  183. m_outputView.printOutput("SSO auth response:");
  184. m_outputView.printOutput(resp->message());
  185. QString
  186. loc = req->m_location;
  187. int
  188. id = nextReqId();
  189. // no session id yet
  190. m_networkAccessManager->signIn(id,
  191. loc,
  192. "");
  193. WccApi::WccSignInRequest
  194. *nreq = qobject_cast<WccApi::WccSignInRequest*>(m_networkAccessManager->requestById(id));
  195. if (!nreq)
  196. {
  197. m_outputView.printOutput("Could not obtain Nokia Form signin request");
  198. emit signInCompleted(false);
  199. return;
  200. }
  201. connect(nreq,
  202. SIGNAL(finished()),
  203. this,
  204. SLOT(ssoTicketSendingFinishedSlot()));
  205. }
  206. void RcSession::ssoTicketSendingFinishedSlot()
  207. {
  208. QObject
  209. *oreq = sender();
  210. WccApi::WccSignInRequest
  211. *req = qobject_cast<WccApi::WccSignInRequest*>(oreq);
  212. if (!req)
  213. {
  214. m_outputView.printOutput("Could not obtain Nokia Form signin request");
  215. emit signInCompleted(false);
  216. return;
  217. }
  218. WccApi::WccResponse
  219. *resp = req->response();
  220. m_outputView.printOutput("SSO Response to sign in request:");
  221. m_outputView.printOutput(resp->message());
  222. QByteArray
  223. sessionIdCookie = req->m_cookie;
  224. setSessionIdCookie(sessionIdCookie);
  225. emit signInCompleted(sessionIdCookie.length() > 0);
  226. }
  227. void RcSession::propertiesReplySlot()
  228. {
  229. QObject
  230. *oreq = sender();
  231. WccApi::WccGetRequest
  232. *req = qobject_cast<WccApi::WccGetRequest*>(oreq);
  233. if (!req)
  234. {
  235. m_outputView.printOutput("Could not obtain remote build target properties request");
  236. emit refreshRcPropertiesCompleted(false,
  237. false);
  238. return;
  239. }
  240. WccApi::WccResponse
  241. *resp = req->response();
  242. QString
  243. newRcProperties = resp->message(),
  244. oldRcProperties = Settings::get(Settings::RcProperties).toString();
  245. bool
  246. hasRcPropertiesChanged = newRcProperties != oldRcProperties;
  247. if (hasRcPropertiesChanged)
  248. {
  249. Settings::set(Settings::RcProperties,
  250. newRcProperties);
  251. }
  252. emit refreshRcPropertiesCompleted(newRcProperties.length() > 0,
  253. hasRcPropertiesChanged);
  254. }
  255. void RcSession::loadSettings()
  256. {
  257. m_forumNokiaUserName = Settings::get(Settings::ForumNokiaUserName).toString();
  258. m_proxyConfig = static_cast<Settings::ProxyConfigKind>(Settings::get(Settings::ProxyConfig).toInt());
  259. m_proxyUrl = Settings::get(Settings::ProxyUrl).toString();
  260. m_proxyPort = Settings::get(Settings::ProxyPort).toInt();
  261. m_proxyUserName = Settings::get(Settings::ProxyUserName).toString();
  262. m_proxyPassword = Settings::get(Settings::ProxyPassword).toString();
  263. switch (m_proxyConfig)
  264. {
  265. case Settings::PCK_NoProxy:
  266. m_networkAccessManager->setNoProxy();
  267. break;
  268. case Settings::PCK_SystemProxy:
  269. m_networkAccessManager->setSystemProxy();
  270. break;
  271. case Settings::PCK_ManualProxy:
  272. m_networkAccessManager->setProxy(m_proxyUrl,
  273. m_proxyPort,
  274. m_proxyUserName,
  275. m_proxyPassword);
  276. break;
  277. default:
  278. ;
  279. break;
  280. }
  281. }
  282. void RcSession::setSessionIdCookie(const QByteArray & sessionIdCookie)
  283. {
  284. m_sessionIdCookie = sessionIdCookie;
  285. }