mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-24 05:35:46 +08:00
cla fix and add token verify
This commit is contained in:
83
app/services/projects/verify_auth_token_service.rb
Normal file
83
app/services/projects/verify_auth_token_service.rb
Normal file
@@ -0,0 +1,83 @@
|
||||
class Projects::VerifyAuthTokenService < ApplicationService
|
||||
attr_accessor :url, :token
|
||||
|
||||
def initialize(url, token)
|
||||
@url = url
|
||||
@token = token
|
||||
@repo = nil
|
||||
@owner = nil
|
||||
@website = nil
|
||||
@success = nil
|
||||
end
|
||||
|
||||
def call
|
||||
Rails.logger.info("###### VerifyAuthTokenService begin ######")
|
||||
regular_url
|
||||
to_verify
|
||||
Rails.logger.info("##### VerifyAuthTokenService end ######")
|
||||
return @success
|
||||
end
|
||||
|
||||
private
|
||||
def regular_url
|
||||
regx = /\/\/[\s\S]*.git$/ #获取字串
|
||||
data = (regx.match @url).to_s[2..-5].split("/")
|
||||
@website = data[0]
|
||||
@owner = data[1]
|
||||
@repo = data[2]
|
||||
end
|
||||
|
||||
|
||||
def to_verify
|
||||
data = case @website
|
||||
when "github.com"
|
||||
github_verify
|
||||
when "gitlab.com"
|
||||
gitlab_verify
|
||||
when "gitee.com"
|
||||
gitee_verify
|
||||
end
|
||||
end
|
||||
|
||||
def gitee_verify
|
||||
url = "/api/v5/repos/#{@owner}/#{@repo}"
|
||||
api_url= "https://gitee.com"
|
||||
client = Faraday.new(url: api_url)
|
||||
client.options["open_timeout"] = 1
|
||||
client.options["timeout"] = 1
|
||||
client.options["write_timeout"] = 1
|
||||
req_params={
|
||||
access_token: @token,
|
||||
owner: @owner,
|
||||
repo: @repo
|
||||
}
|
||||
response = client.public_send("get", url, req_params)
|
||||
@success = true if response.status == 200
|
||||
end
|
||||
|
||||
def github_verify
|
||||
url = "/octocat"
|
||||
api_url= "https://api.github.com"
|
||||
client = Faraday.new(url: api_url)
|
||||
client.options["open_timeout"] = 1
|
||||
client.options["timeout"] = 1
|
||||
client.options["write_timeout"] = 1
|
||||
client.headers["Authorization"] = "Bearer #{@token}"
|
||||
response = client.public_send("get", url)
|
||||
@success = true if response.status == 200
|
||||
end
|
||||
|
||||
def gitlab_verify
|
||||
url = "/api/v4/projects"
|
||||
api_url= "https://gitlab.com"
|
||||
client = Faraday.new(url: api_url)
|
||||
client.options["open_timeout"] = 1
|
||||
client.options["timeout"] = 1
|
||||
client.options["write_timeout"] = 1
|
||||
req_params={
|
||||
private_token: @token
|
||||
}
|
||||
response = client.public_send("get", url, req_params)
|
||||
@success = true if response.status == 200
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user