Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ApplicationController < ActionController::Base
private

def verify_agent!
@current_agent ||= Vestauth.provider.verify(http_method: request.method, uri: request.original_url, headers: request.headers)
@current_agent ||= Vestauth.tool.verify(http_method: request.method, uri: request.original_url, headers: request.headers)
rescue => e
render json: { error: { status: 401, code: 401, message: e.message } }, status: 401
end
Expand Down
9 changes: 7 additions & 2 deletions lib/vestauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
require_relative "vestauth/version"
require_relative "vestauth/agent"
require_relative "vestauth/binary"
require_relative "vestauth/tool"
require_relative "vestauth/provider"

module Vestauth
class Error < StandardError; end

def self.provider
Provider
def self.tool
Tool
end

class << self
alias provider tool
end

Comment on lines 12 to 19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alias provider tool keeps backward compatibility, but the current placement (class << self block separated from the def self.tool) is a bit indirect and can be confusing to maintain. Since this is a module with singleton methods, you can define the alias directly on the singleton class in a tighter, more idiomatic way, and ideally add a short deprecation note so callers know to migrate.

Also, if this is intended as a rename rather than a permanent dual API, consider emitting a deprecation warning from provider to drive adoption (while keeping it non-breaking).

Suggestion

Consider restructuring to make the relationship explicit and optionally deprecate the old name:

module Vestauth
  def self.tool
    Provider
  end

  class << self
    def provider
      warn "Vestauth.provider is deprecated; use Vestauth.tool" if $VERBOSE
      tool
    end
  end
end

This keeps compatibility while encouraging migration. Reply with "@CharlieHelps yes please" if you'd like me to add a commit with this suggestion.

def self.agent
Expand Down
4 changes: 2 additions & 2 deletions lib/vestauth/binary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def initialize(executable: "vestauth")
@executable = executable
end

def provider_verify(http_method:, uri:, signature:, signature_input:, signature_agent:)
def tool_verify(http_method:, uri:, signature:, signature_input:, signature_agent:)
command = [
@executable,
"provider",
"tool",
"verify",
http_method,
uri,
Expand Down
41 changes: 3 additions & 38 deletions lib/vestauth/provider.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,7 @@
# frozen_string_literal: true

module Vestauth
module Provider
module_function

def verify(http_method:, uri:, headers:)
signature = signature_header(headers)
signature_input = signature_input_header(headers)
signature_agent = signature_agent_header(headers)

attrs = {
http_method: http_method,
uri: uri,
signature: signature,
signature_input: signature_input,
signature_agent: signature_agent
}
vestauth_binary.provider_verify(**attrs)
end

def vestauth_binary
Vestauth::Binary.new
end
private_class_method :vestauth_binary
require_relative "tool"

def signature_header(headers)
headers["Signature"] || headers["signature"]
end
private_class_method :signature_header

def signature_input_header(headers)
headers["Signature-Input"] || headers["signature-input"]
end
private_class_method :signature_input_header

def signature_agent_header(headers)
headers["Signature-Agent"] || headers["signature-agent"]
end
private_class_method :signature_agent_header
end
module Vestauth
Provider = Tool
end
42 changes: 42 additions & 0 deletions lib/vestauth/tool.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Vestauth
module Tool
module_function

def verify(http_method:, uri:, headers:)
signature = signature_header(headers)
signature_input = signature_input_header(headers)
signature_agent = signature_agent_header(headers)

attrs = {
http_method: http_method,
uri: uri,
signature: signature,
signature_input: signature_input,
signature_agent: signature_agent
}
vestauth_binary.tool_verify(**attrs)
end

def vestauth_binary
Vestauth::Binary.new
end
private_class_method :vestauth_binary

def signature_header(headers)
headers["Signature"] || headers["signature"]
end
private_class_method :signature_header

def signature_input_header(headers)
headers["Signature-Input"] || headers["signature-input"]
end
private_class_method :signature_input_header

def signature_agent_header(headers)
headers["Signature-Agent"] || headers["signature-agent"]
end
private_class_method :signature_agent_header
end
end
10 changes: 5 additions & 5 deletions spec/binary_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# frozen_string_literal: true

RSpec.describe Vestauth::Binary do
describe "#provider_verify" do
it "calls vestauth provider verify and parses json output" do
describe "#tool_verify" do
it "calls vestauth tool verify and parses json output" do
status = instance_double(Process::Status, success?: true)
binary = described_class.new

expect(Open3).to receive(:capture3).with(
include("vestauth provider verify GET https://api.vestauth.com/whoami")
include("vestauth tool verify GET https://api.vestauth.com/whoami")
).and_return(['{"uid":"agent-123"}', "", status])

result = binary.provider_verify(
result = binary.tool_verify(
http_method: "GET",
uri: "https://api.vestauth.com/whoami",
signature: "sig1=:abc:",
Expand All @@ -28,7 +28,7 @@
allow(Open3).to receive(:capture3).and_return(["", "bad signature", status])

expect do
binary.provider_verify(
binary.tool_verify(
http_method: "GET",
uri: "https://api.vestauth.com/whoami",
signature: "sig1=:abc:",
Expand Down
20 changes: 11 additions & 9 deletions spec/vestauth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
expect(Vestauth::VERSION).not_to be nil
end

it "exposes namespaced provider and agent modules" do
expect(Vestauth.provider).to eq(Vestauth::Provider)
it "exposes namespaced tool/provider and agent modules" do
expect(Vestauth::Tool).to eq(Vestauth::Provider)
expect(Vestauth.tool).to eq(Vestauth::Tool)
expect(Vestauth.provider).to eq(Vestauth::Tool)
expect(Vestauth.agent).to eq(Vestauth::Agent)
expect(Vestauth.binary).to eq(Vestauth::Binary)
end

it "delegates provider verify to binary provider_verify" do
it "delegates tool verify to binary tool_verify" do
binary = instance_double(Vestauth::Binary)
allow(Vestauth::Binary).to receive(:new).and_return(binary)
allow(binary).to receive(:provider_verify).and_return({ "uid" => "agent-123" })
allow(binary).to receive(:tool_verify).and_return({ "uid" => "agent-123" })

result = Vestauth.provider.verify(
result = Vestauth.tool.verify(
http_method: "GET",
uri: "https://api.vestauth.com/whoami",
headers: {
Expand All @@ -26,7 +28,7 @@
}
)

expect(binary).to have_received(:provider_verify).with(
expect(binary).to have_received(:tool_verify).with(
http_method: "GET",
uri: "https://api.vestauth.com/whoami",
signature: "sig1=:abc:",
Expand All @@ -39,15 +41,15 @@
it "passes through missing headers and lets binary verify fail if needed" do
binary = instance_double(Vestauth::Binary)
allow(Vestauth::Binary).to receive(:new).and_return(binary)
allow(binary).to receive(:provider_verify).and_return({ "success" => false })
allow(binary).to receive(:tool_verify).and_return({ "success" => false })

Vestauth.provider.verify(
Vestauth.tool.verify(
http_method: "GET",
uri: "https://api.vestauth.com/whoami",
headers: {}
)

expect(binary).to have_received(:provider_verify).with(
expect(binary).to have_received(:tool_verify).with(
http_method: "GET",
uri: "https://api.vestauth.com/whoami",
signature: nil,
Expand Down