Giter Site home page Giter Site logo

Comments (2)

dsignr avatar dsignr commented on June 2, 2024

@riverrun David, here's how I use refresh tokens right now w/ Phauxth. This file resides in my controllers folder inside API/auth. Maybe you can just add this to the generator for the API with slight changes:

defmodule XYZWeb.API.Auth.Token do
  @moduledoc """
  Custom token implementation using Phauxth.Token behaviour and Phoenix Token.
  """

  @behaviour Phauxth.Token

  alias Phoenix.Token
  alias XYZWeb.Endpoint

  @access_token_max_age 259200 #3 days
  @refresh_token_max_age 31536000 # 1 year
  @access_token_salt "ABC" # change this to a proper salt if you don't want to get pwned
  @refresh_token_salt "DEF" # change this to a proper salt if you don't want to get pwned

  def max_age(token_type \\ :access_token) do
    case token_type do
      :refresh_token -> @refresh_token_max_age
      _ -> @access_token_max_age
    end
  end

  @impl true
  def sign(data) do
    Token.sign(Endpoint, @access_token_salt, data, [])
  end

  # @impl true
  # Max age not supported for signing
  def sign(data, opts \\ [], token_type \\ :access_token) do
    case token_type do
      :access_token -> Token.sign(Endpoint, @access_token_salt, data, opts)
      :refresh_token -> Token.sign(Endpoint, @refresh_token_salt, data, opts)
    end
  end

  defp updated_opts(opts, new_max_age) do
     {_, new_opts} = Keyword.get_and_update(opts, :max_age, fn current_max_age ->
      {current_max_age, new_max_age}
    end)
    new_opts
  end

  # @impl true
  def verify(token, opts \\ [], token_type \\ :access_token) do
    case token_type do
      :access_token -> Token.verify(Endpoint, @access_token_salt, token, updated_opts(opts, @access_token_max_age))
      :refresh_token -> Token.verify(Endpoint, @refresh_token_salt, token, updated_opts(opts, @refresh_token_max_age))
    end
  end
end

from phauxth.

dsignr avatar dsignr commented on June 2, 2024

So a brief explanation of how I use the above. When access token expires, the client will send the refresh token as a Bearer token in the API call, if it's valid, my endpoint will generate a new access token which will be cached on the mobile app or wherever I'm consuming it. One thing to add here is refresh tokens should ONLY be generated once during the lifetime of a user using your app for maximum security. So, maybe the very first time they sign up or when they resent their account. It's a security violation if you keep generating a refresh token upon every renewal request. Only generate an access token for renew endpoints. Cheers.

from phauxth.

Related Issues (20)

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.