pusher-platform 0.6.0 → 0.7.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
- SHA1:
3
- metadata.gz: bdffd489f757406cff5d4919ae39738c7e9f86a8
4
- data.tar.gz: b15393a6ff63d326e4ca39d354e046635db89490
2
+ SHA256:
3
+ metadata.gz: 3f8fa664613e2b4227ca919b6412f448a41b83b6879907344118badaaf31bc6d
4
+ data.tar.gz: f6e2d2ed0058baa06c8b1cb0cb6d258b33a1b19ab4a54ebf903203dfb0327cf5
5
5
  SHA512:
6
- metadata.gz: 2ec68b234270eea843e46a968e9d2fbc4877b01b1d53153a6f23152f0d439fb5162be3b66848e2439be89a5d8bd1eec9cd03bcf453d3fd8e317f64bbbae02138
7
- data.tar.gz: d1d3a2cacdaeb9e2f40e00042e4e8fe8840d4b87c3ab8730b1dc1b336315595c4bfe9266cbbd51c98956eae619573c88a8f000350b896b20b4cd3309b2965a58
6
+ metadata.gz: 01366e9a6c2d267985f5b92d8e88a1d37aaf553caf12fe38eb397ecc0718e82b557efcb193b72eeb960932f2085bfa67b4cf9607d292207cbfc2cdb87efc25ba
7
+ data.tar.gz: cc826fbcdeed2ac0fe95970306e9bc8b1122101d83d998ceb332f45a64fea7be230c8a4fe40f193c52fbb3754473a42e6bfe7515e347ed40e85141686e9407fe
@@ -1,7 +1,8 @@
1
1
  require 'jwt'
2
2
  require 'rack'
3
+ require_relative './error_response'
3
4
 
4
- module Pusher
5
+ module PusherPlatform
5
6
  TOKEN_EXPIRY = 24*60*60
6
7
 
7
8
  class Authenticator
@@ -11,25 +12,13 @@ module Pusher
11
12
  @key_secret = key_secret
12
13
  end
13
14
 
14
- # Takes a Rack request to the authorization endpoint and and handles it
15
- # either returning a new access/refresh token pair, or an error.
16
- #
17
- # @param request [Rack::Request] the request to authenticate
18
- # @return the response object
19
- def authenticate(request, options)
20
- form_data = Rack::Utils.parse_nested_query request.body.read
21
- grant_type = form_data['grant_type']
15
+ def authenticate(auth_payload, options)
16
+ authenticate_based_on_grant_type(auth_payload, options)
17
+ end
22
18
 
23
- if grant_type == "client_credentials"
24
- return authenticate_with_client_credentials(options)
25
- elsif grant_type == "refresh_token"
26
- old_refresh_jwt = form_data['refresh_token']
27
- return authenticate_with_refresh_token(old_refresh_jwt, options)
28
- else
29
- return response(401, {
30
- error: "unsupported_grant_type"
31
- })
32
- end
19
+ def authenticate_with_request(request, options)
20
+ auth_data = Rack::Utils.parse_nested_query request.body.read
21
+ authenticate_based_on_grant_type(auth_data, options)
33
22
  end
34
23
 
35
24
  def generate_access_token(options)
@@ -53,66 +42,82 @@ module Pusher
53
42
 
54
43
  private
55
44
 
45
+ def authenticate_based_on_grant_type(auth_data, options)
46
+ grant_type = auth_data['grant_type'] || auth_data[:grant_type]
47
+
48
+ if grant_type == "client_credentials"
49
+ return authenticate_with_client_credentials(options)
50
+ elsif grant_type == "refresh_token"
51
+ refresh_token = auth_data['refresh_token'] || auth_data[:refresh_token]
52
+ return authenticate_with_refresh_token(refresh_token, options)
53
+ else
54
+ err = ErrorResponse.new({
55
+ status: 401,
56
+ error: 'invalid_grant_type',
57
+ error_description: "Unsupported grant_type #{grant_type}"
58
+ })
59
+ return err
60
+ end
61
+ end
62
+
56
63
  def authenticate_with_client_credentials(options)
57
- return respond_with_new_token_pair(options)
64
+ return new_token_pair(options)
58
65
  end
59
66
 
60
- def authenticate_with_refresh_token(old_refresh_jwt, options)
67
+ def authenticate_with_refresh_token(refresh_token, options)
61
68
  old_refresh_token = begin
