uc3-dmp-rds 0.0.9 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/uc3-dmp-rds/adapter.rb +5 -3
- data/lib/uc3-dmp-rds/authenticator.rb +50 -0
- data/lib/uc3-dmp-rds/version.rb +1 -1
- data/lib/uc3-dmp-rds.rb +1 -5
- metadata +3 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9758c1818c2bd5f8b44b16bc01bf5a99a98ef04fba84d4e123bc970978e8322c
|
4
|
+
data.tar.gz: b311b8e02f25fb735831b63758d86e9691c3d2e71595016d43281019a385c26d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aeb916ba0027daececd547e5a80146eb579f3c6eb8f05d23a395c8cf0b676132b923ef1488e38492c0301b28e374eae7854a347217f09f15be5a367eb7ee883e
|
7
|
+
data.tar.gz: e90e2d8628a56e911aed3d2b2c182520fc5c7d985b91148c91dab378d07e92f4acd160cb041cada3a6fee792ac4c5d1364a08ee82148917792205f3ad478c6c0
|
data/lib/uc3-dmp-rds/adapter.rb
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
require 'active_record'
|
4
4
|
require 'active_record_simple_execute'
|
5
|
-
require 'aws-sdk-sns'
|
6
|
-
require 'aws-sdk-ssm'
|
7
5
|
require 'mysql2'
|
8
6
|
|
9
7
|
module Uc3DmpRds
|
@@ -24,9 +22,11 @@ module Uc3DmpRds
|
|
24
22
|
MSG_MISSING_CREDENTIALS = 'No username and/or password specified'
|
25
23
|
MSG_UNABLE_TO_CONNECT = 'Unable to establish a connection'
|
26
24
|
MSG_UNABLE_TO_QUERY = 'Unable to process the query'
|
25
|
+
MSG_UNAUTHORIZED = 'You are not authorized to perform that action'
|
27
26
|
|
28
27
|
class << self
|
29
28
|
# Connect to the RDS instance
|
29
|
+
# rubocop:disable Metrics/AbcSize
|
30
30
|
def connect(username:, password:)
|
31
31
|
raise AdapterError, MSG_MISSING_CREDENTIALS if username.nil? || username.to_s.strip.empty? ||
|
32
32
|
password.nil? || password.to_s.strip.empty?
|
@@ -44,9 +44,11 @@ module Uc3DmpRds
|
|
44
44
|
rescue StandardError => e
|
45
45
|
raise AdapterError, "#{MSG_UNABLE_TO_CONNECT} - #{e.message}"
|
46
46
|
end
|
47
|
+
# rubocop:enable Metrics/AbcSize
|
47
48
|
|
48
49
|
# Execute the specified query using ActiveRecord's helpers to sanitize the input
|
49
|
-
def execute_query(sql:, **params)
|
50
|
+
def execute_query(user:, sql:, **params)
|
51
|
+
raise AdapterError, MSG_UNAUTHORIZED unless user.is_a?(User)
|
50
52
|
return [] unless sql.is_a?(String) && !sql.strip.empty? && (params.nil? || params.is_a?(Hash))
|
51
53
|
# Verify that all of the kewords are accounted for and that values were supplied
|
52
54
|
raise AdapterError, MSG_KEYWORDS_INVALID unless _verify_params(sql: sql, params: params)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_record_simple_execute'
|
5
|
+
require 'mysql2'
|
6
|
+
|
7
|
+
module Uc3DmpRds
|
8
|
+
# Error from the Rds Adapter
|
9
|
+
class AuthenticatorError < StandardError; end
|
10
|
+
|
11
|
+
# Use Rails' ActiveResource to communicate with the DMPHub REST API
|
12
|
+
class Authenticator
|
13
|
+
MSG_INVALID_TOKEN = 'Invalid user token'
|
14
|
+
MSG_INACTIVE_USER = 'User is inactive'
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def authenticate(token:)
|
18
|
+
raise AuthenticatorError, MSG_INVALID_TOKEN if token.nil? || token.to_s.strip.empty?
|
19
|
+
|
20
|
+
sql = <<~SQL.squish
|
21
|
+
SELECT users.firstname, users.surname, users.email, users.active, i.value orcid,
|
22
|
+
orgs.name org_name, ro.name ror_name, ro.ror_id
|
23
|
+
FROM users
|
24
|
+
INNER JOIN orgs ON users.org_id = orgs.id
|
25
|
+
LEFT OUTER JOIN registry_orgs ro
|
26
|
+
ON orgs.id = ro.org_id
|
27
|
+
LEFT OUTER JOIN identifiers i
|
28
|
+
ON i.identifiable_id = users.id
|
29
|
+
AND i.identifiable_type = 'User'
|
30
|
+
AND i.identifier_scheme_id IN (SELECT sch.id FROM identifier_schemes sch WHERE sch.name = 'orcid')
|
31
|
+
WHERE users.api_token = :token
|
32
|
+
LIMIT 1
|
33
|
+
SQL
|
34
|
+
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?
|
36
|
+
|
37
|
+
user = users.first
|
38
|
+
raise AuthenticatorError, MSG_INACTIVE_USER unless user['active']
|
39
|
+
|
40
|
+
hash = { name: [user['surname'], user['firstname']].join(', '), mbox: user['email'] }
|
41
|
+
hash[:user_id] = { type: 'orcid', identifier: user['orcid'] } unless user['orcid'].nil?
|
42
|
+
return hash.to_json if user['org_name'].nil?
|
43
|
+
|
44
|
+
hash[:affiliation] = { name: user.fetch('ror_name', user['org_name']) }
|
45
|
+
hash[:affiliation][:affiliation_id] = { type: 'ror', identifier: user['ror_id'] } unless user['ror_id'].nil?
|
46
|
+
hash.to_json
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/uc3-dmp-rds/version.rb
CHANGED
data/lib/uc3-dmp-rds.rb
CHANGED
@@ -1,14 +1,10 @@
|
|
1
1
|
# rubocop:disable Naming/FileName
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
require 'aws-sdk-sns'
|
5
|
-
require 'aws-sdk-ssm'
|
6
|
-
|
7
|
-
require 'uc3-dmp-api-core'
|
8
|
-
|
9
4
|
require 'uc3-dmp-rds/adapter'
|
10
5
|
|
11
6
|
# RDS Database adapter
|
12
7
|
module Uc3DmpRds
|
8
|
+
MSG_MISSING_TOKEN = 'Missing API Token. Expected header: `Authorization: token 12345`'
|
13
9
|
end
|
14
10
|
# rubocop:enable Naming/FileName
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uc3-dmp-rds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Riley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_record_simple_execute
|
@@ -24,34 +24,6 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.9.1
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: aws-sdk-sns
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '1.60'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '1.60'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: aws-sdk-ssm
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '1.150'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '1.150'
|
55
27
|
- !ruby/object:Gem::Dependency
|
56
28
|
name: json
|
57
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -160,6 +132,7 @@ files:
|
|
160
132
|
- README.md
|
161
133
|
- lib/uc3-dmp-rds.rb
|
162
134
|
- lib/uc3-dmp-rds/adapter.rb
|
135
|
+
- lib/uc3-dmp-rds/authenticator.rb
|
163
136
|
- lib/uc3-dmp-rds/version.rb
|
164
137
|
homepage: https://github.com/CDLUC3/dmp-hub-cfn/blob/main/src/sam/gems/uc3-dmp-rds
|
165
138
|
licenses:
|