authproto 0.3.0.beta5 → 0.3.0.beta6
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 +7 -0
- data/examples/basic_webapp/Gemfile.lock +1 -1
- data/examples/bookmarks/Gemfile.lock +2 -2
- data/examples/bookmarks/README.md +16 -7
- data/examples/bookmarks/Rakefile +13 -8
- data/examples/bookmarks/app/account.rb +2 -1
- data/examples/bookmarks/app/bookmark.rb +28 -33
- data/examples/bookmarks/app/bookmark_sync.rb +56 -0
- data/examples/bookmarks/app/server.rb +23 -9
- data/examples/bookmarks/app/session.rb +21 -0
- data/examples/bookmarks/db/migrate/20261117120000_add_sessions.rb +36 -0
- data/examples/bookmarks/db/migrate/20261117120100_add_timestamps_to_sessions.rb +18 -0
- data/examples/bookmarks/db/schema.rb +13 -2
- data/examples/bookmarks/views/bookmarks.erb +10 -0
- data/examples/minisky_console/Gemfile.lock +2 -2
- data/lib/authproto/client.rb +24 -11
- data/lib/authproto/client_assertion.rb +147 -0
- data/lib/authproto/metadata/authorization_server.rb +15 -3
- data/lib/authproto/metadata/client_metadata.rb +2 -2
- data/lib/authproto/minisky.rb +1 -0
- data/lib/authproto/par/client.rb +1 -1
- data/lib/authproto/par/request.rb +8 -3
- data/lib/authproto/par.rb +0 -4
- data/lib/authproto/storage/sqlite.rb +53 -16
- data/lib/authproto/token/refresh.rb +4 -5
- data/lib/authproto/token/revocation.rb +3 -3
- data/lib/authproto/version.rb +1 -1
- data/sig/authproto/client.rbs +3 -2
- data/sig/authproto/client_assertion.rbs +14 -0
- data/sig/authproto/metadata/authorization_server.rbs +2 -0
- data/sig/authproto/minisky.rbs +3 -1
- data/sig/authproto/par/request.rbs +3 -0
- data/sig/authproto/par.rbs +0 -1
- data/sig/authproto/token/refresh.rbs +1 -1
- data/sig/authproto/token/revocation.rbs +1 -1
- metadata +7 -3
- data/lib/authproto/par/client_assertion.rb +0 -146
- data/sig/authproto/par/client_assertion.rbs +0 -15
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 287d0ba55778fab027f3a3ceacb569af247a99288932ad07a2d1cf8c36c09a96
|
|
4
|
+
data.tar.gz: 2cf55f579d39efffc9f446c7b3347e433c91c3034d17ff90e7aee3f8697a2787
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a330438ecdfd4d76d8b2abc9b15723b1d15c20a033ef7071a2acf668b3a92db1483a9da5698a7dc658bdaa5a76de9228247decaba22f136292c189710355e14b
|
|
7
|
+
data.tar.gz: da98b53ec6791a8f93c088cf04be379c283e518062f156401f42c6886f3e3ad47bbe1710c59b4ab82794f997c778cfc3a4667fdeac17ecdab079e06461d002dd
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [0.3.0.beta6] – 2026-11-23
|
|
2
|
+
|
|
3
|
+
- added `prompt:` option to `Client#authorize`
|
|
4
|
+
- fixed SQLite storage breaking on production with servers that use forking to create new workers
|
|
5
|
+
- moved `AuthProto::PAR::ClientAssertion` to `AuthProto::ClientAssertion`
|
|
6
|
+
- updated "bookmarks" example to track multiple user sessions and use their access tokens for background sync even after the user logs out in the browser
|
|
7
|
+
|
|
1
8
|
## [0.3.0.beta5] – 2026-11-10
|
|
2
9
|
|
|
3
10
|
- added `process_dpop`, `has_nonce_error?` and `with_nonce_handling` helpers to `Client`, proxying the calls to `dpop_client` for easier access
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: ../..
|
|
3
3
|
specs:
|
|
4
|
-
authproto (0.3.0.
|
|
4
|
+
authproto (0.3.0.beta6)
|
|
5
5
|
didkit (~> 0.5.0)
|
|
6
6
|
jose (~> 1.2)
|
|
7
7
|
jwt (~> 3.3)
|
|
@@ -115,7 +115,7 @@ CHECKSUMS
|
|
|
115
115
|
activemodel (8.1.4.1) sha256=063caf105372fbc98b2d789e4be23de63ac5c043f7d9330f04e2caddb7012805
|
|
116
116
|
activerecord (8.1.4.1) sha256=bd65599ca0b5d210937f835d42c7aff69d271741c75aab022730be6cbce67e87
|
|
117
117
|
activesupport (8.1.4.1) sha256=16ac4f341ca65c85a1fcdb8ac73f68891aae9e3c4a6f9c3a27daed8b00017ed7
|
|
118
|
-
authproto (0.3.0.
|
|
118
|
+
authproto (0.3.0.beta6)
|
|
119
119
|
base58 (0.2.3) sha256=fda5c5d3bda8b429ad0d1a5309c2d1e488ba46f0a2ab99a639890f26c61bf3be
|
|
120
120
|
base64 (0.3.0) sha256=27337aeabad6ffae05c265c450490628ef3ebd4b67be58257393227588f5a97b
|
|
121
121
|
bigdecimal (4.1.3) sha256=61ebe1e5e559bdc3cc6f2c0ee7f427321fc838f59611c294356eb04d6e21cf66
|
|
@@ -80,7 +80,7 @@ SESSION_SECRET=<hex value from second command>
|
|
|
80
80
|
|
|
81
81
|
Storage configuration is not needed here – this app has a hardcoded SQLite storage for AuthProto. The token, session and nonce data is stored in `db/authproto.sqlite3`.
|
|
82
82
|
|
|
83
|
-
The app also has a second SQLite database for the app's actual data – the imported bookmarks and
|
|
83
|
+
The app also has a second SQLite database for the app's actual data – the imported bookmarks, account settings, and import info. This database (with `accounts`, `bookmarks`, and `sessions` tables) is stored in `db/bookmarks.sqlite3` and managed by ActiveRecord.
|
|
84
84
|
|
|
85
85
|
Before starting the app, you need to initialize this database by running the migration:
|
|
86
86
|
|
|
@@ -124,7 +124,8 @@ The main app code is spread across a few files in the `app` directory:
|
|
|
124
124
|
- `oauth.rb` – some global configuration for OAuth and AuthProto, accessed as a singleton from other parts
|
|
125
125
|
- `init.rb` – an initializer setting everything up
|
|
126
126
|
- `server.rb` – the main Sinatra server code
|
|
127
|
-
- `account.rb` and `
|
|
127
|
+
- `account.rb`, `bookmark.rb` and `session.rb` – ActiveRecord models
|
|
128
|
+
- `bookmark_sync.rb` – the background import and OAuth session cleanup task
|
|
128
129
|
|
|
129
130
|
There's a Rakefile with a rake task for updating a given account's import, which could be run e.g. from crontab. The Rakefile only needs to include the initializer code, but can skip the `server.rb` part.
|
|
130
131
|
|
|
@@ -142,6 +143,15 @@ When completing authorization, the server also uses [didkit](https://tangled.org
|
|
|
142
143
|
The app only requests the `rpc:app.bsky.bookmark.getBookmarks?aud=*` permission, so it can't modify or access any of your other data, or even modify bookmarks, it only has read-only access to them (the `*` is so that you can use alternative AppViews like Blacksky's or Eurosky's). It's generally a good idea to use [granular OAuth permissions](https://atproto.com/guides/permission-requests) in your ATProto apps, requesting only what your app will actually need – this makes it easier for the users to trust your app.
|
|
143
144
|
|
|
144
145
|
|
|
146
|
+
### Session handling
|
|
147
|
+
|
|
148
|
+
The `sessions` table in `bookmarks.db` database stores a list of session IDs of AuthProto sessions (just the IDs, without the token info) linked n-to-1 to specific accounts in the `accounts` table. The reason for this is that we want to be able to track all sessions of a given user in order to use their tokens for background import (see below), and the `AuthProto::Storage` is just a key-value store that doesn't expose any querying by field. So when a session completes authorization, its ID is added to this table. There might be more than one active session for a given user, if they e.g. have logged in on multiple devices.
|
|
149
|
+
|
|
150
|
+
When the user logs out, we *don't* delete or revoke the session, but we only mark its `active` boolean flag as `false`. This means the session is no longer used (and won't ever be used again) by the user to access things while browsing the site, but can still be used by our cron jobs or background workers for some background syncing operations. We need this because we need at least one working session with access tokens to make requests to Bluesky's API to sync bookmarks, if we want to do this periodically e.g. once a day, and we need this to work even if the user has logged out from their browser. When checking authentication in the controller's helpers, we also look up if the session exists in the `sessions` table and if it's not logged out (in case e.g. an old browser cookie gets restored somehow).
|
|
151
|
+
|
|
152
|
+
When a background sync operation runs (`app/bookmark_sync.rb`), we pick the newest "active" session or a newest logged-out session if there are no active ones, and use its access tokens to connect. We also clean up (delete locally & revoke remotely) all older logged-out sessions except the currently used one – we only need one of those, and the old ones shouldn't be kept forever.
|
|
153
|
+
|
|
154
|
+
|
|
145
155
|
### The import
|
|
146
156
|
|
|
147
157
|
The bookmark import (`Bookmark.fetch_bookmarks`) is launched in the `/fetch` endpoint when the user clicks "Fetch your bookmarks", and can also be run from the rake task `rake bookmarks:fetch`.
|
|
@@ -154,17 +164,16 @@ The import process uses `Minisky::OAuth` for a bit more high-level XRPC request
|
|
|
154
164
|
sky = Minisky::OAuth.with_oauth_client(OAuth.client, session_id: session_id)
|
|
155
165
|
```
|
|
156
166
|
|
|
157
|
-
The import
|
|
167
|
+
The import uses Minisky's `#fetch_all` helper to make requests to `app.bsky.bookmark.getBookmarks` in a loop, updating the cursor and collecting the entries from each page into one result array. The block passed to the `break_when:` parameter tells it to stop when it reaches the newest previously downloaded bookmark. When the fetch completes, the newly fetched bookmarks are saved inside a transaction.
|
|
158
168
|
|
|
159
169
|
The call goes to the user's PDS, since that's what we have the access token for, but the data we want to fetch is actually on the AppView. This is solved using [XRPC service proxying](https://atproto.com/specs/xrpc#service-proxying), where we call the PDS but use an `atproto-proxy` header to specify where it should proxy the request to:
|
|
160
170
|
|
|
161
171
|
```rb
|
|
162
172
|
headers = { 'atproto-proxy' => 'did:web:api.bsky.app#bsky_appview' }
|
|
163
173
|
...
|
|
164
|
-
|
|
174
|
+
entries = sky.fetch_all('app.bsky.bookmark.getBookmarks', { limit: 100 },
|
|
175
|
+
field: 'bookmarks', break_when: ->(x) { ... }, headers: headers)
|
|
165
176
|
```
|
|
166
177
|
|
|
167
|
-
We
|
|
178
|
+
We also give the user the option to turn the background sync on and off. If it's off, the cron job running the sync task will skip the import and only clean up older sessions (and if only inactive sessions are left, it will remove them all, so at this point the app will no longer own any access tokens to access the user's account again).
|
|
168
179
|
|
|
169
|
-
> [!WARNING]
|
|
170
|
-
> One current limitation in this code is that it uses the AuthProto session data created when the user logs in for the import – which could run in the background using the rake task – but that session is deleted when the user logs out, which is obviously not ideal.
|
data/examples/bookmarks/Rakefile
CHANGED
|
@@ -4,20 +4,25 @@ require 'sinatra/activerecord/rake'
|
|
|
4
4
|
|
|
5
5
|
require_relative 'app/init'
|
|
6
6
|
require_relative 'app/account'
|
|
7
|
-
require_relative 'app/
|
|
7
|
+
require_relative 'app/bookmark_sync'
|
|
8
8
|
|
|
9
9
|
namespace :bookmarks do
|
|
10
|
-
desc "
|
|
10
|
+
desc "Sync new bookmarks and clean up signed-out sessions: bookmarks:fetch[did]"
|
|
11
11
|
task :fetch, [:did] => 'db:load_config' do |task, args|
|
|
12
|
-
# TODO: this will currently fail if the user has explicitly logged out, because that deletes the session
|
|
13
|
-
|
|
14
12
|
did = args[:did] || ENV['DID']
|
|
15
13
|
abort "Pass the owner's DID: bundle exec rake 'bookmarks:fetch[did:plc:...]'" if did.to_s.empty?
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
abort "No
|
|
15
|
+
account = Account.find_by(did: did)
|
|
16
|
+
abort "No bookmark account for #{did}. Sign in through the web app first." unless account
|
|
17
|
+
|
|
18
|
+
count = BookmarkSync.new(account).run
|
|
19
19
|
|
|
20
|
-
count
|
|
21
|
-
|
|
20
|
+
if count
|
|
21
|
+
puts "Fetched #{count} new bookmarks for #{did}."
|
|
22
|
+
else
|
|
23
|
+
puts "Background sync is off for #{did}. Cleaned up signed-out sessions."
|
|
24
|
+
end
|
|
25
|
+
rescue Minisky::AuthError => e
|
|
26
|
+
abort e.message
|
|
22
27
|
end
|
|
23
28
|
end
|
|
@@ -6,11 +6,12 @@ require_relative 'init'
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
#
|
|
9
|
-
# A bookmark owner and the state of their
|
|
9
|
+
# A bookmark owner and the state of their bookmark import.
|
|
10
10
|
#
|
|
11
11
|
|
|
12
12
|
class Account < ActiveRecord::Base
|
|
13
13
|
has_many :bookmarks, dependent: :destroy
|
|
14
|
+
has_many :sessions, dependent: :destroy
|
|
14
15
|
|
|
15
16
|
validates :did, presence: true
|
|
16
17
|
end
|
|
@@ -15,7 +15,7 @@ class Bookmark < ActiveRecord::Base
|
|
|
15
15
|
scope :newest_first, -> { order(created_at: :desc, uri: :desc) }
|
|
16
16
|
|
|
17
17
|
# Fetches bookmarks synchronously, following API cursors and saving text posts.
|
|
18
|
-
#
|
|
18
|
+
# Saves the results atomically after fetching so an incremental retry cannot miss older pages.
|
|
19
19
|
#
|
|
20
20
|
# @param session_id [String] OAuth session belonging to the bookmark owner
|
|
21
21
|
# @param newer_only [Boolean] stop at the newest locally saved bookmark
|
|
@@ -29,46 +29,41 @@ class Bookmark < ActiveRecord::Base
|
|
|
29
29
|
account = Account.find_by!(did: sky.did)
|
|
30
30
|
|
|
31
31
|
newest = account.bookmarks.maximum(:created_at) if newer_only
|
|
32
|
-
imported = 0
|
|
33
|
-
cursor = nil
|
|
34
|
-
|
|
35
|
-
headers = { 'atproto-proxy' => 'did:web:api.bsky.app#bsky_appview' }
|
|
36
|
-
|
|
37
|
-
transaction do
|
|
38
|
-
loop do
|
|
39
|
-
query = { limit: 100 }
|
|
40
|
-
query[:cursor] = cursor if cursor
|
|
41
32
|
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
entries = sky.fetch_all('app.bsky.bookmark.getBookmarks', { limit: 100 },
|
|
34
|
+
field: 'bookmarks',
|
|
35
|
+
headers: {
|
|
36
|
+
'atproto-proxy' => 'did:web:api.bsky.app#bsky_appview'
|
|
37
|
+
},
|
|
38
|
+
break_when: newest && lambda { |entry|
|
|
39
|
+
created_at = entry['createdAt']
|
|
40
|
+
created_at && Time.iso8601(created_at) <= newest
|
|
41
|
+
}
|
|
42
|
+
)
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
created_at = entry['createdAt'] && Time.iso8601(entry['createdAt'])
|
|
44
|
+
imported = 0
|
|
47
45
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
transaction do
|
|
47
|
+
entries.each do |entry|
|
|
48
|
+
post = entry.fetch('item')
|
|
49
|
+
next unless post['$type'] == 'app.bsky.feed.defs#postView'
|
|
52
50
|
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
record = post.fetch('record')
|
|
52
|
+
next if record['text'].blank?
|
|
55
53
|
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
uri = entry.fetch('subject').fetch('uri')
|
|
55
|
+
created_at = entry['createdAt']
|
|
58
56
|
|
|
59
|
-
|
|
60
|
-
bookmark.update!(
|
|
61
|
-
author_handle: post.fetch('author').fetch('handle'),
|
|
62
|
-
created_at: created_at,
|
|
63
|
-
posted_at: Time.iso8601(record.fetch('createdAt')),
|
|
64
|
-
text: record.fetch('text')
|
|
65
|
-
)
|
|
57
|
+
bookmark = account.bookmarks.find_or_initialize_by(uri: uri)
|
|
66
58
|
|
|
67
|
-
|
|
68
|
-
|
|
59
|
+
bookmark.update!(
|
|
60
|
+
author_handle: post.fetch('author').fetch('handle'),
|
|
61
|
+
created_at: created_at && Time.iso8601(created_at),
|
|
62
|
+
posted_at: Time.iso8601(record.fetch('createdAt')),
|
|
63
|
+
text: record.fetch('text')
|
|
64
|
+
)
|
|
69
65
|
|
|
70
|
-
|
|
71
|
-
break if cursor.blank? || reached_newest
|
|
66
|
+
imported += 1
|
|
72
67
|
end
|
|
73
68
|
|
|
74
69
|
account.update!(fetched: true)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'bookmark'
|
|
4
|
+
require_relative 'session'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# Runs an incremental import and cleans up signed-out OAuth sessions.
|
|
9
|
+
#
|
|
10
|
+
|
|
11
|
+
class BookmarkSync
|
|
12
|
+
def initialize(account)
|
|
13
|
+
@account = account
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def run
|
|
17
|
+
used_session = nil
|
|
18
|
+
fetched_bookmarks = nil
|
|
19
|
+
|
|
20
|
+
if @account.sync_enabled?
|
|
21
|
+
used_session, fetched_bookmarks = fetch_bookmarks
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
cleanup_sessions(except: used_session)
|
|
25
|
+
fetched_bookmarks
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def fetch_bookmarks
|
|
32
|
+
# We need one set of working access tokens for the user's account to run the import.
|
|
33
|
+
# Loop over the user's all sessions and find the first one that works.
|
|
34
|
+
|
|
35
|
+
@account.sessions.order(active: :desc, created_at: :desc, id: :desc).each do |session|
|
|
36
|
+
begin
|
|
37
|
+
count = Bookmark.fetch_bookmarks(session.session_id, newer_only: true)
|
|
38
|
+
return [session, count]
|
|
39
|
+
rescue Minisky::AuthError
|
|
40
|
+
next
|
|
41
|
+
rescue AuthProto::Token::RefreshError => error
|
|
42
|
+
raise unless error.message.start_with?('Refresh token has been invalidated: invalid_grant')
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
raise Minisky::AuthError, "No usable OAuth session for #{@account.did}. Sign in through the web app first."
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def cleanup_sessions(except: nil)
|
|
50
|
+
@account.sessions.inactive.each do |session|
|
|
51
|
+
next if session.session_id == except&.session_id
|
|
52
|
+
OAuth.client.delete_session(session.session_id, revoke: true)
|
|
53
|
+
session.destroy!
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -7,6 +7,7 @@ require 'sinatra/reloader'
|
|
|
7
7
|
require_relative 'init'
|
|
8
8
|
require_relative 'account'
|
|
9
9
|
require_relative 'bookmark'
|
|
10
|
+
require_relative 'session'
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
#
|
|
@@ -49,7 +50,12 @@ class BookmarksApp < Sinatra::Base
|
|
|
49
50
|
|
|
50
51
|
oauth_session = OAuth.client.get_session(session_id)
|
|
51
52
|
|
|
52
|
-
if oauth_session
|
|
53
|
+
if oauth_session && !oauth_session.pending?
|
|
54
|
+
user_did = oauth_session.did
|
|
55
|
+
web_session = Session.active.joins(:account).find_by(session_id: session_id, accounts: { did: user_did })
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
if web_session.nil?
|
|
53
59
|
session.delete(:oauth_session_id)
|
|
54
60
|
return nil
|
|
55
61
|
end
|
|
@@ -120,11 +126,11 @@ class BookmarksApp < Sinatra::Base
|
|
|
120
126
|
|
|
121
127
|
result = OAuth.client.handle_callback(params, expected_session_id: pending_session_id)
|
|
122
128
|
|
|
129
|
+
account = Account.create_or_find_by!(did: result.did)
|
|
130
|
+
account.sessions.create!(session_id: result.session_id)
|
|
131
|
+
|
|
123
132
|
session[:oauth_session_id] = result.session_id
|
|
124
133
|
session[:handle] = lookup_handle(result.did)
|
|
125
|
-
|
|
126
|
-
account = Account.create_or_find_by!(did: result.did)
|
|
127
|
-
account.update!(session_id: result.session_id)
|
|
128
134
|
redirect '/bookmarks'
|
|
129
135
|
rescue StandardError => e
|
|
130
136
|
report_error("Authorization failed", e)
|
|
@@ -165,15 +171,23 @@ class BookmarksApp < Sinatra::Base
|
|
|
165
171
|
redirect '/bookmarks'
|
|
166
172
|
end
|
|
167
173
|
|
|
168
|
-
post '/
|
|
174
|
+
post '/sync' do
|
|
169
175
|
require_login
|
|
170
176
|
|
|
171
|
-
|
|
172
|
-
|
|
177
|
+
enabled = params[:enabled]
|
|
178
|
+
halt 400 unless %w[true false].include?(enabled)
|
|
173
179
|
|
|
174
|
-
|
|
180
|
+
current_account.update!(sync_enabled: enabled == 'true')
|
|
181
|
+
redirect '/bookmarks'
|
|
182
|
+
rescue StandardError => e
|
|
183
|
+
report_error("Changing background sync failed", e)
|
|
184
|
+
redirect '/bookmarks'
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
post '/logout' do
|
|
188
|
+
require_login
|
|
175
189
|
|
|
176
|
-
|
|
190
|
+
current_account.sessions.find_by!(session_id: current_session.session_id).update!(active: false)
|
|
177
191
|
|
|
178
192
|
session.clear
|
|
179
193
|
redirect '/'
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_record'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
#
|
|
7
|
+
# Links an Account to an AuthProto session (from the configured AuthProto session storage) through its `session_id`
|
|
8
|
+
# identifier. When the user logs out, the session is marked as inactive and not used for website access anymore,
|
|
9
|
+
# but it's kept temporarily in the session storage & database in order to allow background access for the bookmark
|
|
10
|
+
# import job while the user isn't logged in.
|
|
11
|
+
#
|
|
12
|
+
# Logged-out sessions are cleaned up eventually, although one remaining session is always kept to allow background
|
|
13
|
+
# sync, unless the user disables the syncing.
|
|
14
|
+
#
|
|
15
|
+
|
|
16
|
+
class Session < ActiveRecord::Base
|
|
17
|
+
belongs_to :account
|
|
18
|
+
|
|
19
|
+
scope :active, -> { where(active: true) }
|
|
20
|
+
scope :inactive, -> { where(active: false) }
|
|
21
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class AddSessions < ActiveRecord::Migration[8.1]
|
|
4
|
+
def up
|
|
5
|
+
add_column :accounts, :sync_enabled, :boolean, default: true, null: false
|
|
6
|
+
|
|
7
|
+
create_table :sessions do |t|
|
|
8
|
+
t.references :account, null: false, foreign_key: true
|
|
9
|
+
t.string :session_id, null: false
|
|
10
|
+
t.boolean :active, default: true, null: false
|
|
11
|
+
t.index :session_id, unique: true
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
execute <<~SQL
|
|
15
|
+
INSERT INTO sessions (account_id, session_id)
|
|
16
|
+
SELECT id, session_id FROM accounts WHERE session_id IS NOT NULL
|
|
17
|
+
SQL
|
|
18
|
+
|
|
19
|
+
remove_column :accounts, :session_id
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def down
|
|
23
|
+
add_column :accounts, :session_id, :string
|
|
24
|
+
|
|
25
|
+
execute <<~SQL
|
|
26
|
+
UPDATE accounts SET session_id = (
|
|
27
|
+
SELECT session_id FROM sessions
|
|
28
|
+
WHERE account_id = accounts.id AND active
|
|
29
|
+
ORDER BY id DESC LIMIT 1
|
|
30
|
+
)
|
|
31
|
+
SQL
|
|
32
|
+
|
|
33
|
+
drop_table :sessions
|
|
34
|
+
remove_column :accounts, :sync_enabled
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class AddTimestampsToSessions < ActiveRecord::Migration[8.1]
|
|
4
|
+
def up
|
|
5
|
+
add_timestamps :sessions, null: true
|
|
6
|
+
|
|
7
|
+
execute <<~SQL
|
|
8
|
+
UPDATE sessions SET created_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP
|
|
9
|
+
SQL
|
|
10
|
+
|
|
11
|
+
change_column_null :sessions, :created_at, false
|
|
12
|
+
change_column_null :sessions, :updated_at, false
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def down
|
|
16
|
+
remove_timestamps :sessions
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -10,11 +10,11 @@
|
|
|
10
10
|
#
|
|
11
11
|
# It's strongly recommended that you check this file into your version control system.
|
|
12
12
|
|
|
13
|
-
ActiveRecord::Schema[8.1].define(version:
|
|
13
|
+
ActiveRecord::Schema[8.1].define(version: 2026_11_17_120100) do
|
|
14
14
|
create_table "accounts", force: :cascade do |t|
|
|
15
15
|
t.string "did", null: false
|
|
16
16
|
t.boolean "fetched", default: false, null: false
|
|
17
|
-
t.
|
|
17
|
+
t.boolean "sync_enabled", default: true, null: false
|
|
18
18
|
t.index ["did"], name: "index_accounts_on_did", unique: true
|
|
19
19
|
end
|
|
20
20
|
|
|
@@ -29,5 +29,16 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_07_004141) do
|
|
|
29
29
|
t.index ["account_id", "uri"], name: "index_bookmarks_on_account_id_and_uri", unique: true
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
create_table "sessions", force: :cascade do |t|
|
|
33
|
+
t.integer "account_id", null: false
|
|
34
|
+
t.string "session_id", null: false
|
|
35
|
+
t.boolean "active", default: true, null: false
|
|
36
|
+
t.datetime "created_at", null: false
|
|
37
|
+
t.datetime "updated_at", null: false
|
|
38
|
+
t.index ["account_id"], name: "index_sessions_on_account_id"
|
|
39
|
+
t.index ["session_id"], name: "index_sessions_on_session_id", unique: true
|
|
40
|
+
end
|
|
41
|
+
|
|
32
42
|
add_foreign_key "bookmarks", "accounts"
|
|
43
|
+
add_foreign_key "sessions", "accounts"
|
|
33
44
|
end
|
|
@@ -34,6 +34,16 @@
|
|
|
34
34
|
</button>
|
|
35
35
|
</form>
|
|
36
36
|
|
|
37
|
+
<form method="post" action="/sync" class="fetch-form">
|
|
38
|
+
<input type="hidden" name="authenticity_token" value="<%= html_escape(csrf_token) %>">
|
|
39
|
+
|
|
40
|
+
<span class="muted">Background sync is <%= current_account.sync_enabled? ? 'on' : 'off' %>.</span>
|
|
41
|
+
|
|
42
|
+
<button type="submit" class="link-button" name="enabled" value="<%= !current_account.sync_enabled? %>">
|
|
43
|
+
<%= current_account.sync_enabled? ? 'Turn off background sync' : 'Turn on background sync' %>
|
|
44
|
+
</button>
|
|
45
|
+
</form>
|
|
46
|
+
|
|
37
47
|
<% unless @query.empty? %>
|
|
38
48
|
<p>Bookmarks containing “<%= html_escape @query %>”:</p>
|
|
39
49
|
<% end %>
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: ../..
|
|
3
3
|
specs:
|
|
4
|
-
authproto (0.3.0.
|
|
4
|
+
authproto (0.3.0.beta6)
|
|
5
5
|
didkit (~> 0.5.0)
|
|
6
6
|
jose (~> 1.2)
|
|
7
7
|
jwt (~> 3.3)
|
|
@@ -91,7 +91,7 @@ DEPENDENCIES
|
|
|
91
91
|
sqlite3
|
|
92
92
|
|
|
93
93
|
CHECKSUMS
|
|
94
|
-
authproto (0.3.0.
|
|
94
|
+
authproto (0.3.0.beta6)
|
|
95
95
|
base58 (0.2.3) sha256=fda5c5d3bda8b429ad0d1a5309c2d1e488ba46f0a2ab99a639890f26c61bf3be
|
|
96
96
|
base64 (0.3.0) sha256=27337aeabad6ffae05c265c450490628ef3ebd4b67be58257393227588f5a97b
|
|
97
97
|
bundler (4.0.23) sha256=407ba8a1d928f412847f819aa5f8b6aef7c59fb51feb517112af8a165487287b
|
data/lib/authproto/client.rb
CHANGED
|
@@ -4,6 +4,7 @@ require 'didkit/did'
|
|
|
4
4
|
require 'jose'
|
|
5
5
|
require 'uri'
|
|
6
6
|
|
|
7
|
+
require_relative 'client_assertion'
|
|
7
8
|
require_relative 'configuration'
|
|
8
9
|
require_relative 'dpop/client'
|
|
9
10
|
require_relative 'errors'
|
|
@@ -13,7 +14,6 @@ require_relative 'metadata/client_metadata'
|
|
|
13
14
|
require_relative 'metadata/resource_server'
|
|
14
15
|
require_relative 'par'
|
|
15
16
|
require_relative 'par/client'
|
|
16
|
-
require_relative 'par/client_assertion'
|
|
17
17
|
require_relative 'par/request'
|
|
18
18
|
require_relative 'state/session_manager'
|
|
19
19
|
require_relative 'state/token_set'
|
|
@@ -204,7 +204,7 @@ module AuthProto
|
|
|
204
204
|
# for confidential clients
|
|
205
205
|
#
|
|
206
206
|
# @raise [InvalidClientMetadata] if the metadata or some other parameters are invalid or don't match each other
|
|
207
|
-
# @raise [
|
|
207
|
+
# @raise [ClientAssertion::Error] if the signing key is invalid or not usable
|
|
208
208
|
# @raise [HTTPClient::HTTPError, HTTPClient::SSRFError] if fetching client metadata fails
|
|
209
209
|
#
|
|
210
210
|
def initialize(client_id:, redirect_uri:, metadata: nil, signing_key: nil)
|
|
@@ -250,21 +250,23 @@ module AuthProto
|
|
|
250
250
|
# @param pds [String, URI::HTTPS, nil] full URL of the PDS to authorize the user on
|
|
251
251
|
# @param scope [String] space-separated list of scopes to request authorization for; the scopes must all be
|
|
252
252
|
# declared in the client metadata, and must include `atproto`
|
|
253
|
+
# @param prompt [String, nil] optional authorization prompt mode, such as `consent`
|
|
253
254
|
#
|
|
254
255
|
# @return [Hash{Symbol => String}] `:url` to direct the user to, `:session_id` of the created session
|
|
255
256
|
#
|
|
256
257
|
# @raise [AuthorizationError] if identity arguments or scope are invalid or identity can't be resolved
|
|
258
|
+
# @raise [ArgumentError] if the prompt is invalid or not on the server's list of supported prompt modes
|
|
257
259
|
# @raise [DIDKit::DIDError, DIDKit::FormatError, DIDKit::APIError] if DID validation or document fetching fails
|
|
258
260
|
# @raise [Encryption::EncryptionError] if session serialization cannot encrypt secrets
|
|
259
261
|
# @raise [HTTPClient::SSRFError] if server discovery rejects a destination
|
|
260
262
|
# @raise [InvalidAuthorizationServer] if server discovery or metadata validation fails
|
|
261
263
|
# @raise [DPoP::Client::Error] if generating the PAR proof fails
|
|
262
264
|
# @raise [DPoP::KeySet::KeyError] if generating the session key fails
|
|
263
|
-
# @raise [
|
|
265
|
+
# @raise [ClientAssertion::Error] if client assertion signing fails
|
|
264
266
|
# @raise [PAR::Error] if the pushed authorization request fails
|
|
265
267
|
# @raise [Storage::StorageError] if session storage or locking fails
|
|
266
268
|
#
|
|
267
|
-
def authorize(handle: nil, pds: nil, scope: 'atproto')
|
|
269
|
+
def authorize(handle: nil, pds: nil, scope: 'atproto', prompt: nil)
|
|
268
270
|
validate_auth_params(handle, pds, scope)
|
|
269
271
|
pds = pds.to_s if pds.is_a?(URI::HTTP)
|
|
270
272
|
|
|
@@ -278,6 +280,16 @@ module AuthProto
|
|
|
278
280
|
|
|
279
281
|
authorization_server = resolve_auth_server_info(pds)
|
|
280
282
|
|
|
283
|
+
if prompt
|
|
284
|
+
raise ArgumentError, "prompt must be a nonempty string" if !prompt.is_a?(String) || prompt.empty?
|
|
285
|
+
|
|
286
|
+
supported_prompts = authorization_server.prompt_values_supported
|
|
287
|
+
|
|
288
|
+
if supported_prompts && !supported_prompts.include?(prompt)
|
|
289
|
+
raise ArgumentError, "prompt is not supported by the authorization server: #{prompt.inspect}"
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
|
|
281
293
|
session = session_manager.create_session(
|
|
282
294
|
client_id: @client_id,
|
|
283
295
|
scope: scope,
|
|
@@ -287,7 +299,7 @@ module AuthProto
|
|
|
287
299
|
signing_key_id: @signing_key_kid
|
|
288
300
|
)
|
|
289
301
|
|
|
290
|
-
auth_url = submit_authorization_par(authorization_server, session, login_hint: handle)
|
|
302
|
+
auth_url = submit_authorization_par(authorization_server, session, login_hint: handle, prompt: prompt)
|
|
291
303
|
|
|
292
304
|
{
|
|
293
305
|
url: auth_url,
|
|
@@ -322,7 +334,7 @@ module AuthProto
|
|
|
322
334
|
# @raise [DPoP::Client::Error] if proof/assertion generation fails
|
|
323
335
|
# @raise [Encryption::EncryptionError] if tokens cannot be encrypted for storage
|
|
324
336
|
# @raise [HTTPClient::HTTPError, HTTPClient::SSRFError] if token transport fails
|
|
325
|
-
# @raise [
|
|
337
|
+
# @raise [ClientAssertion::Error] if proof/assertion generation fails
|
|
326
338
|
# @raise [State::SessionError] if token subject conflicts with the session DID
|
|
327
339
|
# @raise [Storage::StorageError] if session storage or locking fails
|
|
328
340
|
# @raise [TokenError] if token exchange or response validation fails; identity failures attempt token revocation
|
|
@@ -667,7 +679,7 @@ module AuthProto
|
|
|
667
679
|
return nil
|
|
668
680
|
end
|
|
669
681
|
|
|
670
|
-
assertion =
|
|
682
|
+
assertion = ClientAssertion.new(client_id: @client_id, signing_key: key)
|
|
671
683
|
private_key = assertion.signing_key
|
|
672
684
|
|
|
673
685
|
if @client_metadata.jwks
|
|
@@ -687,7 +699,7 @@ module AuthProto
|
|
|
687
699
|
|
|
688
700
|
def metadata_key_matches_public_key?(candidate, public_key)
|
|
689
701
|
return false if candidate['revoked']
|
|
690
|
-
return false if candidate.key?('alg') && candidate['alg'] !=
|
|
702
|
+
return false if candidate.key?('alg') && candidate['alg'] != ClientAssertion::ALGORITHM
|
|
691
703
|
return false if candidate.key?('use') && candidate['use'] != 'sig'
|
|
692
704
|
return false if candidate.key?('key_ops') && !candidate['key_ops'].include?('verify')
|
|
693
705
|
|
|
@@ -723,7 +735,7 @@ module AuthProto
|
|
|
723
735
|
Metadata::AuthorizationServer.from_url(auth_server_url)
|
|
724
736
|
end
|
|
725
737
|
|
|
726
|
-
def submit_authorization_par(authorization_server, session, login_hint: nil)
|
|
738
|
+
def submit_authorization_par(authorization_server, session, login_hint: nil, prompt: nil)
|
|
727
739
|
par_client = PAR::Client.new(
|
|
728
740
|
authorization_server: authorization_server,
|
|
729
741
|
dpop_client: @dpop_client
|
|
@@ -734,7 +746,8 @@ module AuthProto
|
|
|
734
746
|
req.redirect_uri = @redirect_uri
|
|
735
747
|
req.state = session.state_token
|
|
736
748
|
req.scope = session.requested_scope
|
|
737
|
-
req.login_hint = login_hint
|
|
749
|
+
req.login_hint = login_hint
|
|
750
|
+
req.prompt = prompt
|
|
738
751
|
req.code_challenge = session.pkce_challenge
|
|
739
752
|
|
|
740
753
|
if @client_metadata.confidential?
|
|
@@ -820,7 +833,7 @@ module AuthProto
|
|
|
820
833
|
|
|
821
834
|
if @client_metadata.confidential?
|
|
822
835
|
body.merge!(
|
|
823
|
-
client_assertion_type:
|
|
836
|
+
client_assertion_type: ClientAssertion::TYPE,
|
|
824
837
|
client_assertion: @client_assertion.generate_jwt(audience: session.auth_server.issuer)
|
|
825
838
|
)
|
|
826
839
|
end
|