panoptes-client 0.3.8 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e752ecee4ee1efa079e7f52cb213e084278edd61
4
- data.tar.gz: 2bee1af072eba3bb0416c1f2ae04abb6b5429673
3
+ metadata.gz: 738ea26bbdcac1cfd64a537f11210c5dbce0fb18
4
+ data.tar.gz: 03143cbb89f128160b9659f744572d1bfa05986c
5
5
  SHA512:
6
- metadata.gz: 95513c729ebbed6c985e3ee77b37842583ce1f55bcb1ac749eb7aad5c833444144d64f71d05aa8e8664d1ff1d8514a26b0616f7e33397cfbe39d769db279c0a7
7
- data.tar.gz: 3f0639d4f6a50583680efac192eb6287968277a64626c062a2f5324704c2e34c410cc8ce8e5f3b46ba09d054ae6c17d51b3560a85ed85c56f96e22f63938278f
6
+ metadata.gz: 8570661b211f00b0f20ab1b076ab1c033d467c89a495e8b4062081e4c6eb246b57555c76ba449d5e16c08b1674b28a778740f11dbb88ec011f15e803c82e0e4a
7
+ data.tar.gz: fd0d5ebb58e5521b884abbe5723e2a09c6c69603f829d38b74e6f43fa40750662b646765edd37945f7e88b0f730ef7e45172ee7d53df4e890f7a3c7fd33b04c9
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /pkg/
9
9
  /spec/reports/
10
10
  /tmp/
11
+ *.gem
@@ -1,3 +1,8 @@
1
+ # 0.4.0
2
+
3
+ * Additional methods for interacting with JWT
4
+ * Refactored all authentication code into separate module
5
+
1
6
  # 0.3.8
2
7
 
3
8
  * Add client.subject(subject_id, *kwargs)
data/Gemfile CHANGED
@@ -3,9 +3,10 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in panoptes-client.gemspec
4
4
  gemspec
5
5
 
6
- gem 'pry'
7
-
8
6
  group :test do
7
+ gem 'pry'
8
+ gem 'pry-byebug'
9
+
9
10
  gem 'vcr'
10
11
  gem 'webmock'
11
12
  end
@@ -1,6 +1,7 @@
1
1
  require 'panoptes/endpoints/json_api_endpoint'
2
2
  require 'panoptes/endpoints/json_endpoint'
3
3
 
4
+ require 'panoptes/client/authentication'
4
5
  require 'panoptes/client/cellect'
5
6
  require 'panoptes/client/classifications'
6
7
  require 'panoptes/client/collections'
@@ -21,6 +22,7 @@ module Panoptes
21
22
  include Panoptes::Client::Me
22
23
 
23
24
  # Panoptes
25
+ include Panoptes::Client::Authentication
24
26
  include Panoptes::Client::Classifications
25
27
  include Panoptes::Client::Collections
26
28
  include Panoptes::Client::Projects
@@ -43,6 +45,7 @@ module Panoptes
43
45
  class ResourceNotFound < GenericError; end
44
46
  class ServerError < GenericError; end
45
47
  class NotLoggedIn < GenericError; end
48
+ class AuthenticationExpired < GenericError; end
46
49
 
47
50
  attr_reader :env, :auth, :panoptes, :talk, :cellect
48
51
 
@@ -61,28 +64,6 @@ module Panoptes
61
64
  )
62
65
  end
63
66
 
64
- def current_user
65
- raise NotLoggedIn unless @auth[:token]
66
-
67
- payload, = JWT.decode @auth[:token], jwt_signing_public_key, algorithm: 'RS512'
68
- payload.fetch('data')
69
- end
70
-
71
- def jwt_signing_public_key
72
- @jwt_signing_public_key ||= OpenSSL::PKey::RSA.new(File.read(@public_key_path))
73
- end
74
-
75
- def public_key_for_env(env)
76
- case env.to_s
77
- when "staging"
78
- File.expand_path(File.join("..", "..", "..", "data", "doorkeeper-jwt-staging.pub"), __FILE__)
79
- when "production"
80
- File.expand_path(File.join("..", "..", "..", "data", "doorkeeper-jwt-production.pub"), __FILE__)
81
- else
82
- nil
83
- end
84
- end
85
-
86
67
  def panoptes_url
87
68
  case env
88
69
  when :production, 'production'.freeze
