Skip to content

Commit 307c7e0

Browse files
committed
Add OauthClient class
This has been copied as closely as possible from `code-club-frontend`: - `lib/oauth_client.rb` [1] - `spec/lib/oauth_client_spec.rb` [2] Some things that I've changed: - Moved into the `RpiAuth` namespace. - Use `RpiAuth.configuration.bypass_auth` instead of reading `BYPASS_AUTH` env var directly as per this PR [3] - Use `RpiAuth.configure` in the `before` block instead of using `ClimateControl.modify` in an `around` block. The former is possible, because the `RpiAuth` configuration is reset before every example [4]. [1]: https://github.com/RaspberryPiFoundation/code-club-frontend/blob/f7e965798d910584fed0d1eb7867f32a899f9ce8/lib/oauth_client.rb [2]: https://github.com/RaspberryPiFoundation/code-club-frontend/blob/main/spec/lib/oauth_client_spec.rb [3]: https://github.com/RaspberryPiFoundation/experience-cs/pull/315 [4]: https://github.com/RaspberryPiFoundation/rpi-auth/blob/e54cc7a30c61cf43874a38c79f1e2f3e2b6d2103/spec/spec_helper.rb#L132-L136
1 parent a6f5f06 commit 307c7e0

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

lib/rpi_auth/oauth_client.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require 'oauth2/client'
4+
require 'oauth2/access_token'
5+
6+
module RpiAuth
7+
class OauthClient
8+
attr_reader :client
9+
10+
def initialize
11+
@client = OAuth2::Client.new(
12+
RpiAuth.configuration.auth_client_id,
13+
RpiAuth.configuration.auth_client_secret,
14+
site: RpiAuth.configuration.auth_token_url,
15+
token_url: RpiAuth.configuration.token_endpoint.path,
16+
authorize_url: RpiAuth.configuration.authorization_endpoint.path
17+
)
18+
end
19+
20+
def refresh_credentials(**credentials)
21+
new_credentials = if RpiAuth.configuration.bypass_auth
22+
credentials.merge(expires_at: 1.hour.from_now)
23+
else
24+
OAuth2::AccessToken.from_hash(client, credentials).refresh.to_hash
25+
end
26+
27+
{
28+
access_token: new_credentials[:access_token],
29+
refresh_token: new_credentials[:refresh_token],
30+
expires_at: new_credentials[:expires_at].to_i
31+
}
32+
end
33+
end
34+
end

spec/rpi_auth/oauth_client_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'oauth2'
5+
6+
RSpec.describe RpiAuth::OauthClient do
7+
include ActiveSupport::Testing::TimeHelpers
8+
9+
describe '#refresh_credentials' do
10+
subject(:refresh_credentials) { oauth_client.refresh_credentials(**credentials) }
11+
12+
let(:oauth_client) { described_class.new }
13+
let(:credentials) { { access_token: 'foo', refresh_token: 'bar' } }
14+
let(:refresh_body) { { token: 'baz', refresh_token: 'quux', expires_in: rand(300..600) } }
15+
16+
before do
17+
RpiAuth.configure do |config|
18+
config.bypass_auth = bypass_auth
19+
config.auth_url = 'https://auth.com:123/'
20+
end
21+
22+
freeze_time
23+
end
24+
25+
after do
26+
unfreeze_time
27+
end
28+
29+
context 'when RpiAuth.configuration.bypass is not set' do
30+
let(:bypass_auth) { false }
31+
32+
before do
33+
stub_request(:post, RpiAuth.configuration.token_endpoint)
34+
.with(body: { grant_type: 'refresh_token', refresh_token: credentials[:refresh_token] })
35+
.to_return(status: 200, body: refresh_body.to_json, headers: { content_type: 'application/json' })
36+
end
37+
38+
it do
39+
expect(refresh_credentials).to eq({ access_token: refresh_body[:token],
40+
refresh_token: refresh_body[:refresh_token],
41+
expires_at: Time.now.to_i + refresh_body[:expires_in] })
42+
end
43+
end
44+
45+
context 'when RpiAuth.configuration.bypass is set' do
46+
let(:bypass_auth) { true }
47+
48+
it { expect(refresh_credentials).to eq credentials.merge(expires_at: 1.hour.from_now.to_i) }
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)