All Projects → ueberauth → Guardian_db

ueberauth / Guardian_db

Licence: mit
Guardian DB integration for tracking tokens and ensuring logout cannot be replayed.

Programming Languages

elixir
2628 projects

Labels

Projects that are alternatives of or similar to Guardian db

Bb8
Full-featured async (tokio-based) postgres connection pool (like r2d2)
Stars: ✭ 287 (-6.51%)
Mutual labels:  database
Recdb Postgresql
RecDB is a recommendation engine built entirely inside PostgreSQL
Stars: ✭ 297 (-3.26%)
Mutual labels:  database
Knime Core
KNIME Analytics Platform
Stars: ✭ 302 (-1.63%)
Mutual labels:  database
Anime Offline Database
Updated every week: A JSON based offline anime database containing the most important meta data as well as cross references to various anime sites such as MAL, ANIDB, ANILIST, KITSU and more...
Stars: ✭ 292 (-4.89%)
Mutual labels:  database
Erd
A Rails engine for drawing your app's ER diagram
Stars: ✭ 296 (-3.58%)
Mutual labels:  database
Wickdb
Pure Rust LSM-tree based embedded storage engine
Stars: ✭ 298 (-2.93%)
Mutual labels:  database
Technical Books
😆 国内外互联网技术大牛们都写了哪些书籍:计算机基础、网络、前端、后端、数据库、架构、大数据、深度学习...
Stars: ✭ 3,957 (+1188.93%)
Mutual labels:  database
Squeal
A Swift wrapper for SQLite databases
Stars: ✭ 303 (-1.3%)
Mutual labels:  database
Preql
An interpreted relational query language that compiles to SQL.
Stars: ✭ 257 (-16.29%)
Mutual labels:  database
Vuefire
🔥 Firebase bindings for Vue.js & Vuex
Stars: ✭ 3,234 (+953.42%)
Mutual labels:  database
Lev
The complete REPL & CLI for managing LevelDB instances.
Stars: ✭ 295 (-3.91%)
Mutual labels:  database
Couchdb Fauxton
Apache CouchDB
Stars: ✭ 295 (-3.91%)
Mutual labels:  database
Aviondb
A decentralised database with MongoDB-like developer interface (Fully Compatible with OrbitDB)
Stars: ✭ 301 (-1.95%)
Mutual labels:  database
Node Orm2
Object Relational Mapping
Stars: ✭ 3,063 (+897.72%)
Mutual labels:  database
Vertica Python
Official native Python client for the Vertica Analytics Database.
Stars: ✭ 301 (-1.95%)
Mutual labels:  database
Crate
CrateDB is a distributed SQL database that makes it simple to store and analyze massive amounts of data in real-time.
Stars: ✭ 3,254 (+959.93%)
Mutual labels:  database
Authmereloaded
The best authentication plugin for the Bukkit/Spigot API!
Stars: ✭ 296 (-3.58%)
Mutual labels:  database
Sql exporter
Database agnostic SQL exporter for Prometheus
Stars: ✭ 301 (-1.95%)
Mutual labels:  database
Think Orm
Think ORM——the PHP Database&ORM Framework
Stars: ✭ 303 (-1.3%)
Mutual labels:  database
Securing Restful Apis With Jwt
How to secure a Nodejs RESTful CRUD API using JSON web tokens?
Stars: ✭ 301 (-1.95%)
Mutual labels:  database

Guardian.DB

Hex.pm Build Status Codecov Inline docs

Guardian.DB is an extension to Guardian that tracks tokens in your application's database to prevent playback.

Installation

Guardian.DB assumes that you are using the Guardian framework for authentication.

To install Guardian.DB, first add it to your mix.exs file:

defp deps do
  [
    {:guardian_db, "~> 2.0"}
  ]
end

Then run mix deps.get on your terminal.

Configure your application as seen in the Configuration section below prior to attempting to generate the migration or you will get an application was not loaded/started error.

Following configuration add the Guardian migration:

run mix guardian.db.gen.migration to generate a migration.

Do not run the migration yet, we need to complete our setup first.

Configuration

config :guardian, Guardian.DB,
  repo: MyApp.Repo, # Add your repository module
  schema_name: "guardian_tokens", # default
  token_types: ["refresh_token"], # store all token types if not set
  sweep_interval: 60 # default: 60 minutes

To sweep expired tokens from your db you should add Guardian.DB.Token.SweeperServer to your supervision tree.

children = [
  {Guardian.DB.Token.SweeperServer, []}
]

Guardian.DB works by hooking into the lifecycle of your Guardian module.

You'll need to add it to:

  • after_encode_and_sign
  • on_verify
  • on_refresh
  • on_revoke

For example:

defmodule MyApp.AuthTokens do
  use Guardian, otp_app: :my_app

  # snip...

  def after_encode_and_sign(resource, claims, token, _options) do
    with {:ok, _} <- Guardian.DB.after_encode_and_sign(resource, claims["typ"], claims, token) do
      {:ok, token}
    end
  end

  def on_verify(claims, token, _options) do
    with {:ok, _} <- Guardian.DB.on_verify(claims, token) do
      {:ok, claims}
    end
  end

  def on_refresh({old_token, old_claims}, {new_token, new_claims}, _options) do
    with {:ok, _, _} <- Guardian.DB.on_refresh({old_token, old_claims}, {new_token, new_claims}) do
      {:ok, {old_token, old_claims}, {new_token, new_claims}}
    end
  end

  def on_revoke(claims, token, _options) do
    with {:ok, _} <- Guardian.DB.on_revoke(claims, token) do
      {:ok, claims}
    end
  end
end

Now run the migration and you'll be good to go.

Considerations

Guardian is already a very robust JWT solution. However, if your application needs the ability to immediately revoke and invalidate tokens that have already been generated, you need something like Guardian.DB to build upon Guardian.

In Guardian, you as a systems administrator have no way of revoking tokens that have already been generated, you can call Guardian.revoke, but in Guardian that function does not actually do anything - it just provides hooks for other libraries, such as this one, to define more specific behavior. Discarding the token after something like a log out action is left up to the client application. If the client application does not discard the token, or does not log out, or the token gets stolen by a malicious script (because the client application stores it in localStorage, for instance), the only thing you can do is wait until the token expires. Depending on the scenario, this may not be acceptable.

With Guardian.DB, records of all generated tokens are kept in your application's database. During each request, the Guardian.Plug.VerifyHeader and Guardian.Plug.VerifySession plugs check the database to make sure the token is there. If it is not, the server returns a 401 Unauthorized response to the client. Furthermore, Guardian.revoke! behavior becomes enhanced, as it actually removes the token from the database. This means that if the user logs out, or you revoke their token (e.g. after noticing suspicious activity on the account), they will need to re-authenticate.

Disadvantages

In Guardian, token verification is very light-weight. The only thing Guardian does is decode incoming tokens and make sure they are valid. This can make it much easier to horizontally scale your application, since there is no need to centrally store sessions and make them available to load balancers or other servers.

With Guardian.DB, every request requires a trip to the database, as Guardian now needs to ensure that a record of the token exists. In large scale applications this can be fairly costly, and can arguably eliminate the main advantage of using a JWT authentication solution, which is statelessness. Furthermore, session authentication already works this way, and in most cases there isn't a good enough reason to reinvent that wheel using JWTs.

In other words, once you have reached a point where you think you need Guardian.DB, it may be time to take a step back and reconsider your whole approach to authentication!

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].