Code Monkey home page Code Monkey logo

example-rails-api-key-authentication's People

Contributors

ezekg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

example-rails-api-key-authentication's Issues

Sign out doubt

When you wrote at the end of the article that we couldn't logout anymore using the current_api_key because we could not see them anymore. What do you mean? Because one of the changes you did was making the destroy use :authenticate_with_api_key! Which means it goes through the code that sets the current_api_key, so which is the problem in using the same current_api_key&.destroy! ?

Love the article by the way!

Use Rails native DB encryption

Hi,

I've enjoyed reading your article and applying the code to side project of mine. Thanks for writting this down for others to learn.

I've noticed that with Rails 7 native DB encryption feature, we can simplify the token encryption.

We need to generate the keys with $ bin/rails db:encryption:init and store the resul in the cedentials with $ bin/rails credentials:edit.

The migration must be changed to :

class CreateApiKeys < ActiveRecord::Migration[7.0]
  def up
    create_table :api_keys do |t|
      t.string :token
      t.references :bearer, polymorphic: true, null: false
    end

    add_index :api_keys, [:bearer_id, :bearer_type]
    add_index :api_keys, :token, unique: true
  end

  def down
    drop_table :api_keys
  end
end

and the ApiKey model can be reduced to this :

class ApiKey < ApplicationRecord
  encrypts :token, deterministic: true

  belongs_to :bearer, polymorphic: true

  def self.authenticate_by_token!(token)
    find_by! token: token
  end

  def self.authenticate_by_token(token)
    authenticate_by_token! token
  rescue ActiveRecord::RecordNotFound
    nil
  end
end

There is nothing to deal with for storing the HMAC key, no manual digest storage, cleaning, swapping…

OAuth 2.0 Update

Hi!

Is it possible to evolve the same code to OAuth 2 which will allow third party access without using Devise?

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.