ibrain-auth 0.1.8 → 0.2.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: 15dfe6736f21d1568b1c1f3b53403c996c6cc8682eddbf16fca744add91c18a0
4
- data.tar.gz: 97e95785738830c2517ac34431a85d6dd8d70ca706fcdb8b4dc38e1ebedde3b3
3
+ metadata.gz: d25f3bfd1d29801c8b5673f6103149f6202d21d522a1b65175ebf9f59334fb58
4
+ data.tar.gz: 0a8f9c8c3cd38bcb6a72a329f421e5c291f0d7db7cf020b253928c9427ca9c81
5
5
  SHA512:
6
- metadata.gz: ee792f65779bdf83b8a681d41295d2df04c5618195c57a0b9410373769504cb407f8d6c7a3c1452fbe95da729b0956659746ec42c876cab886f6769335a0cc4f
7
- data.tar.gz: 5888a9f6b84227b8189f4157569128ea56a6d5e209f88c123849f3516024cdb6a0e3777a565b7262020290252b4778eb9a5a9e76a340b9ff84689c578e5e9780
6
+ metadata.gz: 7dac3e301b88e1e368e2f346e79fb10a74f15fd36170172a2324f14181c981a67faa6205363271dcd5146049d63b1d8eef36e640523cfb7ee397e8c2f88c21a9
7
+ data.tar.gz: 40a6744264df1133f5ce8efdd0f16f39938f9c22aa7a28e6cbed35ed6a713d9d55620a416fadbd41a4dbc287a0776ca60b28fb607f1583c67584868723eff5f3
@@ -6,8 +6,8 @@ module Ibrain::Auth::Mutations
6
6
  field :token, String, null: true
7
7
  field :result, Boolean, null: true
8
8
 
9
- argument :username, String, description: 'Username', required: true
10
- argument :password, String, description: 'Password', required: true
9
+ argument :auth, Ibrain::Auth::Config.sign_in_input, required: true
10
+ argument :device_token, String, description: 'Device token for notification', required: false
11
11
 
12
12
  def resolve(args)
13
13
  # TODO: define logic inside repository
@@ -25,6 +25,12 @@ module Ibrain::Auth::Mutations
25
25
  current_user.jti = jti
26
26
  current_user.save!
27
27
 
28
+ if args[:device_token].present?
29
+ device_token = current_user.device_tokens.find_by(token: args[:device_token])
30
+
31
+ current_user.device_tokens.create!({ token: args[:device_token] }) if device_token.blank?
32
+ end
33
+
28
34
  context[:current_user] = current_user
29
35
 
