ibrain-auth 0.1.9 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b222c67c324dc96e4dce92bc70808174bd2cd6367de3429f0029a845f24775c4
4
- data.tar.gz: 135f08c796212da1f9db7271e4f3b37ca7ac059b07b38809d0590da62765c83d
3
+ metadata.gz: c55531d53a9d3ae38c32c05a522325c7998788d2c1afa99be87fd152fb87c500
4
+ data.tar.gz: 52bc3f40ffab1748a6597219b6f7c752e6a984c427c6f969376876e600e55f7b
5
5
  SHA512:
6
- metadata.gz: 03355febdc2385a78c60600556c3dd0790b8c1043f367026e017154d7d3be6eca0d81b54ec5ef037d276a275028f76eada7ab156c0ff6f373f289accb346097f
7
- data.tar.gz: 89c527e08ef0483f4aa93d477ffbc73e5f29b88c7fd19cd08a87e50a5eb4b8456c834552c9936b0b65328e30508c157acfe0ed782f866665198a8fa82ae514ce
6
+ metadata.gz: 79df30aa4fcffcb37dd98e2f61e38b0ee638d902d8fab4a4024d56491a8011e7314b4bc0b62c8ff602313cfd8c8d1d50856a86f780f4b5b88ced20917880f586
7
+ data.tar.gz: 18aa083ae454684f47c6ce420bc58a34e9c028f1177a699ee277c2a28a12d83281ad01221d7e8aa6da44cc29aff8bb0b05c671d3164a8292f11c92229bdee560
@@ -8,6 +8,7 @@ module Ibrain::Auth::Mutations
8
8
 
9
9
  argument :username, String, description: 'Username', required: true
10
10
  argument :password, String, description: 'Password', required: true
11
+ argument :device_token, String, description: 'Device token for notification', required: false
11
12
 
12
13
  def resolve(args)
13
14
  # TODO: define logic inside repository
@@ -25,6 +26,12 @@ module Ibrain::Auth::Mutations
25
26
  current_user.jti = jti
26
27
  current_user.save!
27
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
+
28
35
  context[:current_user] = current_user
29
36
 
30
37
  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,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Auth
5
+ module Types
6
+ module Attributes
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
@@ -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,7 @@ 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::Attributes::SignUpInput
13
16
  end
@@ -4,14 +4,14 @@ module Ibrain
4
4
  # frozen_string_literal: true
5
5
 
6
6
  module Auth
7
- VERSION = '0.1.9'
7
+ VERSION = '0.2.0'
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.8'
14
+ '0.1.9'
15
15
  end
16
16
 
17
17
  def self.ibrain_auth_gem_version
@@ -11,5 +11,8 @@ 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::Attributes::SignUpInput
14
17
  end
15
18
  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.9
4
+ version: 0.2.0
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-02-25 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,10 @@ 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/attributes/sign_up_input.rb
136
139
  - app/models/ibrain/auth/user.rb
137
140
  - app/repositories/auth_repository.rb
138
141
  - config/initializers/devise.rb
@@ -175,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
178
  - !ruby/object:Gem::Version
176
179
  version: '0'
177
180
  requirements: []
178
- rubygems_version: 3.2.22
181
+ rubygems_version: 3.0.9
179
182
  signing_key:
180
183
  specification_version: 4
181
184
  summary: Its Auth is an sso authen gem for Ruby on Rails.