Skip to content

Commit

Permalink
Merge pull request #6 from rasendubi/oauthscope
Browse files Browse the repository at this point in the history
Add scope flag in OAuthConnection
  • Loading branch information
alekseysidorov committed Nov 25, 2012
2 parents 30a5512 + 55a3a36 commit 1ba9cca
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 20 deletions.
58 changes: 38 additions & 20 deletions src/oauth/oauthconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <json.h>
#include <vreen/private/connection_p.h>
#include <vreen/utils.h>

#include <QDebug>
#include <QWebElement>
Expand All @@ -44,6 +45,9 @@ namespace Vreen {

const static QUrl authUrl("http://api.vk.com/oauth/authorize");
const static QUrl apiUrl("https://api.vk.com/method/");
const static char *scopeNames[] = { "notify", "friends", "photos", "audio",
"video", "docs", "notes", "pages", "status", "offers", "questions", "wall",
"groups", "messages", "notifications", "stats", "ads", "offline" };

class OAuthConnection;
class OAuthConnectionPrivate : public ConnectionPrivate
Expand All @@ -53,24 +57,24 @@ class OAuthConnectionPrivate : public ConnectionPrivate
OAuthConnectionPrivate(OAuthConnection *q, int appId) : ConnectionPrivate(q),
connectionState(Client::StateOffline),
clientId(appId),
scope(QStringList() << QLatin1String("notify")
<< QLatin1String("friends")
<< QLatin1String("photos")
<< QLatin1String("audio")
<< QLatin1String("video")
<< QLatin1String("docs")
<< QLatin1String("notes")
<< QLatin1String("pages")
<< QLatin1String("status")
<< QLatin1String("offers")
<< QLatin1String("questions")
<< QLatin1String("wall")
<< QLatin1String("groups")
<< QLatin1String("messages")
<< QLatin1String("notifications")
<< QLatin1String("stats")
<< QLatin1String("ads")
<< QLatin1String("offline")),
scope(OAuthConnection::notify|
OAuthConnection::friends|
OAuthConnection::photos|
OAuthConnection::audio|
OAuthConnection::video|
OAuthConnection::docs|
OAuthConnection::notes|
OAuthConnection::pages|
OAuthConnection::status|
OAuthConnection::offers|
OAuthConnection::questions|
OAuthConnection::wall|
OAuthConnection::groups|
OAuthConnection::messages|
OAuthConnection::notifications|
OAuthConnection::stats|
OAuthConnection::ads|
OAuthConnection::offline),
redirectUri(QLatin1String("http://oauth.vk.com/blank.html")),
displayType(OAuthConnection::Touch),
responseType(QLatin1String("token")),
Expand All @@ -84,7 +88,7 @@ class OAuthConnectionPrivate : public ConnectionPrivate

//OAuth settings
int clientId;
QStringList scope; //settings
OAuthConnection::Scopes scope; //settings
QString redirectUri;
OAuthConnection::DisplayType displayType;
QString responseType;
Expand Down Expand Up @@ -238,6 +242,20 @@ void OAuthConnection::setDisplayType(OAuthConnection::DisplayType displayType)
}
}

OAuthConnection::Scopes OAuthConnection::scopes() const
{
return d_func()->scope;
}

void OAuthConnection::setScopes(OAuthConnection::Scopes scopes)
{
Q_D(OAuthConnection);
if( d->scope != scopes ) {
d->scope = scopes;
emit scopesChanged(scopes);
}
}

void OAuthConnectionPrivate::requestToken()
{
Q_Q(OAuthConnection);
Expand All @@ -248,7 +266,7 @@ void OAuthConnectionPrivate::requestToken()
}
QUrl url = authUrl;
url.addQueryItem(QLatin1String("client_id"), QByteArray::number(clientId));
url.addQueryItem(QLatin1String("scope"), scope.join(","));
url.addQueryItem(QLatin1String("scope"), flagsToStrList(scope, scopeNames).join(","));
url.addQueryItem(QLatin1String("redirect_uri"), redirectUri);
const char *type[] = {
"page",
Expand Down
29 changes: 29 additions & 0 deletions src/oauth/oauthconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,39 @@ class OAuthConnection : public Connection
Q_OBJECT
Q_DECLARE_PRIVATE(OAuthConnection)
Q_ENUMS(DisplayType)
Q_FLAGS(Scopes)
Q_PROPERTY(int clientId READ clientId WRITE setClientId NOTIFY clientIdChanged)
Q_PROPERTY(DisplayType displayType READ displayType WRITE setDisplayType)
Q_PROPERTY(Scopes scopes READ scopes WRITE setScopes NOTIFY scopesChanged)
public:
enum DisplayType {
Page,
Popup,
Touch,
Wap
};
enum Scope {
notify = 0x1,
friends = 0x2,
photos = 0x4,
audio = 0x8,
video = 0x10,
docs = 0x20,
notes = 0x40,
pages = 0x80,
status = 0x100,
offers = 0x200,
questions = 0x400,
wall = 0x800,
groups = 0x1000,
messages = 0x2000,
notifications = 0x4000,
stats = 0x8000,
ads = 0x10000,
offline = 0x20000
};
Q_DECLARE_FLAGS(Scopes, Scope)

explicit OAuthConnection(int clientId, QObject *parent = 0);
explicit OAuthConnection(QObject *parent = 0);
virtual ~OAuthConnection();
Expand All @@ -67,17 +91,22 @@ class OAuthConnection : public Connection
void setClientId(int clientId);
DisplayType displayType() const;
void setDisplayType(DisplayType displayType);
Scopes scopes() const;
void setScopes(Scopes scopes);
signals:
void authConfirmRequested(QWebPage *page);
void accessTokenChanged(const QByteArray &token, time_t expiresIn);
void clientIdChanged(int clientId);
void scopesChanged(Vreen::OAuthConnection::Scopes scopes);
protected:
QNetworkRequest makeRequest(const QString &method, const QVariantMap &args = QVariantMap());
void decorateRequest(QNetworkRequest &);
private:
Q_PRIVATE_SLOT(d_func(), void _q_loadFinished(bool))
};

Q_DECLARE_OPERATORS_FOR_FLAGS(OAuthConnection::Scopes)

} // namespace Vreen

#endif // VK_OAUTHCONNECTION_H
Expand Down

0 comments on commit 1ba9cca

Please sign in to comment.