-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
273 additions
and
11 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
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 |
---|---|---|
@@ -1,12 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'base64' | ||
|
||
module Hearth | ||
module Signers | ||
# A signer that signs requests using the HTTP Basic Auth scheme. | ||
class HTTPBasic < Signers::Base | ||
def sign(request:, identity:, properties: {}) | ||
# TODO | ||
# rubocop:disable Lint/UnusedMethodArgument | ||
def sign(request:, identity:, properties:) | ||
# TODO: does not handle realm or other properties | ||
identity_string = "#{identity.username}:#{identity.password}" | ||
encoded = Base64.strict_encode64(identity_string) | ||
request.headers['Authorization'] = "Basic #{encoded}" | ||
end | ||
# rubocop:enable Lint/UnusedMethodArgument | ||
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
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,103 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hearth | ||
module Middleware | ||
describe Build do | ||
let(:app) { double('app', call: output) } | ||
|
||
subject { Sign.new(app) } | ||
|
||
describe '#call' do | ||
let(:input) { double('input') } | ||
let(:output) { double('output') } | ||
let(:request) { double('request') } | ||
let(:response) { double('response') } | ||
let(:interceptors) { double('interceptors', apply: nil) } | ||
let(:context) do | ||
Context.new( | ||
request: request, | ||
response: response, | ||
interceptors: interceptors | ||
) | ||
end | ||
|
||
let(:signer) { double('signer', sign: nil) } | ||
let(:identity) { double('identity') } | ||
let(:auth_option) { double('auth_option', signer_properties: {}) } | ||
let(:auth_scheme) do | ||
double( | ||
'auth_scheme', | ||
signer: signer, | ||
identity: identity, | ||
auth_option: auth_option | ||
) | ||
end | ||
|
||
before { context.auth_scheme = auth_scheme } | ||
|
||
it 'signs then calls the next middleware' do | ||
expect(signer).to receive(:sign) | ||
.with(request: request, identity: identity, properties: {}) | ||
.ordered | ||
expect(app).to receive(:call).with(input, context).ordered | ||
|
||
resp = subject.call(input, context) | ||
expect(resp).to be output | ||
end | ||
|
||
it 'calls before_signing interceptors before sign' do | ||
expect(interceptors).to receive(:apply) | ||
.with(hash_including( | ||
hook: Interceptor::Hooks::MODIFY_BEFORE_SIGNING | ||
)).ordered | ||
expect(interceptors).to receive(:apply) | ||
.with(hash_including( | ||
hook: Interceptor::Hooks::READ_BEFORE_SIGNING | ||
)).ordered | ||
expect(signer).to receive(:sign).ordered | ||
expect(app).to receive(:call).ordered | ||
|
||
subject.call(input, context) | ||
end | ||
|
||
context 'modify_before_signing error' do | ||
let(:error) { StandardError.new } | ||
|
||
it 'returns output with the error and skips signing' do | ||
expect(interceptors).to receive(:apply) | ||
.with(hash_including( | ||
hook: Interceptor::Hooks::MODIFY_BEFORE_SIGNING | ||
)) | ||
.and_return(error) | ||
|
||
expect(signer).not_to receive(:sign) | ||
expect(app).not_to receive(:call) | ||
|
||
resp = subject.call(input, context) | ||
|
||
expect(resp.error).to eq(error) | ||
end | ||
end | ||
|
||
context 'read_before_signing error' do | ||
let(:error) { StandardError.new } | ||
|
||
it 'returns output with the error and skips signing' do | ||
expect(interceptors).to receive(:apply) | ||
.with(hash_including( | ||
hook: Interceptor::Hooks::READ_BEFORE_SIGNING | ||
)) | ||
.and_return(error) | ||
|
||
expect(signer).not_to receive(:sign) | ||
expect(app).not_to receive(:call) | ||
|
||
resp = subject.call(input, context) | ||
|
||
expect(resp.error).to eq(error) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hearth | ||
module Signers | ||
describe HTTPApiKey do | ||
let(:request) { HTTP::Request.new } | ||
let(:identity) { Identities::HTTPApiKey.new(key: key) } | ||
let(:key) { 'key' } | ||
|
||
context 'header' do | ||
let(:properties) do | ||
{ name: 'X-Api-Key', in: 'header', scheme: 'OAuth' } | ||
end | ||
|
||
it 'signs the request' do | ||
expect(request.headers['x-api-key']).to be_nil | ||
subject.sign( | ||
request: request, | ||
identity: identity, | ||
properties: properties | ||
) | ||
expect(request.headers['x-api-key']).to eq("OAuth #{key}") | ||
end | ||
end | ||
|
||
context 'query' do | ||
let(:properties) do | ||
{ name: 'api_key', in: 'query' } | ||
end | ||
|
||
it 'signs the request' do | ||
expect(request.uri.query).to be_nil | ||
subject.sign( | ||
request: request, | ||
identity: identity, | ||
properties: properties | ||
) | ||
expect(request.uri.query).to eq("api_key=#{key}") | ||
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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hearth | ||
module Signers | ||
describe HTTPBasic do | ||
let(:request) { HTTP::Request.new } | ||
let(:identity) do | ||
Identities::HTTPLogin.new(username: username, password: password) | ||
end | ||
let(:username) { 'username' } | ||
let(:password) { 'password' } | ||
let(:properties) { {} } | ||
|
||
it 'signs the request' do | ||
expect(request.headers['authorization']).to be_nil | ||
subject.sign( | ||
request: request, | ||
identity: identity, | ||
properties: properties | ||
) | ||
encoded = Base64.strict_encode64("#{username}:#{password}") | ||
expect(request.headers['authorization']).to eq("Basic #{encoded}") | ||
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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hearth | ||
module Signers | ||
describe HTTPBearer do | ||
let(:request) { HTTP::Request.new } | ||
let(:identity) { Identities::HTTPBearer.new(token: token) } | ||
let(:token) { 'token' } | ||
let(:properties) { {} } | ||
|
||
it 'signs the request' do | ||
expect(request.headers['authorization']).to be_nil | ||
subject.sign( | ||
request: request, | ||
identity: identity, | ||
properties: properties | ||
) | ||
expect(request.headers['authorization']).to eq("Bearer #{token}") | ||
end | ||
end | ||
end | ||
end |