From f26985c23967d5cbdd8f0dbf9f0d6a49e29593f9 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Fri, 22 Jul 2022 09:44:05 +0800 Subject: [PATCH] add http bearer auth support to R client (#12974) --- .../src/main/resources/r/api.mustache | 17 +++++++++++++---- .../src/main/resources/r/api_client.mustache | 10 +++++++++- .../src/main/resources/r/api_doc.mustache | 6 ++++++ .../src/test/resources/3_0/r/petstore.yaml | 5 ++++- samples/client/petstore/R/R/api_client.R | 10 +++++++++- samples/client/petstore/R/R/pet_api.R | 9 +++------ samples/client/petstore/R/README.md | 4 ++++ samples/client/petstore/R/docs/PetApi.md | 6 +++--- 8 files changed, 51 insertions(+), 16 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/r/api.mustache b/modules/openapi-generator/src/main/resources/r/api.mustache index 55264329509..600d16d2b18 100644 --- a/modules/openapi-generator/src/main/resources/r/api.mustache +++ b/modules/openapi-generator/src/main/resources/r/api.mustache @@ -97,11 +97,17 @@ {{#authMethods}} #' {{#isBasic}} +{{#isBasicBasic}} #' #Configure HTTP basic authorization: {{{name}}} -#' # provide your username in the user-serial format -#' api.instance$api_client$username <- ''; -#' # provide your api key generated using the developer portal -#' api.instance$api_client$password <- ''; +#' # provide the username +#' api.instance$api_client$username <- 'TODO_YOUR_USERNAME'; +#' # provide the password +#' api.instance$api_client$password <- 'TODO_YOUR_PASSWORD'; +{{/isBasicBasic}} +{{#isBasicBearer}} +#' #Configure HTTP bearer authorization: {{{name}}} +#' api.instance$api_client$bearer_token <- 'TODO_YOUR_BEARER_TOKEN'; +{{/isBasicBearer}} {{/isBasic}} {{#isApiKey}} #' #Configure API key authorization: {{{name}}} @@ -302,6 +308,9 @@ # HTTP basic auth header_params["Authorization"] <- paste("Basic", base64enc::base64encode(charToRaw(paste(self$api_client$username, self$api_client$password, sep = ":")))) {{/isBasicBasic}} + {{#isBasicBearer}} + header_params["Authorization"] <- paste("Bearer", self$api_client$bearer_token, sep = " ") + {{/isBasicBearer}} {{/isBasic}} {{#isApiKey}} # API key authentication diff --git a/modules/openapi-generator/src/main/resources/r/api_client.mustache b/modules/openapi-generator/src/main/resources/r/api_client.mustache index 1f40df74518..a581d4af6b3 100644 --- a/modules/openapi-generator/src/main/resources/r/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/r/api_client.mustache @@ -22,6 +22,7 @@ #' @field password Password for HTTP basic authentication #' @field api_keys API keys #' @field access_token Access token +#' @field bearer_token Bearer token #' @field timeout Default timeout in seconds #' @field retry_status_codes vector of status codes to retry #' @field max_retry_attempts maximum number of retries for the status codes @@ -47,6 +48,8 @@ ApiClient <- R6::R6Class( api_keys = NULL, # Access token access_token = NULL, + # Bearer token + bearer_token = NULL, # Time Out (seconds) timeout = NULL, # Vector of status codes to retry @@ -65,6 +68,7 @@ ApiClient <- R6::R6Class( #' @param password Password. #' @param api_keys API keys. #' @param access_token Access token. + #' @param bearer_token Bearer token. #' @param timeout Timeout. #' @param retry_status_codes Status codes for retry. #' @param max_retry_attempts Maxmium number of retry. @@ -72,7 +76,7 @@ ApiClient <- R6::R6Class( initialize = function(base_path = NULL, user_agent = NULL, default_headers = NULL, username = NULL, password = NULL, api_keys = NULL, - access_token = NULL, timeout = NULL, + access_token = NULL, bearer_token = NULL, timeout = NULL, retry_status_codes = NULL, max_retry_attempts = NULL) { if (!is.null(base_path)) { self$base_path <- base_path @@ -94,6 +98,10 @@ ApiClient <- R6::R6Class( self$access_token <- access_token } + if (!is.null(bearer_token)) { + self$bearer_token <- bearer_token + } + if (!is.null(api_keys)) { self$api_keys <- api_keys } else { diff --git a/modules/openapi-generator/src/main/resources/r/api_doc.mustache b/modules/openapi-generator/src/main/resources/r/api_doc.mustache index ea99453e93e..17c3bdc1e91 100644 --- a/modules/openapi-generator/src/main/resources/r/api_doc.mustache +++ b/modules/openapi-generator/src/main/resources/r/api_doc.mustache @@ -32,9 +32,15 @@ api_instance <- {{{classname}}}$new() {{#hasAuthMethods}} {{#authMethods}} {{#isBasic}} +{{#isBasicBasic}} # Configure HTTP basic authorization: {{{name}}} api_instance$api_client$username <- 'TODO_YOUR_USERNAME'; api_instance$api_client$password <- 'TODO_YOUR_PASSWORD'; +{{/isBasicBasic}} +{{#isBasicBearer}} +# Configure HTTP bearer authorization: {{{name}}} +api.instance$api_client$bearer_token <- 'TODO_YOUR_BEARER_TOKEN'; +{{/isBasicBearer}} {{/isBasic}} {{#isApiKey}} # Configure API key authorization: {{{name}}} diff --git a/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml b/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml index dc669b62e2f..e0bdd9a60cd 100644 --- a/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml @@ -184,7 +184,7 @@ paths: '404': description: Pet not found security: - - api_key: [] + - BearerToken: [] '/pet/{petId}?streaming': get: tags: @@ -671,6 +671,9 @@ components: description: Pet object that needs to be added to the store required: true securitySchemes: + BearerToken: + type : http + scheme : bearer petstore_auth: type: oauth2 flows: diff --git a/samples/client/petstore/R/R/api_client.R b/samples/client/petstore/R/R/api_client.R index 568943a6bd5..ae2cd498a9c 100644 --- a/samples/client/petstore/R/R/api_client.R +++ b/samples/client/petstore/R/R/api_client.R @@ -29,6 +29,7 @@ #' @field password Password for HTTP basic authentication #' @field api_keys API keys #' @field access_token Access token +#' @field bearer_token Bearer token #' @field timeout Default timeout in seconds #' @field retry_status_codes vector of status codes to retry #' @field max_retry_attempts maximum number of retries for the status codes @@ -52,6 +53,8 @@ ApiClient <- R6::R6Class( api_keys = NULL, # Access token access_token = NULL, + # Bearer token + bearer_token = NULL, # Time Out (seconds) timeout = NULL, # Vector of status codes to retry @@ -70,6 +73,7 @@ ApiClient <- R6::R6Class( #' @param password Password. #' @param api_keys API keys. #' @param access_token Access token. + #' @param bearer_token Bearer token. #' @param timeout Timeout. #' @param retry_status_codes Status codes for retry. #' @param max_retry_attempts Maxmium number of retry. @@ -77,7 +81,7 @@ ApiClient <- R6::R6Class( initialize = function(base_path = NULL, user_agent = NULL, default_headers = NULL, username = NULL, password = NULL, api_keys = NULL, - access_token = NULL, timeout = NULL, + access_token = NULL, bearer_token = NULL, timeout = NULL, retry_status_codes = NULL, max_retry_attempts = NULL) { if (!is.null(base_path)) { self$base_path <- base_path @@ -99,6 +103,10 @@ ApiClient <- R6::R6Class( self$access_token <- access_token } + if (!is.null(bearer_token)) { + self$bearer_token <- bearer_token + } + if (!is.null(api_keys)) { self$api_keys <- api_keys } else { diff --git a/samples/client/petstore/R/R/pet_api.R b/samples/client/petstore/R/R/pet_api.R index 37a27ce9408..9e26a6f4d87 100644 --- a/samples/client/petstore/R/R/pet_api.R +++ b/samples/client/petstore/R/R/pet_api.R @@ -373,8 +373,8 @@ #' #Find pet by ID #' api.instance <- PetApi$new() #' -#' #Configure API key authorization: api_key -#' api.instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY'; +#' #Configure HTTP bearer authorization: BearerToken +#' api.instance$api_client$bearer_token <- 'TODO_YOUR_BEARER_TOKEN'; #' #'result <- tryCatch( #' api.instance$GetPetById(var.pet_id), @@ -969,10 +969,7 @@ PetApi <- R6::R6Class( url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), url_path) } - # API key authentication - if ("api_key" %in% names(self$api_client$api_keys) && nchar(self$api_client$api_keys["api_key"]) > 0) { - header_params["api_key"] <- paste(unlist(self$api_client$api_keys["api_key"]), collapse = "") - } + header_params["Authorization"] <- paste("Bearer", self$api_client$bearer_token, sep = " ") resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, url_path), method = "GET", diff --git a/samples/client/petstore/R/README.md b/samples/client/petstore/R/README.md index 18c37ed822d..6b9818a4e50 100644 --- a/samples/client/petstore/R/README.md +++ b/samples/client/petstore/R/README.md @@ -111,6 +111,10 @@ Class | Method | HTTP request | Description ## Documentation for Authorization +### BearerToken + +- **Type**: HTTP basic authentication + ### api_key - **Type**: API key diff --git a/samples/client/petstore/R/docs/PetApi.md b/samples/client/petstore/R/docs/PetApi.md index a89cf8fac8f..bf8a558990d 100644 --- a/samples/client/petstore/R/docs/PetApi.md +++ b/samples/client/petstore/R/docs/PetApi.md @@ -271,8 +271,8 @@ var_pet_id <- 56 # integer | ID of pet to return #Find pet by ID api_instance <- PetApi$new() -# Configure API key authorization: api_key -api_instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY'; +# Configure HTTP bearer authorization: BearerToken +api.instance$api_client$bearer_token <- 'TODO_YOUR_BEARER_TOKEN'; result <- tryCatch( # to save the result into a file, simply add the optional `data_file` parameter, e.g. # api_instance$GetPetById(var_pet_id, data_file = "result.txt"), @@ -304,7 +304,7 @@ Name | Type | Description | Notes ### Authorization -[api_key](../README.md#api_key) +[BearerToken](../README.md#BearerToken) ### HTTP request headers