minisky 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +41 -15
- data/LICENSE.txt +1 -1
- data/README.md +22 -15
- data/SECURITY.md +16 -0
- data/lib/minisky/compat.rb +22 -0
- data/lib/minisky/errors.rb +76 -4
- data/lib/minisky/minisky.rb +52 -11
- data/lib/minisky/requests.rb +360 -41
- data/lib/minisky/version.rb +3 -1
- metadata +12 -17
- data/example/ask_password.rb +0 -76
- data/example/fetch_my_posts.rb +0 -46
- data/example/fetch_profile.rb +0 -53
- data/example/find_missing_follows.rb +0 -67
- data/example/post_skeet.rb +0 -36
- data/example/science_feed.rb +0 -53
data/lib/minisky/requests.rb
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require_relative 'minisky'
|
|
2
4
|
require_relative 'errors'
|
|
3
5
|
|
|
4
6
|
require 'base64'
|
|
5
7
|
require 'json'
|
|
6
8
|
require 'net/http'
|
|
9
|
+
require 'set'
|
|
7
10
|
require 'time'
|
|
8
11
|
require 'uri'
|
|
9
12
|
|
|
@@ -13,6 +16,10 @@ class Minisky
|
|
|
13
16
|
@config = config
|
|
14
17
|
end
|
|
15
18
|
|
|
19
|
+
def has_credentials?
|
|
20
|
+
!!(id && pass)
|
|
21
|
+
end
|
|
22
|
+
|
|
16
23
|
def logged_in?
|
|
17
24
|
!!(access_token && refresh_token)
|
|
18
25
|
end
|
|
@@ -26,19 +33,69 @@ class Minisky
|
|
|
26
33
|
end
|
|
27
34
|
end
|
|
28
35
|
|
|
36
|
+
# Regexp for NSID identifiers, used in lexicon names for record collection and API endpoints
|
|
29
37
|
NSID_REGEXP = /^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\.[a-zA-Z]([a-zA-Z]{0,61}[a-zA-Z])?)$/
|
|
30
38
|
|
|
39
|
+
#
|
|
40
|
+
# This module contains most of the Minisky code for making HTTP requests and managing
|
|
41
|
+
# authentication tokens. The module is included into the {Minisky} API client class and you'll
|
|
42
|
+
# normally use it through that class, but you can also include it into your custom class if you
|
|
43
|
+
# want to implement the data storage differently than using a local YAML file as {Minisky} does.
|
|
44
|
+
#
|
|
45
|
+
|
|
31
46
|
module Requests
|
|
47
|
+
|
|
48
|
+
# A character to print before each request in {#fetch_all} as a progress indicator.
|
|
49
|
+
# Can also be passed explicitly instead or overridden using the `progress:` parameter.
|
|
50
|
+
# Default is `'.'` when running inside IRB, and `nil` otherwise.
|
|
51
|
+
#
|
|
52
|
+
# @return [String, nil]
|
|
53
|
+
#
|
|
32
54
|
attr_accessor :default_progress
|
|
55
|
+
|
|
56
|
+
# By default, when `fetch_all` receives a response with an empty page with no records but
|
|
57
|
+
# which includes a cursor for the next page, it keeps fetching until it receives a response
|
|
58
|
+
# with null cursor. This is more technically correct, but can cause problems with some
|
|
59
|
+
# non-compliant APIs, so you can set this option to stop fetching when an empty page
|
|
60
|
+
# is received.
|
|
61
|
+
#
|
|
62
|
+
# @return [Boolean]
|
|
63
|
+
#
|
|
64
|
+
attr_accessor :stop_fetch_on_empty_page
|
|
65
|
+
|
|
66
|
+
# If set, the `open_timeout` and `read_timeout` parameters will be set to this value
|
|
67
|
+
# on network requests.
|
|
68
|
+
#
|
|
69
|
+
# @return [Numeric, nil]
|
|
70
|
+
#
|
|
71
|
+
attr_accessor :timeout
|
|
72
|
+
|
|
33
73
|
attr_writer :send_auth_headers
|
|
34
74
|
attr_writer :auto_manage_tokens
|
|
35
75
|
|
|
76
|
+
# Tells whether to set authentication headers automatically (default: true if there
|
|
77
|
+
# is a user config).
|
|
78
|
+
#
|
|
79
|
+
# If false, you will need to pass `auth: 'sometoken'` explicitly to requests that
|
|
80
|
+
# require authentication.
|
|
81
|
+
#
|
|
82
|
+
# @return [Boolean] whether to set authentication headers in requests
|
|
83
|
+
#
|
|
36
84
|
def send_auth_headers
|
|
37
|
-
instance_variable_defined?('@send_auth_headers') ? @send_auth_headers :
|
|
85
|
+
instance_variable_defined?('@send_auth_headers') ? @send_auth_headers : (config != nil)
|
|
38
86
|
end
|
|
39
87
|
|
|
88
|
+
# Tells whether the library should manage the access & refresh tokens automatically
|
|
89
|
+
# for you (default: true if there is a user config).
|
|
90
|
+
#
|
|
91
|
+
# If true, {#check_access} is called before each request to make sure that there is a
|
|
92
|
+
# fresh access token available; if false, you will need to call {#log_in} and
|
|
93
|
+
# {#perform_token_refresh} manually when needed.
|
|
94
|
+
#
|
|
95
|
+
# @return [Boolean] whether to automatically manage access tokens
|
|
96
|
+
#
|
|
40
97
|
def auto_manage_tokens
|
|
41
|
-
instance_variable_defined?('@auto_manage_tokens') ? @auto_manage_tokens :
|
|
98
|
+
instance_variable_defined?('@auto_manage_tokens') ? @auto_manage_tokens : (config != nil)
|
|
42
99
|
end
|
|
43
100
|
|
|
44
101
|
alias progress default_progress
|
|
@@ -53,9 +110,38 @@ class Minisky
|
|
|
53
110
|
end
|
|
54
111
|
|
|
55
112
|
def user
|
|
56
|
-
@user ||= User.new(config)
|
|
113
|
+
@user ||= config && User.new(config)
|
|
57
114
|
end
|
|
58
115
|
|
|
116
|
+
# Sends a GET request to the service's API.
|
|
117
|
+
#
|
|
118
|
+
# @param method [String, URI] an XRPC endpoint name or a full URL
|
|
119
|
+
# @param params [Hash, nil] query parameters
|
|
120
|
+
#
|
|
121
|
+
# @param auth [Boolean, String]
|
|
122
|
+
# boolean value which tells whether to send an auth header with the access token or not,
|
|
123
|
+
# or an explicit bearer token to use
|
|
124
|
+
# @param headers [Hash, nil]
|
|
125
|
+
# additional headers to include
|
|
126
|
+
#
|
|
127
|
+
# @return [Hash, String] parsed JSON hash for JSON responses, or raw response body otherwise
|
|
128
|
+
#
|
|
129
|
+
# @raise [ArgumentError] if method name is invalid
|
|
130
|
+
# @raise [BadResponse] if the HTTP response has an error status code
|
|
131
|
+
# @raise [AuthError]
|
|
132
|
+
# - if logging in is required, but login or password isn't provided
|
|
133
|
+
# - if token refresh is needed, but refresh token is missing
|
|
134
|
+
# - if a token has invalid format
|
|
135
|
+
# - if required access token is missing, and {#auto_manage_tokens} is disabled
|
|
136
|
+
#
|
|
137
|
+
# @example Unauthenticated call
|
|
138
|
+
# sky = Minisky.new('public.api.bsky.app', nil)
|
|
139
|
+
# profile = sky.get_request('app.bsky.actor.getProfile', { actor: 'ec.europa.eu' })
|
|
140
|
+
#
|
|
141
|
+
# @example Authenticated call
|
|
142
|
+
# sky = Minisky.new('blacksky.app', 'config.yml')
|
|
143
|
+
# feed = sky.get_request('app.bsky.feed.getTimeline', { limit: 100 })
|
|
144
|
+
|
|
59
145
|
def get_request(method, params = nil, auth: default_auth_mode, headers: nil)
|
|
60
146
|
check_access if auto_manage_tokens && auth == true
|
|
61
147
|
|
|
@@ -72,16 +158,52 @@ class Minisky
|
|
|
72
158
|
handle_response(response)
|
|
73
159
|
end
|
|
74
160
|
|
|
161
|
+
# Sends a POST request to the service's API.
|
|
162
|
+
#
|
|
163
|
+
# @param method [String, URI] an XRPC endpoint name or a full URL
|
|
164
|
+
# @param data [Hash, String, nil] JSON or string data to send
|
|
165
|
+
#
|
|
166
|
+
# @param auth [Boolean, String]
|
|
167
|
+
# boolean value which tells whether to send an auth header with the access token or not,
|
|
168
|
+
# or an explicit bearer token to use
|
|
169
|
+
# @param headers [Hash, nil]
|
|
170
|
+
# additional headers to include
|
|
171
|
+
# @param params [Hash, nil]
|
|
172
|
+
# query parameters to append to the URL
|
|
173
|
+
#
|
|
174
|
+
# @return [Hash, String] parsed JSON hash for JSON responses, or raw response body otherwise
|
|
175
|
+
#
|
|
176
|
+
# @raise [ArgumentError] if method name is invalid
|
|
177
|
+
# @raise [BadResponse] if the HTTP response has an error status code
|
|
178
|
+
# @raise [AuthError]
|
|
179
|
+
# - if logging in is required, but login or password isn't provided
|
|
180
|
+
# - if token refresh is needed, but refresh token is missing
|
|
181
|
+
# - if a token has invalid format
|
|
182
|
+
# - if required access token is missing, and {#auto_manage_tokens} is disabled
|
|
183
|
+
#
|
|
184
|
+
# @example Making a Bluesky post
|
|
185
|
+
# sky = Minisky.new('lab.martianbase.net', 'config.yml')
|
|
186
|
+
#
|
|
187
|
+
# sky.post_request('com.atproto.repo.createRecord', {
|
|
188
|
+
# repo: sky.user.did,
|
|
189
|
+
# collection: 'app.bsky.feed.post',
|
|
190
|
+
# record: {
|
|
191
|
+
# text: "Hello Bluesky!",
|
|
192
|
+
# createdAt: Time.now.iso8601,
|
|
193
|
+
# langs: ['en']
|
|
194
|
+
# }
|
|
195
|
+
# })
|
|
196
|
+
|
|
75
197
|
def post_request(method, data = nil, auth: default_auth_mode, headers: nil, params: nil)
|
|
76
198
|
check_access if auto_manage_tokens && auth == true
|
|
77
199
|
|
|
78
200
|
headers = authentication_header(auth).merge(headers || {})
|
|
79
|
-
headers["Content-Type"] = "application/json" unless headers.keys.any? { |k| k.to_s.downcase == 'content-type' }
|
|
80
201
|
|
|
81
|
-
|
|
82
|
-
data.to_s
|
|
202
|
+
if data.is_a?(String) || data.nil?
|
|
203
|
+
body = data.to_s
|
|
83
204
|
else
|
|
84
|
-
data.to_json
|
|
205
|
+
body = data.to_json
|
|
206
|
+
headers["Content-Type"] = "application/json" unless headers.keys.any? { |k| k.to_s.downcase == 'content-type' }
|
|
85
207
|
end
|
|
86
208
|
|
|
87
209
|
url = build_request_uri(method)
|
|
@@ -90,33 +212,108 @@ class Minisky
|
|
|
90
212
|
url.query = URI.encode_www_form(params)
|
|
91
213
|
end
|
|
92
214
|
|
|
93
|
-
|
|
215
|
+
request = Net::HTTP::Post.new(url, headers)
|
|
216
|
+
request.body = body
|
|
217
|
+
|
|
218
|
+
response = make_request(request)
|
|
94
219
|
handle_response(response)
|
|
95
220
|
end
|
|
96
221
|
|
|
222
|
+
# Fetches and merges paginated responses from a service's endpoint in a loop, updating the
|
|
223
|
+
# cursor after each page, until the cursor is nil or a break condition is met. The data is
|
|
224
|
+
# extracted from a designated field of the response (`field`) and added to a single array,
|
|
225
|
+
# which is returned at the end.
|
|
226
|
+
#
|
|
227
|
+
# A condition for when the fetching should stop can be passed as a block in `break_when`, or
|
|
228
|
+
# alternatively, a max number of pages can be passed to `max_pages` (or both together). If
|
|
229
|
+
# neither is set, the fetching continues until the server returns an empty cursor.
|
|
230
|
+
#
|
|
231
|
+
# When experimenting in the Ruby console, you can pass `nil` as `field` (or skip the parameter)
|
|
232
|
+
# to make a single request and raise an exception, which will tell you what fields are available.
|
|
233
|
+
#
|
|
234
|
+
# @param method [String, URI] an XRPC endpoint name or a full URL
|
|
235
|
+
# @param params [Hash, nil] query parameters
|
|
236
|
+
#
|
|
237
|
+
# @param auth [Boolean, String]
|
|
238
|
+
# boolean value which tells whether to send an auth header with the access token or not,
|
|
239
|
+
# or an explicit bearer token to use
|
|
240
|
+
# @param field [String, nil]
|
|
241
|
+
# name of the field in the responses which contains the data array
|
|
242
|
+
# @param break_when [Proc, nil]
|
|
243
|
+
# if passed, the fetching will stop when the block returns true for any of the
|
|
244
|
+
# returned records, and records matching the condition will be deleted from the last page
|
|
245
|
+
# @param max_pages [Integer, nil]
|
|
246
|
+
# maximum number of pages to fetch
|
|
247
|
+
# @param headers [Hash, nil]
|
|
248
|
+
# additional headers to include
|
|
249
|
+
# @param progress [String, nil]
|
|
250
|
+
# a character to print before each request as a progress indicator
|
|
251
|
+
#
|
|
252
|
+
# @return [Array] records or objects collected from all pages
|
|
253
|
+
#
|
|
254
|
+
# @raise [ArgumentError] if method name is invalid
|
|
255
|
+
# @raise [FieldNotSetError] if field parameter wasn't set (the message tells you what fields were in the response)
|
|
256
|
+
# @raise [BadResponse] if the HTTP response has an error status code
|
|
257
|
+
# @raise [AuthError]
|
|
258
|
+
# - if logging in is required, but login or password isn't provided
|
|
259
|
+
# - if token refresh is needed, but refresh token is missing
|
|
260
|
+
# - if a token has invalid format
|
|
261
|
+
# - if required access token is missing, and {#auto_manage_tokens} is disabled
|
|
262
|
+
#
|
|
263
|
+
# @example Fetching with a `break_when` block
|
|
264
|
+
# sky = Minisky.new('public.api.bsky.app', nil)
|
|
265
|
+
# time_limit = Time.now - 86400 * 30
|
|
266
|
+
#
|
|
267
|
+
# sky.fetch_all('app.bsky.feed.getAuthorFeed',
|
|
268
|
+
# { actor: 'pfrazee.com', limit: 100 },
|
|
269
|
+
# field: 'feed',
|
|
270
|
+
# progress: '|',
|
|
271
|
+
# break_when: ->(x) { Time.at(x['post']['record']['createdAt']) < time_limit }
|
|
272
|
+
# )
|
|
273
|
+
#
|
|
274
|
+
# @example Fetching with `max_pages`
|
|
275
|
+
# sky = Minisky.new('tngl.sh', 'config.yml')
|
|
276
|
+
# sky.fetch_all('app.bsky.feed.getTimeline', { limit: 100 }, field: 'feed', max_pages: 10)
|
|
277
|
+
#
|
|
278
|
+
# @example Making a request in the console with empty `field`
|
|
279
|
+
# sky = Minisky.new('public.api.bsky.app', nil)
|
|
280
|
+
# # => #<Minisky:0x0000000120f5f6b0 @host="public.api.bsky.app", ...>
|
|
281
|
+
#
|
|
282
|
+
# sky.fetch_all('app.bsky.graph.getFollowers', { actor: 'sdk.blue' })
|
|
283
|
+
# # ./lib/minisky/requests.rb:270:in 'block in Minisky::Requests#fetch_all':
|
|
284
|
+
# # Field parameter not provided; available fields: ["followers"] (Minisky::FieldNotSetError)
|
|
285
|
+
#
|
|
286
|
+
# sky.fetch_all('app.bsky.graph.getFollowers', { actor: 'sdk.blue' }, field: 'followers')
|
|
287
|
+
# # => .....
|
|
288
|
+
|
|
97
289
|
def fetch_all(method, params = nil, auth: default_auth_mode,
|
|
98
290
|
field: nil, break_when: nil, max_pages: nil, headers: nil, progress: @default_progress)
|
|
99
291
|
data = []
|
|
100
|
-
params = {}
|
|
292
|
+
params = (params.nil?) ? {} : params.dup
|
|
101
293
|
pages = 0
|
|
294
|
+
seen_cursors = Set.new
|
|
295
|
+
|
|
296
|
+
if max_pages && !(max_pages.is_a?(Integer) && max_pages > 0)
|
|
297
|
+
raise ArgumentError, "max_pages should be a positive integer"
|
|
298
|
+
end
|
|
102
299
|
|
|
103
300
|
loop do
|
|
104
301
|
print(progress) if progress
|
|
105
302
|
|
|
106
303
|
response = get_request(method, params, auth: auth, headers: headers)
|
|
107
|
-
|
|
108
|
-
if field.nil?
|
|
109
|
-
raise FieldNotSetError, response.keys.select { |f| response[f].is_a?(Array) }
|
|
110
|
-
end
|
|
304
|
+
validate_fetch_all_response(response, field)
|
|
111
305
|
|
|
112
306
|
records = response[field]
|
|
113
307
|
cursor = response['cursor']
|
|
114
308
|
|
|
309
|
+
raise InvalidCursorError, "Repeated cursor: #{cursor}" if seen_cursors.include?(cursor)
|
|
310
|
+
|
|
115
311
|
data.concat(records)
|
|
116
312
|
params[:cursor] = cursor
|
|
313
|
+
seen_cursors << cursor
|
|
117
314
|
pages += 1
|
|
118
315
|
|
|
119
|
-
break if !cursor ||
|
|
316
|
+
break if !cursor || pages == max_pages || (stop_fetch_on_empty_page && records.empty?)
|
|
120
317
|
break if break_when && records.any? { |x| break_when.call(x) }
|
|
121
318
|
end
|
|
122
319
|
|
|
@@ -124,11 +321,42 @@ class Minisky
|
|
|
124
321
|
data
|
|
125
322
|
end
|
|
126
323
|
|
|
324
|
+
# Ensures that the user has a fresh access token, by checking the access token's expiry date
|
|
325
|
+
# and performing a refresh if needed, or by logging in with a password if no tokens are present.
|
|
326
|
+
#
|
|
327
|
+
# If {#auto_manage_tokens} is enabled (the default setting), this method is automatically called
|
|
328
|
+
# before {#get_request}, {#post_request} and {#fetch_all}, so you generally don't need to call it
|
|
329
|
+
# yourself.
|
|
330
|
+
#
|
|
331
|
+
# @return [Symbol]
|
|
332
|
+
# - `:logged_in` if a login using a password was performed
|
|
333
|
+
# - `:refreshed` if the access token was expired and was refreshed
|
|
334
|
+
# - `:ok` if no refresh was needed
|
|
335
|
+
# - `:unknown` if the token is not a valid JWT (e.g. an opaque blob)
|
|
336
|
+
#
|
|
337
|
+
# @raise [BadResponse] if login or refresh returns an error status code
|
|
338
|
+
# @raise [AuthError]
|
|
339
|
+
# - if the client doesn't include user config at all
|
|
340
|
+
# - if logging in is required, but login or password isn't provided
|
|
341
|
+
# - if token refresh is needed, but refresh token is missing
|
|
342
|
+
|
|
127
343
|
def check_access
|
|
128
|
-
if !user
|
|
344
|
+
if !user
|
|
345
|
+
raise AuthError, "User config is missing"
|
|
346
|
+
elsif !user.has_credentials?
|
|
347
|
+
raise AuthError, "User id or password is missing"
|
|
348
|
+
elsif !user.logged_in?
|
|
129
349
|
log_in
|
|
130
|
-
:logged_in
|
|
131
|
-
|
|
350
|
+
return :logged_in
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
begin
|
|
354
|
+
expired = access_token_expired?
|
|
355
|
+
rescue AuthError
|
|
356
|
+
return :unknown
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
if expired
|
|
132
360
|
perform_token_refresh
|
|
133
361
|
:refreshed
|
|
134
362
|
else
|
|
@@ -136,8 +364,21 @@ class Minisky
|
|
|
136
364
|
end
|
|
137
365
|
end
|
|
138
366
|
|
|
367
|
+
# Logs in the user using an ID and password stored in the config by calling the
|
|
368
|
+
# `createSession` endpoint, and stores the received access & refresh tokens.
|
|
369
|
+
#
|
|
370
|
+
# This is generally handled automatically by {#check_access}. Calling this method
|
|
371
|
+
# repeatedly many times in a short period of time may use up your rate limit for this
|
|
372
|
+
# endpoint (which is lower than for others) and make it inaccessible to you for some
|
|
373
|
+
# time.
|
|
374
|
+
#
|
|
375
|
+
# @return [Hash] the response JSON with access tokens
|
|
376
|
+
#
|
|
377
|
+
# @raise [AuthError] if login or password are missing
|
|
378
|
+
# @raise [BadResponse] if the server responds with an error status code
|
|
379
|
+
|
|
139
380
|
def log_in
|
|
140
|
-
if user.
|
|
381
|
+
if user.nil? || !user.has_credentials?
|
|
141
382
|
raise AuthError, "To log in, please provide a user id and password"
|
|
142
383
|
end
|
|
143
384
|
|
|
@@ -146,6 +387,11 @@ class Minisky
|
|
|
146
387
|
password: user.pass
|
|
147
388
|
}
|
|
148
389
|
|
|
390
|
+
if user.id =~ /\A[^@]+@[^@]+\z/
|
|
391
|
+
STDERR.puts "Warning: logging in using an email address is deprecated in Minisky and will be " +
|
|
392
|
+
"removed in a future version. Use either a handle or a DID instead."
|
|
393
|
+
end
|
|
394
|
+
|
|
149
395
|
json = post_request('com.atproto.server.createSession', data, auth: false)
|
|
150
396
|
|
|
151
397
|
user.did = json['did']
|
|
@@ -156,8 +402,19 @@ class Minisky
|
|
|
156
402
|
json
|
|
157
403
|
end
|
|
158
404
|
|
|
405
|
+
# Refreshes the access token using the stored refresh token. If successful, this
|
|
406
|
+
# invalidates *both* old tokens and replaces them with new ones from the response.
|
|
407
|
+
#
|
|
408
|
+
# If {#auto_manage_tokens} is enabled (the default setting), this method is automatically called
|
|
409
|
+
# before any requests through {#check_access}, so you generally don't need to call it yourself.
|
|
410
|
+
#
|
|
411
|
+
# @return [Hash] the response JSON with access tokens
|
|
412
|
+
#
|
|
413
|
+
# @raise [AuthError] if the refresh token is missing
|
|
414
|
+
# @raise [BadResponse] if the server responds with an error status code
|
|
415
|
+
|
|
159
416
|
def perform_token_refresh
|
|
160
|
-
if user
|
|
417
|
+
if user&.refresh_token.nil?
|
|
161
418
|
raise AuthError, "Can't refresh access token - refresh token is missing"
|
|
162
419
|
end
|
|
163
420
|
|
|
@@ -170,27 +427,72 @@ class Minisky
|
|
|
170
427
|
json
|
|
171
428
|
end
|
|
172
429
|
|
|
430
|
+
# Attempts to parse a given token as JWT and extract the expiration date from the payload.
|
|
431
|
+
# An access token technically isn't required to be a (valid) JWT, so if the parsing fails
|
|
432
|
+
# for whatever reason, nil is returned.
|
|
433
|
+
#
|
|
434
|
+
# @return [Time, nil] parsed expiration time, or nil if token is not a valid JWT
|
|
435
|
+
|
|
173
436
|
def token_expiration_date(token)
|
|
437
|
+
return nil unless token.valid_encoding?
|
|
438
|
+
|
|
174
439
|
parts = token.split('.')
|
|
175
|
-
|
|
440
|
+
return nil unless parts.length == 3
|
|
176
441
|
|
|
177
442
|
begin
|
|
178
|
-
|
|
443
|
+
json = Base64.urlsafe_decode64(parts[1])
|
|
444
|
+
rescue ArgumentError
|
|
445
|
+
return nil
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
begin
|
|
449
|
+
payload = JSON.parse(json)
|
|
179
450
|
rescue JSON::ParserError
|
|
180
|
-
|
|
451
|
+
return nil
|
|
181
452
|
end
|
|
182
453
|
|
|
454
|
+
return nil if !payload.is_a?(Hash)
|
|
455
|
+
|
|
183
456
|
exp = payload['exp']
|
|
184
|
-
|
|
457
|
+
return nil unless exp.is_a?(Numeric) && exp > 0
|
|
458
|
+
|
|
459
|
+
time = Time.at(exp)
|
|
460
|
+
return nil if time.year < 2023 || time.year > 2100
|
|
185
461
|
|
|
186
|
-
|
|
462
|
+
time
|
|
187
463
|
end
|
|
188
464
|
|
|
465
|
+
# Attempts to parse the user's access token as JWT, extract the expiration date from the
|
|
466
|
+
# payload, and check if the token hasn't expired yet.
|
|
467
|
+
#
|
|
468
|
+
# @return [Boolean] true if the token's expiration time is more than a minute away
|
|
469
|
+
# @raise [AuthError] if the token is not a valid JWT, or user is not logged in
|
|
470
|
+
|
|
189
471
|
def access_token_expired?
|
|
190
|
-
|
|
472
|
+
if user&.access_token.nil?
|
|
473
|
+
raise AuthError, "No access token (user is not logged in)"
|
|
474
|
+
end
|
|
475
|
+
|
|
476
|
+
exp_date = token_expiration_date(user.access_token)
|
|
477
|
+
|
|
478
|
+
if exp_date
|
|
479
|
+
exp_date < Time.now + 60
|
|
480
|
+
else
|
|
481
|
+
raise AuthError, "Token expiration date cannot be decoded"
|
|
482
|
+
end
|
|
191
483
|
end
|
|
192
484
|
|
|
485
|
+
#
|
|
486
|
+
# Clear stored access and refresh tokens, effectively logging out the user.
|
|
487
|
+
#
|
|
488
|
+
# @raise [AuthError] if the client doesn't have a user config
|
|
489
|
+
#
|
|
490
|
+
|
|
193
491
|
def reset_tokens
|
|
492
|
+
if !user
|
|
493
|
+
raise AuthError, "User config is missing"
|
|
494
|
+
end
|
|
495
|
+
|
|
194
496
|
user.access_token = nil
|
|
195
497
|
user.refresh_token = nil
|
|
196
498
|
save_config
|
|
@@ -198,27 +500,24 @@ class Minisky
|
|
|
198
500
|
end
|
|
199
501
|
|
|
200
502
|
if RUBY_VERSION.to_i == 2
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
private :do_get_request, :do_post_request
|
|
204
|
-
|
|
205
|
-
def get_request(method, params = nil, auth: default_auth_mode, headers: nil, **kwargs)
|
|
206
|
-
params ||= kwargs unless kwargs.empty?
|
|
207
|
-
do_get_request(method, params, auth: auth, headers: headers)
|
|
208
|
-
end
|
|
209
|
-
|
|
210
|
-
def post_request(method, params = nil, auth: default_auth_mode, headers: nil, **kwargs)
|
|
211
|
-
params ||= kwargs unless kwargs.empty?
|
|
212
|
-
do_post_request(method, params, auth: auth, headers: headers)
|
|
213
|
-
end
|
|
503
|
+
require_relative 'compat'
|
|
504
|
+
prepend Ruby2Compat
|
|
214
505
|
end
|
|
215
506
|
|
|
216
507
|
|
|
217
508
|
private
|
|
218
509
|
|
|
219
510
|
def make_request(request)
|
|
220
|
-
|
|
221
|
-
|
|
511
|
+
options = {
|
|
512
|
+
use_ssl: (request.uri.scheme == 'https')
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if @timeout
|
|
516
|
+
options[:open_timeout] = @timeout
|
|
517
|
+
options[:read_timeout] = @timeout
|
|
518
|
+
end
|
|
519
|
+
|
|
520
|
+
response = Net::HTTP.start(request.uri.hostname, request.uri.port, options) do |http|
|
|
222
521
|
http.request(request)
|
|
223
522
|
end
|
|
224
523
|
end
|
|
@@ -243,7 +542,7 @@ class Minisky
|
|
|
243
542
|
if auth.is_a?(String)
|
|
244
543
|
{ 'Authorization' => "Bearer #{auth}" }
|
|
245
544
|
elsif auth
|
|
246
|
-
if user
|
|
545
|
+
if user&.access_token
|
|
247
546
|
{ 'Authorization' => "Bearer #{user.access_token}" }
|
|
248
547
|
else
|
|
249
548
|
raise AuthError, "Can't send auth headers, access token is missing"
|
|
@@ -253,6 +552,26 @@ class Minisky
|
|
|
253
552
|
end
|
|
254
553
|
end
|
|
255
554
|
|
|
555
|
+
def validate_fetch_all_response(response, field)
|
|
556
|
+
if !response.is_a?(Hash)
|
|
557
|
+
raise InvalidJSONError, "Received response is not in expected JSON format"
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
if field.nil?
|
|
561
|
+
raise FieldNotSetError, response.keys.select { |f| response[f].is_a?(Array) }
|
|
562
|
+
end
|
|
563
|
+
|
|
564
|
+
if !response.has_key?(field)
|
|
565
|
+
raise InvalidJSONError, "No such field in the response JSON: '#{field}'"
|
|
566
|
+
elsif response[field].nil?
|
|
567
|
+
raise InvalidJSONError, "Field '#{field}' is nil in the received response"
|
|
568
|
+
elsif !response[field].is_a?(Array)
|
|
569
|
+
raise InvalidJSONError, "Invalid '#{field}': expected Array, got #{response[field].class}"
|
|
570
|
+
else
|
|
571
|
+
# ok
|
|
572
|
+
end
|
|
573
|
+
end
|
|
574
|
+
|
|
256
575
|
def handle_response(response)
|
|
257
576
|
status = response.code.to_i
|
|
258
577
|
message = response.message
|
data/lib/minisky/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: minisky
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.5.
|
|
4
|
+
version: 0.5.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kuba Suder
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: base64
|
|
@@ -35,26 +34,23 @@ files:
|
|
|
35
34
|
- CHANGELOG.md
|
|
36
35
|
- LICENSE.txt
|
|
37
36
|
- README.md
|
|
38
|
-
-
|
|
39
|
-
- example/fetch_my_posts.rb
|
|
40
|
-
- example/fetch_profile.rb
|
|
41
|
-
- example/find_missing_follows.rb
|
|
42
|
-
- example/post_skeet.rb
|
|
43
|
-
- example/science_feed.rb
|
|
37
|
+
- SECURITY.md
|
|
44
38
|
- lib/minisky.rb
|
|
39
|
+
- lib/minisky/compat.rb
|
|
45
40
|
- lib/minisky/errors.rb
|
|
46
41
|
- lib/minisky/minisky.rb
|
|
47
42
|
- lib/minisky/requests.rb
|
|
48
43
|
- lib/minisky/version.rb
|
|
49
44
|
- sig/minisky.rbs
|
|
50
|
-
homepage: https://
|
|
45
|
+
homepage: https://ruby.sdk.blue
|
|
51
46
|
licenses:
|
|
52
47
|
- Zlib
|
|
53
48
|
metadata:
|
|
54
|
-
bug_tracker_uri: https://
|
|
55
|
-
changelog_uri: https://
|
|
56
|
-
source_code_uri: https://
|
|
57
|
-
|
|
49
|
+
bug_tracker_uri: https://tangled.org/mackuba.eu/minisky/issues
|
|
50
|
+
changelog_uri: https://tangled.org/mackuba.eu/minisky/blob/master/CHANGELOG.md
|
|
51
|
+
source_code_uri: https://tangled.org/mackuba.eu/minisky
|
|
52
|
+
documentation_uri: https://rubydoc.info/gems/minisky
|
|
53
|
+
rubygems_mfa_required: 'true'
|
|
58
54
|
rdoc_options: []
|
|
59
55
|
require_paths:
|
|
60
56
|
- lib
|
|
@@ -69,8 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
69
65
|
- !ruby/object:Gem::Version
|
|
70
66
|
version: '0'
|
|
71
67
|
requirements: []
|
|
72
|
-
rubygems_version:
|
|
73
|
-
signing_key:
|
|
68
|
+
rubygems_version: 4.0.23
|
|
74
69
|
specification_version: 4
|
|
75
|
-
summary: A minimal client of Bluesky/
|
|
70
|
+
summary: A minimal client of Bluesky/ATProto API
|
|
76
71
|
test_files: []
|