62
- JWT.decode(old_refresh_jwt, @key_secret, true, {
69
+ JWT.decode(refresh_token, @key_secret, true, {
63
70
  iss: "api_keys/#{@key_id}",
64
71
  verify_iss: true,
65
72
  }).first
66
73
  rescue => e
67
74
  error_description = if e.is_a?(JWT::InvalidIssuerError)
68
- "refresh token issuer is invalid"
75
+ "Refresh token issuer is invalid"
69
76
  elsif e.is_a?(JWT::ImmatureSignature)
70
- "refresh token is not valid yet"
77
+ "Refresh token is not valid yet"
71
78
  elsif e.is_a?(JWT::ExpiredSignature)
72
- "refresh tokan has expired"
79
+ "Refresh tokan has expired"
73
80
  else
74
- "refresh token is invalid"
81
+ "Refresh token is invalid"
75
82
  end
76
83
 
77
- return response(401, {
78
- error: "invalid_grant",
79
- error_description: error_description,
80
- # TODO error_uri
84
+ err = ErrorResponse.new({
85
+ status: 401,
86
+ error: 'invalid_refresh_token',
87
+ error_description: error_description
81
88
  })
89
+ return err
82
90
  end
83
91
 
84
92
  if old_refresh_token["refresh"] != true
85
- return response(401, {
86
- error: "invalid_grant",
87
- error_description: "refresh token does not have a refresh claim",
88
- # TODO error_uri
93
+ err = ErrorResponse.new({
94
+ status: 401,
95
+ error: 'invalid_refresh_token',
96
+ error_description: "Refresh token does not have a refresh claim"
89
97
  })
98
+ return err
90
99
  end
91
100
 
92
101
  if options[:user_id] != old_refresh_token["sub"]
93
- return response(401, {
94
- error: "invalid_grant",
95
- error_description: "refresh token has an invalid user id",
96
- # TODO error_uri
97
- })
102
+ return ErrorResponse.new(401, "refresh token has an invalid user id")
98
103
  end
99
104
 
100
- return respond_with_new_token_pair(options)
105
+ return new_token_pair(options)
101
106
  end
102
107
 
103
108
  # Creates a payload dictionary made out of access and refresh token pair and TTL for the access token.
104
109
  #
105
110
  # @param user_id [String] optional id of the user, ignore for anonymous users
106
111
  # @return [Hash] Payload as a hash
107
- def respond_with_new_token_pair(options)
112
+ def new_token_pair(options)
108
113
  access_token = generate_access_token(options)[:token]
109
114
  refresh_token = generate_refresh_token(options)[:token]
110
- return response(200, {
115
+ {
111
116
  access_token: access_token,
112
117
  token_type: "bearer",
113
118
  expires_in: TOKEN_EXPIRY,
114
119
  refresh_token: refresh_token,
115
- })
120
+ }
116
121
  end
117
122
 
118
123
  def generate_refresh_token(options)
@@ -128,12 +133,5 @@ module Pusher
128
133
 
129
134
  { token: JWT.encode(claims, @key_secret, 'HS256') }
130
135
  end
131
-
132
- def response(status, body)
133
- return {
134
- status: status,
135
- json: body,
136
- }
137
- end
138
136
  end
139
137
  end
@@ -1,7 +1,8 @@
1
1
  require 'excon'
2
2
  require 'json'
3
+ require_relative './error_response'
3
4
 
4
- module Pusher
5
+ module PusherPlatform
5
6
  class BaseClient
6
7
  def initialize(options)
7
8
  raise "Unspecified host" if options[:host].nil?
@@ -1,4 +1,4 @@
1
- module Pusher
1
+ module PusherPlatform
2
2
  class Error < ::StandardError
3
3
  end
4
4
  end
@@ -1,15 +1,35 @@
1
- module Pusher
1
+ require_relative './common'
2
+
3
+ module PusherPlatform
2
4
  class ErrorResponse < Error
3
- attr_accessor :status, :headers, :description
5
+ attr_reader :status, :headers, :error_description, :error, :error_uri
4
6
 
5
- def initialize(status, headers, description)
6
- @status = status
7
- @headers = headers
8
- @description = description
7
+ def initialize(options)
8
+ @status = options[:status]
9
+ @headers = options[:headers] || {}
10
+ @error = options[:error]
11
+ @error_description = options[:error_description]
12
+ @error_uri = options[:error_uri]
9
13
  end
10
14
 
11
15
  def to_s
12
- "Pusher::ErrorResponse: #{status} #{description}"
16
+ "PusherPlatform::ErrorResponse - status: #{@status} description: #{@error_description}"
17
+ end
18
+
19
+ def as_json(options = {})
20
+ json = {
21
+ status: @status,
22
+ headers: @headers,
23
+ error: @error,
24
+ error_description: @error_description,
25
+ }
26
+ json[:error_uri] = @error_uri unless @error_uri.nil?
27
+ json
13
28
  end
29
+
30
+ def to_json(*options)
31
+ as_json(*options).to_json(*options)
32
+ end
33
+
14
34
  end
15
35
  end
@@ -3,7 +3,7 @@ require_relative './base_client'
3
3
  require_relative './common'
4
4
  require_relative './error_response'
5
5
 
6
- module Pusher
6
+ module PusherPlatform
7
7
 
8
8
  HOST_BASE = 'pusherplatform.io'
9
9
 
@@ -52,8 +52,12 @@ module Pusher
52
52
  @client.request(options)
53
53
  end
54
54
 
55
- def authenticate(request, options)
56
- @authenticator.authenticate(request, options)
55
+ def authenticate(auth_payload, options)
56
+ @authenticator.authenticate(auth_payload, options)
57
+ end
58
+
59
+ def authenticate_with_request(request, options)
60
+ @authenticator.authenticate_with_request(request, options)
57
61
  end
58
62
 
59
63
  def generate_access_token(options)
@@ -1 +1 @@
1
- require 'pusher-platform/instance'
1
+ require_relative 'pusher-platform/instance'
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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pusher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-26 00:00:00.000000000 Z
11
+ date: 2018-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  requirements: []
92
92
  rubyforge_project:
93
- rubygems_version: 2.6.13
93
+ rubygems_version: 2.7.3
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Pusher Platform Ruby SDK