Google APIs Client Library for C++
webserver_authorization_getter.h
Go to the documentation of this file.
00001 /*
00002  * \copyright Copyright 2013 Google Inc. All Rights Reserved.
00003  * \license @{
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *
00017  * @}
00018  */
00019 
00020 // Author: ewiseblatt@google.com (Eric Wiseblatt)
00021 
00022 #ifndef APISERVING_CLIENTS_CPP_AUTH_WEBSERVER_AUTHORIZATION_GETTER_H_
00023 #define APISERVING_CLIENTS_CPP_AUTH_WEBSERVER_AUTHORIZATION_GETTER_H_
00024 #include <ostream>  // NOLINT
00025 #include <string>
00026 using std::string;
00027 
00028 #include "googleapis/client/auth/oauth2_authorization.h"
00029 #include "googleapis/client/util/abstract_webserver.h"
00030 #include "googleapis/base/callback.h"
00031 #include "googleapis/base/integral_types.h"
00032 #include "googleapis/base/macros.h"
00033 #include "googleapis/base/mutex.h"
00034 #include "googleapis/base/scoped_ptr.h"
00035 #include "googleapis/base/thread_annotations.h"
00036 #include "googleapis/strings/stringpiece.h"
00037 #include "googleapis/util/status.h"
00038 namespace googleapis {
00039 
00040 namespace client {
00041 
00042 /*
00043  * An adapter to use webserver with OAuth2AuthorizationFlows.
00044  * @ingroup AuthSupportOAuth2
00045  *
00046  * This class will likely change significantly or go away in a future release.
00047  *
00048  * It is here to support samples, experimentation, and testing
00049  * OAuth2 web flows.
00050  */
00051 class WebServerAuthorizationCodeGetter {
00052  public:
00053   /*
00054    * Callback used to prompt for authorization.
00055    * Receiving authorization will happen through a web server handler.
00056    */
00057   typedef ResultCallback1< util::Status, const StringPiece& > AskCallback;
00058 
00059   /*
00060    * Standard constructor.
00061    * @param[in] ask_callback Must be a non-NULL repeatable callback.
00062    *                         takes ownership.
00063    */
00064   explicit WebServerAuthorizationCodeGetter(AskCallback* ask_callback);
00065 
00066   /*
00067    * Standard destructor.
00068    */
00069   virtual ~WebServerAuthorizationCodeGetter();
00070 
00071   /*
00072    * How long we'll wait for authorization.
00073    *
00074    * @returns Time in milliseconds.
00075    */
00076   int64 timeout_ms() const       { return timeout_ms_; }
00077 
00078   /*
00079    * Set how long we'll weait.
00080    *
00081    * @param[in] ms Time in milliseconds.
00082    */
00083   void set_timeout_ms(int64 ms)  { timeout_ms_ = ms; }
00084 
00085   /*
00086    * Gets the ask callback.
00087    *
00088    * @return reference. This instance retains ownership.
00089    */
00090   AskCallback* ask_callback() { return ask_callback_.get(); }
00091 
00092   /*
00093    * Returns a repeatable callback for flow to get an authorization code.
00094    *
00095    * @param[in] flow A reference to the flow is used to generate urls.
00096    * @return Ownershp is apssed back to the caller.
00097    */
00098   OAuth2AuthorizationFlow::AuthorizationCodeCallback*
00099     MakeAuthorizationCodeCallback(OAuth2AuthorizationFlow* flow);
00100 
00101   /*
00102    * @param[in] path The path that uri_redirects are expected on.
00103    * @param[in] httpd The webserver to process the redirects wtih
00104    */
00105   virtual void  AddReceiveAuthorizationCodeUrlPath(
00106       const StringPiece& path, AbstractWebServer* httpd);
00107 
00108   virtual util::Status PromptForAuthorizationCode(
00109       OAuth2AuthorizationFlow* flow,
00110       const OAuth2RequestOptions& options,
00111       string* authorization_code);
00112 
00113   /*
00114    * A suitable function for an asker that execute a command (e.g. a browser).
00115    */
00116   static util::Status PromptWithCommand(
00117       const string& program, const string& args, const StringPiece& url);
00118 
00119   /*
00120    * A suitable function for an asker that prompts a console.
00121    */
00122   static util::Status PromptWithOstream(
00123        std::ostream* ostream, const string& prompt, const StringPiece& url);
00124 
00125  protected:
00126   virtual util::Status AskForAuthorization(const StringPiece& url);
00127 
00128  private:
00129   int64 timeout_ms_;
00130   scoped_ptr<AskCallback> ask_callback_;
00131   Mutex mutex_;
00132   CondVar authorization_condvar_        GUARDED_BY(mutex_);
00133   string authorization_code_            GUARDED_BY(mutex_);
00134   util::Status authorization_status_  GUARDED_BY(mutex_);
00135 
00136   util::Status ReceiveAuthorizationCode(WebServerRequest* request);
00137 
00138   DISALLOW_COPY_AND_ASSIGN(WebServerAuthorizationCodeGetter);
00139 };
00140 
00141 }  // namespace client
00142 
00143 } // namespace googleapis
00144 #endif  // APISERVING_CLIENTS_CPP_AUTH_WEBSERVER_AUTHORIZATION_GETTER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines