foobara-auth-http 0.0.2 → 0.0.3
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 +4 -0
- data/README.md +80 -1
- data/lib/foobara/auth_http.rb +3 -2
- data/src/foobara/auth_http/bearer_authenticator.rb +18 -1
- metadata +2 -3
- data/src/foobara/auth_http.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 215085501258008264243a87c8e01db2e9322ea7f33c8cf22553659ff2524d06
|
4
|
+
data.tar.gz: 769f8c1c3cb67b6fcb6c40d65f85ce1c494fc6c1238d6a606e18f7b35423de19
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: caa515d8a6fbac87718425dbcacd4c3f4a672ec40746d62b2af42b32b89190f09752a0e0f632c02bedeb53737e734f4c2c6feb8173f1e09d5d6ba6c02f0f9e89
|
7
|
+
data.tar.gz: d008b9ffd7f400931c4a3360370f438d2148e73834b663d996d9ba0cafb83bf0de80b55acfe66beb5b35c556ccd7c534aeb2f07b1845930958a66c60896d9a49
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,16 @@
|
|
2
2
|
|
3
3
|
Contains helper classes/methods for exposing Foobara::Auth over HTTP
|
4
4
|
|
5
|
+
<!-- TOC -->
|
6
|
+
* [Foobara::AuthHttp](#foobaraauthhttp)
|
7
|
+
* [Installation](#installation)
|
8
|
+
* [Usage](#usage)
|
9
|
+
* [Rack example](#rack-example)
|
10
|
+
* [Rails example](#rails-example)
|
11
|
+
* [Contributing](#contributing)
|
12
|
+
* [License](#license)
|
13
|
+
<!-- TOC -->
|
14
|
+
|
5
15
|
## Installation
|
6
16
|
|
7
17
|
Typical stuff: add `gem "foobara-auth-http"` to your Gemfile or .gemspec file. Or even just
|
@@ -9,7 +19,76 @@ Typical stuff: add `gem "foobara-auth-http"` to your Gemfile or .gemspec file. O
|
|
9
19
|
|
10
20
|
## Usage
|
11
21
|
|
12
|
-
|
22
|
+
### Rack example
|
23
|
+
|
24
|
+
TODO
|
25
|
+
|
26
|
+
### Rails example
|
27
|
+
|
28
|
+
Here's an example of using AuthHttp helpers in a Rails app to expose various Foobara::Auth commands over HTTP
|
29
|
+
and put them to use:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require "foobara/rails_command_connector"
|
33
|
+
require "foobara/auth_http"
|
34
|
+
|
35
|
+
authenticator = Foobara::AuthHttp::BearerAuthenticator
|
36
|
+
|
37
|
+
Foobara::CommandConnectors::RailsCommandConnector.new(authenticator:)
|
38
|
+
require "foobara/rails/routes"
|
39
|
+
|
40
|
+
login_response_mutators = [
|
41
|
+
Foobara::AuthHttp::MoveRefreshTokenToCookie.new(secure: Rails.env.production?),
|
42
|
+
Foobara::AuthHttp::MoveAccessTokenToHeader
|
43
|
+
]
|
44
|
+
|
45
|
+
Rails.application.routes.draw do
|
46
|
+
command Foobara::Auth::Register,
|
47
|
+
inputs_transformers: Foobara::AttributesTransformers.only(:username, :email, :plaintext_password)
|
48
|
+
|
49
|
+
command Foobara::Auth::Login,
|
50
|
+
inputs_transformers: Foobara::AttributesTransformers.only(:username_or_email, :plaintext_password),
|
51
|
+
response_mutators: login_response_mutators
|
52
|
+
|
53
|
+
command Foobara::Auth::RefreshLogin,
|
54
|
+
request_mutators: Foobara::AuthHttp::SetRefreshTokenFromCookie,
|
55
|
+
inputs_transformers: Foobara::AttributesTransformers.only(:refresh_token),
|
56
|
+
response_mutators: login_response_mutators
|
57
|
+
|
58
|
+
command Foobara::Auth::Logout,
|
59
|
+
request_mutators: Foobara::AuthHttp::SetRefreshTokenFromCookie,
|
60
|
+
response_mutators: Foobara::AuthHttp::ClearAccessTokenHeader
|
61
|
+
|
62
|
+
command CreateBlogPost,
|
63
|
+
requires_authentication: true
|
64
|
+
|
65
|
+
command EditBlogPost,
|
66
|
+
requires_authentication: true,
|
67
|
+
allowed_rule: -> { blog_post.owned_by?(authenticated_user) }
|
68
|
+
|
69
|
+
# whatever other routes you need/want
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
A rundown of everything happening here:
|
74
|
+
|
75
|
+
* We are declaring that we want to authenticate using bearer tokens. These are JWT tokens in an
|
76
|
+
`Authorization: Bearer <token>` header.
|
77
|
+
* We are declaring that when we login or refresh our login, we would like to move the new access token
|
78
|
+
from the result to an X-Access-Token header, and we would like
|
79
|
+
to move the new refresh token from the result to a secure http only cookie.
|
80
|
+
* We are declaring that when we want to refresh our login, we want to move the refresh token from the
|
81
|
+
headers to an input to RefreshLogin.
|
82
|
+
* Logout could technically be handled by the client but for convenience/added safety, we expose
|
83
|
+
Logout and move the refresh token to its inputs so that it can invalidate the refresh token.
|
84
|
+
* When we respond from Logout, we set the X-Access-Token header to nil. This is something the client
|
85
|
+
could do but gives an easy way to clobber the client's access token without effort on their end.
|
86
|
+
|
87
|
+
We also expose a few app commands using our authenticator. This is configured as part of command connectors not
|
88
|
+
the foobara-auth domain nor this gem but included here as an example.
|
89
|
+
|
90
|
+
The inputs transformers are just convenience items to simplify any clients that import our exposed commands
|
91
|
+
to simplify their interfaces and any forms they feel like generating.
|
13
92
|
|
14
93
|
## Contributing
|
15
94
|
|
data/lib/foobara/auth_http.rb
CHANGED
@@ -2,10 +2,10 @@ require "foobara/all"
|
|
2
2
|
require "foobara/http_command_connector"
|
3
3
|
require "foobara/auth"
|
4
4
|
|
5
|
-
Foobara::Util.require_directory "#{__dir__}/../../src"
|
6
|
-
|
7
5
|
module Foobara
|
8
6
|
module AuthHttp
|
7
|
+
foobara_domain!
|
8
|
+
|
9
9
|
class << self
|
10
10
|
def install!
|
11
11
|
CommandConnectors::Http.register_authenticator(BearerAuthenticator)
|
@@ -14,4 +14,5 @@ module Foobara
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
Foobara::Util.require_directory "#{__dir__}/../../src"
|
17
18
|
Foobara::Monorepo.project "auth_http", project_path: "#{__dir__}/../../"
|
@@ -1,6 +1,17 @@
|
|
1
1
|
module Foobara
|
2
2
|
module AuthHttp
|
3
3
|
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
|
+
|
4
15
|
def symbol
|
5
16
|
:bearer
|
6
17
|
end
|
@@ -9,6 +20,10 @@ module Foobara
|
|
9
20
|
@explanation ||= "Expects an access token in authorization header in format of: Bearer <token>"
|
10
21
|
end
|
11
22
|
|
23
|
+
def authenticate(request)
|
24
|
+
request.instance_exec(&to_proc)
|
25
|
+
end
|
26
|
+
|
12
27
|
def block
|
13
28
|
return @block if @block
|
14
29
|
|
@@ -41,7 +56,9 @@ module Foobara
|
|
41
56
|
end
|
42
57
|
|
43
58
|
def load_user_record(user_id)
|
44
|
-
|
59
|
+
if user_id
|
60
|
+
@load_user.call(user_id)
|
61
|
+
end
|
45
62
|
end
|
46
63
|
end
|
47
64
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foobara-auth-http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Miles Georgi
|
@@ -63,7 +63,6 @@ files:
|
|
63
63
|
- LICENSE.txt
|
64
64
|
- README.md
|
65
65
|
- lib/foobara/auth_http.rb
|
66
|
-
- src/foobara/auth_http.rb
|
67
66
|
- src/foobara/auth_http/bearer_authenticator.rb
|
68
67
|
- src/foobara/auth_http/clear_access_token_header.rb
|
69
68
|
- src/foobara/auth_http/move_access_token_to_header.rb
|
@@ -91,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
90
|
- !ruby/object:Gem::Version
|
92
91
|
version: '0'
|
93
92
|
requirements: []
|
94
|
-
rubygems_version: 3.6.
|
93
|
+
rubygems_version: 3.6.8
|
95
94
|
specification_version: 4
|
96
95
|
summary: Contains convenience classes/methods for using Foobara::Auth over HTTP
|
97
96
|
test_files: []
|
data/src/foobara/auth_http.rb
DELETED