1#include "AuthenticationManager.h"
2#include <QDesktopServices>
3#include <QOAuthHttpServerReplyHandler>
13void cavoke::auth::AuthenticationManager::init() {
14 oauth2.setAuthorizationUrl(QUrl(authorizationUrl));
15 oauth2.setAccessTokenUrl(QUrl(accessTokenUrl));
16 oauth2.setClientIdentifier(clientId);
17 oauth2.setScope(scope);
19 connect(&oauth2, &QOAuth2AuthorizationCodeFlow::statusChanged,
20 [=](QAbstractOAuth::Status status) {
21 if (status == QAbstractOAuth::Status::Granted) {
22 qDebug() <<
"Now authenticated!!";
23 writeSecurePassword(refresh_token_profile,
24 oauth2.refreshToken());
25 if (oauth2.token().isEmpty()) {
26 qWarning() <<
"Authentication completed successfully, "
27 "but token is empty!! Forcing a relogin";
31 }
else if (status == QAbstractOAuth::Status::NotAuthenticated) {
32 qWarning() <<
"Unauthenticated";
35 oauth2.setModifyParametersFunction(
36 [&](QAbstractOAuth::Stage stage,
auto *parameters) {
37 if (stage == QAbstractOAuth::Stage::RequestingAuthorization)
38 parameters->insert(
"audience", audience);
40 connect(&oauth2, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
this,
41 [&](
const QUrl &url) {
43 QUrl route{logoutUrl};
44 route.setQuery(QUrlQuery{
45 {
"returnTo", QUrl::toPercentEncoding(url.toEncoded())}});
46 QDesktopServices::openUrl(route);
48 readSecurePassword(refresh_token_profile,
49 [&](
const QString &refresh_token) {
50 qDebug() <<
"Loaded refresh token from Keychain!";
51 oauth2.setRefreshToken(refresh_token);
52 oauth2.refreshAccessToken();
55bool cavoke::auth::AuthenticationManager::checkAuthStatus() {
56 return !oauth2.token().isEmpty();
58void cavoke::auth::AuthenticationManager::writeSecurePassword(
59 const QString &profile,
60 const QString &pass) {
62 settings.setValue(profile, pass);
78void cavoke::auth::AuthenticationManager::readSecurePassword(
79 const QString &profile,
82 callback(settings.value(profile).toString());
98void cavoke::auth::AuthenticationManager::deleteSecurePassword(
99 const QString &profile) {
101 settings.setValue(profile,
"");
117void cavoke::auth::AuthenticationManager::relogin() {
118 deleteSecurePassword(refresh_token_profile);
119 oauth2.setRefreshToken(
"");
126const QString cavoke::auth::AuthenticationManager::authorizationUrl =
127 "https://cavoke.eu.auth0.com/authorize";
128const QString cavoke::auth::AuthenticationManager::accessTokenUrl =
129 "https://cavoke.eu.auth0.com/oauth/token";
130const QString cavoke::auth::AuthenticationManager::logoutUrl =
131 "https://cavoke.eu.auth0.com/v2/logout";
132const QString cavoke::auth::AuthenticationManager::clientId =
133 "yxkEiSikGF6JSaFwIikeLQlUNAUUR0ak";
134const QString cavoke::auth::AuthenticationManager::scope =
135 "identity sessions profile users offline_access";
136const QString cavoke::auth::AuthenticationManager::audience =
137 "https://develop.api.cavoke.wlko.me";
141const QString cavoke::auth::AuthenticationManager::refresh_token_profile =
142 "cavoke_profiles_refresh";