authproto 0.3.0.beta3 → 0.3.0.beta5

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.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -1
  3. data/README.md +474 -311
  4. data/examples/basic_webapp/Gemfile +14 -0
  5. data/examples/{confidential_client → basic_webapp}/Gemfile.lock +1 -1
  6. data/examples/basic_webapp/README.md +300 -0
  7. data/examples/basic_webapp/app.rb +351 -0
  8. data/examples/basic_webapp/config/oauth-client-metadata.localhost.json +11 -0
  9. data/examples/basic_webapp/public/styles.css +346 -0
  10. data/examples/basic_webapp/scripts/generate_keys.rb +35 -0
  11. data/examples/basic_webapp/views/authorized.erb +48 -0
  12. data/examples/basic_webapp/views/index.erb +48 -0
  13. data/examples/{confidential_client → basic_webapp}/views/layout.erb +2 -2
  14. data/examples/bookmarks/Gemfile +16 -0
  15. data/examples/bookmarks/Gemfile.lock +158 -0
  16. data/examples/bookmarks/README.md +170 -0
  17. data/examples/bookmarks/Rakefile +23 -0
  18. data/examples/bookmarks/app/account.rb +16 -0
  19. data/examples/bookmarks/app/bookmark.rb +89 -0
  20. data/examples/bookmarks/app/init.rb +26 -0
  21. data/examples/bookmarks/app/oauth.rb +43 -0
  22. data/examples/bookmarks/app/server.rb +184 -0
  23. data/examples/bookmarks/config/oauth-client-metadata.localhost.json +11 -0
  24. data/examples/bookmarks/config/oauth-client-metadata.production.json +19 -0
  25. data/examples/bookmarks/config.ru +5 -0
  26. data/examples/bookmarks/db/migrate/20261107004141_create_bookmarks.rb +27 -0
  27. data/examples/bookmarks/db/schema.rb +33 -0
  28. data/examples/bookmarks/public/styles.css +59 -0
  29. data/examples/bookmarks/scripts/generate_keys.rb +35 -0
  30. data/examples/bookmarks/views/bookmarks.erb +86 -0
  31. data/examples/bookmarks/views/index.erb +17 -0
  32. data/examples/bookmarks/views/layout.erb +20 -0
  33. data/examples/minisky_console/Gemfile.lock +2 -2
  34. data/examples/minisky_console/README.md +29 -25
  35. data/examples/minisky_console/app.rb +3 -3
  36. data/examples/minisky_console/localhost.rb +5 -7
  37. data/examples/minisky_console/production.rb +6 -8
  38. data/lib/authproto/client.rb +505 -151
  39. data/lib/authproto/configuration.rb +1 -1
  40. data/lib/authproto/dpop/client.rb +28 -25
  41. data/lib/authproto/dpop/key_set.rb +2 -0
  42. data/lib/authproto/dpop/nonce_manager.rb +24 -25
  43. data/lib/authproto/dpop/proof_generator.rb +9 -5
  44. data/lib/authproto/dpop/stored_nonce.rb +7 -5
  45. data/lib/authproto/dpop.rb +2 -0
  46. data/lib/authproto/encryption.rb +31 -12
  47. data/lib/authproto/http_client.rb +13 -12
  48. data/lib/authproto/http_helpers.rb +6 -0
  49. data/lib/authproto/metadata/authorization_server.rb +15 -3
  50. data/lib/authproto/metadata/client_metadata.rb +34 -3
  51. data/lib/authproto/metadata/resource_server.rb +7 -6
  52. data/lib/authproto/minisky.rb +120 -33
  53. data/lib/authproto/par/client.rb +4 -2
  54. data/lib/authproto/par/client_assertion.rb +4 -1
  55. data/lib/authproto/par/request.rb +2 -0
  56. data/lib/authproto/par/response.rb +2 -0
  57. data/lib/authproto/par.rb +2 -0
  58. data/lib/authproto/pkce.rb +2 -0
  59. data/lib/authproto/serialization/session.rb +4 -2
  60. data/lib/authproto/serialization/stored_nonce.rb +2 -2
  61. data/lib/authproto/serialization/token_set.rb +5 -2
  62. data/lib/authproto/state/session.rb +23 -20
  63. data/lib/authproto/state/session_info.rb +150 -0
  64. data/lib/authproto/state/session_manager.rb +46 -19
  65. data/lib/authproto/state/token_set.rb +6 -3
  66. data/lib/authproto/state.rb +4 -2
  67. data/lib/authproto/storage/interface.rb +10 -0
  68. data/lib/authproto/storage/key_builder.rb +3 -3
  69. data/lib/authproto/storage/memory.rb +24 -18
  70. data/lib/authproto/storage/sqlite.rb +14 -0
  71. data/lib/authproto/token/identity.rb +54 -0
  72. data/lib/authproto/token/refresh.rb +44 -54
  73. data/lib/authproto/token/response.rb +75 -0
  74. data/lib/authproto/token/revocation.rb +94 -0
  75. data/lib/authproto/version.rb +1 -1
  76. data/lib/authproto.rb +6 -3
  77. data/sig/authproto/client.rbs +38 -13
  78. data/sig/authproto/dpop/client.rbs +9 -3
  79. data/sig/authproto/dpop/nonce_manager.rbs +6 -6
  80. data/sig/authproto/dpop/proof_generator.rbs +4 -4
  81. data/sig/authproto/dpop/stored_nonce.rbs +2 -2
  82. data/sig/authproto/encryption.rbs +4 -2
  83. data/sig/authproto/http_client.rbs +23 -7
  84. data/sig/authproto/http_helpers.rbs +1 -0
  85. data/sig/authproto/metadata/authorization_server.rbs +3 -3
  86. data/sig/authproto/metadata/client_metadata.rbs +2 -1
  87. data/sig/authproto/metadata/resource_server.rbs +2 -2
  88. data/sig/authproto/minisky.rbs +57 -0
  89. data/sig/authproto/state/session.rbs +13 -4
  90. data/sig/authproto/state/session_info.rbs +23 -0
  91. data/sig/authproto/state/session_manager.rbs +10 -3
  92. data/sig/authproto/storage/interface.rbs +1 -0
  93. data/sig/authproto/storage/key_builder.rbs +1 -1
  94. data/sig/authproto/storage/memory.rbs +1 -1
  95. data/sig/authproto/storage/sqlite.rbs +1 -0
  96. data/sig/authproto/token/refresh.rbs +2 -1
  97. data/sig/authproto/token/response.rbs +12 -0
  98. data/sig/authproto/token/revocation.rbs +15 -0
  99. data/sig/authproto.rbs +3 -8
  100. metadata +53 -19
  101. data/examples/confidential_client/Gemfile +0 -14
  102. data/examples/confidential_client/README.md +0 -236
  103. data/examples/confidential_client/app.rb +0 -291
  104. data/examples/confidential_client/public/styles.css +0 -70
  105. data/examples/confidential_client/scripts/generate_keys.rb +0 -21
  106. data/examples/confidential_client/views/authorized.erb +0 -46
  107. data/examples/confidential_client/views/index.erb +0 -44
  108. data/sig/authproto/minisky_oauth.rbs +0 -27
  109. /data/examples/{confidential_client/config/oauth-client-metadata.example.json → basic_webapp/config/oauth-client-metadata.production.json} +0 -0
  110. /data/examples/{confidential_client → basic_webapp}/config.ru +0 -0
  111. /data/examples/{confidential_client → basic_webapp}/screenshots/screenshot-1-sign-in.png +0 -0
  112. /data/examples/{confidential_client → basic_webapp}/screenshots/screenshot-2-success.png +0 -0
