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

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement