yalla_auth_ruby_client 2.0.0 → 2.0.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca648fdb3f6345e91b325abf09524808aaa23ab011a6c1e26b49176c25c9a12b
|
4
|
+
data.tar.gz: ed4ab25b82c4df4ab5463c53a6390a99b4c376e8833f8692e5b25b4032f9e25d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c58757c65e91db1147a29e32c69f23adc7aad22c62aa1b0c1246a26c37afd592cfe4cb6b05f609d05b271e686ab52ffbb0bb952ec6fe18245c01a8e1e8fbd446
|
7
|
+
data.tar.gz: 78be6202e01a6b824bb1ba220647f6f1bdd3bcf8e8c62e4f5a6a4dda82b1b877f89527c8ac7d63ac8e329837a9a11373f2b58348c0b8181e909ee69404e61c12
|
data/README.md
CHANGED
@@ -76,6 +76,12 @@ end
|
|
76
76
|
provided by the authentication service.
|
77
77
|
- `logout` clears the cookie and redirects to `ENV["AUTH_URL"]`.
|
78
78
|
|
79
|
+
For controllers that inherit from `ActionController::API`, the engine includes
|
80
|
+
`YallaAuthRubyClient::ApiControllerAuthentication`, which reads the bearer token
|
81
|
+
from the `Authorization` header. It exposes the same helpers but responds with a
|
82
|
+
`401` JSON body containing the `redirect_uri` when authentication fails instead
|
83
|
+
of issuing an HTTP redirect.
|
84
|
+
|
79
85
|
### Use the authentication middleware
|
80
86
|
|
81
87
|
Add `YallaAuthRubyClient::AuthTokenMiddleware` to your Rails middleware stack
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module YallaAuthRubyClient
|
2
|
+
module ApiControllerAuthentication
|
3
|
+
def logout
|
4
|
+
render json: { redirect_uri: ENV["AUTH_URL"] }
|
5
|
+
end
|
6
|
+
|
7
|
+
def authenticate_user
|
8
|
+
token = bearer_token
|
9
|
+
return false unless token.present?
|
10
|
+
|
11
|
+
begin
|
12
|
+
api_client = OpenapiClient::AuthApi.new
|
13
|
+
response = api_client.auth_validate_token_get(token)
|
14
|
+
|
15
|
+
if response && response.success
|
16
|
+
@yalla_user = response.user
|
17
|
+
@current_user = find_or_create_app_user(@yalla_user)
|
18
|
+
true
|
19
|
+
else
|
20
|
+
false
|
21
|
+
end
|
22
|
+
rescue OpenapiClient::ApiError => e
|
23
|
+
Rails.logger.error "Authentication failed: #{e.message}"
|
24
|
+
false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def authenticate_user!
|
29
|
+
return if authenticate_user
|
30
|
+
|
31
|
+
render json: { error: 'unauthorized', redirect_uri: login_redirect_uri }, status: :unauthorized
|
32
|
+
end
|
33
|
+
|
34
|
+
def current_user
|
35
|
+
@current_user
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def bearer_token
|
41
|
+
auth_header = request.headers['Authorization'].to_s
|
42
|
+
return unless auth_header.present?
|
43
|
+
|
44
|
+
scheme, token = auth_header.split(' ', 2)
|
45
|
+
return unless scheme&.casecmp('Bearer')&.zero?
|
46
|
+
|
47
|
+
token&.strip.presence
|
48
|
+
end
|
49
|
+
|
50
|
+
def find_or_create_app_user(user)
|
51
|
+
AppUser.find_or_create_by(yalla_id: user.id)
|
52
|
+
end
|
53
|
+
|
54
|
+
def login_redirect_uri
|
55
|
+
"#{ENV['AUTH_URL']}/users/sign_in?redirect_uri="
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'openapi_client'
|
2
2
|
require 'rails/railtie'
|
3
3
|
require 'yalla_auth_ruby_client/controller_authentication'
|
4
|
+
require 'yalla_auth_ruby_client/api_controller_authentication'
|
4
5
|
require 'yalla_auth_ruby_client/middleware/auth_token_middleware'
|
5
6
|
|
6
7
|
module YallaAuthRubyClient
|
@@ -9,6 +10,10 @@ module YallaAuthRubyClient
|
|
9
10
|
ActiveSupport.on_load(:action_controller_base) do
|
10
11
|
include YallaAuthRubyClient::ControllerAuthentication
|
11
12
|
end
|
13
|
+
|
14
|
+
ActiveSupport.on_load(:action_controller_api) do
|
15
|
+
include YallaAuthRubyClient::ApiControllerAuthentication
|
16
|
+
end
|
12
17
|
end
|
13
18
|
end
|
14
19
|
end
|
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yalla_auth_ruby_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yalla auth openapi client
|
@@ -101,6 +101,7 @@ files:
|
|
101
101
|
- lib/openapi_client/models/user_role_add.rb
|
102
102
|
- lib/openapi_client/version.rb
|
103
103
|
- lib/yalla_auth_ruby_client.rb
|
104
|
+
- lib/yalla_auth_ruby_client/api_controller_authentication.rb
|
104
105
|
- lib/yalla_auth_ruby_client/controller_authentication.rb
|
105
106
|
- lib/yalla_auth_ruby_client/middleware/auth_token_middleware.rb
|
106
107
|
- spec/api/apps_api_spec.rb
|
@@ -113,6 +114,7 @@ files:
|
|
113
114
|
- spec/models/user_role_add_spec.rb
|
114
115
|
- spec/models/user_spec.rb
|
115
116
|
- spec/spec_helper.rb
|
117
|
+
- yalla_auth_ruby_client-2.0.1.gem
|
116
118
|
- yalla_auth_ruby_client.gemspec
|
117
119
|
homepage: https://matrix.tn
|
118
120
|
licenses:
|