Skip to content

Commit f2386e4

Browse files
authored
Add specs to Plug.BasicAuth (#1181)
1 parent bbbb064 commit f2386e4

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lib/plug/basic_auth.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ defmodule Plug.BasicAuth do
9292
strings with only alphanumeric characters and space
9393
9494
"""
95-
def basic_auth(conn, options \\ []) do
95+
@spec basic_auth(Plug.Conn.t(), [auth_option]) :: Plug.Conn.t()
96+
when auth_option: {:username, String.t()} | {:password, String.t()} | {:realm, String.t()}
97+
def basic_auth(%Plug.Conn{} = conn, options \\ []) when is_list(options) do
9698
username = Keyword.fetch!(options, :username)
9799
password = Keyword.fetch!(options, :password)
98100

@@ -116,7 +118,8 @@ defmodule Plug.BasicAuth do
116118
117119
See the module docs for examples.
118120
"""
119-
def parse_basic_auth(conn) do
121+
@spec parse_basic_auth(Plug.Conn.t()) :: {user :: String.t(), password :: String.t()} | :error
122+
def parse_basic_auth(%Plug.Conn{} = conn) do
120123
with ["Basic " <> encoded_user_and_pass] <- get_req_header(conn, "authorization"),
121124
{:ok, decoded_user_and_pass} <- Base.decode64(encoded_user_and_pass),
122125
[user, pass] <- :binary.split(decoded_user_and_pass, ":") do
@@ -134,6 +137,7 @@ defmodule Plug.BasicAuth do
134137
put_req_header(conn, "authorization", encode_basic_auth("hello", "world"))
135138
136139
"""
140+
@spec encode_basic_auth(String.t(), String.t()) :: String.t()
137141
def encode_basic_auth(user, pass) when is_binary(user) and is_binary(pass) do
138142
"Basic " <> Base.encode64("#{user}:#{pass}")
139143
end
@@ -150,8 +154,11 @@ defmodule Plug.BasicAuth do
150154
* `:realm` - the authentication realm. The value is not fully
151155
sanitized, so do not accept user input as the realm and use
152156
strings with only alphanumeric characters and space
157+
153158
"""
154-
def request_basic_auth(conn, options \\ []) when is_list(options) do
159+
@spec request_basic_auth(Plug.Conn.t(), [option]) :: Plug.Conn.t()
160+
when option: {:realm, String.t()}
161+
def request_basic_auth(%Plug.Conn{} = conn, options \\ []) when is_list(options) do
155162
realm = Keyword.get(options, :realm, "Application")
156163
escaped_realm = String.replace(realm, "\"", "")
157164

0 commit comments

Comments
 (0)