Skip to content

Instantly share code, notes, and snippets.

@bradgessler
Last active September 25, 2024 19:18
Show Gist options
  • Save bradgessler/ea0bce7b20523fe95c66d1a3b6fe678e to your computer and use it in GitHub Desktop.
Save bradgessler/ea0bce7b20523fe95c66d1a3b6fe678e to your computer and use it in GitHub Desktop.
Terminalwire Rails Auth
module ActiveExchange
class Channel
def initialize(name:, server: ActiveExchange.server)
@server = server
@channel = name
@queue = Queue.new
@subscribe = false
end
def broadcast(message)
Rails.logger.info "ActiveExchange: Publishing #{message.inspect} to #{@channel.inspect}"
@server.broadcast(@channel, message)
end
def subscribe
return if @subscribed
@subscribed = true
Rails.logger.info "ActiveExchange: Subscribed to #{@channel.inspect}"
@server.subscribe(@channel, -> (message) { @queue << message })
end
def read
subscribe
@queue.pop
end
end
def self.server
ActionCable.server.pubsub
end
end
class CLI::AuthorizationsController < ApplicationController
before_action :authorize_user
include Superview::Actions
class Form < ApplicationForm
def template
# field(:id).input(type: :hidden)
div class: "flex flex-row gap-4 w-full" do
input(type: "submit", class: "btn btn-primary", value: "Approve")
input(type: "cancel", class: "btn btn-outline btn-error", value: "Deny")
end
end
end
class New < FocusedView
def title = "Authorize TinyZap Command Line App"
def template
p { "Click the button to Authorize the CLI" }
render Form.new(User.new(id: current_user.id))
end
end
class Show < FocusedView
def title = "Successfully Authorized Command Line App"
def template
p { "You may now close this window and continue using the CLI" }
end
end
def create
ActiveExchange::Channel.new(name: "authorization").broadcast(JSON.generate(user_id: current_user.id))
redirect_to url_for(action: :show, id: "close")
end
end
class MainTerminal < ApplicationTerminal
desc "version", "Prints the version of the CLI"
def version
puts "1.0"
end
desc "open", "Opens TinyZap.com in your browser"
def open
browser.launch "https://tinyzap.com/"
end
desc "login", "Login to TinyZap"
def login
puts "Login to TinyZap"
nonce = SecureRandom.hex(32)
authorization_url = "http://localhost:3000/cli/authorizations/new?nonce=#{nonce}"
browser.launch authorization_url
puts "Waiting for authorization approval from #{authorization_url}..."
authorization = ActiveExchange::Channel.new(name: "authorization")
if user_id = JSON.parse(authorization.read).fetch("user_id")
self.current_user = User.find(user_id)
puts "Welcome #{current_user.email}"
else
puts "Well that didn't work"
end
end
desc "logout", "Logout of your account"
def logout
session.reset
puts "Successfully logged out."
end
desc "whoami", "Prints your current login information"
def whoami
if current_user
puts "Email: #{current_user.email}"
else
puts "Run `tinyzap login` to login"
end
end
desc "links", "Manage links"
subcommand "links", LinksTerminal
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment