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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// Copyright (c) 2012 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.
#ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_
#define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_
#include <string>
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/extensions/api/identity/identity_signin_flow.h"
#include "chrome/browser/extensions/api/identity/web_auth_flow.h"
#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/browser/extensions/extension_install_prompt.h"
#include "chrome/browser/signin/signin_global_error.h"
#include "google_apis/gaia/oauth2_mint_token_flow.h"
class GetAuthTokenFunctionTest;
class MockGetAuthTokenFunction;
class GoogleServiceAuthError;
class Profile;
namespace extensions {
namespace identity_constants {
extern const char kInvalidClientId[];
extern const char kInvalidScopes[];
extern const char kAuthFailure[];
extern const char kNoGrant[];
extern const char kUserRejected[];
extern const char kUserNotSignedIn[];
extern const char kInvalidRedirect[];
} // namespace identity_constants
class IdentityGetAuthTokenFunction : public AsyncExtensionFunction,
public OAuth2MintTokenFlow::Delegate,
public ExtensionInstallPrompt::Delegate,
public IdentitySigninFlow::Delegate {
public:
DECLARE_EXTENSION_FUNCTION("experimental.identity.getAuthToken",
EXPERIMENTAL_IDENTITY_GETAUTHTOKEN)
IdentityGetAuthTokenFunction();
protected:
virtual ~IdentityGetAuthTokenFunction();
private:
friend class GetAuthTokenFunctionTest;
friend class MockGetAuthTokenFunction;
// ExtensionFunction:
virtual bool RunImpl() OVERRIDE;
// OAuth2MintTokenFlow::Delegate implementation:
virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE;
virtual void OnMintTokenFailure(
const GoogleServiceAuthError& error) OVERRIDE;
virtual void OnIssueAdviceSuccess(
const IssueAdviceInfo& issue_advice) OVERRIDE;
// IdentitySigninFlow::Delegate implementation:
virtual void SigninSuccess(const std::string& token) OVERRIDE;
virtual void SigninFailed() OVERRIDE;
// ExtensionInstallPrompt::Delegate implementation:
virtual void InstallUIProceed() OVERRIDE;
virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
// Starts a MintTokenFlow with the given mode.
void StartFlow(OAuth2MintTokenFlow::Mode mode);
virtual void ShowLoginPopup();
virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice);
// Caller owns the returned instance.
virtual OAuth2MintTokenFlow* CreateMintTokenFlow(
OAuth2MintTokenFlow::Mode mode);
// Checks if there is a master login token to mint tokens for the extension.
virtual bool HasLoginToken() const;
bool should_prompt_for_scopes_;
scoped_ptr<OAuth2MintTokenFlow> mint_token_flow_;
std::string refresh_token_;
bool should_prompt_for_signin_;
// When launched in interactive mode, and if there is no existing grant,
// a permissions prompt will be popped up to the user.
scoped_ptr<ExtensionInstallPrompt> install_ui_;
scoped_ptr<IdentitySigninFlow> signin_flow_;
};
class IdentityLaunchWebAuthFlowFunction : public AsyncExtensionFunction,
public WebAuthFlow::Delegate {
public:
DECLARE_EXTENSION_FUNCTION("experimental.identity.launchWebAuthFlow",
EXPERIMENTAL_IDENTITY_LAUNCHWEBAUTHFLOW)
IdentityLaunchWebAuthFlowFunction();
private:
virtual ~IdentityLaunchWebAuthFlowFunction();
virtual bool RunImpl() OVERRIDE;
// WebAuthFlow::Delegate implementation.
virtual void OnAuthFlowSuccess(const std::string& redirect_url) OVERRIDE;
virtual void OnAuthFlowFailure() OVERRIDE;
scoped_ptr<WebAuthFlow> auth_flow_;
};
class IdentityAPI : public ProfileKeyedAPI,
public SigninGlobalError::AuthStatusProvider,
public content::NotificationObserver {
public:
explicit IdentityAPI(Profile* profile);
virtual ~IdentityAPI();
void Initialize();
void ReportAuthError(const GoogleServiceAuthError& error);
// ProfileKeyedAPI implementation.
virtual void Shutdown() OVERRIDE;
static ProfileKeyedAPIFactory<IdentityAPI>* GetFactoryInstance();
// AuthStatusProvider implementation.
virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE;
// content::NotificationObserver implementation.
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
private:
friend class ProfileKeyedAPIFactory<IdentityAPI>;
// ProfileKeyedAPI implementation.
static const char* service_name() {
return "IdentityAPI";
}
static const bool kServiceIsNULLWhileTesting = true;
Profile* profile_;
SigninManager* signin_manager_;
GoogleServiceAuthError error_;
// Used to listen to notifications from the TokenService.
content::NotificationRegistrar registrar_;
};
template <>
void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies();
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_
|