yalla_auth_ruby_client 1.0.0 → 2.0.0
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 +37 -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/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 +14 -0
- data/yalla_auth_ruby_client.gemspec +1 -0
- metadata +26 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f130db14d4ab21f5160424c47c583bb75d355bc871afb38448eae5eabe6dfba
|
4
|
+
data.tar.gz: ec774cbda5742b775b4aec193e8eceeee142cca7311feb47644923aa8f98b29a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c51ed7933e90841eb6ae1c41c538dcb782e7ed26cb8921961d5c3964acd92dbdabd02f1522ec8f23f97211c59550c60066b0ae148c419d4cd1c0fa1af88e4ba
|
7
|
+
data.tar.gz: d2f5f225d39421b540ea40420bf19c017eb9e91b43e493c95a4d76a07db6d234da4d10c2bbda4da95830e2a61e85403d5a5fa42d95e20b80e6d7578253769a4b
|
data/README.md
CHANGED
@@ -49,6 +49,43 @@ 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
|
+
### Use the authentication middleware
|
80
|
+
|
81
|
+
Add `YallaAuthRubyClient::AuthTokenMiddleware` to your Rails middleware stack
|
82
|
+
to automatically validate a `token` parameter and store it in a signed cookie.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
# config/application.rb
|
86
|
+
config.middleware.use YallaAuthRubyClient::AuthTokenMiddleware
|
87
|
+
```
|
88
|
+
|
52
89
|
## Getting Started
|
53
90
|
|
54
91
|
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,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,14 @@
|
|
1
|
+
require 'openapi_client'
|
2
|
+
require 'rails/railtie'
|
3
|
+
require 'yalla_auth_ruby_client/controller_authentication'
|
4
|
+
require 'yalla_auth_ruby_client/middleware/auth_token_middleware'
|
5
|
+
|
6
|
+
module YallaAuthRubyClient
|
7
|
+
class Engine < ::Rails::Engine
|
8
|
+
initializer 'yalla_auth_ruby_client.controller_methods' do
|
9
|
+
ActiveSupport.on_load(:action_controller_base) do
|
10
|
+
include YallaAuthRubyClient::ControllerAuthentication
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
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.0
|
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,9 @@ 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/controller_authentication.rb
|
105
|
+
- lib/yalla_auth_ruby_client/middleware/auth_token_middleware.rb
|
86
106
|
- spec/api/apps_api_spec.rb
|
87
107
|
- spec/api/auth_api_spec.rb
|
88
108
|
- spec/api/users_api_spec.rb
|
@@ -112,17 +132,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
132
|
- !ruby/object:Gem::Version
|
113
133
|
version: '0'
|
114
134
|
requirements: []
|
115
|
-
rubygems_version: 3.6.
|
135
|
+
rubygems_version: 3.6.8
|
116
136
|
specification_version: 4
|
117
137
|
summary: API V1 Ruby Gem
|
118
138
|
test_files:
|
119
|
-
- spec/api/auth_api_spec.rb
|
120
139
|
- spec/api/apps_api_spec.rb
|
140
|
+
- spec/api/auth_api_spec.rb
|
121
141
|
- spec/api/users_api_spec.rb
|
142
|
+
- spec/models/app_spec.rb
|
122
143
|
- spec/models/auth_validate_token_get200_response_spec.rb
|
144
|
+
- spec/models/role_spec.rb
|
123
145
|
- spec/models/user_app_add_spec.rb
|
124
146
|
- spec/models/user_role_add_spec.rb
|
125
|
-
- spec/models/app_spec.rb
|
126
147
|
- spec/models/user_spec.rb
|
127
|
-
- spec/models/role_spec.rb
|
128
148
|
- spec/spec_helper.rb
|