Class: JPSClient::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/jpsclient/auth/token.rb

Overview

Token 管理类负责 token 的本地存储、加载和清除token 有效性由服务端 401 响应判断,本地不做过期检查

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Token

Returns a new instance of Token.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jpsclient/auth/token.rb', line 18

def initialize(config)
  @config = config
  @aes_key = config.aes_key if config

  # 从配置中获取 token 存储路径,如果配置中没有则使用默认值
  if config && config.token_dir && !config.token_dir.empty?
    @token_dir = File.expand_path(config.token_dir)
  else
    @token_dir = File.expand_path('~')  # 默认目录
  end

  # 从配置中获取 token 文件名,如果配置中没有则使用默认值
  if config && config.token_file_name && !config.token_file_name.empty?
    @token_file = File.join(@token_dir, config.token_file_name)
  else
    @token_file = File.join(@token_dir, '.jps_auth_token')  # 默认文件名
  end

  # token 数据
  @token = nil
  @username = nil
  @user_id = nil
  @permissions = nil
  @lark_user_id = nil
  @tenant_manager = false
  @expires_at = nil
  @created_at = nil

  # 调试模式
  @verbose = ENV['PINDO_DEBUG'] == 'true'
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



16
17
18
# File 'lib/jpsclient/auth/token.rb', line 16

def created_at
  @created_at
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



16
17
18
# File 'lib/jpsclient/auth/token.rb', line 16

def expires_at
  @expires_at
end

#lark_user_idObject (readonly)

Returns the value of attribute lark_user_id.



15
16
17
# File 'lib/jpsclient/auth/token.rb', line 15

def lark_user_id
  @lark_user_id
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



15
16
17
# File 'lib/jpsclient/auth/token.rb', line 15

def permissions
  @permissions
end

#tenant_managerObject (readonly)

Returns the value of attribute tenant_manager.



15
16
17
# File 'lib/jpsclient/auth/token.rb', line 15

def tenant_manager
  @tenant_manager
end

#tokenObject (readonly)

Returns the value of attribute token.



14
15
16
# File 'lib/jpsclient/auth/token.rb', line 14

def token
  @token
end

#user_idObject (readonly)

Returns the value of attribute user_id.



15
16
17
# File 'lib/jpsclient/auth/token.rb', line 15

def user_id
  @user_id
end

#usernameObject (readonly)

Returns the value of attribute username.



14
15
16
# File 'lib/jpsclient/auth/token.rb', line 14

def username
  @username
end

Instance Method Details

#clearObject

清除 token



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/jpsclient/auth/token.rb', line 148

def clear
  @token = nil
  @username = nil
  @user_id = nil
  @permissions = nil
  @lark_user_id = nil
  @tenant_manager = false
  @expires_at = nil
  @created_at = nil

  FileUtils.rm_f(@token_file) if File.exist?(@token_file)
  puts "✓ Token 已清除" if @verbose
end

#loadObject

加载 token



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jpsclient/auth/token.rb', line 51

def load
  return false unless File.exist?(@token_file)

  begin
    file_content = File.read(@token_file)

    # 根据是否有 AES 密钥决定解密方式
    token_data = if @aes_key
      begin
        aes = AES.new(@aes_key)
        decrypted = aes.decrypt(file_content)
        JSON.parse(decrypted)
      rescue => e
        # 解密失败,可能是明文,尝试直接解析
        puts "尝试解密失败,作为明文读取: #{e.message}" if @verbose
        JSON.parse(file_content)
      end
    else
      JSON.parse(file_content)
    end

    @token = token_data['token']
    @username = token_data['username']
    @user_id = token_data['user_id']
    @permissions = token_data['permissions']
    @lark_user_id = token_data['lark_user_id']
    @tenant_manager = token_data.key?('tenant_manager') ? token_data['tenant_manager'] : false
    @expires_at = token_data['expires_at']
    @created_at = token_data['created_at']

    # 旧版 token 文件缺少 user_id,视为无效,需重新登录获取完整字段
    unless @token && @user_id
      puts "Token 文件缺少必要字段,需要重新登录" if @verbose
      clear
      return false
    end

    return true
  rescue => e
    puts "读取 token 失败: #{e.message}" if @verbose
    clear_corrupted_file
  end

  false
end

#loaded?Boolean

token 是否已加载

Returns:

  • (Boolean)


143
144
145
# File 'lib/jpsclient/auth/token.rb', line 143

def loaded?
  !@token.nil? && !@token.empty?
end

#save(token_data) ⇒ Object

保存 token 传入完整数据 Hash



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/jpsclient/auth/token.rb', line 99

def save(token_data)
  return false unless token_data.is_a?(Hash) && token_data['token']

  @token = token_data['token']
  @username = token_data['username']
  @user_id = token_data['user_id']
  @permissions = token_data['permissions']
  @lark_user_id = token_data['lark_user_id']
  @tenant_manager = token_data.key?('tenant_manager') ? token_data['tenant_manager'] : false
  @created_at = Time.now.to_i
  @expires_at = @created_at + 6 * 24 * 60 * 60  # 6天后过期

  # 确保目录存在
  FileUtils.mkdir_p(@token_dir) unless Dir.exist?(@token_dir)

  save_data = {
    'token' => @token,
    'username' => @username,
    'user_id' => @user_id,
    'permissions' => @permissions,
    'lark_user_id' => @lark_user_id,
    'tenant_manager' => @tenant_manager,
    'expires_at' => @expires_at,
    'created_at' => @created_at
  }

  # 根据是否有 AES 密钥决定加密方式
  content = if @aes_key
    aes = AES.new(@aes_key)
    aes.encrypt(save_data.to_json)
  else
    save_data.to_json
  end

  File.write(@token_file, content)
  puts "✓ Token 已保存到 #{@token_file}" if @verbose

  true
rescue => e
  puts "保存 token 失败: #{e.message}"
  false
end

#to_hObject

转换为 Hash



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/jpsclient/auth/token.rb', line 163

def to_h
  return nil unless @token

  {
    'token' => @token,
    'username' => @username,
    'user_id' => @user_id,
    'permissions' => @permissions,
    'lark_user_id' => @lark_user_id,
    'tenant_manager' => @tenant_manager,
    'expires_at' => @expires_at,
    'created_at' => @created_at
  }
end