yalla_auth_ruby_client 1.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 +4 -4
- data/README.md +43 -0
- data/RELEASE.md +9 -0
- data/lib/generators/yalla_auth_ruby_client/templates/add_yalla_id_to_app_users.rb +6 -0
- data/lib/generators/yalla_auth_ruby_client/user_generator.rb +28 -0
- data/lib/openapi_client/configuration.rb +1 -1
- data/lib/openapi_client/version.rb +1 -1
- data/lib/yalla_auth_ruby_client/api_controller_authentication.rb +58 -0
- data/lib/yalla_auth_ruby_client/controller_authentication.rb +47 -0
- data/lib/yalla_auth_ruby_client/middleware/auth_token_middleware.rb +33 -0
- data/lib/yalla_auth_ruby_client.rb +19 -0
- data/yalla_auth_ruby_client.gemspec +1 -0
- metadata +27 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5d0daaf481f7552506732d1fc02b5adcdd993eaebf7136981817b78eb052849
|
4
|
+
data.tar.gz: 6e2896599fe4d9820cab1117c45f5f5b91927add54c2b85a01b6aa6b7c4219d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 493756c3593d198b8a1d2178a7035b620eac083145bd82cae84cfc5bc5b10b837fc0a5bc7ed3c66119ebc87dc45dd7526e79656565ba5990ac1263e00f61a248
|
7
|
+
data.tar.gz: 9e23efe2ff05c8d85f44d82f32d49fed84966ca3df085bd7d19d4da74dee4e3ec4857fd84fee561353baee6d0de6a9aa12da4beea2a327b7459430a897c17e97
|
data/README.md
CHANGED
@@ -49,6 +49,49 @@ Include the Ruby code directly using `-I` as follows:
|
|
49
49
|
ruby -Ilib script.rb
|
50
50
|
```
|
51
51
|
|
52
|
+
### Install `yalla_id` column
|
53
|
+
|
54
|
+
Create an `AppUser` model in your application first. Then run the generator to
|
55
|
+
add the `yalla_id` column via a migration:
|
56
|
+
|
57
|
+
```shell
|
58
|
+
bin/rails generate yalla_auth_ruby_client:user
|
59
|
+
```
|
60
|
+
|
61
|
+
## Controller helpers
|
62
|
+
|
63
|
+
Include `YallaAuthRubyClient::ControllerAuthentication` in your controllers to
|
64
|
+
use a few helper methods:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class ApplicationController < ActionController::Base
|
68
|
+
include YallaAuthRubyClient::ControllerAuthentication
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
- `authenticate_user` validates the `auth_token` cookie and sets `current_user`.
|
73
|
+
- `authenticate_user!` redirects to `ENV["AUTH_URL"]/users/sign_in` with a
|
74
|
+
`redirect_uri` back to the current URL when authentication fails.
|
75
|
+
- `current_user` returns an instance of `AppUser` created using the `yalla_id`
|
76
|
+
provided by the authentication service.
|
77
|
+
- `logout` clears the cookie and redirects to `ENV["AUTH_URL"]`.
|
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
|
+
|
85
|
+
### Use the authentication middleware
|
86
|
+
|
87
|
+
Add `YallaAuthRubyClient::AuthTokenMiddleware` to your Rails middleware stack
|
88
|
+
to automatically validate a `token` parameter and store it in a signed cookie.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
# config/application.rb
|
92
|
+
config.middleware.use YallaAuthRubyClient::AuthTokenMiddleware
|
93
|
+
```
|
94
|
+
|
52
95
|
## Getting Started
|
53
96
|
|
54
97
|
Please follow the [installation](#installation) procedure and then run the following code:
|
data/RELEASE.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/active_record'
|
3
|
+
|
4
|
+
module YallaAuthRubyClient
|
5
|
+
module Generators
|
6
|
+
class UserGenerator < Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
source_root File.expand_path('templates', __dir__)
|
9
|
+
|
10
|
+
def self.next_migration_number(dirname)
|
11
|
+
if ActiveRecord.timestamped_migrations
|
12
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
13
|
+
else
|
14
|
+
sprintf("%03d", current_migration_number(dirname) + 1)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_migration_file
|
19
|
+
unless File.exist?(File.join(destination_root, 'app/models/app_user.rb'))
|
20
|
+
say_status :error, 'AppUser model not found. Please create app/models/app_user.rb before running this generator.', :red
|
21
|
+
return
|
22
|
+
end
|
23
|
+
|
24
|
+
migration_template 'add_yalla_id_to_app_users.rb', 'db/migrate/add_yalla_id_to_app_users.rb'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -158,7 +158,7 @@ module OpenapiClient
|
|
158
158
|
|
159
159
|
def initialize
|
160
160
|
@scheme = ENV['AUTH_URL'].split("://").first
|
161
|
-
@host = ENV['AUTH_URL'].split("://").last
|
161
|
+
@host = ENV['AUTH_URL'].split("://").last.gsub("/", "")
|
162
162
|
@base_path = ''
|
163
163
|
@server_index = nil
|
164
164
|
@server_operation_index = {}
|
@@ -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
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module YallaAuthRubyClient
|
2
|
+
module ControllerAuthentication
|
3
|
+
def logout
|
4
|
+
cookies.delete(:auth_token, httponly: true)
|
5
|
+
redirect_to ENV["AUTH_URL"], allow_other_host: true
|
6
|
+
end
|
7
|
+
|
8
|
+
def authenticate_user
|
9
|
+
token = cookies.signed[:auth_token]
|
10
|
+
return false unless token.present?
|
11
|
+
|
12
|
+
begin
|
13
|
+
api_client = OpenapiClient::AuthApi.new
|
14
|
+
response = api_client.auth_validate_token_get(token)
|
15
|
+
|
16
|
+
if response && response.success
|
17
|
+
@yalla_user = response.user
|
18
|
+
@current_user = find_or_create_app_user(@yalla_user)
|
19
|
+
true
|
20
|
+
else
|
21
|
+
cookies.delete(:auth_token)
|
22
|
+
false
|
23
|
+
end
|
24
|
+
rescue OpenapiClient::ApiError => e
|
25
|
+
Rails.logger.error "Authentication failed: #{e.message}"
|
26
|
+
cookies.delete(:auth_token)
|
27
|
+
false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def authenticate_user!
|
32
|
+
return if authenticate_user
|
33
|
+
|
34
|
+
redirect_to "#{ENV['AUTH_URL']}/users/sign_in?redirect_uri=#{request.original_url}", allow_other_host: true
|
35
|
+
end
|
36
|
+
|
37
|
+
def current_user
|
38
|
+
@current_user
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def find_or_create_app_user(user)
|
44
|
+
AppUser.find_or_create_by(yalla_id: user.id)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "openapi_client"
|
2
|
+
require "action_dispatch/middleware/cookies"
|
3
|
+
|
4
|
+
module YallaAuthRubyClient
|
5
|
+
class AuthTokenMiddleware
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
request = Rack::Request.new(env)
|
12
|
+
|
13
|
+
if (token = request.params["token"])
|
14
|
+
api_client = OpenapiClient::AuthApi.new
|
15
|
+
begin
|
16
|
+
response = api_client.auth_validate_token_get(token)
|
17
|
+
|
18
|
+
if response && response.success
|
19
|
+
request_env = ActionDispatch::Request.new(env)
|
20
|
+
request_env.cookie_jar.signed[:auth_token] = { value: token, httponly: true }
|
21
|
+
end
|
22
|
+
rescue OpenapiClient::ApiError => e
|
23
|
+
Rails.logger.error "Authentication failed: #{e.message}"
|
24
|
+
end
|
25
|
+
|
26
|
+
clean_url = request.fullpath.gsub(/[\?&]token=[^&]*/, "").sub(/\?$/, "")
|
27
|
+
return [302, { "Location" => clean_url, "Content-Type" => "text/html" }, ["Redirecting..."]] unless clean_url == request.fullpath
|
28
|
+
end
|
29
|
+
|
30
|
+
@app.call(env)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'openapi_client'
|
2
|
+
require 'rails/railtie'
|
3
|
+
require 'yalla_auth_ruby_client/controller_authentication'
|
4
|
+
require 'yalla_auth_ruby_client/api_controller_authentication'
|
5
|
+
require 'yalla_auth_ruby_client/middleware/auth_token_middleware'
|
6
|
+
|
7
|
+
module YallaAuthRubyClient
|
8
|
+
class Engine < ::Rails::Engine
|
9
|
+
initializer 'yalla_auth_ruby_client.controller_methods' do
|
10
|
+
ActiveSupport.on_load(:action_controller_base) do
|
11
|
+
include YallaAuthRubyClient::ControllerAuthentication
|
12
|
+
end
|
13
|
+
|
14
|
+
ActiveSupport.on_load(:action_controller_api) do
|
15
|
+
include YallaAuthRubyClient::ApiControllerAuthentication
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yalla_auth_ruby_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yalla auth openapi client
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: typhoeus
|
@@ -29,6 +29,20 @@ dependencies:
|
|
29
29
|
- - ">="
|
30
30
|
- !ruby/object:Gem::Version
|
31
31
|
version: 1.0.1
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: rails
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '5'
|
39
|
+
type: :runtime
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '5'
|
32
46
|
- !ruby/object:Gem::Dependency
|
33
47
|
name: rspec
|
34
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,6 +72,7 @@ extra_rdoc_files: []
|
|
58
72
|
files:
|
59
73
|
- Gemfile
|
60
74
|
- README.md
|
75
|
+
- RELEASE.md
|
61
76
|
- Rakefile
|
62
77
|
- docs/App.md
|
63
78
|
- docs/AppsApi.md
|
@@ -69,6 +84,8 @@ files:
|
|
69
84
|
- docs/UserRoleAdd.md
|
70
85
|
- docs/UsersApi.md
|
71
86
|
- git_push.sh
|
87
|
+
- lib/generators/yalla_auth_ruby_client/templates/add_yalla_id_to_app_users.rb
|
88
|
+
- lib/generators/yalla_auth_ruby_client/user_generator.rb
|
72
89
|
- lib/openapi_client.rb
|
73
90
|
- lib/openapi_client/api/apps_api.rb
|
74
91
|
- lib/openapi_client/api/auth_api.rb
|
@@ -83,6 +100,10 @@ files:
|
|
83
100
|
- lib/openapi_client/models/user_app_add.rb
|
84
101
|
- lib/openapi_client/models/user_role_add.rb
|
85
102
|
- lib/openapi_client/version.rb
|
103
|
+
- lib/yalla_auth_ruby_client.rb
|
104
|
+
- lib/yalla_auth_ruby_client/api_controller_authentication.rb
|
105
|
+
- lib/yalla_auth_ruby_client/controller_authentication.rb
|
106
|
+
- lib/yalla_auth_ruby_client/middleware/auth_token_middleware.rb
|
86
107
|
- spec/api/apps_api_spec.rb
|
87
108
|
- spec/api/auth_api_spec.rb
|
88
109
|
- spec/api/users_api_spec.rb
|
@@ -112,17 +133,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
133
|
- !ruby/object:Gem::Version
|
113
134
|
version: '0'
|
114
135
|
requirements: []
|
115
|
-
rubygems_version: 3.6.
|
136
|
+
rubygems_version: 3.6.8
|
116
137
|
specification_version: 4
|
117
138
|
summary: API V1 Ruby Gem
|
118
139
|
test_files:
|
119
|
-
- spec/api/auth_api_spec.rb
|
120
140
|
- spec/api/apps_api_spec.rb
|
141
|
+
- spec/api/auth_api_spec.rb
|
121
142
|
- spec/api/users_api_spec.rb
|
143
|
+
- spec/models/app_spec.rb
|
122
144
|
- spec/models/auth_validate_token_get200_response_spec.rb
|
145
|
+
- spec/models/role_spec.rb
|
123
146
|
- spec/models/user_app_add_spec.rb
|
124
147
|
- spec/models/user_role_add_spec.rb
|
125
|
-
- spec/models/app_spec.rb
|
126
148
|
- spec/models/user_spec.rb
|
127
|
-
- spec/models/role_spec.rb
|
128
149
|
- spec/spec_helper.rb
|