Git Product home page Git Product logo

Comments (2)

adamniedzielski avatar adamniedzielski commented on June 28, 2024

The topic of authentication in Websockets connections is completely foreign to me. Sadly I can't offer any guidance here.

from tiddle.

Reizar avatar Reizar commented on June 28, 2024

For anyone else who comes across this and is looking for the same thing. Here is how I got Tiddle working in ActionCable.
@adamniedzielski might be worth adding to the documentation.

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    protected

    def find_verified_user
      user = User.find_for_authentication(email: request.params[:user_email])
      reject_unauthorized_connection unless user

      token = Tiddle::TokenIssuer.build.find_token(user, request.params[:user_token])
      if token && unexpired?(token)
        return user
      end

      reject_unauthorized_connection
    end

    def unexpired?(token)
      return true unless token.respond_to?(:expires_in)
      return true if token.expires_in.blank? || token.expires_in.zero?

      Time.current <= token.last_used_at + token.expires_in
    end
  end
end

This isn't a perfect solution and I don't really like sending tokens via query params, but it works. Note that it makes a few assumptions and hard codes variable names (model is User, key is email and sent via a query param of user_email and token is sent via query param of user_token)

from tiddle.

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.