Commit 16aad40a authored by Paweł Wegner's avatar Paweł Wegner

ExchangeCodeRequest: return both access and refresh token.

parent 7905c1b6
......@@ -433,15 +433,14 @@ ICloudProvider::Hints MegaNz::hints() const {
ICloudProvider::ExchangeCodeRequest::Pointer MegaNz::exchangeCodeAsync(
const std::string& code, ExchangeCodeCallback callback) {
auto r =
std::make_shared<Request<EitherError<std::string>>>(shared_from_this());
r->set([=](Request<EitherError<std::string>>::Ptr r) {
auto r = std::make_shared<Request<EitherError<Token>>>(shared_from_this());
r->set([=](Request<EitherError<Token>>::Ptr r) {
auto token = authorizationCodeToToken(code);
EitherError<std::string> ret =
EitherError<Token> ret =
token->token_.empty()
? EitherError<std::string>(
? EitherError<Token>(
Error{IHttpRequest::Failure, "invalid authorization code"})
: EitherError<std::string>(token->token_);
: EitherError<Token>({token->token_, ""});
callback(ret);
r->done(ret);
});
......
......@@ -42,7 +42,7 @@ class ICloudProvider {
using Pointer = std::shared_ptr<ICloudProvider>;
using Hints = std::unordered_map<std::string, std::string>;
using ExchangeCodeRequest = IRequest<EitherError<std::string>>;
using ExchangeCodeRequest = IRequest<EitherError<Token>>;
using ListDirectoryRequest =
IRequest<EitherError<std::vector<IItem::Pointer>>>;
using GetItemRequest = IRequest<EitherError<IItem>>;
......@@ -214,13 +214,13 @@ class ICloudProvider {
/**
* Exchanges authorization code which was sent to redirect_uri() by cloud
* provider for a token (refresh_token).
* provider for a token.
*
* @return object representing the pending request
*/
virtual ExchangeCodeRequest::Pointer exchangeCodeAsync(
const std::string& code,
ExchangeCodeCallback = [](EitherError<std::string>) {}) = 0;
ExchangeCodeCallback = [](EitherError<Token>) {}) = 0;
/**
* Lists directory.
......
......@@ -34,11 +34,17 @@ namespace cloudstorage {
struct Error;
template <class Left, class Right> class Either;
template <class Left, class Right>
class Either;
template <class T>
using EitherError = Either<Error, T>;
struct Token {
std::string token_;
std::string access_token_;
};
/**
* Class representing pending request. When there is no reference to the
* request, it's immediately cancelled.
......@@ -194,7 +200,7 @@ class Either<Left, void> {
std::shared_ptr<Left> left_;
};
using ExchangeCodeCallback = std::function<void(EitherError<std::string>)>;
using ExchangeCodeCallback = std::function<void(EitherError<Token>)>;
using GetItemCallback = std::function<void(EitherError<IItem>)>;
using GetItemDataCallback = std::function<void(EitherError<IItem>)>;
using DeleteItemCallback = std::function<void(EitherError<void>)>;
......
......@@ -31,11 +31,12 @@ ExchangeCodeRequest::ExchangeCodeRequest(std::shared_ptr<CloudProvider> p,
const std::string& authorization_code,
ExchangeCodeCallback callback)
: Request(p) {
set([=](Request<EitherError<std::string>>::Ptr r) {
set([=](Request<EitherError<Token>>::Ptr r) {
std::stringstream stream;
if (!provider()->auth()->exchangeAuthorizationCodeRequest(stream)) {
callback(authorization_code);
return r->done(authorization_code);
Token token{authorization_code, ""};
callback(token);
return r->done(token);
}
auto input = std::make_shared<std::stringstream>(),
output = std::make_shared<std::stringstream>(),
......@@ -53,11 +54,11 @@ ExchangeCodeRequest::ExchangeCodeRequest(std::shared_ptr<CloudProvider> p,
if (IHttpRequest::isSuccess(code)) {
try {
auto lock = provider()->auth_lock();
auto token = provider()
->auth()
->exchangeAuthorizationCodeResponse(*output)
->refresh_token_;
auto auth_token =
provider()->auth()->exchangeAuthorizationCodeResponse(
*output);
lock.unlock();
Token token{auth_token->refresh_token_, auth_token->token_};
callback(token);
r->done(token);
} catch (std::exception e) {
......
......@@ -28,7 +28,7 @@
namespace cloudstorage {
class ExchangeCodeRequest : public Request<EitherError<std::string>> {
class ExchangeCodeRequest : public Request<EitherError<Token>> {
public:
ExchangeCodeRequest(std::shared_ptr<CloudProvider>, const std::string& code,
ExchangeCodeCallback);
......
......@@ -248,6 +248,7 @@ void Request<T>::subrequest(std::shared_ptr<IGenericRequest> request) {
}
}
template class Request<EitherError<Token>>;
template class Request<EitherError<std::vector<char>>>;
template class Request<EitherError<std::string>>;
template class Request<EitherError<IItem>>;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment