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/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
& rt
,
78 const std::string
& eid
,
79 const std::string
& cid
,
80 const std::vector
<std::string
>& scopes_arg
,
84 std::string login_refresh_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
) {}
94 virtual void OnIssueAdviceSuccess(const IssueAdviceInfo
& issue_advice
) {}
95 virtual void OnMintTokenFailure(const GoogleServiceAuthError
& error
) {}
98 virtual ~Delegate() {}
101 OAuth2MintTokenFlow(net::URLRequestContextGetter
* context
,
103 const Parameters
& parameters
);
104 virtual ~OAuth2MintTokenFlow();
107 // Implementation of template methods in OAuth2ApiCallFlow.
108 virtual GURL
CreateApiCallUrl() OVERRIDE
;
109 virtual std::string
CreateApiCallBody() OVERRIDE
;
111 virtual void ProcessApiCallSuccess(
112 const net::URLFetcher
* source
) OVERRIDE
;
113 virtual void ProcessApiCallFailure(
114 const net::URLFetcher
* source
) OVERRIDE
;
115 virtual void ProcessNewAccessToken(const std::string
& access_token
) OVERRIDE
;
116 virtual void ProcessMintAccessTokenFailure(
117 const GoogleServiceAuthError
& error
) OVERRIDE
;
120 friend class OAuth2MintTokenFlowTest
;
121 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
, CreateApiCallBody
);
122 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
, ParseIssueAdviceResponse
);
123 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
, ParseMintTokenResponse
);
124 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
, ProcessApiCallSuccess
);
125 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
, ProcessApiCallFailure
);
126 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest
,
127 ProcessMintAccessTokenFailure
);
129 void ReportSuccess(const std::string
& access_token
);
130 void ReportIssueAdviceSuccess(const IssueAdviceInfo
& issue_advice
);
131 void ReportFailure(const GoogleServiceAuthError
& error
);
133 static bool ParseIssueAdviceResponse(
134 const base::DictionaryValue
* dict
, IssueAdviceInfo
* issue_advice
);
135 static bool ParseMintTokenResponse(
136 const base::DictionaryValue
* dict
, std::string
* access_token
);
139 Parameters parameters_
;
140 base::WeakPtrFactory
<OAuth2MintTokenFlow
> weak_factory_
;
142 DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFlow
);
145 #endif // GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_