lg_pod_plugin 1.1.6.6 → 1.1.6.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 49669409fc52a28372c3033cd25e908b939267d30147689f982b84c9cea0d748
4
- data.tar.gz: ddd14338375b62677fd81ee52e0a3f15170b58a852870c203b3a27f2121a7151
3
+ metadata.gz: ad00d26eddca0197af9e74cce63dde7b58620b66694e5c97efeb6c8ca00c451d
4
+ data.tar.gz: 6f67d7bf931020022b87d2bb2de40b74d25716e6a4ef5b1dad0f70b0a7a03e67
5
5
  SHA512:
6
- metadata.gz: 2a9bb5866675919b09e0ebcad919085431ec96eb103caa56477356a833e9369365e9fed62506c8919866d4c6ae8ecb2bfb918a5dd22dc956cf16adc28cc674c7
7
- data.tar.gz: 85604ebf38280f2c65d929dcf60c0af3a29434b244290a4f67682b337090f81e8fc93e51bd165ee10529d0ee1bd826c9ac794894e344732fc93ebbf3531c1a37
6
+ metadata.gz: efa9785b96cd87f2d8af38fa907b6df5f036a7d4c6f3caa8c4cf338d07233271d27c78d1a0177fddda2117a760cefd3adb6f9c111045634e35d733131e774198
7
+ data.tar.gz: ee86b67391bafbfc0db8b4b9063935b3f736f38487f181e2a729402d6cc1368806ace7f8acb21850b229a0436307e6e099dde50448e305ffa9d3a3acafe2875d
data/lib/command/init.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'claide'
2
+ require 'json'
2
3
  require_relative 'command'
3
4
 
4
5
  module LgPodPlugin
@@ -7,8 +8,7 @@ module LgPodPlugin
7
8
  self.command = "init"
8
9
  self.abstract_command = false
9
10
  self.summary = '初始化gitlab projects 信息'
10
- attr_accessor :username
11
- attr_accessor :password
11
+ attr_accessor :token
12
12
  attr_accessor :host
13
13
  self.description = <<-DESC
14
14
  Manipulate the download cache for pods, like printing the cache content
@@ -17,16 +17,44 @@ module LgPodPlugin
17
17
 
18
18
  def initialize(argv)
19
19
  self.host = argv.option('host')
20
- self.username = argv.option('username')
21
- self.password = argv.option('password')
20
+ self.token = argv.option('token')
22
21
  super
23
22
  end
24
23
 
25
24
  def run
26
- raise unless self.host
27
- raise unless self.username
28
- raise unless self.password
29
- GitLabAPI.request_gitlab_access_token(self.host, self.username, self.password)
25
+ if self.host.nil? || self.host == ""
26
+ LgPodPlugin.log_red "传入host不能为空"
27
+ return
28
+ end
29
+ if self.token.nil? || self.token == ""
30
+ LgPodPlugin.log_red "传入token不能为空"
31
+ return
32
+ end
33
+ token_vaild = GitLabAPI.request_user_emails(self.host, self.token)
34
+ if token_vaild == "invalid token"
35
+ LgPodPlugin.log_red "无效的access_token, 请检查私人令牌是否在有限期内"
36
+ return
37
+ end
38
+ refresh_token = ""
39
+ expires_in = 7879680
40
+ created_at = Time.now.to_i
41
+ encrypt_access_token = LUtils.encrypt(self.token, "AZMpxzVxzbo3sFDLRZMpxzVxzbo3sFDZ")
42
+ hash = {"access_token": encrypt_access_token}
43
+ hash["token_type"] = "Bearer"
44
+ hash["expires_in"] = expires_in
45
+ hash["scope"] = "api"
46
+ hash["created_at"] = created_at
47
+ hash["refresh_token"] = refresh_token
48
+ db_path = LFileManager.download_director.join("database")
49
+ db_path.mkdir unless db_path.exist?
50
+ token_file = db_path.join("access_token.json")
51
+ str = JSON.generate(hash)
52
+ File.open(token_file.to_path, 'w+') { |f| f.write(str) }
53
+ LSqliteDb.shared.init_database
54
+ user_id = LUserAuthInfo.get_user_id(self.host)
55
+ user_model = LUserAuthInfo.new(user_id, "", "", self.host, self.token, "", (created_at + expires_in))
56
+ LSqliteDb.shared.insert_user_info(user_model)
57
+ LgPodPlugin.log_green "设置私人访问令牌成功"
30
58
  end
