yalla_auth_ruby_client 2.0.0 → 2.0.1

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: 1f130db14d4ab21f5160424c47c583bb75d355bc871afb38448eae5eabe6dfba
4
- data.tar.gz: ec774cbda5742b775b4aec193e8eceeee142cca7311feb47644923aa8f98b29a
3
+ metadata.gz: d5d0daaf481f7552506732d1fc02b5adcdd993eaebf7136981817b78eb052849
4
+ data.tar.gz: 6e2896599fe4d9820cab1117c45f5f5b91927add54c2b85a01b6aa6b7c4219d6
5
5
  SHA512:
6
- metadata.gz: 1c51ed7933e90841eb6ae1c41c538dcb782e7ed26cb8921961d5c3964acd92dbdabd02f1522ec8f23f97211c59550c60066b0ae148c419d4cd1c0fa1af88e4ba
7
- data.tar.gz: d2f5f225d39421b540ea40420bf19c017eb9e91b43e493c95a4d76a07db6d234da4d10c2bbda4da95830e2a61e85403d5a5fa42d95e20b80e6d7578253769a4b
6
+ metadata.gz: 493756c3593d198b8a1d2178a7035b620eac083145bd82cae84cfc5bc5b10b837fc0a5bc7ed3c66119ebc87dc45dd7526e79656565ba5990ac1263e00f61a248
7
+ data.tar.gz: 9e23efe2ff05c8d85f44d82f32d49fed84966ca3df085bd7d19d4da74dee4e3ec4857fd84fee561353baee6d0de6a9aa12da4beea2a327b7459430a897c17e97
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
@@ -11,5 +11,5 @@ Generator version: 7.12.0
11
11
  =end
12
12
 
13
13
  module OpenapiClient
14
- VERSION = '2.0.0'
14
+ VERSION = '2.0.1'
15
15
  end
@@ -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=#{request.original_url}"
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
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.0
4
+ version: 2.0.1
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