1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_CORE_PLATFORM_CLOUD_OAUTH_CLIENT_H_ 17 #define TENSORFLOW_CORE_PLATFORM_CLOUD_OAUTH_CLIENT_H_ 18 19 #include <memory> 20 21 #include "json/json.h" 22 #include "tensorflow/core/platform/cloud/http_request.h" 23 #include "tensorflow/core/platform/env.h" 24 #include "tensorflow/core/platform/status.h" 25 26 namespace tensorflow { 27 28 /// OAuth 2.0 client. 29 class OAuthClient { 30 public: 31 OAuthClient(); 32 explicit OAuthClient( 33 std::unique_ptr<HttpRequest::Factory> http_request_factory, Env* env); ~OAuthClient()34 virtual ~OAuthClient() {} 35 36 /// \brief Retrieves a bearer token using a private key. 37 /// 38 /// Retrieves the authentication bearer token using a JSON file 39 /// with the client's private key. 40 virtual Status GetTokenFromServiceAccountJson( 41 Json::Value json, StringPiece oauth_server_uri, StringPiece scope, 42 string* token, uint64* expiration_timestamp_sec); 43 44 /// Retrieves a bearer token using a refresh token. 45 virtual Status GetTokenFromRefreshTokenJson(Json::Value json, 46 StringPiece oauth_server_uri, 47 string* token, 48 uint64* expiration_timestamp_sec); 49 50 /// Parses the JSON response with the token from an OAuth 2.0 server. 51 virtual Status ParseOAuthResponse(StringPiece response, 52 uint64 request_timestamp_sec, string* token, 53 uint64* expiration_timestamp_sec); 54 55 private: 56 std::unique_ptr<HttpRequest::Factory> http_request_factory_; 57 Env* env_; 58 TF_DISALLOW_COPY_AND_ASSIGN(OAuthClient); 59 }; 60 61 } // namespace tensorflow 62 63 #endif // TENSORFLOW_CORE_PLATFORM_CLOUD_OAUTH_CLIENT_H_ 64