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
25 changes: 25 additions & 0 deletions lib/amqp/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,31 @@ defmodule AMQP.Connection do
end
end

@doc """
Updates the secret used to authenticate the given Connection.

This is useful when the credentials used to open the connection have a
limited lifetime, such as an OAuth 2 token, and need to be renewed before
they expire so the broker does not close the connection.

`new_secret` is the new secret and `reason` is a short human readable string
explaining why the secret is being updated.

## Examples

iex> {:ok, conn} = AMQP.Connection.open()
iex> AMQP.Connection.update_secret(conn, "new-token", "token refresh")
:ok

"""
@spec update_secret(t, String.t(), String.t()) :: :ok | {:error, any}
def update_secret(conn, new_secret, reason) do
case :amqp_connection.update_secret(conn.pid, new_secret, reason) do
:ok -> :ok
error -> {:error, error}
end
end

defp normalize_ssl_options(options) when is_list(options) do
for {k, v} <- options do
if k in [:cacertfile, :certfile, :keyfile] do
Expand Down
7 changes: 7 additions & 0 deletions test/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ defmodule ConnectionTest do
assert params[:host] == ~c"amqp.test.com"
end

test "update the secret of an open connection" do
{:ok, conn} = Connection.open()
assert :ok = Connection.update_secret(conn, "guest", "token refresh")
assert Process.alive?(conn.pid)
assert :ok = Connection.close(conn)
end

defp get_connection_name(conn) do
params = :amqp_connection.info(conn.pid, [:amqp_params])[:amqp_params]
amqp_params_network(client_properties: props) = params
Expand Down
Loading