31
59
 
32
60
  end
@@ -23,23 +23,11 @@ module LgPodPlugin
23
23
  user_info = LSqliteDb.shared.query_user_info(user_id)
24
24
  # 用户授权 token 不存在, 提示用户输入用户名密码
25
25
  unless user_info
26
- user_info = GitLabAPI.get_gitlab_access_token_input(uri, user_id,nil ,nil )
26
+ user_info = GitLabAPI.get_gitlab_access_token(uri, user_id)
27
27
  return nil unless user_info
28
28
  end
29
- time_now = Time.now.to_i
30
- # 判断 token 是否失效
31
- if user_info.expires_in <= time_now
32
- # 刷新 token 失败时, 通过已经保存的用户名密码来刷新 token
33
- new_user_info = GitLabAPI.refresh_gitlab_access_token uri.hostname, user_info.refresh_token
34
- if new_user_info.nil?
35
- username = user_info.username
36
- password = user_info.password
37
- user_info = GitLabAPI.get_gitlab_access_token_input(uri, user_id, username, password)
38
- return nil unless user_info
39
- else
40
- user_info = new_user_info
41
- end
42
- end
29
+ user_info = GitLabAPI.check_gitlab_access_token_valid(uri, user_info)
30
+ return nil unless user_info
43
31
  config = LConfig.new
44
32
  config.host = uri.hostname
45
33
  config.access_token = user_info.access_token
@@ -7,6 +7,33 @@ module LgPodPlugin
7
7
 
8
8
  class GitLabAPI
9
9
 
10
+ # 通过读取本地文件获取 access_token
11
+ def self.get_gitlab_access_token(uri, user_id)
12
+ text = LUtils.encrypt("AzfAjh-xTzyRBXmYuGBr", "AZMpxzVxzbo3sFDLRZMpxzVxzbo3sFDZ")
13
+ db_path = LFileManager.download_director.join("database")
14
+ db_path.mkdir unless db_path.exist?
15
+ token_file = db_path.join("access_token.json")
16
+ return self.get_gitlab_access_token_input(uri, user_id, nil, nil) unless token_file.exist?
17
+ json = JSON.parse(File.read("#{token_file.to_path}"))
18
+ encrypt_access_token = json["access_token"]
19
+ return self.get_gitlab_access_token_input(uri, user_id, nil, nil) if encrypt_access_token.nil?
20
+ access_token = LUtils.decrypt(encrypt_access_token, "AZMpxzVxzbo3sFDLRZMpxzVxzbo3sFDZ")
21
+ token_vaild = GitLabAPI.request_user_emails(uri.hostname, access_token)
22
+ if token_vaild == "invalid token"
23
+ FileUtils.rm_rf token_file
24
+ return self.get_gitlab_access_token_input(uri, user_id, nil, nil) if encrypt_access_token.nil?
25
+ end
26
+ user_id = LUserAuthInfo.get_user_id(uri.hostname)
27
+ refresh_token = json["refresh_token"]
28
+ expires_in = json["expires_in"] ||= 7879680
29
+ created_at = json["created_at"] ||= Time.now.to_i
30
+ user_model = LUserAuthInfo.new(user_id, "", "", uri.hostname, access_token, refresh_token, (created_at + expires_in))
31
+ LSqliteDb.shared.insert_user_info(user_model)
32
+ LgPodPlugin.log_green "请求成功: `access_token` => #{access_token}, expires_in => #{expires_in}"
33
+ return user_model
34
+ end
35
+
36
+ # 通过输入用户名和密码 获取 access_token
10
37
  def self.get_gitlab_access_token_input(uri, user_id, username = nil, password = nil)
11
38
  unless username && password
12
39
  LgPodPlugin.log_yellow "请输入 `#{uri.to_s}` 的用户名"
@@ -18,6 +45,40 @@ module LgPodPlugin
18
45
  user_info = LSqliteDb.shared.query_user_info(user_id)
19
46
  return user_info
20
47
  end
