GithubHelp home page GithubHelp logo

skylerparr / fire_auth Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fritzflorian/fire_auth

0.0 2.0 1.0 26 KB

Firebase Authentication for Plug

License: Apache License 2.0

Elixir 100.00%

fire_auth's Introduction

FireAuth

Server side verification of firebase authentication using ID tokens.

In short this library will allow you to use firebase auth as an authentication method for your backend. The client app/webapp can generate an token that is then sent with each request to your backend to identifiy users.

Client Side

On the client side you will have to generate an ID token. See the firebase doc for detailed instructions.

After you got the token simply include it as a header in each request:

Authorization : Bearer <put-your-token-here>

Server Side

Add the library to your mix file.

{:fire_auth, "~> 0.1.0"}

Add your firebase project id to your config.

config :fire_auth,
  project_id: "project-id"

Add the following plug to validate the id token.

plug FireAuth

This will validate the id token in the request header and put information about it into conn.assigns.fire_auth.info.

To use the library for full authentication (load a user form the DB) use this plug.

plug FireAuth, [load_user: &load_user/1, load_groups: &load_groups/2]

Where &load_user/1 and &load_groups/2 are used to load the user model from your database and to extract the users groups out of the loaded user model.

For example this would be typical implementations.

def load_user(%{id: firebase_id} = _info) do
	# Ideally do an insert or update here
	Repo.get_by(User, firebase_id: firebase_id)
end

def load_groups(user, _info) do
	user.groups
end

With this set you can secure individual routes using the FireAuth.Secure plug.

# can only be accesed if the request contains an valid token
plug FireAuth.Secure

# only secure the :index action (in a phoenix project) 
plug FireAuth.Secure when action in [:index]

# can only be accessed by users with the required_group
plug FireAuth.Secure, group: "required_group"
   

It is a very simple system, but works very well for most smaller projects and is especially nice nice to get started fast.

Testing Secure Routes

To test secured routes you can set the :fire_auth_user assign in your connection before it enters the router. This will ignore the header and use this user instead.

For example you can add an setup like the following.

setup %{conn: conn} = config do
  if config[:login] do
    groups = config[:groups] || []
    user = Factory.insert(:user, groups: groups)
    conn = Plug.Conn.assign(conn, :fire_auth_user, user)
    {:ok, conn: conn, user: user}
  else
    {:ok, conn: conn}
  end
end

Then use it in your tests like this.

@tag :login
test "some test", %{conn: conn, user: user} do
  # The conn is authenticated with the user
end

fire_auth's People

Contributors

fritzflorian avatar skylerparr avatar

Watchers

James Cloos avatar  avatar

Forkers

qixxit

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.