ws-net_documents-use_token 2.1.0

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: c04281820113c57642e727a864574408d7cbb65733389bc058f9e9df5d8ea563
4
+ data.tar.gz: f51708c5c693f0c9b82e462b03cbfece024fa7ee42a5f4a915e7797534e8465f
5
+ SHA512:
6
+ metadata.gz: af7f51a679cb0930e6363451e7a3ee1fe79cf325261ba3f467fe6a1fc82f1352ec5786ab8170237ba8b126c212390ee8d24b8f1ff80dd5d6c2bacf3fe53e8d90
7
+ data.tar.gz: e73b1490b63804acd85fa3aebc60e5a9e936ced4cae33d72c5d815445f75a0b619fb3fee71252b5b460bb4aeeab0641cb2b46f690e2d35e65542b78c0b18fded
@@ -0,0 +1,7 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ module Controls
4
+ AccessToken = NetDocuments::Client::Controls::AccessToken
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ module Controls
4
+ module ID
5
+ def self.example(...) = Identifier::UUID::Controls::Incrementing.example(...)
6
+ def self.random = Identifier::UUID::Random.get
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ module Controls
4
+ module Provider
5
+ def self.example
6
+ UseToken::Provider.name
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ module Controls
4
+ RefreshToken = NetDocuments::Client::Controls::RefreshToken
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ module Controls
4
+ Time = NetDocuments::Client::Controls::Time
5
+
6
+ module Time
7
+ Expiration = GetToken::Controls::Time::Expiration
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,64 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ module Controls
4
+ module Token
5
+ User = NetDocuments::Client::Controls::Token::User
6
+
7
+ def self.example(provider: nil, user_id: nil, access_token: nil, refresh_token: nil, version: nil, expiration_time: nil)
8
+ provider ||= self.provider
9
+ user_id ||= self.user_id
10
+ access_token ||= self.access_token
11
+ refresh_token ||= self.refresh_token
12
+ version ||= self.version
13
+ expiration_time ||= self.expiration_time
14
+
15
+ token = UseToken::Token.new
16
+ token.provider = provider
17
+ token.user_id = user_id
18
+ token.access_token = access_token
19
+ token.refresh_token = refresh_token
20
+ token.version = version
21
+ token.expiration_time = expiration_time
22
+
23
+ token
24
+ end
25
+
26
+ def self.provider
27
+ Provider.example
28
+ end
29
+
30
+ def self.user_id
31
+ ID.example
32
+ end
33
+
34
+ def self.access_token
35
+ AccessToken.example
36
+ end
37
+
38
+ def self.refresh_token
39
+ RefreshToken.example
40
+ end
41
+
42
+ def self.version
43
+ TokenVersion.example
44
+ end
45
+
46
+ def self.expiration_time
47
+ Time::Expiration.example
48
+ end
49
+
50
+ module Expired
51
+ def self.example(expiration_time: nil, refresh_token: nil)
52
+ expiration_time ||= Controls::Time::Offset.example(offset_milliseconds:)
53
+
54
+ Token.example(expiration_time:, refresh_token:)
55
+ end
56
+
57
+ def self.offset_milliseconds
58
+ -1
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,7 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ module Controls
4
+ TokenVersion = GetToken::Controls::TokenVersion
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ module Controls
4
+ module Write
5
+ Granted = TokenComponent::Controls::Write::Events::Granted
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ module Controls
4
+ module Write
5
+ Issued = TokenComponent::Controls::Write::Events::Issued
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ require "identifier/uuid/controls"
2
+
3
+ require "net_documents/client/controls"
4
+ require "token_component/controls"
5
+ require "get_token/controls"
6
+
7
+ require "net_documents/use_token/controls/time"
8
+ require "net_documents/use_token/controls/id"
9
+ require "net_documents/use_token/controls/provider"
10
+ require "net_documents/use_token/controls/token_version"
11
+ require "net_documents/use_token/controls/access_token"
12
+ require "net_documents/use_token/controls/refresh_token"
13
+ require "net_documents/use_token/controls/token"
14
+ require "net_documents/use_token/controls/write/issued"
15
+ require "net_documents/use_token/controls/write/granted"
@@ -0,0 +1,11 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ class Log < Log::Structured
4
+ def tag!(tags)
5
+ tags << :net_documents_use_token
6
+ tags << :net_documents
7
+ tags << :token
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ module Provider
4
+ def self.name
5
+ TokenProvider.net_documents
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ class Result
4
+ attr_accessor :value
5
+
6
+ def success
7
+ @success ||= false
8
+ end
9
+ attr_writer :success
10
+ alias :success? :success
11
+
12
+ def self.failure
13
+ result = Result.new
14
+ result.success = false
15
+ result
16
+ end
17
+
18
+ def self.success(value)
19
+ result = Result.new
20
+ result.value = value
21
+ result.success = true
22
+ result
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ Token = GetToken::Token
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ module Substitute
4
+ def self.build
5
+ UseToken.new
6
+ end
7
+
8
+ class UseToken
9
+ attr_accessor :access_token
10
+ alias :set_access_token :access_token=
11
+
12
+ def failure
13
+ @failure ||= false
14
+ end
15
+ alias :failure? :failure
16
+ attr_writer :failure
17
+
18
+ def set_failure
19
+ self.failure = true
20
+ end
21
+
22
+ def call(user_id, &block)
23
+ if failure?
24
+ return Result.failure
25
+ end
26
+
27
+ value = block.call(access_token)
28
+
29
+ Result.success(value)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,145 @@
1
+ module NetDocuments
2
+ class UseToken
3
+ TokenExpiredError = Class.new(RuntimeError)
4
+
5
+ include Dependency
6
+ include Initializer
7
+ include Log::Dependency
8
+
9
+ initializer :user_id
10
+
11
+ dependency :get_token, GetToken
12
+ dependency :refresh, NetDocuments::Client::Token::User::Refresh
13
+ dependency :issue, TokenComponent::Client::Issue
14
+ dependency :request, TokenComponent::Client::Request
15
+ dependency :clock, Clock::UTC
16
+
17
+ def self.provider
18
+ Provider.name
19
+ end
20
+
21
+ def self.build(user_id)
22
+ instance = new(user_id)
23
+
24
+ GetToken.configure(instance, provider)
25
+ NetDocuments::Client::Token::User::Refresh.configure(instance, attr_name: :refresh)
26
+ TokenComponent::Client::Issue.configure(instance, provider)
27
+ TokenComponent::Client::Request.configure(instance)
28
+ Clock::UTC.configure(instance)
29
+
30
+ instance
31
+ end
32
+
33
+ def self.configure(receiver, user_id, attr_name: nil)
34
+ attr_name ||= :use_net_documents_token
35
+ instance = build(user_id)
36
+ receiver.public_send(:"#{attr_name}=", instance)
37
+ end
38
+
39
+ def self.call(user_id, &block)
40
+ instance = build(user_id)
41
+ instance.(&block)
42
+ end
43
+
44
+ def call(&block)
45
+ token = get_token.(user_id)
46
+
47
+ result = Result.new
48
+
49
+ if token.nil?
50
+ logger.info("Missing NetDocuments token", { user_id: user_id })
51
+ return result
52
+ end
53
+
54
+ access_token = token.access_token
55
+ refresh_token = token.refresh_token
56
+ token_version = token.version
57
+
58
+ if token.expired?(clock.now)
59
+ logger.debug("NetDocuments token is expired", { user_id: user_id, expiration_time: token.expiration_time })
60
+ access_token = refresh_access_token(refresh_token, token_version)
61
+
62
+ if access_token.nil?
63
+ return result
64
+ end
65
+ end
66
+
67
+ begin
68
+ result.value = block.call(access_token)
69
+ result.success = true
70
+
71
+ rescue ::NetDocuments::Client::TokenRejectedError
72
+ logger.debug("NetDocuments token is rejected", { user_id: user_id, expiration_time: token.expiration_time })
73
+
74
+ access_token = refresh_access_token(refresh_token, token_version)
75
+
76
+ if not access_token.nil?
77
+ retry
78
+ end
79
+ end
80
+
81
+ result
82
+ end
83
+
84
+ def refresh_access_token(refresh_token, token_version)
85
+ logger.trace("Refreshing NetDocuments token", { user_id: user_id })
86
+
87
+ begin
88
+ refresh_token_response = refresh.(refresh_token)
89
+ rescue ::NetDocuments::Client::Token::InvalidGrantError
90
+ logger.debug("NetDocuments token refresh failed", { user_id: user_id })
91
+
92
+ request_new_token(token_version)
93
+ return nil
94
+ end
95
+
96
+ issued_time = clock.now
97
+ user_token = ::NetDocuments::Client::Token::User::Token.import(refresh_token_response, issued_time)
98
+ access_token = user_token.access_token
99
+
100
+ issue_token(user_token)
101
+
102
+ logger.debug("Refreshed NetDocuments token", { user_id: user_id })
103
+
104
+ access_token
105
+ end
106
+
107
+ def issue_token(user_token)
108
+ logger.trace("Issuing new NetDocuments token", { user_id: user_id })
109
+
110
+ issue_id = Identifier::UUID.random
111
+
112
+ access_token = user_token.access_token
113
+ refresh_token = user_token.refresh_token
114
+ expiration_time = user_token.expiration_time
115
+
116
+ issue.(
117
+ user_id: user_id,
118
+ issue_id: issue_id,
119
+ access_token: access_token,
120
+ refresh_token: refresh_token,
121
+ expiration_time: expiration_time
122
+ )
123
+
124
+ logger.debug("Issued new NetDocuments token", { user_id: user_id, issue_id: issue_id })
125
+ end
126
+
127
+ def request_new_token(obsolete_token_version)
128
+ logger.trace("Requesting new NetDocuments token", { user_id: user_id })
129
+
130
+ request_id = Identifier::UUID::Random.get
131
+ correlation_stream_name = nil
132
+
133
+ request.(
134
+ user_id: user_id,
135
+ request_id: request_id,
136
+ provider: self.class.provider,
137
+ obsolete_token_version: obsolete_token_version,
138
+ correlation_stream_name: correlation_stream_name,
139
+ previous_message: nil
140
+ )
141
+
142
+ logger.debug("Requested new NetDocuments token", { user_id: user_id, request_id: request_id })
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,15 @@
1
+ require "identifier/uuid"
2
+ require "message_store/postgres"
3
+
4
+ require "log/structured"
5
+ require "substitute/record"
6
+ require "net_documents/client"
7
+ require "token_provider"
8
+ require "get_token"
9
+
10
+ require "net_documents/use_token/log"
11
+ require "net_documents/use_token/token"
12
+ require "net_documents/use_token/provider"
13
+ require "net_documents/use_token/result"
14
+ require "net_documents/use_token/use_token"
15
+ require "net_documents/use_token/use_token/substitute"
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ws-net_documents-use_token
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.0
5
+ platform: ruby
6
+ authors:
7
+ - WSGR
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ws-get_token
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ws-net_documents-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ws-token_provider
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ws-substitute-record
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: ws-log-structured
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: evt-clock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: evt-identifier-uuid
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: evt-message_store-postgres
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: " "
126
+ email: no-reply@wsgr.com
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - lib/net_documents/use_token.rb
132
+ - lib/net_documents/use_token/controls.rb
133
+ - lib/net_documents/use_token/controls/access_token.rb
134
+ - lib/net_documents/use_token/controls/id.rb
135
+ - lib/net_documents/use_token/controls/provider.rb
136
+ - lib/net_documents/use_token/controls/refresh_token.rb
137
+ - lib/net_documents/use_token/controls/time.rb
138
+ - lib/net_documents/use_token/controls/token.rb
139
+ - lib/net_documents/use_token/controls/token_version.rb
140
+ - lib/net_documents/use_token/controls/write/granted.rb
141
+ - lib/net_documents/use_token/controls/write/issued.rb
142
+ - lib/net_documents/use_token/log.rb
143
+ - lib/net_documents/use_token/provider.rb
144
+ - lib/net_documents/use_token/result.rb
145
+ - lib/net_documents/use_token/token.rb
146
+ - lib/net_documents/use_token/use_token.rb
147
+ - lib/net_documents/use_token/use_token/substitute.rb
148
+ homepage: https://github.com/wsgr-neuron/net-documents-use-token
149
+ licenses:
150
+ - Proprietary
151
+ metadata:
152
+ github_repo: ssh://github.com/wsgr-neuron/net-documents-use-token
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '3.0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubygems_version: 3.3.3
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: ''
172
+ test_files: []