Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / google_apis / gaia / oauth2_mint_token_flow.h
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.
4
5 #ifndef GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_
6 #define GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_
7
8 #include <string>
9 #include <vector>
10
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"
15 #include "url/gurl.h"
16
17 class GoogleServiceAuthError;
18 class OAuth2MintTokenFlowTest;
19
20 namespace base {
21 class DictionaryValue;
22 }
23
24 namespace content {
25 class URLFetcher;
26 }
27
28 namespace net {
29 class URLRequestContextGetter;
30 }
31
32 // IssueAdvice: messages to show to the user to get a user's approval.
33 // The structure is as follows:
34 // * Description 1
35 //   - Detail 1.1
36 //   - Details 1.2
37 // * Description 2
38 //   - Detail 2.1
39 //   - Detail 2.2
40 //   - Detail 2.3
41 // * Description 3
42 //   - Detail 3.1
43 struct IssueAdviceInfoEntry {
44  public:
45   IssueAdviceInfoEntry();
46   ~IssueAdviceInfoEntry();
47
48   base::string16 description;
49   std::vector<base::string16> details;
50
51   bool operator==(const IssueAdviceInfoEntry& rhs) const;
52 };
53
54 typedef std::vector<IssueAdviceInfoEntry> IssueAdviceInfo;
55
56 // This class implements the OAuth2 flow to Google to mint an OAuth2 access
57 // token for the given client and the given set of scopes from the OAuthLogin
58 // scoped "master" OAuth2 token for the user logged in to Chrome.
59 class OAuth2MintTokenFlow : public OAuth2ApiCallFlow {
60  public:
61   // There are four different modes when minting a token to grant
62   // access to third-party app for a user.
63   enum Mode {
64     // Get the messages to display to the user without minting a token.
65     MODE_ISSUE_ADVICE,
66     // Record a grant but do not get a token back.
67     MODE_RECORD_GRANT,
68     // Mint a token for an existing grant.
69     MODE_MINT_TOKEN_NO_FORCE,
70     // Mint a token forcefully even if there is no existing grant.
71     MODE_MINT_TOKEN_FORCE,
72   };
73
74   // Parameters needed to mint a token.
75   struct Parameters {
76    public:
77     Parameters();
78     Parameters(const std::string& eid,
79                const std::string& cid,
80                const std::vector<std::string>& scopes_arg,
81                const std::string& device_id,
82                Mode mode_arg);
83     ~Parameters();
84
85     std::string extension_id;
86     std::string client_id;
87     std::vector<std::string> scopes;
88     std::string device_id;
89     Mode mode;
90   };
91
92   class Delegate {
93    public:
94     virtual void OnMintTokenSuccess(const std::string& access_token,
95                                     int time_to_live) {}
96     virtual void OnIssueAdviceSuccess(const IssueAdviceInfo& issue_advice)  {}
97     virtual void OnMintTokenFailure(const GoogleServiceAuthError& error) {}
98
99    protected:
100     virtual ~Delegate() {}
101   };
102
103   OAuth2MintTokenFlow(Delegate* delegate, const Parameters& parameters);
104   ~OAuth2MintTokenFlow() override;
105
106  protected:
107   // Implementation of template methods in OAuth2ApiCallFlow.
108   GURL CreateApiCallUrl() override;
109   std::string CreateApiCallBody() override;
110
111   void ProcessApiCallSuccess(const net::URLFetcher* source) override;
112   void ProcessApiCallFailure(const net::URLFetcher* source) override;
113
114  private:
115   friend class OAuth2MintTokenFlowTest;
116   FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, CreateApiCallBody);
117   FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ParseIssueAdviceResponse);
118   FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ParseMintTokenResponse);
119   FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ProcessApiCallSuccess);
120   FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ProcessApiCallFailure);
121
122   void ReportSuccess(const std::string& access_token, int time_to_live);
123   void ReportIssueAdviceSuccess(const IssueAdviceInfo& issue_advice);
124   void ReportFailure(const GoogleServiceAuthError& error);
125
126   static bool ParseIssueAdviceResponse(
127       const base::DictionaryValue* dict, IssueAdviceInfo* issue_advice);
128   static bool ParseMintTokenResponse(
129       const base::DictionaryValue* dict, std::string* access_token,
130       int* time_to_live);
131
132   Delegate* delegate_;
133   Parameters parameters_;
134   base::WeakPtrFactory<OAuth2MintTokenFlow> weak_factory_;
135
136   DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFlow);
137 };
138
139 #endif  // GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_