forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake_oauth2_token_service_delegate.cc
110 lines (94 loc) · 3.69 KB
/
fake_oauth2_token_service_delegate.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "google_apis/gaia/fake_oauth2_token_service_delegate.h"
#include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
FakeOAuth2TokenServiceDelegate::AccountInfo::AccountInfo(
const std::string& refresh_token)
: refresh_token(refresh_token),
error(GoogleServiceAuthError::NONE) {}
FakeOAuth2TokenServiceDelegate::FakeOAuth2TokenServiceDelegate(
net::URLRequestContextGetter* request_context)
: request_context_(request_context) {
}
FakeOAuth2TokenServiceDelegate::~FakeOAuth2TokenServiceDelegate() {
}
OAuth2AccessTokenFetcher*
FakeOAuth2TokenServiceDelegate::CreateAccessTokenFetcher(
const std::string& account_id,
net::URLRequestContextGetter* getter,
OAuth2AccessTokenConsumer* consumer) {
AccountInfoMap::const_iterator it = refresh_tokens_.find(account_id);
DCHECK(it != refresh_tokens_.end());
return new OAuth2AccessTokenFetcherImpl(consumer, getter,
it->second->refresh_token);
}
bool FakeOAuth2TokenServiceDelegate::RefreshTokenIsAvailable(
const std::string& account_id) const {
return !GetRefreshToken(account_id).empty();
}
bool FakeOAuth2TokenServiceDelegate::RefreshTokenHasError(
const std::string& account_id) const {
auto it = refresh_tokens_.find(account_id);
// TODO(rogerta): should we distinguish between transient and persistent?
return it == refresh_tokens_.end() ? false : IsError(it->second->error);
}
std::string FakeOAuth2TokenServiceDelegate::GetRefreshToken(
const std::string& account_id) const {
AccountInfoMap::const_iterator it = refresh_tokens_.find(account_id);
if (it != refresh_tokens_.end())
return it->second->refresh_token;
return std::string();
}
std::vector<std::string> FakeOAuth2TokenServiceDelegate::GetAccounts() {
std::vector<std::string> account_ids;
for (AccountInfoMap::const_iterator iter = refresh_tokens_.begin();
iter != refresh_tokens_.end(); ++iter) {
account_ids.push_back(iter->first);
}
return account_ids;
}
void FakeOAuth2TokenServiceDelegate::RevokeAllCredentials() {
std::vector<std::string> account_ids = GetAccounts();
for (std::vector<std::string>::const_iterator it = account_ids.begin();
it != account_ids.end(); it++) {
RevokeCredentials(*it);
}
}
void FakeOAuth2TokenServiceDelegate::LoadCredentials(
const std::string& primary_account_id) {
FireRefreshTokensLoaded();
}
void FakeOAuth2TokenServiceDelegate::UpdateCredentials(
const std::string& account_id,
const std::string& refresh_token) {
IssueRefreshTokenForUser(account_id, refresh_token);
}
void FakeOAuth2TokenServiceDelegate::IssueRefreshTokenForUser(
const std::string& account_id,
const std::string& token) {
ScopedBatchChange batch(this);
if (token.empty()) {
refresh_tokens_.erase(account_id);
FireRefreshTokenRevoked(account_id);
} else {
refresh_tokens_[account_id].reset(new AccountInfo(token));
FireRefreshTokenAvailable(account_id);
// TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
}
}
void FakeOAuth2TokenServiceDelegate::RevokeCredentials(
const std::string& account_id) {
IssueRefreshTokenForUser(account_id, std::string());
}
net::URLRequestContextGetter*
FakeOAuth2TokenServiceDelegate::GetRequestContext() const {
return request_context_.get();
}
void FakeOAuth2TokenServiceDelegate::SetLastErrorForAccount(
const std::string& account_id,
const GoogleServiceAuthError& error) {
auto it = refresh_tokens_.find(account_id);
DCHECK(it != refresh_tokens_.end());
it->second->error = error;
}