@@ -0,0 +1,86 @@
1
+ require 'deprecate'
2
+
3
+ module Panoptes
4
+ class Client
5
+ module Authentication
6
+ extend Gem::Deprecate
7
+
8
+ def jwt_payload
9
+ raise NotLoggedIn unless @auth[:token]
10
+ payload, = decode_token(@auth[:token])
11
+ payload
12
+ rescue JWT::ExpiredSignature
13
+ raise AuthenticationExpired
14
+ end
15
+
16
+ def token_contents
17
+ if !@payload.nil? && expiry_from_payload(@payload) > Time.now.utc
18
+ @payload.fetch('data', {})
19
+ elsif @payload.nil?
20
+ @payload = jwt_payload
21
+ @expires_at = expiry_from_payload(@payload)
22
+ @payload.fetch('data', ())
23
+ else
24
+ raise AuthenticationExpired
25
+ end
26
+ end
27
+
28
+ def token_expiry
29
+ @expires_at || expiry_from_payload(jwt_payload)
30
+ end
31
+
32
+ def authenticated?
33
+ !!token_contents['id']
34
+ end
35
+
36
+ def authenticated_user_login
37
+ raise NotLoggedIn unless authenticated?
38
+ token_contents.fetch('login', nil)
39
+ end
40
+
41
+ def authenticated_user_display_name
42
+ raise NotLoggedIn unless authenticated?
43
+ token_contents.fetch('dname', nil)
44
+ end
45
+
46
+ def authenticated_user_id
47
+ raise NotLoggedIn unless authenticated?
48
+ token_contents.fetch('id')
49
+ end
50
+
51
+ def authenticated_admin?
52
+ raise NotLoggedIn unless authenticated?
53
+ token_contents.fetch('admin', false)
54
+ end
55
+
56
+ def current_user
57
+ token_contents
58
+ end
59
+ deprecate :current_user, :token_contents, 2019, 7
60
+
61
+ def jwt_signing_public_key
62
+ @jwt_signing_public_key ||= OpenSSL::PKey::RSA.new(File.read(@public_key_path))
63
+ end
64
+
65
+ def expiry_from_payload(payload)
66
+ Time.at(payload.fetch('exp',0)).utc
67
+ end
68
+
69
+ def decode_token(token)
70
+ payload, = JWT.decode token, jwt_signing_public_key, algorithm: 'RS512'
71
+ payload
72
+ end
73
+
74
+ def public_key_for_env(env)
75
+ case env.to_s
76
+ when "staging"
77
+ File.expand_path(File.join("..","..", "..", "..", "data", "doorkeeper-jwt-staging.pub"), __FILE__)
78
+ when "production"
79
+ File.expand_path(File.join("..","..", "..", "..", "data", "doorkeeper-jwt-production.pub"), __FILE__)
80
+ else
81
+ nil
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -7,7 +7,7 @@ module Panoptes
7
7
  # @param focus_type [String] filter by focussable type
8
8
  # @return list of discussions
9
9
  def create_comment(discussion_id:, body:)
10
- user_id = current_user["id"]
10
+ user_id = token_contents["id"]
11
11
  response = talk.post("/comments", comments: {discussion_id: discussion_id, body: body, user_id: user_id})
12
12
  response.fetch("comments")[0]
13
13
  end
@@ -1,5 +1,5 @@
1
1
  module Panoptes
2
2
  class Client
3
- VERSION = "0.3.8".freeze
3
+ VERSION = "0.4.0".freeze
4
4
  end
5
5
  end
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "deprecate"
21
22
  spec.add_dependency "faraday"
22
23
  spec.add_dependency "faraday-panoptes", "~> 0.3.0"
23
24
  spec.add_dependency "jwt", "~> 1.5.0"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: panoptes-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marten Veldthuis
@@ -10,8 +10,22 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2018-11-21 00:00:00.000000000 Z
13
+ date: 2019-03-11 00:00:00.000000000 Z
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: deprecate
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
15
29
  - !ruby/object:Gem::Dependency
16
30
  name: faraday
17
31
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +164,7 @@ files:
150
164
  - data/doorkeeper-jwt-staging.pub
151
165
  - lib/panoptes-client.rb
152
166
  - lib/panoptes/client.rb
167
+ - lib/panoptes/client/authentication.rb
153
168
  - lib/panoptes/client/cellect.rb
154
169
  - lib/panoptes/client/classifications.rb
155
170
  - lib/panoptes/client/collections.rb
@@ -195,4 +210,3 @@ signing_key:
195
210
  specification_version: 4
196
211
  summary: API wrapper for https://panoptes.zooniverse.org
197
212
  test_files: []
198
- has_rdoc: