2023-02-15 18:37:36 +08:00
|
|
|
|
class InstallationsController < ApplicationController
|
|
|
|
|
|
include RegisterHelper
|
|
|
|
|
|
before_action :require_login
|
|
|
|
|
|
|
|
|
|
|
|
def index
|
|
|
|
|
|
@install_bots = BotInstall.where(:installer_id => current_user.id)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2023-02-16 09:57:25 +08:00
|
|
|
|
def update_secret
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
|
bot = Bot.find params[:id]
|
|
|
|
|
|
application = Doorkeeper::Application.find_by(uid: bot.client_id, secret: bot.client_secret)
|
|
|
|
|
|
bot.client_secret = Doorkeeper::OAuth::Helpers::UniqueToken.generate
|
|
|
|
|
|
bot.save!
|
|
|
|
|
|
application.secret = bot.client_secret
|
|
|
|
|
|
application.save!
|
|
|
|
|
|
render_ok
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def update_private_key
|
|
|
|
|
|
bot = Bot.find params[:id]
|
|
|
|
|
|
bot.private_key = OpenSSL::PKey::RSA::generate(2048).to_s
|
|
|
|
|
|
bot.save!
|
|
|
|
|
|
render_ok
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2023-02-16 09:19:37 +08:00
|
|
|
|
def auth_active
|
2023-02-16 09:41:39 +08:00
|
|
|
|
begin
|
|
|
|
|
|
@bot = Bot.find params[:id]
|
2023-02-16 13:52:38 +08:00
|
|
|
|
tip_exception("该Bot已激活") if Doorkeeper::Application.find_by(uid: @bot.client_id, secret: @bot.client_secret).present?
|
2023-02-16 09:41:39 +08:00
|
|
|
|
@bot.client_id = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_id].blank?
|
|
|
|
|
|
@bot.client_secret = Doorkeeper::OAuth::Helpers::UniqueToken.generate if params[:client_secret].blank?
|
|
|
|
|
|
@bot.private_key = OpenSSL::PKey::RSA::generate(2048).to_s
|
|
|
|
|
|
@bot.owner_id = current_user.id
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
|
# 注册bot对应oauth应用
|
|
|
|
|
|
Doorkeeper::Application.create!(name: @bot.name, uid: @bot.client_id, secret: @bot.client_secret, redirect_uri: "https://gitlink.org.cn")
|
|
|
|
|
|
# 注册bot对应用户
|
2023-02-21 16:43:28 +08:00
|
|
|
|
result = autologin_register(User.generate_user_login('b'), nil, "#{SecureRandom.hex(6)}", 'bot', nil, nickname: @bot.name)
|
2023-02-16 09:41:39 +08:00
|
|
|
|
tip_exception(-1, result[:message]) if result[:message].present?
|
|
|
|
|
|
@bot.uid = result[:user][:id]
|
|
|
|
|
|
@bot.save
|
|
|
|
|
|
render_ok
|
|
|
|
|
|
end
|
2023-02-15 18:37:36 +08:00
|
|
|
|
rescue Exception => e
|
|
|
|
|
|
tip_exception(-1, e.message)
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def access_tokens
|
|
|
|
|
|
@install_bot = BotInstall.find params[:id]
|
|
|
|
|
|
@bot = @install_bot.bot
|
|
|
|
|
|
@application = Doorkeeper::Application.find_by(uid: @bot.client_id, secret: @bot.client_secret)
|
2023-02-16 11:01:21 +08:00
|
|
|
|
tip_exception("该Bot未激活") if @application.blank?
|
|
|
|
|
|
# 给bot生成token,因为bot是机器人操作
|
|
|
|
|
|
@access_token = Doorkeeper::AccessToken.create!({ :application_id => @application.id,
|
|
|
|
|
|
:resource_owner_id => @bot.uid,
|
|
|
|
|
|
:scopes => "public write",
|
|
|
|
|
|
:expires_in => "604800",
|
|
|
|
|
|
:use_refresh_token => true
|
|
|
|
|
|
})
|
2023-02-15 18:37:36 +08:00
|
|
|
|
render_ok(token: @access_token.token)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|