@@ -0,0 +1,170 @@
1
+ # Bluesky bookmarks example
2
+
3
+ This example project implements a simple webapp which lets the user authenticate with their Bluesky/ATProto account, and then imports their private Bluesky bookmarks to a local database and lets them browse and search them.
4
+
5
+
6
+ ## Requirements
7
+
8
+ - Ruby 3.3+
9
+ - a domain name and an SSL certificate for your application for live deployment
10
+
11
+
12
+ ## Setup
13
+
14
+ 1. Clone the repository and navigate to the example app directory:
15
+
16
+ ```bash
17
+ cd examples/bookmarks
18
+ ```
19
+
20
+ 2. Install dependencies:
21
+
22
+ ```bash
23
+ bundle install
24
+ ```
25
+
26
+
27
+ ## App configuration
28
+
29
+ ### Client metadata
30
+
31
+ An ATProto OAuth app needs to have a [Client Metadata JSON file](https://atproto.com/specs/oauth#client-id-metadata-document), which configures the app's security settings and some user-facing info. This JSON file is normally hosted at the `/oauth-client-metadata.json` path. This app includes such endpoint, and reads the contents of the JSON to serve there from a file at `config/oauth-client-metadata.json`.
32
+
33
+ The configuration differs for localhost testing and a production site:
34
+
35
+ For localhost testing:
36
+
37
+ - Create a metadata file from the included **<u>localhost</u>** example:
38
+ ```bash
39
+ cp config/oauth-client-metadata.localhost.json config/oauth-client-metadata.json
40
+ ```
41
+ Here, this file is only used by the app to read the redirect URI and scope from there – it's not accessed by the authorization server.
42
+
43
+ For a live version:
44
+
45
+ - Create a metadata file from the included **<u>production</u>** example:
46
+ ```bash
47
+ cp config/oauth-client-metadata.production.json config/oauth-client-metadata.json
48
+ ```
49
+ - Replace the `YOUR_DOMAIN_HERE` in the URLs with the hostname where your app will be hosted
50
+ - If you want to use the app as a confidential client, generate the signing keys for client authentication (client assertions):
51
+ ```bash
52
+ bundle exec ruby scripts/generate_keys.rb
53
+ ```
54
+ and copy the private and public keys to `config/keys.json` and `config/oauth-client-metadata.json` respectively.
55
+ - Otherwise, if you want to run it as a public client:
56
+ - change `token_endpoint_auth_method` in the metadata JSON to `"none"`
57
+ - remove the `token_endpoint_auth_signing_alg` line
58
+ - remove the `jwks` section
59
+
60
+
61
+ ### ENV vars
62
+
63
+ Apart from the `oauth-client-metadata.json` & `keys.json` files, the app is configured through ENV variables. You can set them through `export` on the command line, but it also has [dotenv](https://github.com/bkeepers/dotenv) set up, so you can put the config vars in a file named `.env`.
64
+
65
+ You will need to generate session & encryption keys first. You can do it like this:
66
+
67
+ ```bash
68
+ openssl rand -base64 32
69
+ openssl rand -hex 32
70
+
71
+ cat > .env
72
+ ATPROTO_MASTER_KEY=<base64 value from first command>
73
+ SESSION_SECRET=<hex value from second command>
74
+ ```
75
+
76
+ `ATPROTO_MASTER_KEY` is used by AuthProto to encrypt some of the stored session/nonce data in the configured storage; `SESSION_SECRET` is used by `Rack::Session::Cookie` for the browser session.
77
+
78
+
79
+ ### Storage
80
+
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
+
83
+ The app also has a second SQLite database for the app's actual data – the imported bookmarks and account/import info. This database (with `accounts` and `bookmarks` tables) is stored in `db/bookmarks.sqlite3` and managed by ActiveRecord.
84
+
85
+ Before starting the app, you need to initialize this database by running the migration:
86
+
87
+ ```sh
88
+ bundle exec rake db:migrate
89
+ ```
90
+
91
+ The [sinatra-activerecord gem](https://github.com/sinatra-activerecord/sinatra-activerecord) is used for some integration of ActiveRecord with the server & rake tasks in a non-Rails environment.
92
+
93
+
94
+ ## Running on localhost
95
+
96
+ ```sh
97
+ bundle exec rackup
98
+ ```
99
+
100
+ Open <http://127.0.0.1:9292> in the browser and sign in using your handle, then click on the "Fetch your bookmarks" link. You should then see a paginated list of your bookmarks with a search form (for simplicity, only post text is displayed, without images or other embeds, and posts with no text are skipped).
101
+
102
+
103
+ ## Live deployment
104
+
105
+ ```bash
106
+ RACK_ENV=production bundle exec rackup
107
+ ```
108
+
109
+ This will start the server on `http://localhost:9292`. For a live deployment, the application must be accessible from the Internet for OAuth to work – the Authorization Server (PDS or entryway) needs to be able to load your `oauth-client-metadata.json` file before you can start the OAuth flow – so you will need to configure e.g. Nginx or Caddy to route requests from a given domain to your app, or use something like Passenger inside Nginx/Apache.
110
+
111
+ You will also need to set the `PERMITTED_DOMAIN` env var to the domain you run the app on. This is passed to the [:host_authorization](https://sinatrarb.com/intro.html#available-settings) option in Sinatra, which uses it to configure the [Rack::Protection::HostAuthorization](https://github.com/sinatra/sinatra/blob/main/rack-protection/lib/rack/protection/host_authorization.rb) middleware.
112
+
113
+ PERMITTED_DOMAIN=your.domain.com
114
+
115
+ Remember to also set that domain in the URLs in `oauth-client-metadata.json` under `client_id` & `redirect_uris`.
116
+
117
+ You can also use e.g. Tailscale funnels or Cloudflare tunnels to make an app on a local machine available from outside, see the instructions in the [basic_webapp's readme](../basic_webapp#tailscale).
118
+
119
+
120
+ ## How the app works
121
+
122
+ The main app code is spread across a few files in the `app` directory:
123
+
124
+ - `oauth.rb` – some global configuration for OAuth and AuthProto, accessed as a singleton from other parts
125
+ - `init.rb` – an initializer setting everything up
126
+ - `server.rb` – the main Sinatra server code
127
+ - `account.rb` and `bookmark.rb` – ActiveRecord models
128
+
129
+ 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
+
132
+ ### OAuth Authorization
133
+
134
+ The app uses both `AuthProto::Client` and `Minisky::OAuth` in different parts.
135
+
136
+ `Minisky::OAuth` is designed to be a stateful client, tracking a specific session of a specific user. This is not necessarily the best tool for a stateless webapp server controller, where each entry into an endpoint should look up the relevant user session from scratch (since there could be multiple users using the app in parallel). `AuthProto::Client` does not store session references in instance variables – session ID is always passed when needed in the argument to specific methods.
137
+
138
+ So the `server.rb` uses a plain `AuthProto::Client`; the `oauth.rb` file makes one available as a global singleton object as `OAuth.client`. `/authenticate` and `/callback` call `#authorize` and `#handle_callback` on it respectively in order to start and complete the authorization flow. Pending session ID and then authorized session ID is stored in the browser session, and then used to look up session info for authenticated user using the `#current_session` helper.
139
+
140
+ When completing authorization, the server also uses [didkit](https://tangled.org/mackuba.eu/didkit) to look up the authorized user's handle from their DID document, to display it later in the header.
141
+
142
+ 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
+
145
+ ### The import
146
+
147
+ 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`.
148
+
149
+ For simplicity, the import runs synchronously inside the controller, and everything is rendered server-side with ERB HTML output. In a real app, you'd rather want to perform such import instead inside a background worker process run with something like [Sidekiq](https://github.com/sidekiq/sidekiq), and possibly use JavaScript fetch requests for a more responsive UI.
150
+
151
+ The import process uses `Minisky::OAuth` for a bit more high-level XRPC request API. `Minisky::OAuth` has a convenience initializer `with_oauth_client`, which you can use to pass it an existing `AuthProto::Client` instance to work with underneath – in this case we pass the same global OAuth client as before:
152
+
153
+ ```rb
154
+ sky = Minisky::OAuth.with_oauth_client(OAuth.client, session_id: session_id)
155
+ ```
156
+
157
+ The import runs a loop making calls to `app.bsky.bookmark.getBookmarks` using `#get_request`, updating the cursor and saving the imported bookmarks after each iteration.
158
+
159
+ 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
+
161
+ ```rb
162
+ headers = { 'atproto-proxy' => 'did:web:api.bsky.app#bsky_appview' }
163
+ ...
164
+ response = sky.get_request('app.bsky.bookmark.getBookmarks', params, headers: headers)
165
+ ```
166
+
167
+ We could also use the `#fetch_all` Minisky method here, which handles the iteration automatically and just returns a list of collected results, but then we'd only be able to save the bookmark records at the end once we fetch all of them (although the current code wraps the whole import in a transaction anyway).
168
+
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.
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sinatra/activerecord/rake'
4
+
5
+ require_relative 'app/init'
6
+ require_relative 'app/account'
7
+ require_relative 'app/bookmark'
8
+
9
+ namespace :bookmarks do
10
+ desc "Fetch only newer bookmarks for an account: bookmarks:fetch[did]"
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
+ did = args[:did] || ENV['DID']
15
+ abort "Pass the owner's DID: bundle exec rake 'bookmarks:fetch[did:plc:...]'" if did.to_s.empty?
16
+
17
+ session_id = Account.find_by(did: did)&.session_id
18
+ abort "No saved OAuth session for #{did}; sign in through the web app first" unless session_id
19
+
20
+ count = Bookmark.fetch_bookmarks(session_id, newer_only: true)
21
+ puts "Fetched #{count} newer bookmarks for #{did}."
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+
5
+ require_relative 'init'
6
+
7
+
8
+ #
9
+ # A bookmark owner and the state of their most recent OAuth login and import.
10
+ #
11
+
12
+ class Account < ActiveRecord::Base
13
+ has_many :bookmarks, dependent: :destroy
14
+
15
+ validates :did, presence: true
16
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'time'
5
+
6
+ require_relative 'init'
7
+ require_relative 'account'
8
+
9
+ class Bookmark < ActiveRecord::Base
10
+ belongs_to :account
11
+
12
+ # Keep the API's bookmark timestamp instead of ActiveRecord's insertion time.
13
+ self.record_timestamps = false
14
+
15
+ scope :newest_first, -> { order(created_at: :desc, uri: :desc) }
16
+
17
+ # Fetches bookmarks synchronously, following API cursors and saving text posts.
18
+ # A failed fetch rolls back the import so an incremental retry cannot miss older pages.
19
+ #
20
+ # @param session_id [String] OAuth session belonging to the bookmark owner
21
+ # @param newer_only [Boolean] stop at the newest locally saved bookmark
22
+ # @return [Integer] number of imported bookmarks
23
+ # @raise [Minisky::AuthError] if the session cannot be resumed
24
+ #
25
+ def self.fetch_bookmarks(session_id, newer_only: false)
26
+ sky = Minisky::OAuth.with_oauth_client(OAuth.client, session_id: session_id)
27
+ sky.check_access
28
+
29
+ account = Account.find_by!(did: sky.did)
30
+
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
+
42
+ response = sky.get_request('app.bsky.bookmark.getBookmarks', query, headers: headers)
43
+ reached_newest = false
44
+
45
+ response.fetch('bookmarks').each do |entry|
46
+ created_at = entry['createdAt'] && Time.iso8601(entry['createdAt'])
47
+
48
+ if newest && created_at && created_at <= newest
49
+ reached_newest = true
50
+ next
51
+ end
52
+
53
+ post = entry.fetch('item')
54
+ next unless post['$type'] == 'app.bsky.feed.defs#postView'
55
+
56
+ record = post.fetch('record')
57
+ next if record['text'].blank?
58
+
59
+ bookmark = account.bookmarks.find_or_initialize_by(uri: entry.fetch('subject').fetch('uri'))
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
+ )
66
+
67
+ imported += 1
68
+ end
69
+
70
+ cursor = response['cursor']
71
+ break if cursor.blank? || reached_newest
72
+ end
73
+
74
+ account.update!(fetched: true)
75
+ end
76
+
77
+ imported
78
+ end
79
+
80
+ # Returns the Bluesky post URL using the author DID and record key in its AT URI.
81
+ #
82
+ # @return [String] public post URL
83
+ #
84
+ def post_url
85
+ did, rkey = uri.delete_prefix('at://').split('/').values_at(0, 2)
86
+
87
+ "https://bsky.app/profile/#{did}/post/#{rkey}"
88
+ end
89
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'authproto'
4
+ require 'authproto/minisky'
5
+ require 'didkit'
6
+ require 'dotenv/load'
7
+ require 'sinatra/activerecord'
8
+
9
+ require_relative 'oauth'
10
+
11
+ AuthProto.configure do |config|
12
+ config.http_client = AuthProto::HTTPClient.new(timeout: 10)
13
+
14
+ config.storage = AuthProto::Storage::SQLite.new(
15
+ path: ENV.fetch('AUTHPROTO_DATABASE', File.expand_path('../db/authproto.sqlite3', __dir__))
16
+ )
17
+ end
18
+
19
+ Sinatra::Base.register(Sinatra::ActiveRecordExtension)
20
+ Sinatra::Base.set(:database, {
21
+ adapter: 'sqlite3',
22
+ database: ENV.fetch('BOOKMARKS_DATABASE', File.expand_path('../db/bookmarks.sqlite3', __dir__)),
23
+ timeout: 5_000
24
+ })
25
+
26
+ OAuth.init
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'authproto'
4
+ require 'json'
5
+
6
+ module OAuth
7
+ class Error < StandardError; end
8
+
9
+ class << self
10
+ attr_accessor :metadata, :metadata_path
11
+
12
+ def init
13
+ @metadata_path = ENV.fetch('OAUTH_METADATA_PATH', File.expand_path('../config/oauth-client-metadata.json', __dir__))
14
+
15
+ unless File.exist?(@metadata_path)
16
+ raise "Copy a metadata template to config/oauth-client-metadata.json before starting the app"
17
+ end
18
+
19
+ @metadata = JSON.parse(File.read(@metadata_path))
20
+
21
+ client_id = @metadata.fetch('client_id')
22
+ redirect_uri = @metadata.fetch('redirect_uris')[0]
23
+
24
+ if AuthProto::Client.localhost_app?(client_id)
25
+ @oauth_client = AuthProto::Client.for_localhost(redirect_uri: redirect_uri, scope: @metadata['scope'])
26
+ else
27
+ keys_path = File.expand_path('../config/keys.json', __dir__)
28
+ signing_key = JSON.parse(File.read(keys_path))['keys'].first if File.exist?(keys_path)
29
+
30
+ @oauth_client = AuthProto::Client.new(
31
+ client_id: client_id,
32
+ redirect_uri: redirect_uri,
33
+ metadata: @metadata,
34
+ signing_key: signing_key
35
+ )
36
+ end
37
+ end
38
+
39
+ def client
40
+ @oauth_client
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,184 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+ require 'sinatra/base'
5
+ require 'sinatra/reloader'
6
+
7
+ require_relative 'init'
8
+ require_relative 'account'
9
+ require_relative 'bookmark'
10
+
11
+
12
+ #
13
+ # A server-rendered OAuth example that caches and searches private Bluesky bookmarks.
14
+ #
15
+
16
+ class BookmarksApp < Sinatra::Base
17
+ PER_PAGE = 20
18
+
19
+ configure :development do
20
+ register Sinatra::Reloader
21
+ end
22
+
23
+ configure do
24
+ set :root, File.expand_path('..', __dir__)
25
+ set :host_authorization, permitted_hosts: ['localhost', '127.0.0.1', '[::1]', ENV['PERMITTED_DOMAIN']].compact
26
+
27
+ session_secret = ENV.fetch('SESSION_SECRET') { SecureRandom.hex(32) }
28
+ raise "SESSION_SECRET must be at least 64 characters long" if session_secret.length < 64
29
+
30
+ master_key = ENV['ATPROTO_MASTER_KEY']
31
+ raise "ATPROTO_MASTER_KEY must be set" if master_key.nil?
32
+
33
+ use Rack::Session::Cookie,
34
+ key: 'bookmarks.session',
35
+ expire_after: 86_400,
36
+ secret: session_secret,
37
+ secure: production?,
38
+ httponly: true,
39
+ same_site: :lax
40
+
41
+ use Rack::Protection::AuthenticityToken
42
+ end
43
+
44
+ helpers do
45
+ def current_session
46
+ @current_session ||= begin
47
+ session_id = session[:oauth_session_id]
48
+ return nil unless session_id
49
+
50
+ oauth_session = OAuth.client.get_session(session_id)
51
+
52
+ if oauth_session.nil? || oauth_session.pending?
53
+ session.delete(:oauth_session_id)
54
+ return nil
55
+ end
56
+
57
+ oauth_session
58
+ end
59
+ end
60
+
61
+ def require_login
62
+ redirect '/' unless current_session
63
+ end
64
+
65
+ def current_account
66
+ @current_account ||= Account.find_by!(did: current_session.did)
67
+ end
68
+
69
+ def html_escape(text)
70
+ Rack::Utils.escape_html(text)
71
+ end
72
+
73
+ def csrf_token
74
+ Rack::Protection::AuthenticityToken.token(session)
75
+ end
76
+
77
+ def lookup_handle(did)
78
+ DID.new(did).get_verified_handle
79
+ rescue StandardError => e
80
+ puts "Handle lookup failed: #{e.class}: #{e.message}"
81
+ nil
82
+ end
83
+
84
+ def report_error(message, error)
85
+ puts "#{message}: #{error.class}: #{error.message}"
86
+ puts error.backtrace.take_while { |l| !l.include?('/lib/sinatra/base') }
87
+
88
+ session[:error] = "#{message}: #{error.message}"
89
+ end
90
+ end
91
+
92
+ before do
93
+ cache_control :no_store
94
+ end
95
+
96
+ get '/' do
97
+ redirect '/bookmarks' if current_session
98
+
99
+ erb :index
100
+ end
101
+
102
+ get '/oauth-client-metadata.json' do
103
+ content_type :json
104
+
105
+ File.read(OAuth.metadata_path)
106
+ end
107
+
108
+ post '/authenticate' do
109
+ auth = OAuth.client.authorize(handle: params[:handle].to_s.strip, scope: OAuth.metadata['scope'])
110
+
111
+ session[:pending_oauth_session_id] = auth.fetch(:session_id)
112
+ redirect auth.fetch(:url)
113
+ rescue StandardError => e
114
+ report_error("Authorization failed", e)
115
+ redirect '/'
116
+ end
117
+
118
+ get '/callback' do
119
+ pending_session_id = session.delete(:pending_oauth_session_id)
120
+
121
+ result = OAuth.client.handle_callback(params, expected_session_id: pending_session_id)
122
+
123
+ session[:oauth_session_id] = result.session_id
124
+ 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
+ redirect '/bookmarks'
129
+ rescue StandardError => e
130
+ report_error("Authorization failed", e)
131
+ redirect '/'
132
+ end
133
+
134
+ get '/bookmarks' do
135
+ require_login
136
+
137
+ @query = params[:q].to_s.strip
138
+ @fetched = current_account.fetched?
139
+
140
+ bookmarks = current_account.bookmarks.newest_first
141
+
142
+ if @query.empty?
143
+ @total_pages = [(bookmarks.count.to_f / PER_PAGE).ceil, 1].max
144
+ @page = params[:page].to_i.clamp(1, @total_pages)
145
+ first_page = (@page - 2).clamp(1, [@total_pages - 4, 1].max)
146
+ @page_numbers = first_page..[first_page + 4, @total_pages].min
147
+ @bookmarks = bookmarks.limit(PER_PAGE).offset((@page - 1) * PER_PAGE)
148
+ else
149
+ pattern = "%#{Bookmark.sanitize_sql_like(@query)}%"
150
+ @bookmarks = bookmarks.where("text LIKE ? ESCAPE '\\'", pattern)
151
+ end
152
+
153
+ erb :bookmarks
154
+ end
155
+
156
+ post '/fetch' do
157
+ require_login
158
+
159
+ count = Bookmark.fetch_bookmarks(current_session.session_id)
160
+
161
+ session[:notice] = "Fetched #{count} bookmarks."
162
+ redirect '/bookmarks'
163
+ rescue StandardError => e
164
+ report_error("Fetching bookmarks failed", e)
165
+ redirect '/bookmarks'
166
+ end
167
+
168
+ post '/logout' do
169
+ require_login
170
+
171
+ did = current_session.did
172
+ session_id = current_session.session_id
173
+
174
+ OAuth.client.delete_session(session_id, revoke: true)
175
+
176
+ Account.where(did: did, session_id: session_id).update_all(session_id: nil)
177
+
178
+ session.clear
179
+ redirect '/'
180
+ rescue StandardError => e
181
+ report_error("Logging out failed", e)
182
+ redirect '/bookmarks'
183
+ end
184
+ end
@@ -0,0 +1,11 @@
1
+ {
2
+ "client_id": "http://localhost/",
3
+ "client_name": "Bluesky Bookmarks",
4
+ "redirect_uris": ["http://127.0.0.1:9292/callback"],
5
+ "grant_types": ["authorization_code", "refresh_token"],
6
+ "response_types": ["code"],
7
+ "scope": "atproto rpc:app.bsky.bookmark.getBookmarks?aud=*",
8
+ "token_endpoint_auth_method": "none",
9
+ "application_type": "native",
10
+ "dpop_bound_access_tokens": true
11
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "client_id": "https://YOUR_DOMAIN_HERE/oauth-client-metadata.json",
3
+ "client_name": "Bluesky Bookmarks",
4
+ "redirect_uris": ["https://YOUR_DOMAIN_HERE/callback"],
5
+ "grant_types": ["authorization_code", "refresh_token"],
6
+ "response_types": ["code"],
7
+ "scope": "atproto rpc:app.bsky.bookmark.getBookmarks?aud=*",
8
+ "token_endpoint_auth_method": "private_key_jwt",
9
+ "token_endpoint_auth_signing_alg": "ES256",
10
+ "application_type": "web",
11
+ "dpop_bound_access_tokens": true,
12
+ "jwks": {
13
+ "keys": [
14
+ {
15
+ "x": "...", "y": "..."
16
+ }
17
+ ]
18
+ }
19
+ }
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'app/server'
4
+
5
+ run BookmarksApp
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateBookmarks < ActiveRecord::Migration[8.1]
4
+ def change
5
+ create_table :accounts do |t|
6
+ t.string :did, null: false
7
+ t.boolean :fetched, default: false, null: false
8
+ t.string :session_id
9
+ end
10
+
11
+ add_index :accounts, :did, unique: true
12
+
13
+ create_table :bookmarks do |t|
14
+ t.string :uri, null: false
15
+ t.string :author_handle, null: false
16
+ t.datetime :created_at
17
+ t.datetime :posted_at, null: false
18
+ t.text :text, null: false
19
+ t.integer :account_id, null: false
20
+ end
21
+
22
+ add_index :bookmarks, [:account_id, :created_at], order: { created_at: :desc }
23
+ add_index :bookmarks, [:account_id, :uri], unique: true
24
+
25
+ add_foreign_key :bookmarks, :accounts
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # This file is the source Rails uses to define your schema when running `bin/rails
6
+ # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
7
+ # be faster and is potentially less error prone than running all of your
8
+ # migrations from scratch. Old migrations may fail to apply correctly if those
9
+ # migrations use external dependencies or application code.
10
+ #
11
+ # It's strongly recommended that you check this file into your version control system.
12
+
13
+ ActiveRecord::Schema[8.1].define(version: 2026_11_07_004141) do
14
+ create_table "accounts", force: :cascade do |t|
15
+ t.string "did", null: false
16
+ t.boolean "fetched", default: false, null: false
17
+ t.string "session_id"
18
+ t.index ["did"], name: "index_accounts_on_did", unique: true
19
+ end
20
+
21
+ create_table "bookmarks", force: :cascade do |t|
22
+ t.string "uri", null: false
23
+ t.string "author_handle", null: false
24
+ t.datetime "created_at"
25
+ t.datetime "posted_at", null: false
26
+ t.text "text", null: false
27
+ t.integer "account_id", null: false
28
+ t.index ["account_id", "created_at"], name: "index_bookmarks_on_account_id_and_created_at", order: { created_at: :desc }
29
+ t.index ["account_id", "uri"], name: "index_bookmarks_on_account_id_and_uri", unique: true
30
+ end
31
+
32
+ add_foreign_key "bookmarks", "accounts"
33
+ end