Module: Wdt::Authentication
- Included in:
- Client
- Defined in:
- lib/wdt/authentication.rb
Constant Summary collapse
- SEPARATOR1 =
对请求的参数进行签名
'-'
- SEPARATOR2 =
':'
- SEPARATOR3 =
';'
Instance Method Summary collapse
- #get_signature(params) ⇒ Object
- #sign(params) ⇒ Object
-
#stringify_keys(obj) ⇒ Object
把 hash 的 key 从 symbol 转化为 string params :b=>{:c=>2} return “b”=>{“c”=>2}.
Instance Method Details
#get_signature(params) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/wdt/authentication.rb', line 33 def get_signature(params) data = "" params.sort.each do |key, value| key = key.to_s value = value.to_s if data.size > 0 data << SEPARATOR3 end data << len_foo(key, 2) data << SEPARATOR1 data << key data << SEPARATOR2 data << len_foo(value, 4) data << SEPARATOR1 data << value end Digest::MD5.hexdigest("#{data}#{appsecret}") end |
#sign(params) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/wdt/authentication.rb', line 13 def sign(params) # 添加系统级参数 params = params.merge( appkey: @appkey, sid: @sid, timestamp: Time.now.to_i ) # 将 hash 的 key 全部转化为 string params = stringify_keys(params) # 去掉参数中的 sign 参数 params.delete("sign") # 添加参数签名 signature = get_signature(params) params.merge!("sign" => signature) end |
#stringify_keys(obj) ⇒ Object
把 hash 的 key 从 symbol 转化为 string params :b=>{:c=>2} return “b”=>{“c”=>2}
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/wdt/authentication.rb', line 58 def stringify_keys(obj) return obj.reduce({}) do |memo, (k, v)| memo.tap { |m| m[k.to_s] = stringify_keys(v) } end if obj.is_a? Hash return obj.reduce([]) do |memo, v| memo << stringify_keys(v); memo end if obj.is_a? Array obj end |