Skip to content

Commit 807aeb0

Browse files
authored
Merge pull request #61 from github/itoys/multi-base-urls
Allows to set multiple base auth urls
2 parents 3332257 + e60af16 commit 807aeb0

6 files changed

Lines changed: 25 additions & 21 deletions

File tree

Gemfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
source 'https://rubygems.org'
2-
3-
gem 'rails'
1+
source "https://rubygems.org"
42

53
group :development, :test do
4+
gem "rails", "~> 6"
65
gem "rspec-rails", "~> 3"
76
gem "pry", "~> 0"
87
end

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,12 @@ two environment variables to use this protocol:
114114
format. This environment variable will be the contents of a `.pub` file,
115115
newlines and all.
116116

117-
`CHATOPS_AUTH_BASE_URL` is the base URL of your server as the chatops client
117+
`CHATOPS_AUTH_BASE_URL` is the base URLs of your servers as the chatops client
118118
sees it. This is specified as an environment variable since rails will trust
119119
client headers about a forwarded hostname. For example, if your chatops client
120120
has added the url `https://example.com/_chatops`, you'd set this to
121-
`https://example.com`.
121+
`https://example.com`. You can specify more than one base url divided by comma,
122+
e.g. `https://example.com,https://example2.com`
122123

123124
You can also optionally set `CHATOPS_AUTH_ALT_PUBLIC_KEY` to a second public key
124125
which will be accepted. This is helpful when rolling keys.

chatops-controller.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
1717
s.files = Dir["{app,config,db,lib}/**/*", "README.md"]
1818
s.test_files = Dir["spec/**/*"]
1919

20+
s.add_dependency "rails"
2021
s.add_dependency "actionpack", ">= 6.0"
2122
s.add_dependency "activesupport", ">= 6.0"
2223

lib/chatops.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def self.alt_public_key
2323
ENV["CHATOPS_AUTH_ALT_PUBLIC_KEY"]
2424
end
2525

26-
def self.auth_base_url
27-
ENV[auth_base_url_env_var_name]
26+
def self.auth_base_urls
27+
ENV.fetch(auth_base_url_env_var_name, "").split(",").map(&:strip)
2828
end
2929

3030
def self.auth_base_url_env_var_name

lib/chatops/controller.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,29 @@ def ensure_user_given
118118
end
119119

120120
def ensure_chatops_authenticated
121-
body = request.raw_post || ""
122-
signature_string = [@chatops_url, @chatops_nonce, @chatops_timestamp, body].join("\n")
123-
# We return this just to aid client debugging.
124-
response.headers["Chatops-Signature-String"] = Base64.strict_encode64(signature_string)
125121
raise ConfigurationError.new("You need to add a client's public key in .pem format via #{Chatops.public_key_env_var_name}") unless Chatops.public_key.present?
126-
if signature_valid?(Chatops.public_key, @chatops_signature, signature_string) ||
127-
signature_valid?(Chatops.alt_public_key, @chatops_signature, signature_string)
122+
123+
body = request.raw_post || ""
124+
125+
@chatops_urls.each do |url|
126+
signature_string = [url, @chatops_nonce, @chatops_timestamp, body].join("\n")
127+
# We return this just to aid client debugging.
128+
response.headers["Chatops-Signature-String"] = Base64.strict_encode64(signature_string)
129+
if signature_valid?(Chatops.public_key, @chatops_signature, signature_string) ||
130+
signature_valid?(Chatops.alt_public_key, @chatops_signature, signature_string)
128131
return true
132+
end
129133
end
134+
130135
return jsonrpc_error(-32800, 403, "Not authorized")
131136
end
132137

133138
def ensure_valid_chatops_url
134-
unless Chatops.auth_base_url.present?
139+
unless Chatops.auth_base_urls.present?
135140
raise ConfigurationError.new("You need to set the server's base URL to authenticate chatops RPC via #{Chatops.auth_base_url_env_var_name}")
136141
end
137-
if Chatops.auth_base_url[-1] == "/"
138-
raise ConfigurationError.new("Don't include a trailing slash in #{Chatops.auth_base_url_env_var_name}; the rails path will be appended and it must match exactly.")
139-
end
140-
@chatops_url = Chatops.auth_base_url + request.path
142+
143+
@chatops_urls = Chatops.auth_base_urls.map { |url| url.chomp("/") + request.path }
141144
end
142145

143146
def ensure_valid_chatops_nonce

spec/lib/chatops/controller_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def ensure_app_given
5656

5757
@private_key = OpenSSL::PKey::RSA.new(2048)
5858
ENV["CHATOPS_AUTH_PUBLIC_KEY"] = @private_key.public_key.to_pem
59-
ENV["CHATOPS_AUTH_BASE_URL"] = "http://test.host"
59+
ENV["CHATOPS_AUTH_BASE_URL"] = "http://old.host,http://test.host/"
6060
end
6161

6262
def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
@@ -315,15 +315,15 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
315315
:room_id => "#someroom",
316316
:unknown_key => "few" # This should get ignored
317317
}, {
318-
"app" => "foo"
318+
"app" => "foo"
319319
}
320320
expect(json_response).to eq({
321321
"jsonrpc" => "2.0",
322322
"id" => nil,
323323
"result" => "{\"params\":{\"action\":\"proxy_parameters\",\"chatop\":\"proxy_parameters\",\"controller\":\"anonymous\",\"mention_slug\":\"mention_slug_here\",\"message_id\":\"message_id_here\",\"room_id\":\"#someroom\",\"user\":\"foo\"},\"jsonrpc_params\":{\"app\":\"foo\"}}"
324324
})
325325
expect(response.status).to eq 200
326-
end
326+
end
327327

328328

329329
it "uses typical controller fun like before_action" do

0 commit comments

Comments
 (0)