libgss 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDEzZGE2OTI5OGE3MDMzNTgxZjU2Zjg0ZmY4YzVlMDAzYzMwOGQzOA==
4
+ OTdkNjg4OGZmYTAzNDNjN2VjNTM4Mjg0NzIzOGU4MTMzODdjYzgxZQ==
5
5
  data.tar.gz: !binary |-
6
- YjExOGRiOGM5NzlhNDNjNTNkMmRiODM4NDc5MTVhM2ExZjBkNzVhZQ==
6
+ OWY0MWFkMDNmMDEzZTlmMzY0NWI5NzJmMzRhZjU1ZmU3NWFkNDE2OQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZmU2NzFiYThmNDIzMTA0MDk4ODg1NDQzYmIzODYyN2I0MDI1ZmZhY2QzOGJi
10
- YzkwMjY1NDkzNTI1NWIxNDAzYjg5Njc4NDYzOWY2OTdmMWJjYWQ2MWFlOWNk
11
- M2Y1ODdkMjE3MzVhMjllNmViYzI0YTFhYWZkNmI1MjA3Zjc5MjQ=
9
+ ZDcyMzY2YWI4NDVmZWJjZTIzZTE2MTEzMWQyZDBmZTU2Mjk1OWQyZWJiYTdl
10
+ Yjc1MDljNzhmODkzYjUxYmVlYTlmMjFmOGI3MjIxYmFkZDQwNzI2MTQwZmFi
11
+ Zjk2MmM4NGQyZTk4MGE3NWZkNjVlNjU4OTlhMDlhMWJkYTE2YzI=
12
12
  data.tar.gz: !binary |-
