tilde.news/app/controllers/keybase_proofs_controller.rb

82 lines
2.6 KiB
Ruby
Raw Normal View History

2019-06-12 13:08:57 +00:00
class KeybaseProofsController < ApplicationController
2019-06-13 01:20:13 +00:00
before_action :require_logged_in_user, only: [:new, :create, :destroy]
2019-06-12 13:08:57 +00:00
before_action :check_new_params, only: :new
before_action :check_user_matches, only: :new
before_action :force_to_json, only: [:kbconfig]
def new
@title = "Connect Your Keybase Account"
2019-06-12 13:08:57 +00:00
@kb_username = params[:kb_username]
@kb_signature = params[:kb_signature]
@kb_ua = params[:kb_ua]
@kb_avatar = Keybase.avatar_url(@kb_username)
end
def create
kb_username = post_params[:kb_username]
kb_signature = post_params[:kb_signature]
kb_ua = post_params[:kb_ua]
if Keybase.proof_valid?(kb_username, kb_signature, @user.username)
@user.add_or_update_keybase_proof(kb_username, kb_signature)
@user.save!
return redirect_to Keybase.success_url(kb_username, kb_signature, kb_ua, @user.username)
else
flash[:error] = "Failed to connect your account to Keybase. Try again from Keybase."
return redirect_to settings_path
end
end
2019-06-13 01:20:13 +00:00
def destroy
@user.remove_keybase_proof(params[:id])
@user.save!
redirect_to(
user_path(@user),
notice: "Removed from profile. You still need to delete it from the Keybase site or app."
)
end
2019-06-12 13:08:57 +00:00
def kbconfig
return render json: {} unless Keybase.enabled?
@domain = Keybase.DOMAIN
@name = Rails.application.name
@brand_color = "#AC130D"
@description = "Computing-focused community centered around link aggregation and discussion"
@contacts = ["admin@#{Keybase.DOMAIN}"]
@prefill_url = "#{new_keybase_proof_url}?kb_username=%{kb_username}&" \
"kb_signature=%{sig_hash}&kb_ua=%{kb_ua}&username=%{username}"
enable mod_userdir diff of `rails routes`: 12,13d11 < GET /newest/:user(.:format) home#newest_by_user < GET /newest/:user/page/:page(.:format) home#newest_by_user 32d29 < user_threads GET /threads/:user(.:format) comments#user_threads 120,122c117,132 < u GET /u(.:format) users#tree < user GET /u/:username(.:format) users#show < user_standing GET /u/:username/standing(.:format) users#standing --- > users_tree GET /users(.:format) users#tree > user GET /~:username(.:format) users#show > user_standing GET /~:username/standing(.:format) users#standing > GET /~:user/stories(/page/:page)(.:format) home#newest_by_user > user_threads GET /~:user/threads(.:format) comments#user_threads > user_ban POST /~:username/ban(.:format) users#ban > user_unban POST /~:username/unban(.:format) users#unban > user_disable_invite POST /~:username/disable_invitation(.:format) users#disable_invitation > user_enable_invite POST /~:username/enable_invitation(.:format) users#enable_invitation > u GET /u(.:format) redirect(302, /users) > GET /u/:username(.:format) redirect(302, /~%{username}) > GET /@:username(.:format) redirect(302, /~%{username}) > GET /u/:username/standing(.:format) redirect(302, ~%{username}/standing) > GET /newest/:user(.:format) redirect(302, ~%{user}/stories) > GET /newest/:user(/page/:page)(.:format) redirect(302, ~%{user}/stories/page/%{page}) > GET /threads/:user(.:format) redirect(302, ~%{user}/threads) 125,128d134 < user_ban POST /users/:username/ban(.:format) users#ban < user_unban POST /users/:username/unban(.:format) users#unban < user_disable_invite POST /users/:username/disable_invitation(.:format) users#disable_invitation < user_enable_invite POST /users/:username/enable_invitation(.:format) users#enable_invitation
2023-08-29 12:45:28 +00:00
@profile_url = "/~%{username}"
@check_url = "/~%{username}.json"
2019-06-12 13:08:57 +00:00
@logo_black = "https://lobste.rs/small-black-logo.svg"
@logo_full = "https://lobste.rs/full-color.logo.svg"
@user_re = User.username_regex_s[1...-1]
end
private
def force_to_json
request.format = :json
end
def check_user_matches
unless case_insensitive_match?(@user.username, params[:username])
flash[:error] = "not logged in as the correct user"
return redirect_to settings_path
end
end
def case_insensitive_match?(first_string, second_string)
# can replace this with first_string.casecmp?(second_string) when ruby >= 2.4.6
first_string.casecmp(second_string).zero?
end
def post_params
params.require(:keybase_proof).permit(:kb_username, :kb_signature, :kb_ua, :username)
end
def check_new_params
redirect_to settings_path unless [:kb_username, :kb_signature, :kb_ua, :username].all? do |k|
params[k].present?
end
end
end