forgeplus/app/models/bot.rb

52 lines
1.3 KiB
Ruby

# == Schema Information
#
# Table name: bot
#
# id :integer not null, primary key
# bot_name :string(255)
# bot_des :text(4294967295)
# webhook :string(255)
# is_public :integer
# logo :string(255)
# state :integer
# client_id :string(255)
# client_secret :string(255)
# web_url :string(255)
# category :string(255)
# install_num :integer default("0")
# update_time :datetime not null
# create_time :datetime not null
# private_key :text(65535)
# uid :integer
# owner_id :integer
#
# Indexes
#
# name (bot_name) UNIQUE
#
class Bot < ApplicationRecord
self.table_name = "bot"
has_many :install_bots
def name
self.bot_name
end
def self.decode_jwt_token(token)
decoded_token = JWT.decode token, nil, false
return [nil, "Token已过期"] if Time.now.to_i - 10*60 - decoded_token[0]["exp"].to_i > 0
bot = Bot.find_by(id: decoded_token[0]["iss"])
return [nil, "Token不存在"] if bot.blank?
rsa_private = OpenSSL::PKey::RSA.new(bot.private_key)
rsa_public = rsa_private.public_key
JWT.decode token, rsa_public, true, { algorithm: 'RS256' }
[User.find_by(id: bot.owner_id), ""]
rescue JWT::DecodeError
Rails.logger.error "jwt token decode error:#{token}"
[nil, "无效Token"]
end
end