api_user_auth 0.0.14 → 0.1.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/app/controllers/api_user_auth/auth_controller.rb +15 -0
- data/app/models/api_user_auth/auth_user.rb +50 -24
- data/app/models/api_user_auth/provider_token.rb +30 -0
- data/app/models/concerns/api_user_auth/auth_user_helper.rb +39 -0
- data/app/models/concerns/api_user_auth/providers_helper.rb +10 -0
- data/config/locales/api_user_auth.en.yml +6 -0
- data/config/locales/api_user_auth.ru.yml +6 -0
- data/config/routes.rb +1 -0
- data/db/migrate/20180726140712_create_api_user_auth_provider_tokens.rb +12 -0
- data/lib/api_user_auth/providers/facebook.rb +1 -0
- data/lib/api_user_auth/providers/google.rb +1 -1
- data/lib/api_user_auth/providers/instagram.rb +1 -0
- data/lib/api_user_auth/version.rb +1 -1
- data/spec/controllers/api_user_auth/auth_controller_spec.rb +2 -0
- data/spec/dummy/db/schema.rb +11 -1
- data/spec/dummy/log/development.log +82 -464
- data/spec/dummy/log/test.log +1524 -42878
- data/spec/factories/api_user_auth_provider_tokens.rb +5 -0
- data/spec/models/api_user_auth/auth_user_spec.rb +20 -8
- data/spec/models/api_user_auth/provider_token_spec.rb +41 -0
- metadata +51 -45
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/test.sqlite3 +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e03f4578b5a4c4526a3cf16ef4f5153f2b6ba53ac614f2152140188a75ed9511
|
4
|
+
data.tar.gz: fc2c4f7d22cfcf3966ae63b446a1f484c3ca3db6cd7e7f3743e00e8ce402f6c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7f7735446438b9825b2626bb747c450cab316438f748f5a8b63fc15b755110a6e8c72a00d86b742e2626b967c6867cfde622765e534ece03bf2a535874b157a
|
7
|
+
data.tar.gz: 3c32189bf9ecd93bfed90fa13e6eb278e77db613f58c6d7ded4c520a4d06db497790e172c5d66ce6b9cd5cba5a3a1edc14b9450ae3b938572350d682dd6a5321
|
@@ -50,6 +50,21 @@ module ApiUserAuth
|
|
50
50
|
render json: auth_user.to_json
|
51
51
|
end
|
52
52
|
|
53
|
+
def add_provider
|
54
|
+
if request.headers['Authorization'].blank?
|
55
|
+
raise Exceptions::Unauthorized,
|
56
|
+
'Header [Authorization] can not be blank!'
|
57
|
+
end
|
58
|
+
token = request.headers['Authorization'].sub(/Bearer\s*=?/, '')
|
59
|
+
auth_user = AuthUser.find_fy_token(token)
|
60
|
+
if auth_user.present?
|
61
|
+
auth_user.add_provider_login(params)
|
62
|
+
render json: {}, status: 200
|
63
|
+
else
|
64
|
+
render json: {}, status: 400
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
53
68
|
private
|
54
69
|
|
55
70
|
def base_params
|
@@ -1,27 +1,16 @@
|
|
1
1
|
module ApiUserAuth
|
2
2
|
# Base user auth model
|
3
3
|
class AuthUser < ApplicationRecord
|
4
|
+
include AuthUserHelper
|
5
|
+
|
4
6
|
after_create :send_welcome
|
5
7
|
|
8
|
+
has_many :provider_tokens,
|
9
|
+
class_name: 'ApiUserAuth::ProviderToken'
|
10
|
+
|
6
11
|
attr_accessor :is_new
|
7
12
|
|
8
|
-
def self.create_by_params(params)
|
9
|
-
if params[:email].blank?
|
10
|
-
raise Exceptions::WrongParams, 'Email can not be blank!'
|
11
|
-
end
|
12
|
-
if params[:password].blank?
|
13
|
-
raise Exceptions::WrongParams, 'Password can not be blank!'
|
14
|
-
end
|
15
|
-
auth_user = AuthUser.find_or_initialize_by(email: params[:email])
|
16
13
|
|
17
|
-
if auth_user.new_record?
|
18
|
-
auth_user.is_new = true
|
19
|
-
auth_user.update_password(params[:password])
|
20
|
-
else
|
21
|
-
raise Exceptions::WrongParams, 'User already exists !'
|
22
|
-
end
|
23
|
-
auth_user
|
24
|
-
end
|
25
14
|
|
26
15
|
def self.login_by_params(params)
|
27
16
|
if params[:email].blank?
|
@@ -104,14 +93,51 @@ module ApiUserAuth
|
|
104
93
|
'Wrong provider!'
|
105
94
|
end
|
106
95
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
96
|
+
provider_token = ProviderToken.find_by_data(provider_data)
|
97
|
+
|
98
|
+
if provider_token.blank?
|
99
|
+
auth_user = AuthUser.find_or_initialize_by(email: provider_data[:email])
|
100
|
+
auth_user.encrypted_password = params[:token]
|
101
|
+
auth_user.generate_token
|
102
|
+
auth_user.is_new = auth_user.new_record?
|
103
|
+
auth_user.user_provider_data = provider_data
|
104
|
+
auth_user.provider = params[:provider]
|
105
|
+
auth_user.save
|
106
|
+
auth_user
|
107
|
+
else
|
108
|
+
provider_token.auth_user
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def add_provider_login(params)
|
113
|
+
if params[:provider].blank?
|
114
|
+
raise Exceptions::WrongParams, 'Provider can not be blank!'
|
115
|
+
end
|
116
|
+
if params[:token].blank?
|
117
|
+
raise Exceptions::WrongParams, 'Token can not be blank!'
|
118
|
+
end
|
119
|
+
|
120
|
+
provider_data = case params[:provider]
|
121
|
+
when /facebook/i
|
122
|
+
Providers::Facebook.get_user(params[:token])
|
123
|
+
when /google/i
|
124
|
+
Providers::Google.get_user(params[:token])
|
125
|
+
when /instagram/i
|
126
|
+
Providers::Instagram.get_user(params[:token])
|
127
|
+
else
|
128
|
+
raise ::ApiUserAuth::Exceptions::ProviderError,
|
129
|
+
'Wrong provider!'
|
130
|
+
end
|
131
|
+
|
132
|
+
ProviderToken.create_by_data(provider_data, self)
|
133
|
+
|
134
|
+
# auth_user.encrypted_password = params[:token]
|
135
|
+
# auth_user.generate_token
|
136
|
+
# auth_user.is_new = auth_user.new_record?
|
137
|
+
# auth_user.user_provider_data = provider_data
|
138
|
+
# auth_user.provider = params[:provider]
|
139
|
+
# auth_user.save
|
140
|
+
# auth_user
|
115
141
|
end
|
116
142
|
|
117
143
|
def self.find_fy_token(token)
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ApiUserAuth
|
2
|
+
# Model for social provider registration
|
3
|
+
class ProviderToken < ApplicationRecord
|
4
|
+
belongs_to :auth_user,
|
5
|
+
class_name: 'ApiUserAuth::AuthUser'
|
6
|
+
|
7
|
+
enum provider: %I[facebook google instagram]
|
8
|
+
|
9
|
+
def self.find_by_data(data)
|
10
|
+
find_by(
|
11
|
+
provider: data[:provider],
|
12
|
+
user_id: data[:id]
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.create_by_data(data, auth_user)
|
17
|
+
auth_user ||= create_auth_user(data)
|
18
|
+
auth_user.provider_tokens.create(
|
19
|
+
provider: data[:provider],
|
20
|
+
user_id: data[:id],
|
21
|
+
user_data: data
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.create_auth_user(data)
|
26
|
+
email = data[:id] + '@' + data[:provider] + '.com'
|
27
|
+
AuthUser.create(email: email, password: SecureRandom.uuid)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module ApiUserAuth
|
2
|
+
# Auth user helper
|
3
|
+
module AuthUserHelper
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
# Class methods
|
7
|
+
module ClassMethods
|
8
|
+
def create_by_params(params)
|
9
|
+
email_exception if params[:email].blank?
|
10
|
+
password_exception if params[:password].blank?
|
11
|
+
|
12
|
+
auth_user = AuthUser.find_or_initialize_by(email: params[:email])
|
13
|
+
|
14
|
+
if auth_user.new_record?
|
15
|
+
auth_user.is_new = true
|
16
|
+
auth_user.update_password(params[:password])
|
17
|
+
else
|
18
|
+
user_exist_exception
|
19
|
+
end
|
20
|
+
auth_user
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def email_exception
|
26
|
+
raise Exceptions::WrongParams, I18n.t('api_user_auth.errors.email')
|
27
|
+
end
|
28
|
+
|
29
|
+
def password_exception
|
30
|
+
raise Exceptions::WrongParams, I18n.t('api_user_auth.errors.password')
|
31
|
+
end
|
32
|
+
|
33
|
+
def user_exist_exception
|
34
|
+
raise Exceptions::WrongParams, I18n.t('api_user_auth.errors.user_exist')
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/config/routes.rb
CHANGED
@@ -3,6 +3,7 @@ ApiUserAuth::Engine.routes.draw do
|
|
3
3
|
collection do
|
4
4
|
post 'login', action: :login
|
5
5
|
post 'provider', action: :provider
|
6
|
+
post 'add_provider', action: :add_provider
|
6
7
|
patch 'forgot_password', action: :forgot_password
|
7
8
|
patch 'password', action: :password
|
8
9
|
post 'login', action: :login
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class CreateApiUserAuthProviderTokens < ActiveRecord::Migration[5.2]
|
2
|
+
def change
|
3
|
+
create_table :api_user_auth_provider_tokens do |t|
|
4
|
+
t.references :auth_user, index: true
|
5
|
+
t.integer :provider
|
6
|
+
t.string :user_id, null: false
|
7
|
+
t.jsonb :user_data, default: {}
|
8
|
+
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -26,7 +26,7 @@ module ApiUserAuth
|
|
26
26
|
def user_data
|
27
27
|
{
|
28
28
|
id: @data[:id], name: @data[:displayName],
|
29
|
-
email: @data[:emails].first.try(:[], :value),
|
29
|
+
email: @data[:emails].first.try(:[], :value), provider: 'google',
|
30
30
|
img_url: (@data[:image] || {}).try(:[], :url),
|
31
31
|
info: {
|
32
32
|
birthday: @data[:birthday],
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
#
|
11
11
|
# It's strongly recommended that you check this file into your version control system.
|
12
12
|
|
13
|
-
ActiveRecord::Schema.define(version:
|
13
|
+
ActiveRecord::Schema.define(version: 2018_07_26_140712) do
|
14
14
|
|
15
15
|
# These are extensions that must be enabled in order to support this database
|
16
16
|
enable_extension "pgcrypto"
|
@@ -28,4 +28,14 @@ ActiveRecord::Schema.define(version: 2018_07_03_111608) do
|
|
28
28
|
t.index ["email"], name: "index_api_user_auth_auth_users_on_email", unique: true
|
29
29
|
end
|
30
30
|
|
31
|
+
create_table "api_user_auth_provider_tokens", force: :cascade do |t|
|
32
|
+
t.bigint "auth_user_id"
|
33
|
+
t.integer "provider"
|
34
|
+
t.string "user_id", null: false
|
35
|
+
t.jsonb "user_data", default: {}
|
36
|
+
t.datetime "created_at", null: false
|
37
|
+
t.datetime "updated_at", null: false
|
38
|
+
t.index ["auth_user_id"], name: "index_api_user_auth_provider_tokens_on_auth_user_id"
|
39
|
+
end
|
40
|
+
|
31
41
|
end
|