uc3-dmp-rds 0.0.10 → 0.0.12

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
  SHA256:
3
- metadata.gz: 3091e4b4c758fd05dd75c866ef07a3f43f13daa15c909ee32349161f74b8a32f
4
- data.tar.gz: a173d9fa491a0fdb756ec409f8ba0f106e0d155f80a752c23fb9d89964201997
3
+ metadata.gz: 702eaad4224de108ac5263d173ffd37b34ca51871cdfc54f0998f06c8782aa5b
4
+ data.tar.gz: 7fe595f87d591809e7b9295e438368e26f0d9fc5f71f7891ef223bc094b02b24
5
5
  SHA512:
6
- metadata.gz: c762ea939a7536dc10e0a551fe3d8ab6dda43944e59e5a84f32fdcdc9af06712ee4baef7b54e99ed253a99f38cdd12ec39630af990f46cc4957f7189a00c5d4b
7
- data.tar.gz: e82053a5d6a44958f7f08d965493dfa4045f36ab7d5e6735f8cc0fabc013c7d1531af879dc4ad4fd83934602f50a6ef5c67f8a7c6ca8c25bfc88248391236e03
6
+ metadata.gz: 0e79712aed60f791fa03366ad7abe203d9ac3bbba18c1615113c3027619606081b2806f2b3748eadff50dea47487e9257230de8fc743619b1bacaddb1b86ef53
7
+ data.tar.gz: 99014e54e2fb2d97bdbe47950f3f727d75fe9c9d808098fcd1d4c9de830b4a33a8a60e4b7e25c2f9faa3a5657b12d4623bfcd987135f2eb824bb7c9a9dcb83bf
@@ -14,9 +14,37 @@ module Uc3DmpRds
14
14
  MSG_INACTIVE_USER = 'User is inactive'
15
15
 
16
16
  class << self
17
+ # Retrieves the User API token from the headers
18
+ def token_from_headers(headers: {})
19
+ return nil unless headers.is_a?(Hash) && headers.keys.any?
20
+
21
+ authorization = json.select { |k, _v| k.downcase.strip == 'authorization' }
22
+ parts = authorization.split(' ')
23
+ return nil unless parts.first.downcase == 'token' && parts.length == 2
24
+
25
+ parts.last.strip
26
+ end
27
+
28
+ # Look up the user based on the API token. Will fail if Uc3DmpRds::Adapter does not
29
+ # have an established connection!
17
30
  def authenticate(token:)
18
31
  raise AuthenticatorError, MSG_INVALID_TOKEN if token.nil? || token.to_s.strip.empty?
19
32
 
33
+ users = _query_user(token: token)
34
+ raise AuthenticatorError, MSG_INVALID_TOKEN unless users.is_a?(Array) && users.any?
35
+
36
+ user = users.first
37
+ raise AuthenticatorError, MSG_INACTIVE_USER unless user['active']
38
+
39
+ _serialize_user(user: user)
40
+ end
41
+
42
+ private
43
+
44
+ # Query ActiveRecord for the User's record
45
+ def _query_user(token:)
46
+ return nil if token.nil?
47
+
20
48
  sql = <<~SQL.squish
21
49
  SELECT users.firstname, users.surname, users.email, users.active, i.value orcid,
22
50
  orgs.name org_name, ro.name ror_name, ro.ror_id
@@ -32,10 +60,11 @@ module Uc3DmpRds
32
60
  LIMIT 1
33
61
  SQL
34
62
  users = ActiveRecord::Base.simple_execute(sql, token: token.to_s.strip)
35
- raise AuthenticatorError, MSG_INVALID_TOKEN unless users.is_a?(Array) and users.any?
63
+ end
36
64
 
37
- user = users.first
38
- raise AuthenticatorError, MSG_INACTIVE_USER unless user['active']
65
+ # Convert the ActiveRecord query results into a JSON object
66
+ def _serialize_user(user:)
67
+ return {} if user.nil? || user['mbox'].nil?
39
68
 
40
69
  hash = { name: [user['surname'], user['firstname']].join(', '), mbox: user['email'] }
41
70
  hash[:user_id] = { type: 'orcid', identifier: user['orcid'] } unless user['orcid'].nil?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uc3DmpRds
4
- VERSION = '0.0.10'
4
+ VERSION = '0.0.12'
5
5
  end
data/lib/uc3-dmp-rds.rb CHANGED
@@ -5,5 +5,6 @@ require 'uc3-dmp-rds/adapter'
5
5
 
6
6
  # RDS Database adapter
7
7
  module Uc3DmpRds
8
+ MSG_MISSING_TOKEN = 'Missing API Token. Expected header: `Authorization: token 12345`'
8
9
  end
9
10
  # rubocop:enable Naming/FileName
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uc3-dmp-rds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Riley