OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
asargent_no_longer_on_chrome
2012/03/03 00:09:31
2012 strikes again! Sneaky bugger
Munjal (Google)
2012/03/03 00:48:42
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ | |
6 #define CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" | |
13 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" | |
14 #include "chrome/common/net/gaia/oauth2_mint_token_consumer.h" | |
15 #include "chrome/common/net/gaia/oauth2_mint_token_fetcher.h" | |
16 | |
17 class GoogleServiceAuthError; | |
18 class OAuth2MintTokenFlowTest; | |
19 | |
20 namespace net { | |
21 class URLRequestContextGetter; | |
22 } | |
23 | |
24 // This class implements the OAuth2 flow to Google to mint an OAuth2 | |
25 // token for the given client and the given set of scopes from the | |
26 // OAuthLogin scoped "master" OAuth2 token for the user logged in to | |
27 // Chrome. | |
28 class OAuth2MintTokenFlow | |
29 : public OAuth2AccessTokenConsumer, | |
30 public OAuth2MintTokenConsumer { | |
31 public: | |
32 class Delegate { | |
33 public: | |
34 virtual void OnMintTokenSuccess(const std::string& access_token) { } | |
35 virtual void OnMintTokenFailure(const GoogleServiceAuthError& error) { } | |
36 }; | |
37 | |
38 OAuth2MintTokenFlow(net::URLRequestContextGetter* context, | |
39 Delegate* delegate); | |
40 virtual ~OAuth2MintTokenFlow(); | |
41 | |
42 // Start the process to mint a token. | |
43 void Start(const std::string& login_refresh_token, | |
44 const std::string& extension_id, | |
45 const std::string& client_id, | |
46 const std::vector<std::string>& scopes); | |
47 | |
48 // OAuth2AccessTokenConsumer implementation. | |
49 virtual void OnGetTokenSuccess(const std::string& access_token) OVERRIDE; | |
50 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | |
51 // OAuth2MintTokenConsumer implementation. | |
52 virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE; | |
53 virtual void OnMintTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | |
54 | |
55 // Getters for various members. | |
56 const std::string& extension_id() const { return extension_id_; } | |
57 const std::string& client_id() const { return client_id_; } | |
58 | |
59 protected: | |
60 // Helper to create an instnace of access token fetcher. | |
asargent_no_longer_on_chrome
2012/03/03 00:09:31
typo: "instnace"
Munjal (Google)
2012/03/03 00:48:42
Done.
| |
61 // Caller owns the returned instance. | |
62 virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(); | |
63 | |
64 // Helper to create an instnace of mint token fetcher. | |
65 // Caller owns the returned instance. | |
66 virtual OAuth2MintTokenFetcher* CreateMintTokenFetcher(); | |
67 | |
68 private: | |
69 // The steps this class performs are: | |
70 // 1. Create a login scoped access token from login scoped refresh token. | |
71 // 2. Use login scoped access token to call the API to mint an access token | |
72 // for the app. | |
73 enum State { | |
74 INITIAL, | |
75 FETCH_LOGIN_ACCESS_TOKEN_STARTED, | |
76 FETCH_LOGIN_ACCESS_TOKEN_DONE, | |
77 MINT_ACCESS_TOKEN_STARTED, | |
78 MINT_ACCESS_TOKEN_DONE, | |
79 ERROR_STATE | |
80 }; | |
81 | |
82 enum SetupError { | |
83 NONE, | |
84 AUTH_ERROR, | |
85 INTERNAL_ERROR, | |
86 USER_CANCELLED, | |
87 | |
88 // This is used for histograms, and should always be the last value. | |
89 SETUP_ERROR_BOUNDARY | |
90 }; | |
91 | |
92 friend class OAuth2MintTokenFlowTest; | |
93 | |
94 // Creates an instance of URLFetcher that does not send or save cookies. | |
95 // The URLFether's method will be GET if body is empty, POST otherwise. | |
96 // Caller owns the returned instance. | |
97 content::URLFetcher* CreateURLFetcher( | |
98 const GURL& url, const std::string& body, const std::string& auth_token); | |
99 void BeginGetLoginAccessToken(); | |
100 void EndGetLoginAccessToken(const GoogleServiceAuthError* error); | |
101 void BeginMintAccessToken(); | |
102 void EndMintAccessToken(const GoogleServiceAuthError* error); | |
103 | |
104 void ReportSuccess(); | |
105 void ReportFailure(const GoogleServiceAuthError& error); | |
106 | |
107 static std::string GetErrorString(SetupError error); | |
108 | |
109 net::URLRequestContextGetter* context_; | |
110 Delegate* delegate_; | |
111 State state_; | |
112 | |
113 std::string login_refresh_token_; | |
114 std::string extension_id_; | |
115 std::string client_id_; | |
116 std::vector<std::string> scopes_; | |
117 | |
118 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; | |
119 scoped_ptr<OAuth2MintTokenFetcher> oauth2_mint_token_fetcher_; | |
120 std::string login_access_token_; | |
121 std::string app_access_token_; | |
122 | |
123 DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFlow); | |
124 }; | |
125 | |
126 #endif // CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ | |
OLD | NEW |