diff --git a/app/controllers/api/v1/user_tokens_controller.rb b/app/controllers/api/v1/user_tokens_controller.rb new file mode 100644 index 000000000..84c110a28 --- /dev/null +++ b/app/controllers/api/v1/user_tokens_controller.rb @@ -0,0 +1,10 @@ +class Api::V1::UserTokensController < Api::V1::BaseController + include AesCryptHelper + before_action :require_login + + def show + password = decrypt(params[:password]) rescue params[:password].to_s + tip_exception("密码不正确") unless current_user.check_password?(password) + render_ok({access_token: current_user.gitea_token}) + end +end \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a254e50eb..892820ad6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -326,6 +326,10 @@ class ApplicationController < ActionController::Base end end end + elsif params[:access_token].present? + user = User.find_by(gitea_token: params[:access_token]) + tip_exception(401, "无效token") if user.blank? + User.current = user else User.current = find_current_user uid_logger("user_setup: " + (User.current.logged? ? "#{User.current.try(:login)} (id=#{User.current.try(:id)})" : "anonymous")) diff --git a/config/routes/api.rb b/config/routes/api.rb index 0003cf7ad..f803dab72 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -67,6 +67,7 @@ defaults format: :json do collection do post :check_user_id post :check_user_login + get 'access_token', to: 'user_tokens#show' end end diff --git a/db/migrate/20251031043136_add_user_index_gitea_token.rb b/db/migrate/20251031043136_add_user_index_gitea_token.rb new file mode 100644 index 000000000..23adf5209 --- /dev/null +++ b/db/migrate/20251031043136_add_user_index_gitea_token.rb @@ -0,0 +1,5 @@ +class AddUserIndexGiteaToken < ActiveRecord::Migration[5.2] + def change + add_index :users, :gitea_token + end +end \ No newline at end of file