13
- Y2Y2NzE0MjEzMGU4MjcwMDc1Mzc4NTBkNTFlMTU5ZmNjOGMyOGRmMDU0Mzc1
14
- ZDY3NTdmYjYyOGJhMmYzZjQ2OTNjMWM2NzM0ZjY3NWM3NDkwNjdmZjA1NTQ3
15
- MjlhOTQ1MjBmODMxYjJhOGFiZjZlMWExMjNjZTM0MWY4NTM5NjE=
13
+ NTA5M2U4NWQ2ZDlhODI4ZTUyZjQyNzgyZGNmNzYyMGQ0ZWE1NmNhNzU5OTk5
14
+ M2M2MjM1MTljZmY2MGFkYzJkNGY2ZGYxNTllNjUxZjMxOWJjNzIwMjhkZWYz
15
+ NTlhZmU3NThmNDIwYmY3ZWY5MDQ4MWRiNWFhZDFkMGFkZDA4MmM=
@@ -0,0 +1,132 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'libgss'
3
+
4
+ require 'oauth'
5
+
6
+ require 'digest/hmac'
7
+
8
+ module Libgss
9
+ class HttpClientWithSignatureKey
10
+
11
+ attr_reader :impl, :network
12
+ def initialize(impl, network)
13
+ @impl, @network = impl, network
14
+ end
15
+
16
+ def post(uri, body, original_headers = {}, &block)
17
+ headers = {
18
+ "oauth_consumer_key" => network.consumer_key || "",
19
+ "oauth_token" => network.auth_token,
20
+ }
21
+ oauth_params = {
22
+ "body" => body,
23
+ "oauth_signature_method" => "HMAC-SHA1"
24
+ }.update(headers)
25
+
26
+ req_hash = {
27
+ "method" => "POST",
28
+ "uri" => uri,
29
+ "parameters" => oauth_params
30
+ }
31
+
32
+ options = {
33
+ :consumer_secret => network.consumer_secret,
34
+ :token_secret => network.signature_key
35
+ }
36
+
37
+ signature_class = Libgss.use_oauth_gem ? ::OAuth::Signature : Signature
38
+ $stdout.puts("signature_class: #{signature_class.name}") if ENV["VERBOSE"]
39
+
40
+ headers["oauth_signature"] = signature_class.sign(req_hash, options)
41
+
42
+ res = @impl.post(uri, body, headers.update(original_headers))
43
+ end
44
+
45
+ class Signature
46
+ class << self
47
+ def sign(req_hash, options)
48
+ new(req_hash, options).sign
49
+ end
50
+ end
51
+
52
+ def initialize(req_hash, options)
53
+ @request, @options = req_hash, options
54
+ end
55
+
56
+ def sign
57
+ s = digest # ダイジェストを計算
58
+ Base64.encode64(s). # BASE64でエンコード
59
+ chomp. # 末尾から改行コード(\r or \n)を取り除いた文字列を返す
60
+ gsub(/\n/,'') # 改行を全て取り除く
61
+ end
62
+
63
+ def digest
64
+ s = signature_base_string # base_stringを計算
65
+ digest_class = ::Digest::SHA1 # http://doc.ruby-lang.org/ja/1.9.3/class/Digest=3a=3aSHA1.html
66
+
67
+ # digest_class.digest(s) # Digestクラスを使って計算
68
+
69
+ # http://doc.ruby-lang.org/ja/1.9.3/library/digest=2fhmac.html
70
+ # http://doc.ruby-lang.org/ja/1.9.3/class/Digest=3a=3aHMAC.html
71
+ ::Digest::HMAC.digest(s, secret, digest_class)
72
+ end
73
+
74
+ def signature_base_string
75
+ base = [
76
+ @request["method"],
77
+ normalized_uri,
78
+ normalized_parameters]
79
+ base.map { |v| escape(v) }.join("&")
80
+ end
81
+
82
+ def normalized_uri
83
+ u = URI.parse(@request["uri"])
84
+ "#{u.scheme.downcase}://#{u.host.downcase}#{(u.scheme.downcase == 'http' && u.port != 80) || (u.scheme.downcase == 'https' && u.port != 443) ? ":#{u.port}" : ""}#{(u.path && u.path != '') ? u.path : '/'}"
85
+ rescue
86
+ @request["uri"]
87
+ end
88
+
89
+ def normalized_parameters
90
+ normalize(parameters_for_signature)
91
+ end
92
+
93
+ def parameters_for_signature
94
+ parameters.reject { |k,v| k == "oauth_signature"}
95
+ end
96
+
97
+ def parameters
98
+ @request["parameters"]
99
+ end
100
+
101
+
102
+ def secret
103
+ "#{escape(@options[:consumer_secret])}&#{escape(@options[:token_secret])}"
104
+ end
105
+
106
+
107
+ RESERVED_CHARACTERS = /[^a-zA-Z0-9\-\.\_\~]/
108
+
109
+ def escape(value)
110
+ URI::escape(value.to_s, RESERVED_CHARACTERS)
111
+ rescue ArgumentError
112
+ URI::escape(value.to_s.force_encoding(Encoding::UTF_8), RESERVED_CHARACTERS)
113
+ end
114
+
115
+ def normalize(params)
116
+ params.sort.map do |k, values|
117
+
118
+ if values.is_a?(Array)
119
+ # multiple values were provided for a single key
120
+ values.sort.collect do |v|
121
+ [escape(k),escape(v)] * "="
122
+ end
123
+ else
124
+ [escape(k),escape(values)] * "="
125
+ end
126
+ end * "&"
127
+ end
128
+
129
+ end
130
+
131
+ end
132
+ end
@@ -17,6 +17,9 @@ module Libgss
17
17
 
18
18
  attr_reader :auth_token, :signature_key
19
19
 
20
+ attr_accessor :consumer_secret
21
+ attr_accessor :consumer_key
22
+
20
23
  attr_accessor :api_version
21
24
  attr_accessor :platform
22
25
  attr_accessor :player_id
@@ -43,6 +46,10 @@ module Libgss
43
46
  end
44
47
  @ssl_base_url = @base_url if @ssl_disabled
45
48
  @platform = "fontana"
49
+
50
+ @consumer_secret = options[:consumer_secret] || ENV["CONSUMER_SECRET"]
51
+ @ignore_signature_key = !!options[:ignore_signature_key]
52
+
46
53
  @httpclient = HTTPClient.new
47
54
  @httpclient.ssl_config.verify_mode = nil # 自己署名の証明書をOKにする
48
55
  end
@@ -53,16 +60,10 @@ module Libgss
53
60
  r << fields.join(", ") << ">"
54
61
  end
55
62
 
56
- def register
57
- res = @httpclient.post(registration_url)
58
- process_json_response(res) do |obj|
59
- self.player_id = obj["player_id"].sub(/\Afontana:/, '')
60
- !!self.player_id
61
- end
62
- end
63
-
64
- def login
65
- res = @httpclient.post(login_url, "player[id]" => player_id)
63
+ def login(extra = {})
64
+ attrs = { "player[id]" => player_id }
65
+ extra.each{|k, v| attrs[ "player[#{k}]" ] = v }
66
+ res = @httpclient.post(login_url, attrs)
66
67
  process_json_response(res) do |obj|
