Enable access token refresh (#14251)

* Enable the ruby client to support refreshing access tokens

- The client can now be configured with an access token getter proc
- The proc overrides the the static access token if it is set

* Run generators
This commit is contained in:
Lisa Burns 2022-12-20 23:19:51 -08:00 committed by GitHub
parent 851ddecda3
commit 02d4852f26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 91 additions and 10 deletions

View File

@ -70,13 +70,17 @@ require '{{{gemName}}}'
config.username = 'YOUR_USERNAME'
config.password = 'YOUR_PASSWORD'{{/isBasicBasic}}{{#isBasicBearer}}
# Configure Bearer authorization{{#bearerFormat}} ({{{.}}}){{/bearerFormat}}: {{{name}}}
config.access_token = 'YOUR_BEARER_TOKEN'{{/isBasicBearer}}{{/isBasic}}{{#isApiKey}}
config.access_token = 'YOUR_BEARER_TOKEN'
# Configure a proc to get access tokens in lieu of the static access_token configuration
config.access_token_getter = -> { 'YOUR TOKEN GETTER PROC' } {{/isBasicBearer}}{{/isBasic}}{{#isApiKey}}
# Configure API key authorization: {{{name}}}
config.api_key['{{{name}}}'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
# config.api_key_prefix['{{{name}}}'] = 'Bearer'{{/isApiKey}}{{#isOAuth}}
# Configure OAuth2 access token for authorization: {{{name}}}
config.access_token = 'YOUR ACCESS TOKEN'{{/isOAuth}}
config.access_token = 'YOUR ACCESS TOKEN'
# Configure a proc to get access tokens in lieu of the static access_token configuration
config.access_token_getter = -> { 'YOUR TOKEN GETTER PROC' } {{/isOAuth}}
{{/authMethods}}end
{{/hasAuthMethods}}

View File

@ -54,6 +54,11 @@ module {{moduleName}}
# Defines the access token (Bearer) used with OAuth2.
attr_accessor :access_token
# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
# Overrides the access_token if set
# @return [Proc]
attr_accessor :access_token_getter
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
# details will be logged with `logger.debug` (see the `logger` attribute).
# Default to false.
@ -178,6 +183,12 @@ module {{moduleName}}
end
end
# Gets access_token using access_token_getter or uses the static access_token
def access_token_with_refresh
return access_token if access_token_getter.nil?
access_token_getter.call
end
# Gets Basic Auth token string
def basic_auth_token
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
@ -215,7 +226,7 @@ module {{moduleName}}
format: '{{{.}}}',
{{/bearerFormat}}
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
{{/isBasicBearer}}
{{/isBasic}}
@ -225,7 +236,7 @@ module {{moduleName}}
type: 'oauth2',
in: 'header',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
{{/isOAuth}}
{{/authMethods}}

View File

@ -62,6 +62,11 @@ module Petstore
# Defines the access token (Bearer) used with OAuth2.
attr_accessor :access_token
# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
# Overrides the access_token if set
# @return [Proc]
attr_accessor :access_token_getter
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
# details will be logged with `logger.debug` (see the `logger` attribute).
# Default to false.
@ -208,6 +213,12 @@ module Petstore
end
end
# Gets access_token using access_token_getter or uses the static access_token
def access_token_with_refresh
return access_token if access_token_getter.nil?
access_token_getter.call
end
# Gets Basic Auth token string
def basic_auth_token
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
@ -236,7 +247,7 @@ module Petstore
in: 'header',
format: 'JWT',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
'http_basic_test' =>
{
@ -250,7 +261,7 @@ module Petstore
type: 'oauth2',
in: 'header',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
}
end

View File

@ -62,6 +62,11 @@ module Petstore
# Defines the access token (Bearer) used with OAuth2.
attr_accessor :access_token
# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
# Overrides the access_token if set
# @return [Proc]
attr_accessor :access_token_getter
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
# details will be logged with `logger.debug` (see the `logger` attribute).
# Default to false.
@ -211,6 +216,12 @@ module Petstore
end
end
# Gets access_token using access_token_getter or uses the static access_token
def access_token_with_refresh
return access_token if access_token_getter.nil?
access_token_getter.call
end
# Gets Basic Auth token string
def basic_auth_token
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
@ -239,7 +250,7 @@ module Petstore
in: 'header',
format: 'JWT',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
'http_basic_test' =>
{
@ -253,7 +264,7 @@ module Petstore
type: 'oauth2',
in: 'header',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
}
end

View File

@ -62,6 +62,11 @@ module Petstore
# Defines the access token (Bearer) used with OAuth2.
attr_accessor :access_token
# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
# Overrides the access_token if set
# @return [Proc]
attr_accessor :access_token_getter
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
# details will be logged with `logger.debug` (see the `logger` attribute).
# Default to false.
@ -208,6 +213,12 @@ module Petstore
end
end
# Gets access_token using access_token_getter or uses the static access_token
def access_token_with_refresh
return access_token if access_token_getter.nil?
access_token_getter.call
end
# Gets Basic Auth token string
def basic_auth_token
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
@ -236,7 +247,7 @@ module Petstore
in: 'header',
format: 'JWT',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
'http_basic_test' =>
{
@ -250,7 +261,7 @@ module Petstore
type: 'oauth2',
in: 'header',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
}
end

View File

@ -62,6 +62,11 @@ module XAuthIDAlias
# Defines the access token (Bearer) used with OAuth2.
attr_accessor :access_token
# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
# Overrides the access_token if set
# @return [Proc]
attr_accessor :access_token_getter
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
# details will be logged with `logger.debug` (see the `logger` attribute).
# Default to false.
@ -208,6 +213,12 @@ module XAuthIDAlias
end
end
# Gets access_token using access_token_getter or uses the static access_token
def access_token_with_refresh
return access_token if access_token_getter.nil?
access_token_getter.call
end
# Gets Basic Auth token string
def basic_auth_token
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")

View File

@ -62,6 +62,11 @@ module DynamicServers
# Defines the access token (Bearer) used with OAuth2.
attr_accessor :access_token
# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
# Overrides the access_token if set
# @return [Proc]
attr_accessor :access_token_getter
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
# details will be logged with `logger.debug` (see the `logger` attribute).
# Default to false.
@ -208,6 +213,12 @@ module DynamicServers
end
end
# Gets access_token using access_token_getter or uses the static access_token
def access_token_with_refresh
return access_token if access_token_getter.nil?
access_token_getter.call
end
# Gets Basic Auth token string
def basic_auth_token
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")

View File

@ -62,6 +62,11 @@ module Petstore
# Defines the access token (Bearer) used with OAuth2.
attr_accessor :access_token
# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
# Overrides the access_token if set
# @return [Proc]
attr_accessor :access_token_getter
# Set this to enable/disable debugging. When enabled (set to true), HTTP request/response
# details will be logged with `logger.debug` (see the `logger` attribute).
# Default to false.
@ -208,6 +213,12 @@ module Petstore
end
end
# Gets access_token using access_token_getter or uses the static access_token
def access_token_with_refresh
return access_token if access_token_getter.nil?
access_token_getter.call
end
# Gets Basic Auth token string
def basic_auth_token
'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")