Google APIs Client Library for C++
|
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 00021 #ifndef APISERVING_CLIENTS_CPP_AUTH_OAUTH2_PENDING_AUTHORIZATIONS_H_ 00022 #define APISERVING_CLIENTS_CPP_AUTH_OAUTH2_PENDING_AUTHORIZATIONS_H_ 00023 00024 #include <stdlib.h> 00025 #include <map> 00026 using std::map; 00027 #include <string> 00028 using std::string; 00029 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/thread_annotations.h" 00035 #include "googleapis/strings/stringpiece.h" 00036 #include "googleapis/strings/numbers.h" 00037 #include "googleapis/util/hash.h" 00038 #include "googleapis/util/status.h" 00039 namespace googleapis { 00040 00041 namespace client { 00042 00043 /* 00044 * Callback used to process authorization codes received 00045 * from the OAuth 2.0 server. 00046 * @ingroup AuthSupportOAuth2 00047 * 00048 * In practice this is used for the response handling for Authorization 00049 * Codes where we give a redirect_uri to the OAuth 2.0 server for where 00050 * to forward the code. The url will be the response with the state parameter 00051 * used to map back to the original request. 00052 * 00053 * In practice you'll probably want to curry additional data parameters 00054 * with the callback so that you have additional context to the inquiry. 00055 * 00056 * @param[in] status If not ok then we're cancelling the request for some 00057 * reason, such as a timeout. 00058 * @param[in] code The authorization code if ok, otherwise ignored. 00059 */ 00060 typedef Callback2<const util::Status&, const StringPiece&> 00061 OAuth2BasicAuthorizationCodeNotificationHandler; 00062 00063 /* 00064 * Manages callbacks for outstanding authorization code requests. 00065 * 00066 * This class is threadsafe. 00067 */ 00068 template<class CALLBACK> 00069 class OAuth2PendingAuthorizations { 00070 public: 00071 /* 00072 * Standard constructor. 00073 */ 00074 OAuth2PendingAuthorizations() {} 00075 00076 /* 00077 * Standard destructor. 00078 */ 00079 virtual ~OAuth2PendingAuthorizations() { 00080 for (typename map<int, CALLBACK*>::iterator it = map_.begin(); 00081 it != map_.end(); 00082 ++it) { 00083 CancelCallback(it->second); 00084 } 00085 } 00086 00087 void CancelCallback(CALLBACK* callback) { delete callback; } 00088 00089 /* 00090 * Adds a notification handler, returning the state value to associate with. 00091 * 00092 * @param[in] handler Will eventually be called exactly once 00093 * 00094 * @return value to use for the state query parameter in the url. This 00095 * will be used later as the key to retrieve the callback to invoke. 00096 */ 00097 int AddAuthorizationCodeHandler(CALLBACK* handler) LOCKS_EXCLUDED(mutex_) { 00098 MutexLock l(&mutex_); 00099 int key; 00100 for (key = random(); map_.find(key) != map_.end(); key = random()) { 00101 // find unused key 00102 } 00103 map_.insert(make_pair(key, handler)); 00104 return key; 00105 } 00106 00107 /* 00108 * Looks up registered handler for key and removes it if found. 00109 * 00110 * This will remove the handler if it was found so it is only returned once. 00111 * 00112 * @param[in] key Returned by AddAuthorizationCodeHandler 00113 * @return The handler added for the key or NULL. 00114 */ 00115 CALLBACK* FindAndRemoveHandlerForKey(int key) LOCKS_EXCLUDED(mutex_) { 00116 CALLBACK* handler = NULL; 00117 MutexLock l(&mutex_); 00118 typename map<int, CALLBACK*>::iterator it = map_.find(key); 00119 if (it != map_.end()) { 00120 handler = it->second; 00121 map_.erase(it); 00122 } 00123 return handler; 00124 } 00125 00126 private: 00127 Mutex mutex_; 00128 map<int, CALLBACK*> map_ GUARDED_BY(mutex_); 00129 00130 DISALLOW_COPY_AND_ASSIGN(OAuth2PendingAuthorizations); 00131 }; 00132 00133 } // namespace client 00134 00135 } // namespace googleapis 00136 #endif // APISERVING_CLIENTS_CPP_AUTH_OAUTH2_PENDING_AUTHORIZATIONS_H_