-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a token_cache configuration option to share tokens across proces…
…ses (#11)
- Loading branch information
1 parent
0b3be15
commit 184fc1a
Showing
16 changed files
with
612 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module HelpScout | ||
class API | ||
class AccessToken | ||
class Cache | ||
attr_reader :backend, :key | ||
|
||
def initialize(backend: nil, key: nil) | ||
@backend = backend || HelpScout.configuration.token_cache | ||
@key = key || HelpScout.configuration.token_cache_key | ||
end | ||
|
||
def configured? | ||
backend.present? && key.present? | ||
end | ||
|
||
def delete | ||
backend.delete(key) | ||
end | ||
|
||
def fetch_token(&token_request) | ||
raise ArgumentError, 'A request fallback block is required' unless block_given? | ||
|
||
if (cached_token_json = backend.read(key)) | ||
AccessToken.new(JSON.parse(cached_token_json, symbolize_names: true)) | ||
else | ||
token_request.call.tap { |token| write(token) } | ||
end | ||
end | ||
|
||
private | ||
|
||
def write(token) | ||
backend.write(key, token.to_json, expires_in: token.expires_in) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module HelpScout | ||
class API | ||
class AccessToken | ||
class Request | ||
attr_reader :response | ||
|
||
def execute | ||
@response = request_token | ||
end | ||
|
||
private | ||
|
||
def request_token | ||
connection = API::Client.new(authorize: false).connection | ||
http_response = connection.post('oauth2/token', token_request_params) | ||
|
||
case http_response.status | ||
when 200 then AccessToken.new(Response.new(http_response).body) | ||
when 429 then raise API::ThrottleLimitReached, http_response.body&.dig('error') | ||
else raise API::InternalError, "unexpected response (status #{http_response.status})" | ||
end | ||
end | ||
|
||
def token_request_params | ||
@_token_request_params ||= { | ||
grant_type: 'client_credentials', | ||
client_id: HelpScout.app_id, | ||
client_secret: HelpScout.app_secret | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ss_token_is_not_set_in_the_cache/requests_and_returns_a_new_access_token_from_the_API.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
...is_enabled/and_the_access_token_is_not_set_in_the_cache/writes_the_token_to_the_cache.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_support/cache/memory_store' | ||
|
||
RSpec.describe HelpScout::API::AccessToken do | ||
describe '.create' do | ||
subject { described_class.create } | ||
|
||
it 'returns an AccessToken' do | ||
expect(subject).to be_a described_class | ||
end | ||
|
||
context 'when caching is configured' do | ||
let(:cache) { double('token_cache') } | ||
|
||
around { |example| with_config(token_cache: cache) { example.run } } | ||
|
||
it 'attempts to fetch the token from the cache' do | ||
expect_any_instance_of(HelpScout::API::AccessToken::Cache).to receive(:fetch_token) | ||
|
||
subject | ||
end | ||
end | ||
|
||
context 'when caching is not configured' do | ||
around { |example| with_config(token_cache: nil) { example.run } } | ||
|
||
it 'attempts to request a token' do | ||
expect_any_instance_of(HelpScout::API::AccessToken::Request).to receive(:execute) | ||
|
||
subject | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.