67
68
  @player_id ||= obj["player_id"]
68
69
  @auth_token = obj["auth_token"]
@@ -71,12 +72,17 @@ module Libgss
71
72
  end
72
73
  end
73
74
 
75
+ def ignore_signature_key?
76
+ @ignore_signature_key
77
+ end
78
+
74
79
  def setup
75
- (load_player_id || register) && login
80
+ load_player_id
81
+ login
76
82
  end
77
83
 
78
84
  def new_action_request
79
- ActionRequest.new(@httpclient, action_url)
85
+ ActionRequest.new(httpclient_for_action, action_url)
80
86
  end
81
87
 
82
88
  def new_public_asset_request(asset_path)
@@ -87,6 +93,12 @@ module Libgss
87
93
  AssetRequest.new(@httpclient, protected_asset_url(asset_path))
88
94
  end
89
95
 
96
+ def httpclient_for_action
97
+ @httpclient_for_action ||=
98
+ @ignore_signature_key ? @httpclient :
99
+ HttpClientWithSignatureKey.new(@httpclient, self)
100
+ end
101
+
90
102
  private
91
103
 
92
104
  # ストレージからplayer_idをロードします
@@ -1,3 +1,3 @@
1
1
  module Libgss
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/libgss.rb CHANGED
@@ -6,6 +6,13 @@ module Libgss
6
6
  autoload :Action , "libgss/action"
7
7
  autoload :ActionRequest, "libgss/action_request"
8
8
  autoload :Outputs , "libgss/outputs"
9
+ autoload :HttpClientWithSignatureKey, "libgss/http_client_with_signature_key"
9
10
 
10
11
  autoload :AssetRequest , "libgss/asset_request"
12
+
13
+ class << self
14
+ attr_accessor :use_oauth_gem
15
+ end
16
+
17
+ self.use_oauth_gem = (ENV["USE_OAUTH_GEM"] =~ /\Atrue\Z|\Aon\Z/i)
11
18
  end
data/libgss.gemspec CHANGED
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_runtime_dependency "httpclient"
26
26
  spec.add_runtime_dependency "json"
27
+ spec.add_runtime_dependency "oauth"
27
28
  end
@@ -7,8 +7,7 @@ describe Libgss::ActionRequest do
7
7
  end
8
8
 
9
9
  let(:network) do
10
- network = Libgss::Network.new("http://localhost:3000")
11
- network.player_id = "1000001"
10
+ network = new_network
12
11
  network.login.should == true
13
12
  network
14
13
  end
@@ -3,11 +3,7 @@ require 'spec_helper'
3
3
 
4
4
  describe Libgss::ActionRequest do
5
5
 
6
- let(:network) do
7
- network = Libgss::Network.new("http://localhost:3000")
8
- network.player_id = "1000001"
9
- network
10
- end
6
+ let(:network){ new_network }
11
7
 
12
8
  let(:request) do
13
9
  network.login
@@ -4,8 +4,7 @@ require 'spec_helper'
4
4
  describe "Libgss::ActionRequest friendship" do
5
5
 
6
6
  let(:network) do
7
- network = Libgss::Network.new("http://localhost:3000")
8
- network.player_id = "1000001"
7
+ network = new_network
9
8
  network.login
10
9
  network
11
10
  end
@@ -4,7 +4,7 @@ require 'spec_helper'
4
4
  describe Libgss::ActionRequest do
5
5
 
6
6
  let(:network) do
7
- network = Libgss::Network.new("http://localhost:3000")
7
+ network = new_network
8
8
  network.player_id = "1000001"
9
9
  network.login
10
10
  network
@@ -3,11 +3,7 @@ require 'spec_helper'
3
3
 
4
4
  describe Libgss::ActionRequest do
5
5
 
6
- let(:network) do
7
- network = Libgss::Network.new("http://localhost:3000")
8
- network.player_id = "1000001"
9
- network
10
- end
6
+ let(:network){ new_network }
11
7
 
12
8
  let(:request) do
13
9
  network.login
@@ -3,11 +3,7 @@ require 'spec_helper'
3
3
 
4
4
  describe Libgss::ActionRequest do
5
5
 
