uc3-dmp-provenance 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 993e91f781ab200fbc8c0b8c9d1791c6b1199a5b68f2e74431d7776d1e211f31
4
+ data.tar.gz: e25c780228885022b212f94f5398d9e4db77cf92f0d41e55c26e96646bbe3182
5
+ SHA512:
6
+ metadata.gz: b5dd9cfcc96c53b2d86f923c742567157ea830285dec38f1417e3fdde349882f375f0808c13f064147426f5274f159d6806029c64d0815371a362bc2cd969d51
7
+ data.tar.gz: ae2bb06dac1dee500281d3c4c117f9be5926b43901fe45dc1965fc9031921a66b72af346e900c11279c59702e459767307526c4518325901d313f08be7f2b737
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Uc3DmpProvenance
2
+
3
+ Helper methods for working with Provenance JSON records
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uc3-dmp-cognito'
4
+ require 'uc3-dmp-dynamo'
5
+
6
+ module Uc3DmpProvenance
7
+ # Standard Error Message from Uc3DmpProvenance
8
+ class Uc3DmpProvenanceError << StandardError; end
9
+
10
+ # Helper for fetching Provenance JSON records
11
+ class Finder
12
+ class << self
13
+ # Get the Provenance item for the Lambda :event
14
+ #
15
+ # Expecting the :claims hash from the requestContext[:authorizer] portion of the :event.
16
+ # It should look something like this:
17
+ # {
18
+ # "sub": "abcdefghijklmnopqrstuvwxyz",
19
+ # "token_use": "access",
20
+ # "scope": "https://auth.dmphub-dev.cdlib.org/dev.write",
21
+ # "auth_time": "1675895546",
22
+ # "iss": "https://cognito-idp.us-west-2.amazonaws.com/us-west-A_123456",
23
+ # "exp": "Wed Feb 08 22:42:26 UTC 2023",
24
+ # "iat": "Wed Feb 08 22:32:26 UTC 2023",
25
+ # "version": "2",
26
+ # "jti": "5d3be8a7-c595-1111-yyyy-xxxxxxxxxx",
27
+ # "client_id": "abcdefghijklmnopqrstuvwxyz"
28
+ # }
29
+ # -------------------------------------------------------------------------------------------
30
+ def from_lambda_cotext(identity:)
31
+ return nil unless identity.is_a?(Hash) && !identity['iss'].nil? && !identity['client_id'].nil?
32
+
33
+ client = Uc3DmpDynamo::Client.new
34
+ client_name = _cognito_client_id_to_name(claim: identity)
35
+
36
+ resp = client.get_item(
37
+ key: { PK: Helper.append_pk_prefix(client_name), SK: Helper::SK_PROVENANCE_PREFIX }
38
+ )
39
+ resp.nil? || resp.empty? ? nil : resp
40
+ end
41
+
42
+ # Fetch the Provenance by it's PK.
43
+ #
44
+ # Expecting either the name (e.g. `dmptool` or the qualified PK (e.g. `PROVENANCE#dmptool`)
45
+ def from_pk(pk:)
46
+ return nil if pk.nil?
47
+
48
+ pk = Helper.append_pk_prefix(pk)
49
+ resp = client.get_item(
50
+ key: { PK: Helper.append_pk_prefix(pk), SK: Helper::SK_PROVENANCE_PREFIX }
51
+ )
52
+ resp.nil? || resp.empty? ? nil : resp
53
+ end
54
+
55
+ private
56
+
57
+ # Method to fetch the client's name from the Cognito UserPool based on the client_id
58
+ def _cognito_client_id_to_name(claim:)
59
+ return nil if claim.nil? || !claim.is_a?(Hash) || claim['iss'].nil? || claim['client_id'].nil?
60
+
61
+ user_pool_id = claim['iss'].split('/').last
62
+ Uc3DmpCognito.get_client_name(client_id: claim['client_id'])
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aws-sdk-cognitoidentityprovider'
4
+
5
+ module Uc3DmpProvenance
6
+ # Generic helper methods meant for use by the other classes in this gem.
7
+ class Helper
8
+ PK_PROVENANCE_PREFIX = 'PROVENANCE#'
9
+ SK_PROVENANCE_PREFIX = 'PROFILE'
10
+
11
+ DOI_REGEX = %r{[0-9]{2}\.[0-9]{5}/[a-zA-Z0-9/_.-]+}.freeze
12
+ URL_REGEX = %r{(https?://)?([a-zA-Z0-9\-_]\.)+[a-zA-Z0-9\-_]{2,3}(:[0-9]+)?/?}.freeze
13
+
14
+ class << self
15
+ # Append the PK prefix for the object
16
+ # -------------------------------------------------------------------------------------
17
+ def append_pk_prefix(provenance:)
18
+ provenance.is_a?(String) ? "#{PK_PROVENANCE_PREFIX}#{remove_pk_prefix(provenance: provenance)}" : nil
19
+ end
20
+
21
+ # Strip off the PK prefix
22
+ # -------------------------------------------------------------------------------------
23
+ def remove_pk_prefix(provenance:)
24
+ provenance.is_a?(String) ? provenance.gsub(PK_PROVENANCE_PREFIX, '') : provenance
25
+ end
26
+
27
+ # Appends the Provenance system's identifier to the value
28
+ # For a DOI, it will return the DOI as-is
29
+ #
30
+ # For a :provenance whose PK is 'PROVENANCE#example' and homepage is 'https://example.com' and
31
+ # callbackUri is 'https://example.com/callback':
32
+ # when the :value is '12345', it will return 'example#12345'
33
+ # when the :value is 'https://example.com/dmps/12345', it will return 'example#dmp/12345'
34
+ # when the :value is 'https://example.com/callback/12345' it will return 'example#12345'
35
+ #
36
+ # rubocop:disable Metrics/AbcSize
37
+ def format_provenance_callback_url(provenance:, value:)
38
+ # return it as-is if there is no provenance or it's already a URL
39
+ return value if provenance.nil?
40
+
41
+ # return it as-is if it's a DOI
42
+ doi = value.match(DOI_REGEX).to_s
43
+ return value unless doi.nil? || doi == '' || !value.start_with?('http')
44
+
45
+ # Remove the homepage or callbackUri because we will add this when needed. we just want the id
46
+ val = value.downcase
47
+ .gsub(provenance.fetch('callbackUri', '').downcase, '')
48
+ .gsub(provenance.fetch('homepage', '').downcase, '')
49
+ val = val.gsub(%r{https?://}, '')
50
+ val = val[1..val.length] if val.start_with?('/')
51
+ id = provenance['PK']&.gsub(PK_PROVENANCE_PREFIX, '')
52
+ "#{id}##{val}"
53
+ end
54
+ # rubocop:enable Metrics/AbcSize
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uc3DmpProvenance
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,14 @@
1
+ # rubocop:disable Naming/FileName
2
+ # frozen_string_literal: true
3
+
4
+ require 'json'
5
+
6
+ require 'uc3-dmp-dynamo'
7
+
8
+ require 'uc3-dmp-provenance/finder'
9
+ require 'uc3-dmp-provenance/helper'
10
+
11
+ module Uc3DmpProvenance
12
+ MSG_PROVENANCE_NOT_FOUND = 'Provenance does not exist.'
13
+ end
14
+ # rubocop:enable Naming/FileName
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uc3-dmp-provenance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Riley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: logger
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: uc3-dmp-dynamo
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.83'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.83'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 11.1.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 11.1.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 3.9.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.9.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 1.50.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 1.50.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 2.20.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 2.20.0
111
+ description: Helpers for working with JSON that represents a provenance system (e.g.
112
+ DMPTool or NIH API)
113
+ email:
114
+ - brian.riley@ucop.edu
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - README.md
120
+ - lib/uc3-dmp-provenance.rb
121
+ - lib/uc3-dmp-provenance/finder.rb
122
+ - lib/uc3-dmp-provenance/helper.rb
123
+ - lib/uc3-dmp-provenance/version.rb
124
+ homepage: https://github.com/CDLUC3/dmp-hub-cfn/blob/main/src/sam/gems/uc3-dmp-provenance
125
+ licenses:
126
+ - MIT
127
+ metadata:
128
+ rubygems_mfa_required: 'false'
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '2.7'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubygems_version: 3.1.6
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: DMPTool gem that provides support for Provenance records
148
+ test_files: []