This PR introduces official automatic OAuth authentication.
Adding authentication to all kind of requests made with LHS was the #1 on the annoyance list, when it comes to LHS, often leading people to come up with proxy methods, shared auth options, or storing thread variables themselves, all of it ending up in additional boilerplate code that could've been avoided . Well this PR is gonna take care of it.
Automatic Authentication (OAuth)
LHS provides a way to have records automatically fetch and use OAuth authentication when performing requests within Rails.
In order to enable automatic oauth authentication, perform the following steps:
Make sure LHS is configured to perform auto_oauth. Provide a block that, when executed in the controller context, returns a valid access_token/bearer_token.
# config/initializers/lhs.rb
LHS.configure do |config|
config.auto_oauth = -> { access_token }
end
2. Opt-in records requiring oauth authentication:
```ruby
# app/models/record.rb
class Record < LHS::Record
oauth
# ...
end
Include the LHS::OAuth context into your application controller:
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include LHS::OAuth
# ...
end
Make sure you have the LHC::Auth interceptor enabled:
# config/initializers/lhc.rb
LHC.configure do |config|
config.interceptors = [LHC::Auth]
end
Now you can perform requests based on the record that will be auto authenticated from now on:
Configure multiple auth providers (even per endpoint)
In case you need to configure multiple auth provider access_tokens within your application,
make sure you provide a proc returning a hash when configuring auto_oauth,
naming every single provider and the responsive method to retrieve the access_tokens in the controller context:
# config/initializers/lhs.rb
LHS.configure do |config|
config.auto_oauth = proc do
{
provider1: access_token_provider_1,
provider2: access_token_provider_2
}
end
end
Then make sure you either define which provider to use on a record level:
# model/record.rb
class Record < LHS::Record
oauth(:provider1)
#...
end
or on an endpoint level:
# model/record.rb
class Record < LHS::Record
endpoint 'https://service/records', oauth: :provider1
#...
end
This PR introduces official automatic OAuth authentication.
Adding authentication to all kind of requests made with LHS was the #1 on the annoyance list, when it comes to LHS, often leading people to come up with proxy methods, shared auth options, or storing thread variables themselves, all of it ending up in additional boilerplate code that could've been avoided . Well this PR is gonna take care of it.
Automatic Authentication (OAuth)
LHS provides a way to have records automatically fetch and use OAuth authentication when performing requests within Rails.
In order to enable automatic oauth authentication, perform the following steps:
auto_oauth
. Provide a block that, when executed in the controller context, returns a valid access_token/bearer_token.LHS.configure do |config| config.auto_oauth = -> { access_token } end
LHS::OAuth
context into your application controller:LHC::Auth
interceptor enabled:Now you can perform requests based on the record that will be auto authenticated from now on:
Configure multiple auth providers (even per endpoint)
In case you need to configure multiple auth provider access_tokens within your application, make sure you provide a proc returning a hash when configuring
auto_oauth
, naming every single provider and the responsive method to retrieve the access_tokens in the controller context:Then make sure you either define which provider to use on a record level:
or on an endpoint level: