From 89e13ecfccc385b9ea93041df614b904828bb284 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Fri, 31 Oct 2025 15:57:12 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E7=94=A8=E6=88=B7=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=98=BE=E7=A4=BAtoken=E5=92=8C=E6=90=BA=E5=B8=A6token?= =?UTF-8?q?=E8=AF=B7=E6=B1=82api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/v1/user_tokens_controller.rb | 10 ++++++++++ app/controllers/application_controller.rb | 4 ++++ config/routes/api.rb | 1 + .../20251031043136_add_user_index_gitea_token.rb | 5 +++++ 4 files changed, 20 insertions(+) create mode 100644 app/controllers/api/v1/user_tokens_controller.rb create mode 100644 db/migrate/20251031043136_add_user_index_gitea_token.rb 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