6
- let(:network) do
7
- network = Libgss::Network.new("http://localhost:3000")
8
- network.player_id = "1000001"
9
- network
10
- end
6
+ let(:network){ new_network }
11
7
 
12
8
  let(:request) do
13
9
  network.login
@@ -3,11 +3,7 @@ require 'spec_helper'
3
3
 
4
4
  describe Libgss::ActionRequest do
5
5
 
6
- let(:network) do
7
- network = Libgss::Network.new("http://localhost:3000")
8
- network.player_id = "1000001"
9
- network
10
- end
6
+ let(:network){ new_network }
11
7
 
12
8
  let(:request) do
13
9
  network.login
@@ -3,11 +3,7 @@ require 'spec_helper'
3
3
 
4
4
  describe Libgss::ActionRequest do
5
5
 
6
- let(:network) do
7
- network = Libgss::Network.new("http://localhost:3000")
8
- network.player_id = "1000001"
9
- network
10
- end
6
+ let(:network){ new_network }
11
7
 
12
8
  let(:request) do
13
9
  network.login
@@ -4,8 +4,7 @@ require 'spec_helper'
4
4
  describe Libgss::ActionRequest do
5
5
 
6
6
  let(:network) do
7
- network = Libgss::Network.new("http://localhost:3000")
8
- network.player_id = "1000001"
7
+ network = new_network
9
8
  network.login
10
9
  network
11
10
  end
@@ -3,11 +3,7 @@ require 'spec_helper'
3
3
 
4
4
  describe Libgss::ActionRequest do
5
5
 
6
- let(:network) do
7
- network = Libgss::Network.new("http://localhost:3000")
8
- network.player_id = "1000001"
9
- network
10
- end
6
+ let(:network){ new_network }
11
7
 
12
8
  let(:request) do
13
9
  network.login
@@ -7,8 +7,7 @@ describe "Libgss::AssetRequest" do
7
7
  end
8
8
 
9
9
  let(:network) do
10
- network = Libgss::Network.new("http://localhost:3000")
11
- network.player_id = "1000001"
10
+ network = new_network
12
11
  network.public_asset_url_prefix = "http://localhost:3000/a/"
13
12
  network.login
14
13
  network
@@ -6,17 +6,6 @@ describe Libgss::Network do
6
6
  Libgss::Network.new("http://localhost:3000")
7
7
  end
8
8
 
9
- describe "#registration" do
10
- context "valid" do
11
- it do
12
- network.player_id.should == nil
13
- network.register
14
- network.player_id.should_not == nil
15
- network.player_id.should_not =~ /^fontana:/
16
- end
17
- end
18
- end
19
-
20
9
  describe "#setup" do
21
10
  context "valid" do
22
11
  it do
@@ -25,7 +14,7 @@ describe Libgss::Network do
25
14
  network.signature_key.should == nil
26
15
  res = network.setup
27
16
  network.player_id.should_not == nil
28
- network.player_id.should_not =~ /^fontana:/
17
+ network.player_id.should =~ /^fontana:/
29
18
  network.auth_token.should_not == nil
30
19
  network.signature_key.should_not == nil
31
20
  res.should == true
data/spec/spec_helper.rb CHANGED
@@ -7,3 +7,11 @@ def request_fixture_load(fixture_name)
7
7
  c = HTTPClient.new
8
8
  c.post("http://localhost:3000/libgss_test/fixture_loadings/#{fixture_name}.json", "_method" => "put")
9
9
  end
10
+
11
+
12
+ def new_network(url = "http://localhost:3000", player_id = "1000001")
13
+ network = Libgss::Network.new(url)
14
+ network.player_id = player_id
15
+ network.consumer_secret = "cpqomf5gs4ob6prd5w5zd52yg9du7150"
16
+ network
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libgss
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - akima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-31 00:00:00.000000000 Z
11
+ date: 2013-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ! '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: oauth
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: network library for Groovenauts GSS
84
98
  email:
85
99
  - t-akima@groovenauts.jp
@@ -100,6 +114,7 @@ files:
100
114
  - lib/libgss/action.rb
101
115
  - lib/libgss/action_request.rb
102
116
  - lib/libgss/asset_request.rb
117
+ - lib/libgss/http_client_with_signature_key.rb
103
118
  - lib/libgss/network.rb
104
119
  - lib/libgss/outputs.rb
105
120
  - lib/libgss/version.rb