1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_
6 #define GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_
11 #include "base/memory/weak_ptr.h"
12 #include "base/strings/string16.h"
13 #include "google_apis/gaia/oauth2_api_call_flow.h"
15 class GoogleServiceAuthError;
16 class OAuth2MintTokenFlowTest;
19 class DictionaryValue;
27 class URLRequestContextGetter;
30 // IssueAdvice: messages to show to the user to get a user's approval.
31 // The structure is as follows:
41 struct IssueAdviceInfoEntry {
43 IssueAdviceInfoEntry();
44 ~IssueAdviceInfoEntry();
47 std::vector<string16> details;
49 bool operator==(const IssueAdviceInfoEntry& rhs) const;
52 typedef std::vector<IssueAdviceInfoEntry> IssueAdviceInfo;
54 // This class implements the OAuth2 flow to Google to mint an OAuth2
55 // token for the given client and the given set of scopes from the
56 // OAuthLogin scoped "master" OAuth2 token for the user logged in to
58 class OAuth2MintTokenFlow : public OAuth2ApiCallFlow {
60 // There are four differnt modes when minting a token to grant
61 // access to third-party app for a user.
63 // Get the messages to display to the user without minting a token.
65 // Record a grant but do not get a token back.
67 // Mint a token for an existing grant.
68 MODE_MINT_TOKEN_NO_FORCE,
69 // Mint a token forcefully even if there is no existing grant.
70 MODE_MINT_TOKEN_FORCE,
73 // Parameters needed to mint a token.
77 Parameters(const std::string& at,
78 const std::string& eid,
79 const std::string& cid,
80 const std::vector<std::string>& scopes_arg,
84 std::string access_token;
85 std::string extension_id;
86 std::string client_id;
87 std::vector<std::string> scopes;
93 virtual void OnMintTokenSuccess(const std::string& access_token,
95 virtual void OnIssueAdviceSuccess(const IssueAdviceInfo& issue_advice) {}
96 virtual void OnMintTokenFailure(const GoogleServiceAuthError& error) {}
99 virtual ~Delegate() {}
102 OAuth2MintTokenFlow(net::URLRequestContextGetter* context,
104 const Parameters& parameters);
105 virtual ~OAuth2MintTokenFlow();
108 // Implementation of template methods in OAuth2ApiCallFlow.
109 virtual GURL CreateApiCallUrl() OVERRIDE;
110 virtual std::string CreateApiCallBody() OVERRIDE;
112 virtual void ProcessApiCallSuccess(
113 const net::URLFetcher* source) OVERRIDE;
114 virtual void ProcessApiCallFailure(
115 const net::URLFetcher* source) OVERRIDE;
116 virtual void ProcessNewAccessToken(const std::string& access_token) OVERRIDE;
117 virtual void ProcessMintAccessTokenFailure(
118 const GoogleServiceAuthError& error) OVERRIDE;
121 friend class OAuth2MintTokenFlowTest;
122 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, CreateApiCallBody);
123 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ParseIssueAdviceResponse);
124 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ParseMintTokenResponse);
125 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ProcessApiCallSuccess);
126 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ProcessApiCallFailure);
127 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest,
128 ProcessMintAccessTokenFailure);
130 void ReportSuccess(const std::string& access_token, int time_to_live);
131 void ReportIssueAdviceSuccess(const IssueAdviceInfo& issue_advice);
132 void ReportFailure(const GoogleServiceAuthError& error);
134 static bool ParseIssueAdviceResponse(
135 const base::DictionaryValue* dict, IssueAdviceInfo* issue_advice);
136 static bool ParseMintTokenResponse(
137 const base::DictionaryValue* dict, std::string* access_token,
141 Parameters parameters_;
142 base::WeakPtrFactory<OAuth2MintTokenFlow> weak_factory_;
144 DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFlow);
147 #endif // GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_