48
+ # 检查 token 是否在有效期内
49
+ def self.check_gitlab_access_token_valid(uri, user_info)
50
+ time_now = Time.now.to_i
51
+ # 判断 token 是否失效
52
+ if user_info.expires_in <= time_now
53
+ refresh_token = user_info.refresh_token
54
+ if refresh_token.nil? || refresh_token == "" # 使用本地令牌访问
55
+ project_name = LUtils.get_git_project_name(uri.to_s)
56
+ token_vaild = GitLabAPI.request_user_emails(uri.hostname, user_info.access_token)
57
+ if token_vaild == "success"
58
+ new_user_info = LUserAuthInfo.new(user_info.id, "", "", uri.hostname, user_info.access_token, nil, (time_now + 7879680))
59
+ LSqliteDb.shared.insert_user_info(user_info)
60
+ return new_user_info
61
+ else
62
+ token_file = LFileManager.download_director.join("database").join("access_token.json")
63
+ FileUtils.rm_rf token_file if token_file.exist?
64
+ return self.get_gitlab_access_token_input(uri, user_info.id, nil, nil)
65
+ end
66
+ else
67
+ # 刷新 token 失败时, 通过已经保存的用户名密码来刷新 token
68
+ new_user_info = GitLabAPI.refresh_gitlab_access_token uri.hostname, refresh_token
69
+ if new_user_info.nil?
70
+ username = user_info.username
71
+ password = user_info.password
72
+ user_info = GitLabAPI.get_gitlab_access_token_input(uri, user_info.user_id, username, password)
73
+ return nil unless user_info
74
+ else
75
+ user_info = new_user_info
76
+ end
77
+ end
78
+ else
79
+ return user_info
80
+ end
81
+ end
21
82
 
22
83
  public
23
84
  # 获取 GitLab access_token
@@ -228,6 +289,31 @@ module LgPodPlugin
228
289
  end
229
290
  end
230
291
 
292
+ # 通过名称搜索项目信息
293
+ public
294
+ def self.request_user_emails(host, access_token)
295
+ begin
296
+ hash_map = {"access_token": access_token}
297
+ uri = URI("#{host}/api/v4/user/emails")
298
+ uri.query = URI.encode_www_form(hash_map)
299
+ res = Net::HTTP.get_response(uri)
300
+ if res.body
301
+ array = JSON.parse(res.body)
302
+ else
303
+ array = []
304
+ end
305
+ if array.is_a?(Array)
306
+ return "success"
307
+ elsif array.is_a?(Hash)
308
+ return "invalid token"
309
+ else
310
+ return "invalid token"
311
+ end
312
+ rescue
313
+ return "invalid token"
314
+ end
315
+ end
316
+
231
317
  end
232
318
 
233
319
  end
@@ -30,13 +30,6 @@ module LgPodPlugin
30
30
  end
31
31
  return if origin_uri.nil?
32
32
  @uri = origin_uri
33
- # redirect_url = LProject.shared.redirect_url_hash[origin_uri.host]
34
- # if redirect_url
35
- # @uri = redirect_url
36
- # else
37
- # @uri = URI(get_redirect_url(origin_uri.scheme + "://" + origin_uri.host))
38
- # LProject.shared.redirect_url_hash[origin_uri.host] = @uri
39
- # end
40
33
  @host = @uri.host
41
34
  @scheme = @uri.scheme ||= "https"
42
35
  @hostname = @scheme + "://" + @host
@@ -3,20 +3,20 @@ require 'resolv'
3
3
  require "ipaddr"
4
4
  require 'base64'
5
5
  require 'fileutils'
6
- # require_relative 'aes-crypt'
6
+ require_relative 'aes-crypt'
7
7
 
8
8
  module LgPodPlugin
9
9
 
10
10
  class LUtils
11
11
 
12
- # def self.encrypt(message, password)
13
- # encrypted_data = AESCrypt.encrypt(message, password)
14
- # encrypted_data.tr("\n", "")
15
- # end
16
- #
17
- # def self.decrypt(message, password)
18
- # AESCrypt.decrypt message, password
19
- # end
12
+ def self.encrypt(message, password)
13
+ encrypted_data = AESCrypt.encrypt(message, password)
14
+ encrypted_data.tr("\n", "")
15
+ end
16
+
17
+ def self.decrypt(message, password)
18
+ AESCrypt.decrypt message, password
19
+ end
20
20
 
21
21
  def self.md5(text)
22
22
  return "" unless text
@@ -1,3 +1,3 @@
1
1
  module LgPodPlugin
2
- VERSION = "1.1.6.6"
2
+ VERSION = "1.1.6.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lg_pod_plugin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6.6
4
+ version: 1.1.6.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - dongzb01
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-08 00:00:00.000000000 Z
11
+ date: 2023-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cocoapods