<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Federico Meini</title>
    <description>The latest articles on DEV Community by Federico Meini (@fedme).</description>
    <link>https://dev.to/fedme</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F684274%2F3640df45-97e4-4d72-a214-e4e70d3dc0db.png</url>
      <title>DEV Community: Federico Meini</title>
      <link>https://dev.to/fedme</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fedme"/>
    <language>en</language>
    <item>
      <title>Elixir Überauth: how to keep state between request and callback?</title>
      <dc:creator>Federico Meini</dc:creator>
      <pubDate>Wed, 11 Aug 2021 13:00:01 +0000</pubDate>
      <link>https://dev.to/fedme/uberauth-how-to-keep-state-between-request-and-callback-1hb7</link>
      <guid>https://dev.to/fedme/uberauth-how-to-keep-state-between-request-and-callback-1hb7</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/ueberauth/ueberauth"&gt;Überauth&lt;/a&gt; is probably the go-to OAuth login extension for Elixir projects. &lt;/p&gt;

&lt;p&gt;The team behind the library has recently improved its security against &lt;a href="https://github.com/ueberauth/ueberauth_google/pull/82"&gt;CSRF attacks&lt;/a&gt;. Unfortunately, the security improvement comes at a cost for the end user, as it is now impossible to keep state between the &lt;em&gt;request&lt;/em&gt; and &lt;em&gt;callback&lt;/em&gt; phases of the OAuth process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Most OAuth providers (e.g. &lt;em&gt;Google&lt;/em&gt;) allow developers to pass custom state inside a &lt;code&gt;state&lt;/code&gt; query parameter as part od the &lt;em&gt;request&lt;/em&gt; URL. The OAuth provider then passes the state back when calling our &lt;em&gt;callback&lt;/em&gt; endpoint.&lt;/p&gt;

&lt;p&gt;Überauth now uses the &lt;code&gt;state&lt;/code&gt; query parameter to pass the CSRF token, overwriting whatever custom state developers put in the request URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Workaround
&lt;/h2&gt;

&lt;p&gt;Luckily, there is another way. We can put our custom state in a session cookie in the &lt;a href="https://github.com/ueberauth/ueberauth#request-phase"&gt;request phase&lt;/a&gt; and retrieve it from the session in the &lt;a href="https://github.com/ueberauth/ueberauth#callback-phase"&gt;callback phase&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code example
&lt;/h3&gt;

&lt;p&gt;The following code snippet shows how to save some custom state in the session cookie and retrieve in the &lt;em&gt;callback&lt;/em&gt; phase of the OAuth flow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;MyAppWeb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;AuthController&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="no"&gt;MyAppWeb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:controller&lt;/span&gt;

  &lt;span class="n"&gt;plug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Ueberauth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;providers:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:google_custom&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

  &lt;span class="nv"&gt;@provider_config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="no"&gt;Ueberauth&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Strategy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Google&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;default_scope:&lt;/span&gt; &lt;span class="s2"&gt;"email profile"&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;%{&lt;/span&gt;&lt;span class="s2"&gt;"provider"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"google"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"custom_state"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;custom_state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="c1"&gt;# Store custom state in the session&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;put_session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:auth_custom_state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;custom_state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Ueberauth&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run_request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"google"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;@provider_config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="p"&gt;%{&lt;/span&gt;&lt;span class="ss"&gt;assigns:&lt;/span&gt; &lt;span class="p"&gt;%{&lt;/span&gt;&lt;span class="ss"&gt;ueberauth_auth:&lt;/span&gt; &lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;conn&lt;/span&gt;
      &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Ueberauth&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run_callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"google"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;@provider_config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Get custom state back from session&lt;/span&gt;
    &lt;span class="n"&gt;auth_custom_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_session&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:auth_custom_state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="no"&gt;IO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inspect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auth_custom_state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;label:&lt;/span&gt; &lt;span class="s2"&gt;"Auth custom state"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that code, I am able to start the OAuth flow passing some custom state in the URL (e.g. &lt;code&gt;https://localhost:4000/auth/google?custom_state=some_values_here&lt;/code&gt;) and then take it back from the session in the &lt;code&gt;callback&lt;/code&gt; function.&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>ueberauth</category>
      <category>oauth</category>
      <category>authentication</category>
    </item>
  </channel>
</rss>
