Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added supported for "namespaced" relay ids #1052

Merged
merged 1 commit into from Oct 17, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/cog/bus_enforcer.ex
Expand Up @@ -28,7 +28,8 @@ defmodule Cog.BusEnforcer do
{_, :undefined} ->
false
_ ->
case Repo.one(Queries.Relay.for_id(username)) do
id = extract_id_from_name(username)
case Repo.one(Queries.Relay.for_id(id)) do
nil ->
addr = format_address(mqtt_client(client, :peername))
Logger.info("Denied connect attempt for unknown client #{username} from #{addr}")
Expand All @@ -52,18 +53,34 @@ defmodule Cog.BusEnforcer do
:undefined ->
false
username ->
case Repo.exists?(Relay, username) do
id = extract_id_from_name(username)
case Repo.exists?(Relay, id) do
false ->
false
true ->
relays_prefix = "bot/relays/#{username}"
commands_prefix = "/bot/commands/#{username}"
relays_prefix = "bot/relays/#{id}"
commands_prefix = "/bot/commands/#{id}"
String.starts_with?(topic, relays_prefix) or
String.starts_with?(topic, commands_prefix)
end
end
end

# Extracts the id part of a MQTT username
# Valid ID formats are:
# * <UUID>
# * <UUID>/<name>
# Cog authenticates IDs only so we need to
# extract just the ID here.
defp extract_id_from_name(username) do
case String.split(username, "/", parts: 2) do
[id] ->
id
[id, _] ->
id
end
end

defp format_address({addr, _}) do
addr = Tuple.to_list(addr)
if length(addr) == 4 do
Expand Down