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/gtest_prod_util.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string16.h"
14 #include "google_apis/gaia/oauth2_api_call_flow.h"
17 class GoogleServiceAuthError
;
18 class OAuth2MintTokenFlowTest
;
21 class DictionaryValue
;
29 class URLRequestContextGetter
;
32 // IssueAdvice: messages to show to the user to get a user's approval.
33 // The structure is as follows:
43 struct IssueAdviceInfoEntry
{
45 IssueAdviceInfoEntry();
46 ~IssueAdviceInfoEntry();
48 base::string16 description
;
49 std::vector
<base::string16
> details
;
51 bool operator==(const IssueAdviceInfoEntry
& rhs
) const;
54 typedef std::vector
<IssueAdviceInfoEntry
> IssueAdviceInfo
;
56 // This class implements the OAuth2 flow to Google to mint an OAuth2
57 // token for the given client and the given set of scopes from the
58 // OAuthLogin scoped "master" OAuth2 token for the user logged in to
60 class OAuth2MintTokenFlow
: public OAuth2ApiCallFlow
{
62 // There are four differnt modes when minting a token to grant
63 // access to third-party app for a user.
65 // Get the messages to display to the user without minting a token.
67 // Record a grant but do not get a token back.
69 // Mint a token for an existing grant.
70 MODE_MINT_TOKEN_NO_FORCE
,
71 // Mint a token forcefully even if there is no existing grant.
72 MODE_MINT_TOKEN_FORCE
,
75 // Parameters needed to mint a token.
79 Parameters(const std::string
& at
,
80 const std::string
& eid
,
81 const std::string
& cid
,
82 const std::vector
<std::string
>& scopes_arg
,
86 std::string access_token
;
87 std::string extension_id
;
88 std::string client_id
;
89 std::vector
<std::string
> scopes
;
95 virtual void OnMintTokenSuccess(const std::string
& access_token
,
97 virtual void OnIssueAdviceSuccess(const IssueAdviceInfo
& issue_advice
) {}
98 virtual void OnMintTokenFailure(const GoogleServiceAuthError
& error
) {}
101 virtual ~Delegate() {}
104 OAuth2MintTokenFlow(net::URLRequestContextGetter
* context
,
106 const Parameters
& parameters
);
107 virtual ~OAuth2MintTokenFlow();
110 // Implementation of template methods in OAuth2ApiCallFlow.
111 virtual GURL
CreateApiCallUrl() OVERRIDE
;
112 virtual std::string
CreateApiCallBody() OVERRIDE
;
114 virtual void ProcessApiCallSuccess(
115 const net::URLFetcher
* source
) OVERRIDE
;
116 virtual void ProcessApiCallFailure(
117 const net::URLFetcher
* source
) OVERRIDE
;
118 virtual void ProcessNewAccessToken(const std::string
& access_token
) OVERRIDE
;
119 virtual void ProcessMintAccessTokenFailure(
120 const GoogleServiceAuthError
& error
) OVERRIDE
;
123 friend class OAuth2MintTokenFlowTest
;
124 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
, CreateApiCallBody
);
125 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
, ParseIssueAdviceResponse
);
126 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
, ParseMintTokenResponse
);
127 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
, ProcessApiCallSuccess
);
128 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
, ProcessApiCallFailure
);
129 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
,
130 ProcessMintAccessTokenFailure
);
132 void ReportSuccess(const std::string
& access_token
, int time_to_live
);
133 void ReportIssueAdviceSuccess(const IssueAdviceInfo
& issue_advice
);
134 void ReportFailure(const GoogleServiceAuthError
& error
);
136 static bool ParseIssueAdviceResponse(
137 const base::DictionaryValue
* dict
, IssueAdviceInfo
* issue_advice
);
138 static bool ParseMintTokenResponse(
139 const base::DictionaryValue
* dict
, std::string
* access_token
,
143 Parameters parameters_
;
144 base::WeakPtrFactory
<OAuth2MintTokenFlow
> weak_factory_
;
146 DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFlow
);
149 #endif // GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_