Skip to content

Commit c35bd8a

Browse files
committed
Make AuthHttp a domain and add BearerToken.load_user helper
1 parent 633896d commit c35bd8a

File tree

7 files changed

+62
-18
lines changed

7 files changed

+62
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [0.0.3] - 2025-04-25
2+
3+
- Add BearerToken.load_user
4+
15
## [0.0.2] - 2025-04-22
26

37
- Add several helpful request/response mutators

Gemfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
foobara-auth-http (0.0.2)
4+
foobara-auth-http (0.0.3)
55
foobara (~> 0.0.108)
66
foobara-auth (~> 0.0.1)
77
foobara-http-command-connector (~> 0.0.1)
@@ -26,7 +26,7 @@ GEM
2626
diff-lcs (1.6.1)
2727
docile (1.4.1)
2828
dotenv (3.1.8)
29-
extract-repo (0.0.10)
29+
extract-repo (0.0.11)
3030
foobara (~> 0.0.102)
3131
foobara-sh-cli-connector (~> 0.0.1)
3232
ffi (1.17.2-x86_64-linux-gnu)
@@ -53,7 +53,7 @@ GEM
5353
foobara-type-generator (~> 0.0.1)
5454
foobara-typescript-react-command-form-generator (~> 0.0.1)
5555
foobara-typescript-remote-command-generator (~> 0.0.1)
56-
foobara (0.0.109)
56+
foobara (0.0.111)
5757
bigdecimal
5858
foobara-lru-cache (~> 0.0.2)
5959
foobara-util (~> 0.0.11)
@@ -148,7 +148,7 @@ GEM
148148
pp (>= 0.6.0)
149149
rdoc (>= 4.0.0)
150150
reline (>= 0.4.2)
151-
json (2.10.2)
151+
json (2.11.3)
152152
jwt (2.10.1)
153153
base64
154154
language_server-protocol (3.17.0.4)

lib/foobara/auth_http.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
require "foobara/http_command_connector"
33
require "foobara/auth"
44

5-
Foobara::Util.require_directory "#{__dir__}/../../src"
6-
75
module Foobara
86
module AuthHttp
7+
foobara_domain!
8+
99
class << self
1010
def install!
1111
CommandConnectors::Http.register_authenticator(BearerAuthenticator)
@@ -14,4 +14,5 @@ def install!
1414
end
1515
end
1616

17+
Foobara::Util.require_directory "#{__dir__}/../../src"
1718
Foobara::Monorepo.project "auth_http", project_path: "#{__dir__}/../../"

spec/bearer_authenticator_spec.rb

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
let(:authenticator) { described_class.new }
99

10+
let(:user) do
11+
Foobara::Auth::Register.run!(username: "Barbara", email: "barbara@example.com", plaintext_password: "1234")
12+
end
13+
let(:access_token) do
14+
Foobara::Auth::Login.run!(email: user.email, plaintext_password: "1234")[:access_token]
15+
end
16+
1017
it "has a symbol and explanation" do
1118
expect(authenticator.symbol).to eq(:bearer)
1219
expect(authenticator.explanation).to match(/bearer/i)
@@ -25,12 +32,6 @@ def execute
2532
end
2633
end
2734
end
28-
let(:user) do
29-
Foobara::Auth::Register.run!(username: "Barbara", email: "barbara@example.com", plaintext_password: "1234")
30-
end
31-
let(:access_token) do
32-
Foobara::Auth::Login.run!(email: user.email, plaintext_password: "1234")[:access_token]
33-
end
3435

3536
let(:path) { "/run/HelloWorld" }
3637
let(:headers) do
@@ -73,4 +74,29 @@ def execute
7374
end
7475
end
7576
end
77+
78+
describe ".load_user" do
79+
let(:authenticator) do
80+
user = some_user_object
81+
82+
described_class.load_user do |_user_id|
83+
user
84+
end
85+
end
86+
let(:some_user_object) { Object.new }
87+
88+
let(:request) do
89+
Foobara::CommandConnectors::Http::Request.new(
90+
path: "/whatever",
91+
headers: { "authorization" => "Bearer #{access_token}" }
92+
)
93+
end
94+
let(:loaded_user) do
95+
authenticator.authenticate(request)
96+
end
97+
98+
it "can load a custom user object" do
99+
expect(loaded_user).to eq(some_user_object)
100+
end
101+
end
76102
end

src/foobara/auth_http.rb

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/foobara/auth_http/bearer_authenticator.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
module Foobara
22
module AuthHttp
33
class BearerAuthenticator < CommandConnector::Authenticator
4+
class << self
5+
def load_user(&block)
6+
new(load_user: block)
7+
end
8+
end
9+
10+
def initialize(load_user: nil, **)
11+
@load_user = load_user || ->(user_id) { Auth::FindUser.run!(id: user_id) }
12+
super(**)
13+
end
14+
415
def symbol
516
:bearer
617
end
@@ -9,6 +20,10 @@ def explanation
920
@explanation ||= "Expects an access token in authorization header in format of: Bearer <token>"
1021
end
1122

23+
def authenticate(request)
24+
request.instance_exec(&to_proc)
25+
end
26+
1227
def block
1328
return @block if @block
1429

@@ -41,7 +56,9 @@ def verify_access_token(token)
4156
end
4257

4358
def load_user_record(user_id)
44-
Auth::FindUser.run!(id: user_id)
59+
if user_id
60+
@load_user.call(user_id)
61+
end
4562
end
4663
end
4764
end

version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Foobara
22
module AuthHttp
3-
VERSION = "0.0.2".freeze
3+
VERSION = "0.0.3".freeze
44

55
local_ruby_version = File.read("#{__dir__}/.ruby-version").chomp
66
local_ruby_version_minor = local_ruby_version[/\A(\d+\.\d+)\.\d+\z/, 1]

0 commit comments

Comments
 (0)