pusher-platform 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c2d621a6b9915f71967e78ca6ad32130fc88049
4
- data.tar.gz: de6d68d17c1df236de5b3cf67f6f3659334e8a27
3
+ metadata.gz: 227190e608cfbaa5d09acc4cc329c6837ee820d1
4
+ data.tar.gz: ffefafb388f8bd3f2687f465bdb535ed97c9615e
5
5
  SHA512:
6
- metadata.gz: 0c8dd63f3a5cc649125cd8cab1cca0e3262b003cfcd5ccdc441f027483384cb1838d4fbb2ba219ca67fc6d7523989f1ee0ed292a431a0868d25b86f937fa0dfb
7
- data.tar.gz: c3dcf874cfe172b2f27983fc6249d6abf5e68d88007623b1b070540bf9ad929da2afb2a0a7987dac22584787959b9419dc3cb3714e8b010178741b20df8d6a49
6
+ metadata.gz: 001425ee5c3bd2c8078fdcc33a0107e71f96644732aaada61bef328a85c7d4c2a2bb464cbc53cb8301b44ddfce8c4fa7a8095c64145fa9cb0970c97bfdd59ae8
7
+ data.tar.gz: 601711c1329d4c4938f79184de57ba25f89172e8b892152cd7545f2e15489356394feb6223dad54f268d39b4a0b38ab810da8df22afd80743556d1a11d37a351
@@ -1 +1 @@
1
- require 'pusher-platform/app'
1
+ require 'pusher-platform/instance'
@@ -6,10 +6,10 @@ module Pusher
6
6
  TOKEN_EXPIRY = 24*60*60
7
7
 
8
8
  class Authenticator
9
- def initialize(app_id, app_key_id, app_key_secret)
10
- @app_id = app_id
11
- @app_key_id = app_key_id
12
- @app_key_secret = app_id
9
+ def initialize(instance_id, key_id, key_secret)
10
+ @instance_id = instance_id
11
+ @key_id = key_id
12
+ @key_secret = key_secret
13
13
  end
14
14
 
15
15
  # Takes a Rack request to the authorization endpoint and and handles it
@@ -33,6 +33,25 @@ module Pusher
33
33
  end
34
34
  end
35
35
 
36
+ def generate_access_token(options)
37
+ now = Time.now.utc.to_i
38
+
39
+ claims = {
40
+ app: @instance_id,
41
+ iss: "api_keys/#{@key_id}",
42
+ iat: now - TOKEN_LEEWAY,
43
+ exp: now + TOKEN_EXPIRY + TOKEN_LEEWAY,
44
+ }
45
+
46
+ claims.merge({ sub: options[:user_id] }) unless options[:user_id].nil?
47
+ claims.merge({ su: true }) if options[:su]
48
+
49
+ {
50
+ token: JWT.encode(claims, @key_secret, 'HS256'),
51
+ expires_in: TOKEN_EXPIRY
52
+ }
53
+ end
54
+
36
55
  private
37
56
 
38
57
  def authenticate_with_client_credentials(options)
@@ -41,8 +60,8 @@ module Pusher
41
60
 
42
61
  def authenticate_with_refresh_token(old_refresh_jwt, options)
43
62
  old_refresh_token = begin
