type1fool

type1fool

How to resolve "csrf_attack" failure when testing a Ueberauth controller?

@yordisprieto Firstly, shout out to you and the other maintainers for Ueberauth! Implementing Google OAuth was quite easy thanks to this package.

I’m attempting to write tests for the callback in a Phoenix application, and I can’t get past a Ueberauth.Failure.Error. I have been digging through docs for Ueberauth, Plug.Conn, and Phoenix.Controller, and I didn’t see any tests in ueberauth_example.

When the server is running, I can successfully authenticate with Google, so I’m just blocked on the test. My conn/controller chops are a little rusty since I’m usually writing LiveView code, so I wouldn’t be surprised if there’s a non-Ueberauth-specific solution here. I tried manually setting _csrf_token in the conn session before calling get/2, but that didn’t work.

How does one put a CSRF token on a conn during testing?


Test code

defmodule MyAppWeb.AuthenticationTest do
  @moduledoc false
  alias MyApp.AuthenticationFixtures
  alias MyApp.Identity.User
  alias MyApp.IdentityFixtures
  use MyAppWeb.ConnCase, async: true
  use Plug.Test

  describe "callback/2" do
    test "starts session for valid user", %{conn: conn} do
      %User{email: email} = IdentityFixtures.user_fixture()
      auth = AuthenticationFixtures.auth_attrs(email: email)
      conn = get(conn, ~p"/auth/google/callback", ueberauth_auth: auth)
      assert redirected_to(conn, 302) == ~p"/"
    end
  end
end

Error

%Ueberauth.Failure{
  provider: :google,
  strategy: Ueberauth.Strategy.Google,
  errors: [
    %Ueberauth.Failure.Error{
      message_key: "csrf_attack",
      message: "Cross-Site Request Forgery attack"
    }
  ]
}

Most Liked

type1fool

type1fool

:+1: I opened #204 on ueberauth.

yordisprieto

yordisprieto

Honestly, by now, I wish to redo Ueberauth; you are not the only one with a skill issue :face_holding_back_tears: I need to dig more into it, I also remember that I did not decide to make the state generator be able to be deterministic (meaning swap the implementation in testing) to keep thing simple; I guess it is the time to add such thing.

I need to find the time to help you on this one, and probably, help the entire ueberauth ecosystem

Where Next?

Popular in Questions Top

tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35953 110
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement