ueberauth / guardian

Elixir Authentication
MIT License
3.4k stars 381 forks source link

Dialyzer auth_error #681

Closed David-Klemenc closed 2 years ago

David-Klemenc commented 2 years ago

Dialyzer is complaining about Plug.Conn structs not matching:

The inferred type for the 1st argument is not a supertype of the expected type for the auth_error/3

Success type:                                                                            |  Behaviour callback type:
%Plug.Conn{                                                                                 %Plug.Conn{
  :adapter => {atom(), _},                                                                    :adapter => {atom(), _},
  :assigns => %{atom() => _},                                                                 :assigns => %{atom() => _},
  :before_send => [(map() -> map())],                                                    <
  :body_params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _},                     :body_params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _},
  :cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _},                         :cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _},
  :halted => boolean(),                                                                       :halted => boolean(),
  :host => binary(),                                                                          :host => binary(),
  :method => binary(),                                                                        :method => binary(),
  :owner => pid(),                                                                            :owner => pid(),
  :params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _},                          :params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _},
  :path_info => [binary()],                                                                   :path_info => [binary()],
  :path_params => %{                                                                          :path_params => %{
    binary() =>                                                                                 binary() =>
      binary() | [binary() | [any()] | map()] | %{binary() => binary() | [any()] | map() |        binary()
                                                                                         >        | [binary() | [any()] | %{binary() => _}]
                                                                                         >        | %{binary() => binary() | [any()] | %{binary() => _}}
  },                                                                                          },
  :port => char(),                                                                            :port => char(),
  :private => %{atom() => _},                                                                 :private => %{atom() => _},
  :query_params => %Plug.Conn.Unfetched{                                                      :query_params => %Plug.Conn.Unfetched{
    :aspect => atom(),                                                                          :aspect => atom(),
    binary() =>                                                                                 binary() =>
      binary() | [binary() | [any()] | map()] | %{binary() => binary() | [any()] | map() |        binary()
                                                                                         >        | [binary() | [any()] | %{binary() => _}]
                                                                                         >        | %{binary() => binary() | [any()] | %{binary() => _}}
  },                                                                                          },
  :query_string => binary(),                                                                  :query_string => binary(),
  :remote_ip =>                                                                               :remote_ip =>
    {byte(), byte(), byte(), byte()}                                                            {byte(), byte(), byte(), byte()}
    | {char(), char(), char(), char(), char(), char(), char(), char()},                         | {char(), char(), char(), char(), char(), char(), char(), char()},
  :req_cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => binary()},              :req_cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => binary()},
  :req_headers => [{binary(), binary()}],                                                     :req_headers => [{binary(), binary()}],
  :request_path => binary(),                                                                  :request_path => binary(),
  :resp_body =>                                                                               :resp_body =>
    nil                                                                                         nil
    | binary()                                                                                  | binary()
    | maybe_improper_list(                                                                      | maybe_improper_list(
        binary() | maybe_improper_list(any(), binary() | []) | byte(),                              binary() | maybe_improper_list(any(), binary() | []) | byte(),
        binary() | []                                                                               binary() | []
      ),                                                                                          ),
  :resp_cookies => %{binary() => %{}},                                                   |    :resp_cookies => %{binary() => map()},
  :resp_headers => [{binary(), binary()}],                                                    :resp_headers => [{binary(), binary()}],
  :scheme => :http | :https,                                                                  :scheme => :http | :https,
  :script_name => [binary()],                                                                 :script_name => [binary()],
  :secret_key_base => nil | binary(),                                                         :secret_key_base => nil | binary(),
  :state => :chunked | :file | :sent | :set | :set_chunked | :set_file | :unset,              :state => :chunked | :file | :sent | :set | :set_chunked | :set_file | :unset,
  :status => nil | non_neg_integer()                                                          :status => nil | non_neg_integer()
}                                                                                           }

Could this be a consequence of mismatching plug dependencies?

If somebody stumbles onto this and needs to quickly get CI working -> ignore the error:

defmodule MyApp.Utilities.GuardianErrorHandler do
  import Plug.Conn
  @behaviour Guardian.Plug.ErrorHandler
  @dialyzer {:nowarn_function, auth_error: 3} # ignore dialyzer error
  @impl Guardian.Plug.ErrorHandler

  def auth_error(conn, {type, _reason}, _opts) do
    body = %{invalid_token: to_string(type)}

    conn
    |> put_resp_content_type("application/json")
    |> send_resp(401, Jason.encode!(%{error: body}))
  end
end
David-Klemenc commented 2 years ago

If anybody stumbles upon this it was related to this issue: plug - 1042

Turns out all I had to do was:

mix dialyzer.clean