All Projects → cconstantin → plug_rails_cookie_session_store

cconstantin / plug_rails_cookie_session_store

Licence: MIT License
Rails compatible Plug session store

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to plug rails cookie session store

Logster
Easily parsable single line, plain text and JSON logger for Plug and Phoenix applications
Stars: ✭ 171 (+83.87%)
Mutual labels:  phoenix, plug
live dj
💿 Join or create video playlists to share a real-time experience with others! 🎧
Stars: ✭ 19 (-79.57%)
Mutual labels:  phoenix, elixir-phoenix
Appsignal Elixir
🟪 AppSignal for Elixir package
Stars: ✭ 176 (+89.25%)
Mutual labels:  phoenix, plug
Ecto morph
morph your Ecto capabilities into the s t r a t o s p h e r e !
Stars: ✭ 72 (-22.58%)
Mutual labels:  phoenix, elixir-phoenix
guardian trackable
A Guardian hook to track user sign ins.
Stars: ✭ 25 (-73.12%)
Mutual labels:  phoenix, plug
Authex
Authex is an opinionated JWT authentication and authorization library for Elixir.
Stars: ✭ 73 (-21.51%)
Mutual labels:  phoenix, plug
versionary
Plug for API versioning
Stars: ✭ 40 (-56.99%)
Mutual labels:  elixir-phoenix, elixir-plug
Elixirbooks
List of Elixir books
Stars: ✭ 1,021 (+997.85%)
Mutual labels:  phoenix, elixir-phoenix
plug rest
REST behaviour and Plug router for hypermedia web applications in Elixir
Stars: ✭ 52 (-44.09%)
Mutual labels:  phoenix, plug
phoenix-client-ssl
Set of Plugs / Lib to help with SSL Client Auth.
Stars: ✭ 18 (-80.65%)
Mutual labels:  phoenix, plug
Shorten api tutorial
🔗How to make a link shortener using Elixir, Phoenix and Mnesia
Stars: ✭ 60 (-35.48%)
Mutual labels:  phoenix, elixir-phoenix
pryin
PryIn is an Application Performance Monitoring platform for your Elixir/Phoenix application.
Stars: ✭ 25 (-73.12%)
Mutual labels:  phoenix, elixir-phoenix
Phoenix In Action
Code snippets and examples from the book Phoenix in Action from Manning and Geoffrey Lessel
Stars: ✭ 60 (-35.48%)
Mutual labels:  phoenix, elixir-phoenix
Veil
Simple passwordless authentication for your Phoenix apps
Stars: ✭ 153 (+64.52%)
Mutual labels:  phoenix, elixir-phoenix
Phoenix Ecto Append Only Log Example
📝 A step-by-step example/tutorial showing how to build a Phoenix (Elixir) App where all data is immutable (append only). Precursor to Blockchain, IPFS or Solid!
Stars: ✭ 58 (-37.63%)
Mutual labels:  phoenix, elixir-phoenix
Absinthe plug
Plug support for Absinthe, the GraphQL toolkit for Elixir
Stars: ✭ 209 (+124.73%)
Mutual labels:  phoenix, plug
Drab
Remote controlled frontend framework for Phoenix.
Stars: ✭ 833 (+795.7%)
Mutual labels:  phoenix, elixir-phoenix
Liberator
An Elixir library for building RESTful applications.
Stars: ✭ 28 (-69.89%)
Mutual labels:  phoenix, plug
alternate
Plug and Phoenix helpers to localize your web app via the URL
Stars: ✭ 26 (-72.04%)
Mutual labels:  phoenix, plug
mindwendel
Create a challenge. Ready? Brainstorm. mindwendel helps you to easily brainstorm and upvote ideas and thoughts within your team.
Stars: ✭ 22 (-76.34%)
Mutual labels:  phoenix, elixir-phoenix

PlugRailsCookieSessionStore

Rails compatible Plug session store.

This allows you to share session information between Rails and a Plug-based framework like Phoenix.

Version Information

Version 2.0 and higher require OTP 22 or higher.

Installation

Add PlugRailsCookieSessionStore as a dependency to your mix.exs file:

def deps do
  [{:plug_rails_cookie_session_store, "~> 2.0"}]
end

How to use with Phoenix

Copy/share the encryption information from Rails to Phoenix.

There are 4 things to copy:

  • secret_key_base
  • signing_salt
  • encryption_salt
  • session_key

Since Rails 5.2, secret_key_base in test and development is derived as a MD5 hash of the application's name. To fetch key value you can run:

Rails.application.secret_key_base

https://www.rubydoc.info/github/rails/rails/Rails%2FApplication:secret_key_base

The secret_key_base should be copied to Phoenix's config.exs file. There should already be a key named like that and you should override it.

The other three values can be found somewhere in the initializers directory of your Rails project. Some people don't set the signing_salt and encryption_salt. If you don't find them, set them like so:

Rails.application.config.session_store :cookie_store, key: '_SOMETHING_HERE_session'
Rails.application.config.action_dispatch.encrypted_cookie_salt =  'encryption salt'
Rails.application.config.action_dispatch.encrypted_signed_cookie_salt = 'signing salt'

Configure the Cookie Store in Phoenix.

Edit the endpoint.ex file and add the following:

# ...
plug Plug.Session,
  store: PlugRailsCookieSessionStore,
  key: "_SOMETHING_HERE_session",
  domain: '.myapp.com',
  secure: true,
  signing_with_salt: true,
  signing_salt: "signing salt",
  encrypt: true,
  encryption_salt: "encryption salt",
  key_iterations: 1000,
  key_length: 64,
  key_digest: :sha,
  serializer: Poison # see serializer details below
end

Set up a serializer

Plug & Rails must use the same strategy for serializing cookie data.

  • JSON: Since 4.1, Rails defaults to serializing cookie data with JSON. Support this strategy by getting a JSON serializer and passing it to Plug.Session. For example, add Poison to your dependencies, then:

    plug Plug.Session,
      store: PlugRailsCookieSessionStore,
      # ... see encryption config above
      serializer: Poison
    end

    You can confirm that your app uses JSON by searching for

    Rails.application.config.action_dispatch.cookies_serializer = :json

    in an initializer.

  • Marshal: Previous to 4.1, Rails defaulted to Ruby's Marshal library for serializing cookie data. You can deserialize this by adding ExMarshal to your project and defining a serializer module:

    defmodule RailsMarshalSessionSerializer do
      @moduledoc """
      Share a session with a Rails app using Ruby's Marshal format.
      """
      def encode(value) do
        {:ok, ExMarshal.encode(value)}
      end
    
      def decode(value) do
        {:ok, ExMarshal.decode(value)}
      end
    end

    Then, pass that module as a serializer to Plug.Session:

    plug Plug.Session,
      store: PlugRailsCookieSessionStore,
      # ... see encryption config above
      serializer: RailsMarshalSessionSerializer
    end
  • Rails 3.2: Rails 3.2 uses unsalted signing, to make Phoenix share session with Rails 3.2 project you need to set up ExMarshal mentioned above, with following configuration in your Plug.Session:

    plug Plug.Session,
      store: PlugRailsCookieSessionStore,
      # ... see encryption/ExMarshal config above
      signing_with_salt: false,
    end

That's it!

To test it, set a session value in your Rails application:

session[:foo] = 'bar'

And print it on Phoenix in whatever Controller you want:

Logger.debug get_session(conn, "foo")
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].