morpho 0.3.3 → 0.3.4
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/api/morpho/entities/sign_in/external.rb +12 -0
- data/app/api/morpho/helpers/jwt_utils.rb +42 -0
- data/app/api/morpho/helpers/user_external_login.rb +53 -0
- data/app/api/morpho/helpers/user_login.rb +1 -35
- data/app/api/morpho/resources/externals.rb +21 -0
- data/app/api/morpho/resources/tokens.rb +1 -0
- data/app/models/morpho/user.rb +9 -0
- data/config/locales/morpho.en.yml +2 -0
- data/config/locales/morpho.es.yml +2 -0
- data/lib/morpho/version.rb +1 -1
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 897ee2fc194496e055864aec5475926004ab7a5665b00c0a4979f4fb686b4c48
|
4
|
+
data.tar.gz: 0e5a7348610783970b1243db39ed40405a47eab4a339f4d88aba4fdaa70e1a0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4debc02baf4abf21d34c2ed9c83f9e8ce12e364e46dccf6a8529a74963ec7765af47e49265585af2cbdd7d7b47dd65162ffe06ec0acfa9ecb918f6da2c82fe8
|
7
|
+
data.tar.gz: 571c6a523befd73e153e09ec8d91addb35f8d95654c64e797744702c0066b95bb71a152994c792f9ce60a0adfc1ee5d6bffcca8dcc69af1afb3b8e5fbffa85bb
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Morpho
|
2
|
+
module Entities
|
3
|
+
module SignIn
|
4
|
+
class External < ::Morpho::Entities::Base
|
5
|
+
expose :email, documentation: { type: 'string', desc: 'User email address' }
|
6
|
+
expose :name, documentation: { type: 'string', desc: 'User name' }
|
7
|
+
expose :provider, documentation: { type: 'string', desc: 'User authentication provider' }
|
8
|
+
expose :uid, documentation: { type: 'string', desc: 'User provider uid' }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Morpho
|
2
|
+
module Helpers
|
3
|
+
module JWTUtils
|
4
|
+
protected
|
5
|
+
|
6
|
+
def current_user
|
7
|
+
@current_user ||= Morpho::User.find_by(email: jwt_token[:email])
|
8
|
+
rescue
|
9
|
+
nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def http_token
|
13
|
+
@http_token ||= if request.headers[Morpho.config.jwt.header].present?
|
14
|
+
request.headers[Morpho.config.jwt.header].split(' ').last
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def jwt_token
|
19
|
+
@jwt_token ||= HashWithIndifferentAccess.new(jwt_decode(http_token).first)
|
20
|
+
end
|
21
|
+
|
22
|
+
def jwt_encode(payload)
|
23
|
+
Morpho::Cipher.jwt_encode(payload)
|
24
|
+
end
|
25
|
+
|
26
|
+
def jwt_decode(token)
|
27
|
+
begin
|
28
|
+
return Morpho::Cipher.jwt_decode(token)
|
29
|
+
rescue
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def user_payload(user)
|
35
|
+
expires_at = Time.now.to_i + Morpho.config.jwt.expiration_time
|
36
|
+
issued_at = Time.now.to_i
|
37
|
+
|
38
|
+
{ authentication_token: jwt_encode({ exp: expires_at, iat: issued_at, email: user.email }), expires_at: expires_at, refresh_token: user.refresh_token }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Morpho
|
2
|
+
module Helpers
|
3
|
+
module UserExternalLogin
|
4
|
+
protected
|
5
|
+
|
6
|
+
def login(user_params)
|
7
|
+
provider = user_params[:provider].downcase
|
8
|
+
|
9
|
+
if Morpho::Authentication.exists?(provider: provider, uid: user_params[:uid])
|
10
|
+
authentication = Morpho::Authentication.find_by(provider: provider, uid: user_params[:uid])
|
11
|
+
|
12
|
+
user = authentication.user
|
13
|
+
|
14
|
+
user.activate! unless user.active?
|
15
|
+
user.register_last_login_activity!(request.ip)
|
16
|
+
user.generate_refresh_token!
|
17
|
+
token = user_payload(user)
|
18
|
+
|
19
|
+
present token, with: Morpho::Entities::SignIn::AuthenticationToken
|
20
|
+
elsif Morpho::User.exists?(email: user_params[:email])
|
21
|
+
user = Morpho::User.find_by(email: user_params[:email])
|
22
|
+
|
23
|
+
if user.add_provider_to_user(provider, user_params[:uid])
|
24
|
+
user.activate! unless user.active?
|
25
|
+
user.register_last_login_activity!(request.ip)
|
26
|
+
user.generate_refresh_token!
|
27
|
+
token = user_payload(user)
|
28
|
+
|
29
|
+
present token, with: Morpho::Entities::SignIn::AuthenticationToken
|
30
|
+
else
|
31
|
+
render_unprocessable_entity_detailed([I18n.t('morpho.api.messages.provider.unregistered')])
|
32
|
+
end
|
33
|
+
else
|
34
|
+
user = Morpho::User.create_from_provider(provider, user_params[:uid], {
|
35
|
+
email: user_params[:email],
|
36
|
+
name: user_params[:name]
|
37
|
+
})
|
38
|
+
|
39
|
+
if user.persisted?
|
40
|
+
user.activate!
|
41
|
+
user.register_last_login_activity!(request.ip)
|
42
|
+
user.generate_refresh_token!
|
43
|
+
token = user_payload(user)
|
44
|
+
|
45
|
+
present token, with: Morpho::Entities::SignIn::AuthenticationToken
|
46
|
+
else
|
47
|
+
render_unprocessable_entity_detailed([I18n.t('morpho.api.messages.provider.unregistered')])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -18,6 +18,7 @@ module Morpho
|
|
18
18
|
if user.active?
|
19
19
|
if !user.login_locked?
|
20
20
|
if user.valid_password?(user_params[:password])
|
21
|
+
user.register_last_login_activity!(request.ip)
|
21
22
|
user.generate_refresh_token!
|
22
23
|
token = user_payload(user)
|
23
24
|
|
@@ -36,41 +37,6 @@ module Morpho
|
|
36
37
|
render_unauthorized_detailed([I18n.t('morpho.api.messages.unauthorized_detailed.unexistent')])
|
37
38
|
end
|
38
39
|
end
|
39
|
-
|
40
|
-
def current_user
|
41
|
-
@current_user ||= Morpho::User.find_by(email: jwt_token[:email])
|
42
|
-
rescue
|
43
|
-
nil
|
44
|
-
end
|
45
|
-
|
46
|
-
def http_token
|
47
|
-
@http_token ||= if request.headers[Morpho.config.jwt.header].present?
|
48
|
-
request.headers[Morpho.config.jwt.header].split(' ').last
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def jwt_token
|
53
|
-
@jwt_token ||= HashWithIndifferentAccess.new(jwt_decode(http_token).first)
|
54
|
-
end
|
55
|
-
|
56
|
-
def jwt_encode(payload)
|
57
|
-
Morpho::Cipher.jwt_encode(payload)
|
58
|
-
end
|
59
|
-
|
60
|
-
def jwt_decode(token)
|
61
|
-
begin
|
62
|
-
return Morpho::Cipher.jwt_decode(token)
|
63
|
-
rescue
|
64
|
-
nil
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def user_payload(user)
|
69
|
-
expires_at = Time.now.to_i + Morpho.config.jwt.expiration_time
|
70
|
-
issued_at = Time.now.to_i
|
71
|
-
|
72
|
-
{ authentication_token: jwt_encode({ exp: expires_at, iat: issued_at, email: user.email }), expires_at: expires_at, refresh_token: user.refresh_token }
|
73
|
-
end
|
74
40
|
end
|
75
41
|
end
|
76
42
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Morpho
|
2
|
+
module Resources
|
3
|
+
class Externals < ::Grape::API
|
4
|
+
helpers Morpho::Helpers::HTTPResponses,
|
5
|
+
Morpho::Helpers::JWTUtils,
|
6
|
+
Morpho::Helpers::UserExternalLogin
|
7
|
+
|
8
|
+
namespace :externals do
|
9
|
+
desc 'Request user authentication from external provider' do
|
10
|
+
success Morpho::Entities::User
|
11
|
+
end
|
12
|
+
params do
|
13
|
+
requires :data, type: Morpho::Entities::SignIn::External
|
14
|
+
end
|
15
|
+
post do
|
16
|
+
login(params[:data])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/app/models/morpho/user.rb
CHANGED
@@ -12,6 +12,15 @@ module Morpho
|
|
12
12
|
self.activation_state == 'active'
|
13
13
|
end
|
14
14
|
|
15
|
+
def register_last_login_activity!(ip_address)
|
16
|
+
self.set_last_login_at(Time.now)
|
17
|
+
self.set_last_ip_address(ip_address)
|
18
|
+
end
|
19
|
+
|
20
|
+
def register_last_activity_time!
|
21
|
+
self.set_last_activity_at(Time.now)
|
22
|
+
end
|
23
|
+
|
15
24
|
def resend_activation_needed_email!
|
16
25
|
self.setup_activation
|
17
26
|
self.reload
|
data/lib/morpho/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: morpho
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hugo Gilmar Erazo
|
@@ -248,18 +248,22 @@ files:
|
|
248
248
|
- app/api/morpho/entities/error.rb
|
249
249
|
- app/api/morpho/entities/sign_in/authentication_token.rb
|
250
250
|
- app/api/morpho/entities/sign_in/credentials.rb
|
251
|
+
- app/api/morpho/entities/sign_in/external.rb
|
251
252
|
- app/api/morpho/entities/sign_in/refresh_token.rb
|
252
253
|
- app/api/morpho/entities/sign_in/success.rb
|
253
254
|
- app/api/morpho/entities/user.rb
|
254
255
|
- app/api/morpho/entities/user_sign_up.rb
|
255
256
|
- app/api/morpho/helpers/http_responses.rb
|
257
|
+
- app/api/morpho/helpers/jwt_utils.rb
|
256
258
|
- app/api/morpho/helpers/user_activation.rb
|
259
|
+
- app/api/morpho/helpers/user_external_login.rb
|
257
260
|
- app/api/morpho/helpers/user_login.rb
|
258
261
|
- app/api/morpho/helpers/user_password_reset.rb
|
259
262
|
- app/api/morpho/helpers/user_refresh_authentication_token.rb
|
260
263
|
- app/api/morpho/helpers/user_registration.rb
|
261
264
|
- app/api/morpho/helpers/user_unlock.rb
|
262
265
|
- app/api/morpho/resources/activations.rb
|
266
|
+
- app/api/morpho/resources/externals.rb
|
263
267
|
- app/api/morpho/resources/passwords.rb
|
264
268
|
- app/api/morpho/resources/tokens.rb
|
265
269
|
- app/api/morpho/resources/unlocks.rb
|