30
36
  OpenStruct.new(
@@ -8,6 +8,8 @@ module Ibrain::Auth::Mutations
8
8
  current_user.jti = nil
9
9
  sign_out if current_user.save
10
10
 
11
+ current_user.device_tokens.delete_all unless user_signed_in?
12
+
11
13
  OpenStruct.new(result: !user_signed_in?)
12
14
  end
13
15
  end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain::Auth::Mutations
4
+ class SignUpMutation < BaseMutation
5
+ field :user, Types::Objects::UserType, null: true
6
+ field :token, String, null: true
7
+ field :result, Boolean, null: true
8
+
9
+ argument :user, Ibrain::Auth::Config.sign_up_input, required: true
10
+ argument :device_token, String, description: 'Device token for notificaiton', required: false
11
+
12
+ def resolve(args)
13
+ # TODO: define logic inside repository
14
+ repo = ::AuthRepository.new(nil, normalize_params(args))
15
+ user = repo.sign_up
16
+
17
+ return OpenStruct.new({ user: nil, token: nil, result: false, is_verified: false }) if user.blank?
18
+
19
+ sign_in(resource_name, user)
20
+ @current_user = warden.authenticate!(auth_options)
21
+
22
+ warden.set_user(current_user)
23
+ current_user.jwt_token, jti = auth_headers(request, user)
24
+
25
+ current_user.jti = jti
26
+ current_user.save!
27
+
28
+ if args[:device_token].present?
29
+ device_token = current_user.device_tokens.find_by(token: args[:device_token])
30
+
31
+ current_user.device_tokens.create!({ token: args[:device_token] }) if device_token.blank?
32
+ end
33
+
34
+ context[:current_user] = current_user
35
+
36
+ OpenStruct.new(
37
+ user: user_signed_in? ? current_user : nil,
38
+ token: current_user.try(:jwt_token),
39
+ result: user_signed_in?,
40
+ is_verified: true
41
+ )
42
+ end
43
+
44
+ private
45
+
46
+ def normalize_params(args)
47
+ ActionController::Parameters.new({ auth: args })
48
+ end
49
+
50
+ def auth_options
51
+ { scope: resource_name }
52
+ end
53
+ end
54
+ end
@@ -5,15 +5,17 @@ module Ibrain::Auth::Mutations
5
5
  field :user, Types::Objects::UserType, null: true
6
6
  field :token, String, null: true
7
7
  field :result, Boolean, null: true
8
+ field :is_verified, Boolean, null: true
8
9
 
9
10
  argument :id_token, String, description: 'Id Token from SSO', required: true
11
+ argument :device_token, String, description: 'Device token for notificaiton', required: false
10
12
 
11
13
  def resolve(args)
12
14
  # TODO: define logic inside repository
13
15
  repo = ::AuthRepository.new(nil, normalize_params(args))
14
16
  user = repo.sign_in
15
17
 
16
- raise ActionController::InvalidAuthenticityToken, I18n.t('ibrain.errors.account.incorrect') if user.blank?
18
+ return OpenStruct.new({ user: nil, token: nil, result: false, is_verified: false }) if user.blank?
17
19
 
18
20
  sign_in(resource_name, user)
19
21
  @current_user = warden.authenticate!(auth_options)
@@ -24,12 +26,19 @@ module Ibrain::Auth::Mutations
24
26
  current_user.jti = jti
25
27
  current_user.save!
26
28
 
29
+ if args[:device_token].present?
30
+ device_token = current_user.device_tokens.find_by(token: args[:device_token])
31
+
32
+ current_user.device_tokens.create!({ token: args[:device_token] }) if device_token.blank?
33
+ end
34
+
27
35
  context[:current_user] = current_user
28
36
 
29
37
  OpenStruct.new(
30
38
  user: user_signed_in? ? current_user : nil,
31
39
  token: current_user.try(:jwt_token),
32
- result: user_signed_in?
40
+ result: user_signed_in?,
41
+ is_verified: true
33
42
  )
34
43
  end
35
44
 
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain::Auth::Mutations
4
+ class SsoSignUpMutation < BaseMutation
5
+ field :user, Types::Objects::UserType, null: true
6
+ field :token, String, null: true
7
+ field :result, Boolean, null: true
8
+
9
+ argument :id_token, String, description: 'Id Token from SSO', required: true
10
+ argument :user, Ibrain::Auth::Config.sign_up_input, required: true
11
+ argument :device_token, String, description: 'Device token for notificaiton', required: false
12
+
13
+ def resolve(args)
14
+ # TODO: define logic inside repository
15
+ repo = ::AuthRepository.new(nil, normalize_params(args))
16
+ user = repo.sign_up
17
+
18
+ return OpenStruct.new({ user: nil, token: nil, result: false, is_verified: false }) if user.blank?
19
+
20
+ sign_in(resource_name, user)
21
+ @current_user = warden.authenticate!(auth_options)
22
+
23
+ warden.set_user(current_user)
24
+ current_user.jwt_token, jti = auth_headers(request, user)
25
+
26
+ current_user.jti = jti
27
+ current_user.save!
28
+
29
+ if args[:device_token].present?
30
+ device_token = current_user.device_tokens.find_by(token: args[:device_token])
31
+
32
+ current_user.device_tokens.create!({ token: args[:device_token] }) if device_token.blank?
33
+ end
34
+
35
+ context[:current_user] = current_user
36
+
37
+ OpenStruct.new(
38
+ user: user_signed_in? ? current_user : nil,
39
+ token: current_user.try(:jwt_token),
40
+ result: user_signed_in?,
41
+ is_verified: true
42
+ )
43
+ end
44
+
45
+ private
46
+
47
+ def normalize_params(args)
48
+ ActionController::Parameters.new({ auth: args })
49
+ end
50
+
51
+ def auth_options
52
+ { scope: resource_name }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Auth
5
+ module Types
6
+ module Input
7
+ class SignInInput < Ibrain::Types::BaseInputObject
8
+ argument :username, String, required: true
9
+ argument :password, String, required: true
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Auth
5
+ module Types
6
+ module Input
7
+ class SignUpInput < Ibrain::Types::BaseInputObject
8
+ argument :first_name, String, required: false
9
+ argument :last_name, String, required: false
10
+ argument :email, String, required: false
11
+ argument :phone, String, required: false
12
+ argument :job_id, ID, required: false
13
+ argument :address, String, required: false
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -17,7 +17,7 @@ module Ibrain
17
17
  # for hasura
18
18
  hasura_keys = {
19
19
  'https://hasura.io/jwt/claims': {
20
- 'x-hasura-allowed-roles': User.roles.keys,
20
+ 'x-hasura-allowed-roles': Ibrain.user_class.roles.keys,
21
21
  'x-hasura-default-role': role,
22
22
  'x-hasura-user-id': id.to_s
23
23
  }
@@ -12,15 +12,16 @@ class AuthRepository < Ibrain::BaseRepository
12
12
 
13
13
  def create
14
14
  user = is_sso? ? sso_verify : collection.ibrain_find(manual_params, available_columns)
15
- user.assign_attributes(normalize_params)
15
+ user.assign_attributes(normalize_params.except(:id_token))
16
16
  user.save
17
17
 
18
18
  user
19
19
  end
20
20
 
21
21
  def sign_in
22
- user = is_sso? ? sso_verify : collection.ibrain_find(manual_params, available_columns)
22
+ return sso_verify if is_sso?
23
23
 
24
+ user = collection.ibrain_find(manual_params, available_columns)
24
25
  return unless user.try(:valid_password?, manual_params[:password])
25
26
 
26
27
  user
@@ -49,7 +50,7 @@ class AuthRepository < Ibrain::BaseRepository
49
50
  end
50
51
 
51
52
  def normalize_params
52
- params.require(:auth).permit(:id_token)
53
+ params.require(:auth).permit(permitted_attributes)
53
54
  end
54
55
 
55
56
  def manual_params
@@ -57,10 +58,11 @@ class AuthRepository < Ibrain::BaseRepository
57
58
  end
58
59
 
59
60
  def sso_verify
60
- response = HTTParty.post(url, headers: base_headers, body: { 'idToken' => normalize_params[:id_token] }.to_json )
61
- uid = response.try(:fetch, 'users', []).try(:at, 0).try(:fetch, 'localId', nil)
61
+ response = HTTParty.post(firebase_url, headers: base_headers, body: { 'idToken' => normalize_params[:id_token] }.to_json )
62
+ user_information = response.try(:fetch, 'users', []).try(:at, 0)
62
63
 
63
- raise ActiveRecord::NotFound, I18n.t('ibrain.errors.account.not_found') if uid.blank?
64
+ uid = user_information.try(:fetch, 'localId', nil)
65
+ raise ActiveRecord::RecordNotFound, I18n.t('ibrain.errors.account.not_found') if uid.blank?
64
66
 
65
67
  collection.find_by(uid: uid)
66
68
  end
@@ -72,4 +74,17 @@ class AuthRepository < Ibrain::BaseRepository
72
74
  def is_sso?
73
75
  normalize_params[:id_token].present?
74
76
  end
77
+
78
+ def permitted_attributes
79
+ Ibrain.user_class.permitted_attributes.reject { |k| permintted_columns.include?(k) }.map(&:to_sym).concat([:id_token])
80
+ end
81
+
82
+ def permintted_columns
83
+ %w[
84
+ reset_password_token reset_password_sent_at
85
+ remember_created_at sign_in_count uid jti
86
+ current_sign_in_at last_sign_in_at current_sign_in_ip
87
+ last_sign_in_ip role encrypted_password
88
+ ]
89
+ end
75
90
  end
@@ -10,4 +10,10 @@ Ibrain::Auth.config do |config|
10
10
 
11
11
  # Set user table name for rails ORM
12
12
  config.user_table_name = 'ibrain_users'
13
+
14
+ # sign_up graphql input
15
+ config.sign_up_input = Ibrain::Auth::Types::Input::SignUpInput
16
+
17
+ # sign_in graphql input
18
+ config.sign_up_input = Ibrain::Auth::Types::Input::SignInInput
13
19
  end
@@ -4,14 +4,14 @@ module Ibrain
4
4
  # frozen_string_literal: true
5
5
 
6
6
  module Auth
7
- VERSION = '0.1.8'
7
+ VERSION = '0.2.1'
8
8
 
9
9
  def self.ibrain_auth_version
10
10
  VERSION
11
11
  end
12
12
 
13
13
  def self.previous_ibrain_auth_minor_version
14
- '0.1.7'
14
+ '0.1.0'
15
15
  end
16
16
 
17
17
  def self.ibrain_auth_gem_version
@@ -11,5 +11,11 @@ module Ibrain
11
11
 
12
12
  # JWT user table name
13
13
  preference :user_table_name, :string, default: 'ibrain_users'
14
+
15
+ # sign_up input
16
+ preference :sign_up_input, :class, default: Ibrain::Auth::Types::Input::SignUpInput
17
+
18
+ # sign_in input
19
+ preference :sign_in_input, :class, default: Ibrain::Auth::Types::Input::SignInInput
14
20
  end
15
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibrain-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tai Nguyen Van
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-14 00:00:00.000000000 Z
11
+ date: 2022-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: devise
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.2.4
75
+ version: 0.3.0
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.2.4
82
+ version: 0.3.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rails
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -132,7 +132,11 @@ files:
132
132
  - app/graphql/ibrain/auth/mutations/base_mutation.rb
133
133
  - app/graphql/ibrain/auth/mutations/sign_in_mutation.rb
134
134
  - app/graphql/ibrain/auth/mutations/sign_out_mutation.rb
135
+ - app/graphql/ibrain/auth/mutations/sign_up_mutation.rb
135
136
  - app/graphql/ibrain/auth/mutations/sso_sign_in_mutation.rb
137
+ - app/graphql/ibrain/auth/mutations/sso_sign_up_mutation.rb
138
+ - app/graphql/ibrain/auth/types/input/sign_in_input.rb
139
+ - app/graphql/ibrain/auth/types/input/sign_up_input.rb
136
140
  - app/models/ibrain/auth/user.rb
137
141
  - app/repositories/auth_repository.rb
138
142
  - config/initializers/devise.rb