1 /* 2 * 3 * Copyright 2016 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #ifndef GRPC_CORE_LIB_SECURITY_CREDENTIALS_OAUTH2_OAUTH2_CREDENTIALS_H 20 #define GRPC_CORE_LIB_SECURITY_CREDENTIALS_OAUTH2_OAUTH2_CREDENTIALS_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include "src/core/lib/json/json.h" 25 #include "src/core/lib/security/credentials/credentials.h" 26 27 // auth_refresh_token parsing. 28 typedef struct { 29 const char* type; 30 char* client_id; 31 char* client_secret; 32 char* refresh_token; 33 } grpc_auth_refresh_token; 34 35 /// Returns 1 if the object is valid, 0 otherwise. 36 int grpc_auth_refresh_token_is_valid( 37 const grpc_auth_refresh_token* refresh_token); 38 39 /// Creates a refresh token object from string. Returns an invalid object if a 40 /// parsing error has been encountered. 41 grpc_auth_refresh_token grpc_auth_refresh_token_create_from_string( 42 const char* json_string); 43 44 /// Creates a refresh token object from parsed json. Returns an invalid object 45 /// if a parsing error has been encountered. 46 grpc_auth_refresh_token grpc_auth_refresh_token_create_from_json( 47 const grpc_json* json); 48 49 /// Destructs the object. 50 void grpc_auth_refresh_token_destruct(grpc_auth_refresh_token* refresh_token); 51 52 // -- Oauth2 Token Fetcher credentials -- 53 // 54 // This object is a base for credentials that need to acquire an oauth2 token 55 // from an http service. 56 57 typedef void (*grpc_fetch_oauth2_func)(grpc_credentials_metadata_request* req, 58 grpc_httpcli_context* http_context, 59 grpc_polling_entity* pollent, 60 grpc_iomgr_cb_func cb, 61 grpc_millis deadline); 62 63 typedef struct grpc_oauth2_pending_get_request_metadata { 64 grpc_credentials_mdelem_array* md_array; 65 grpc_closure* on_request_metadata; 66 grpc_polling_entity* pollent; 67 struct grpc_oauth2_pending_get_request_metadata* next; 68 } grpc_oauth2_pending_get_request_metadata; 69 70 typedef struct { 71 grpc_call_credentials base; 72 gpr_mu mu; 73 grpc_mdelem access_token_md; 74 gpr_timespec token_expiration; 75 bool token_fetch_pending; 76 grpc_oauth2_pending_get_request_metadata* pending_requests; 77 grpc_httpcli_context httpcli_context; 78 grpc_fetch_oauth2_func fetch_func; 79 grpc_polling_entity pollent; 80 } grpc_oauth2_token_fetcher_credentials; 81 82 // Google refresh token credentials. 83 typedef struct { 84 grpc_oauth2_token_fetcher_credentials base; 85 grpc_auth_refresh_token refresh_token; 86 } grpc_google_refresh_token_credentials; 87 88 // Access token credentials. 89 typedef struct { 90 grpc_call_credentials base; 91 grpc_mdelem access_token_md; 92 } grpc_access_token_credentials; 93 94 // Private constructor for refresh token credentials from an already parsed 95 // refresh token. Takes ownership of the refresh token. 96 grpc_call_credentials* 97 grpc_refresh_token_credentials_create_from_auth_refresh_token( 98 grpc_auth_refresh_token token); 99 100 // Exposed for testing only. 101 grpc_credentials_status 102 grpc_oauth2_token_fetcher_credentials_parse_server_response( 103 const struct grpc_http_response* response, grpc_mdelem* token_md, 104 grpc_millis* token_lifetime); 105 106 #endif /* GRPC_CORE_LIB_SECURITY_CREDENTIALS_OAUTH2_OAUTH2_CREDENTIALS_H */ 107