44
- JWT.decode(old_refresh_jwt, @app_key_secret, true, {
45
- iss: "api_keys/#{@app_key_id}",
63
+ JWT.decode(old_refresh_jwt, @key_secret, true, {
64
+ iss: "api_keys/#{@key_id}",
46
65
  verify_iss: true,
47
66
  leeway: 30,
48
67
  }).first
@@ -88,8 +107,8 @@ module Pusher
88
107
  # @param user_id [String] optional id of the user, ignore for anonymous users
89
108
  # @return [Hash] Payload as a hash
90
109
  def respond_with_new_token_pair(options)
91
- access_token = generate_access_token(options)
92
- refresh_token = generate_refresh_token(options)
110
+ access_token = generate_access_token(options)[:token]
111
+ refresh_token = generate_refresh_token(options)[:token]
93
112
  return response(200, {
94
113
  access_token: access_token,
95
114
  token_type: "bearer",
@@ -98,32 +117,18 @@ module Pusher
98
117
  })
99
118
  end
100
119
 
101
- def generate_access_token(options)
102
- now = Time.now.utc.to_i
103
-
104
- claims = {
105
- app: @app_id,
106
- iss: "api_keys/#{@app_key_id}",
107
- iat: now - TOKEN_LEEWAY,
108
- exp: now + TOKEN_EXPIRY + TOKEN_LEEWAY,
109
- sub: options[:user_id],
110
- }
111
-
112
- JWT.encode(claims, @app_key_secret, "HS256")
113
- end
114
-
115
120
  def generate_refresh_token(options)
116
121
  now = Time.now.utc.to_i
117
122
 
118
123
  claims = {
119
- app: @app_id,
120
- iss: "api_keys/#{@app_key_id}",
124
+ app: @instance_id,
125
+ iss: "api_keys/#{@key_id}",
121
126
  iat: now - TOKEN_LEEWAY,
122
127
  refresh: true,
123
128
  sub: options[:user_id],
124
129
  }
125
130
 
126
- JWT.encode(claims, @app_key_secret, "HS256")
131
+ { token: JWT.encode(claims, @key_secret, 'HS256') }
127
132
  end
128
133
 
129
134
  def response(status, body)
@@ -5,7 +5,13 @@ module Pusher
5
5
  class BaseClient
6
6
  def initialize(options)
7
7
  raise "Unspecified host" if options[:host].nil?
8
- @connection = Excon.new("https://#{options[:host]}")
8
+ port_string = options[:port] || ''
9
+ host_string = "https://#{options[:host]}#{port_string}"
10
+ @connection = Excon.new(host_string)
11
+
12
+ @instance_id = options[:instance_id]
13
+ @service_name = options[:service_name]
14
+ @service_version = options[:service_version]
9
15
  end
10
16
 
11
17
  def request(options)
@@ -24,7 +30,7 @@ module Pusher
24
30
 
25
31
  response = @connection.request(
26
32
  method: options[:method],
27
- path: options[:path],
33
+ path: "services/#{@service_name}/#{@service_version}/#{@instance_id}/#{options[:path]}",
28
34
  headers: headers,
29
35
  body: options[:body],
30
36
  )
@@ -0,0 +1,74 @@
1
+ require_relative './authenticator'
2
+ require_relative './base_client'
3
+ require_relative './common'
4
+ require_relative './error_response'
5
+
6
+ module Pusher
7
+
8
+ HOST_BASE = 'pusherplatform.io'
9
+
10
+ class Instance
11
+ def initialize(options)
12
+ raise "No instance provided" if options[:instance].nil?
13
+ raise "No service name provided" if options[:service_name].nil?
14
+ raise "No service version provided" if options[:service_version].nil?
15
+ instance = options[:instance]
16
+ @service_name = options[:service_name]
17
+ @service_version = options[:service_version]
18
+
19
+ key_parts = options[:key].match(/^([^:]+):(.+)$/)
20
+ raise "Invalid key" if key_parts.nil?
21
+
22
+ @key_id = key_parts[1]
23
+ @key_secret = key_parts[2]
24
+
25
+ split_instance = instance.split(':')
26
+
27
+ @platform_version = split_instance[0]
28
+ @cluster = split_instance[1]
29
+ @instance_id = split_instance[2]
30
+
31
+ @client = if options[:client]
32
+ options[:client]
33
+ else
34
+ BaseClient.new(
35
+ host: options[:host] || "#{@cluster}.#{HOST_BASE}",
36
+ port: options[:port],
37
+ instance_id: @instance_id,
38
+ service_name: @service_name,
39
+ service_version: @service_version
40
+ )
41
+ end
42
+
43
+ @authenticator = Authenticator.new(@instance_id, @key_id, @key_secret)
44
+ end
45
+
46
+ def request(options)
47
+ options = scope_request_options(options)
48
+ if options[:jwt].nil?
49
+ options = options.merge(
50
+ { jwt: @authenticator.generate_access_token({ su: true })[:token] }
51
+ )
52
+ end
53
+ @client.request(options)
54
+ end
55
+
56
+ def authenticate(request, options)
57
+ @authenticator.authenticate(request, options)
58
+ end
59
+
60
+ def generate_access_token(options)
61
+ @authenticator.generate_access_token(options)
62
+ end
63
+
64
+ private
65
+
66
+ def scope_request_options(options)
67
+ path = options[:path]
68
+ .gsub(/\/+/, "/")
69
+ .gsub(/\/+$/, "")
70
+ options.merge({ path: path })
71
+ end
72
+
73
+ end
74
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pusher-platform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pusher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-19 00:00:00.000000000 Z
11
+ date: 2017-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon
@@ -65,11 +65,11 @@ extensions: []
65
65
  extra_rdoc_files: []
66
66
  files:
67
67
  - lib/pusher-platform.rb
68
- - lib/pusher-platform/app.rb
69
68
  - lib/pusher-platform/authenticator.rb
70
69
  - lib/pusher-platform/base_client.rb
71
70
  - lib/pusher-platform/common.rb
72
71
  - lib/pusher-platform/error_response.rb
72
+ - lib/pusher-platform/instance.rb
73
73
  homepage:
74
74
  licenses:
75
75
  - MIT
@@ -1,61 +0,0 @@
1
- require_relative './authenticator'
2
- require_relative './base_client'
3
- require_relative './common'
4
- require_relative './error_response'
5
-
6
- module Pusher
7
- class App
8
- def initialize(options)
9
- raise "Invalid app ID" if options[:app_id].nil?
10
- @app_id = options[:app_id]
11
-
12
- app_key_parts = /^([^:]+):(.+)$/.match(options[:app_key])
13
- raise "Invalid app key" if app_key_parts.nil?
14
-
15
- @app_key_id = app_key_parts[1]
16
- @app_key_secret = app_key_parts[2]
17
-
18
- @client = if options[:client]
19
- options[:client]
20
- else
21
- raise "Invalid cluster" if options[:cluster].nil?
22
- BaseClient.new(host: options[:cluster])
23
- end
24
-
25
- @authenticator = Authenticator.new(@app_id, @app_key_id, @app_key_secret)
26
- end
27
-
28
- def request(options)
29
- options = scope_request_options("apps", options)
30
- if options[:jwt].nil?
31
- options = options.merge({ jwt: generate_superuser_jwt() })
32
- end
33
- @client.request(options)
34
- end
35
-
36
- def authenticate(request, options)
37
- @authenticator.authenticate(request, options)
38
- end
39
-
40
- private
41
-
42
- def scope_request_options(prefix, options)
43
- path = "/#{prefix}/#{@app_id}/#{options[:path]}"
44
- .gsub(/\/+/, "/")
45
- .gsub(/\/+$/, "")
46
- options.merge({ path: path })
47
- end
48
-
49
- def generate_superuser_jwt
50
- now = Time.now.utc.to_i
51
- claims = {
52
- app: @app_id,
53
- iss: "api_keys/#{@app_key_id}",
54
- su: true,
55
- iat: now - 30, # some leeway for the server
56
- exp: now + 60*5, # 5 minutes should be enough for a single request
57
- }
58
- JWT.encode(claims, @app_key_secret)
59
- end
60
- end
61
- end