Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable access token refresh #14251

Merged
merged 2 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -215,7 +226,7 @@ module {{moduleName}}
format: '{{{.}}}',
{{/bearerFormat}}
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
{{/isBasicBearer}}
{{/isBasic}}
Expand All @@ -225,7 +236,7 @@ module {{moduleName}}
type: 'oauth2',
in: 'header',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
{{/isOAuth}}
{{/authMethods}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class Configuration
# 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.
Expand Down Expand Up @@ -208,6 +213,12 @@ def api_key_with_prefix(param_name, param_alias = nil)
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")
Expand Down Expand Up @@ -236,7 +247,7 @@ def auth_settings
in: 'header',
format: 'JWT',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
'http_basic_test' =>
{
Expand All @@ -250,7 +261,7 @@ def auth_settings
type: 'oauth2',
in: 'header',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
}
end
Expand Down
15 changes: 13 additions & 2 deletions samples/client/petstore/ruby-faraday/lib/petstore/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class Configuration
# 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.
Expand Down Expand Up @@ -211,6 +216,12 @@ def api_key_with_prefix(param_name, param_alias = nil)
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")
Expand Down Expand Up @@ -239,7 +250,7 @@ def auth_settings
in: 'header',
format: 'JWT',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
'http_basic_test' =>
{
Expand All @@ -253,7 +264,7 @@ def auth_settings
type: 'oauth2',
in: 'header',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
}
end
Expand Down
15 changes: 13 additions & 2 deletions samples/client/petstore/ruby/lib/petstore/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class Configuration
# 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.
Expand Down Expand Up @@ -208,6 +213,12 @@ def api_key_with_prefix(param_name, param_alias = nil)
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")
Expand Down Expand Up @@ -236,7 +247,7 @@ def auth_settings
in: 'header',
format: 'JWT',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
'http_basic_test' =>
{
Expand All @@ -250,7 +261,7 @@ def auth_settings
type: 'oauth2',
in: 'header',
key: 'Authorization',
value: "Bearer #{access_token}"
value: "Bearer #{access_token_with_refresh}"
},
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class Configuration
# 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.
Expand Down Expand Up @@ -208,6 +213,12 @@ def api_key_with_prefix(param_name, param_alias = nil)
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class Configuration
# 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.
Expand Down Expand Up @@ -208,6 +213,12 @@ def api_key_with_prefix(param_name, param_alias = nil)
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class Configuration
# 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.
Expand Down Expand Up @@ -208,6 +213,12 @@ def api_key_with_prefix(param_name, param_alias = nil)
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")
Expand Down