ibrain-auth 0.2.5 → 0.2.7

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: 26aeb022ddcbbdd76111478f2940e701d19dd097f2cec23766d3c5ebb12fad17
4
- data.tar.gz: fc936d12ad9d6cc5b6346c18e3e0a523d10b281b02506223bb50ed6c3fffdd25
3
+ metadata.gz: 9deb56fb20025aafc070789b5bfa07422ed6d101c176d02650222e9a79b32736
4
+ data.tar.gz: 466ef586478d5c5643982d369a85da485601dcaec871b32a7bf7423e089d141b
5
5
  SHA512:
6
- metadata.gz: 3ac0bfb83423c1b39cd2a4cb0da02cd340a856fe80252fb231b5e1f3fe20b973c3372af3f03406106d73c02dad7fcd572bbd7378df42af951a942c98ac432d9d
7
- data.tar.gz: c5f6150f42313cd3ea40a6fe31fa2fabb11c9248a3c9c2dd58153fad1296590630612f89c175167b01dd04a9559afdadeef808b453f941e56dea00859988e32a
6
+ metadata.gz: 3d92287766c4a26ee54357eb4f966ac4a51a0417228a2f8ba47f779eff4b316e2821f637863be06c18460a5ce2150fe062150653dfc9c3f3a66a567b98a623f0
7
+ data.tar.gz: 5a20e97b2e6a721f592c8cb4b0a27624c430a4a18e1bb80406328d42efc6ef1e9d787af93564767e17e728992efe8b0cc361840efae12d5353d09058ff6bd6f8
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain::Auth::Mutations
4
+ class GenerateFirebaseTokenMutation < BaseMutation
5
+ field :result, Boolean, null: true
6
+ field :token, String, null: true
7
+
8
+ argument :attributes, ::Ibrain::Auth::Types::Input::GenerateFirebaseTokenInput, required: true
9
+
10
+ def resolve(_args)
11
+ token = repo.generate_custom_token!
12
+
13
+ graphql_returning(token)
14
+ end
15
+
16
+ private
17
+
18
+ def normalize_parameters
19
+ attribute_params.permit(:uid)
20
+ rescue StandardError
21
+ ActionController::Parameters.new({})
22
+ end
23
+
24
+ def repo
25
+ ::FirebaseRepository.new(nil, normalize_parameters)
26
+ end
27
+
28
+ def graphql_returning(token)
29
+ OpenStruct.new(
30
+ token: token,
31
+ result: true
32
+ )
33
+ end
34
+ end
35
+ end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ibrain::Auth::Mutations
4
- class SsoSignInMutation < BaseMutation
4
+ class SocialSignInMutation < BaseMutation
5
5
  field :user, Types::Objects::UserType, null: true
6
6
  field :token, String, null: true
7
7
  field :result, Boolean, null: true
8
8
  field :is_verified, Boolean, null: true
9
9
 
10
- argument :id_token, String, description: 'Id Token from SSO', required: true
10
+ argument :id_token, String, description: 'Id Token from firebase', required: true
11
11
  argument :device_token, String, description: 'Device token for notificaiton', required: false
12
12
 
13
13
  def resolve(args)
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ibrain
4
+ module Auth
5
+ module Types
6
+ module Input
7
+ class GenerateFirebaseTokenInput < Ibrain::Types::BaseInputObject
8
+ argument :uid, String, required: true
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -40,6 +40,13 @@ module Ibrain
40
40
  where(query).first
41
41
  end
42
42
  end
43
+
44
+ def social_find_or_initialize(params)
45
+ user = find_by(provider: params[:provider], uid: params[:uid])
46
+ return user if user.present?
47
+
48
+ create!(params)
49
+ end
43
50
  end
44
51
  end
45
52
  end
@@ -11,7 +11,7 @@ class AuthRepository < Ibrain::BaseRepository
11
11
  end
12
12
 
13
13
  def create
14
- user = is_sso? ? sso_verify : collection.ibrain_find(manual_params, available_columns)
14
+ user = is_social? ? firebase_verify : collection.ibrain_find(manual_params, available_columns)
15
15
  user.assign_attributes(normalize_params.except(:id_token))
16
16
  user.save
17
17
 
@@ -19,7 +19,7 @@ class AuthRepository < Ibrain::BaseRepository
19
19
  end
20
20
 
21
21
  def sign_in
22
- return sso_verify if is_sso?
22
+ return firebase_verify if is_social?
23
23
 
24
24
  user = collection.ibrain_find(manual_params, available_columns)
25
25
  return unless user.try(:valid_password?, manual_params[:password])
@@ -57,21 +57,31 @@ class AuthRepository < Ibrain::BaseRepository
57
57
  params.permit(:username, :password)
58
58
  end
59
59
 
60
- def sso_verify
60
+ def firebase_verify
61
61
  response = HTTParty.post(firebase_url, headers: base_headers, body: { 'idToken' => normalize_params[:id_token] }.to_json )
62
62
  user_information = response.try(:fetch, 'users', []).try(:at, 0)
63
63
 
64
64
  uid = user_information.try(:fetch, 'localId', nil)
65
+ provider = user_information.
66
+ try(:fetch, 'providerUserInfo', []).
67
+ try(:at, 0).try(:fetch, 'providerId', '').
68
+ try(:gsub, '.com', '')
65
69
  raise ActiveRecord::RecordNotFound, I18n.t('ibrain.errors.account.not_found') if uid.blank?
66
70
 
67
- collection.find_by(uid: uid)
71
+ collection.social_find_or_initialize({
72
+ uid: uid,
73
+ provider: provider,
74
+ remote_avatar_url: user_information.try(:fetch, 'photoUrl', nil),
75
+ email: user_information.try(:fetch, 'email', nil),
76
+ password: 'Eco@123456'
77
+ })
68
78
  end
69
79
 
70
80
  def available_columns
71
81
  collection.column_names.select { |f| ACCOUNT_COUMNS.include?(f) }
72
82
  end
73
83
 
74
- def is_sso?
84
+ def is_social?
75
85
  normalize_params[:id_token].present?
76
86
  end
77
87
 
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ class FirebaseRepository < Ibrain::BaseRepository
4
+ def initialize(record, _params)
5
+ super(nil, record)
6
+
7
+ @private_key_json = File.open(Ibrain::Auth::Config.firebase_private_key_path).read
8
+ @firebase_owner_email = Ibrain::Auth::Config.firebase_owner_email
9
+ end
10
+
11
+ def generate_custom_token!
12
+ now = Time.now.to_i
13
+
14
+ payload = {
15
+ iss: firebase_owner_email,
16
+ sub: firebase_owner_email,
17
+ aud: Ibrain::Auth::Config.firebase_aud_url,
18
+ iat: now,
19
+ exp: now + 3600,
20
+ uid: params[:uid],
21
+ claims: {}
22
+ }
23
+
24
+ JWT.encode payload, private_key, "RS256"
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :private_key_json, :firebase_owner_email
30
+
31
+ def method_name
32
+ end
33
+
34
+ def json_firebase
35
+ JSON.parse(private_key_json, symbolize_names: true)
36
+ end
37
+
38
+ def private_key
39
+ OpenSSL::PKey::RSA.new json_firebase[:private_key]
40
+ end
41
+ end
@@ -16,4 +16,13 @@ Ibrain::Auth.config do |config|
16
16
 
17
17
  # sign_in graphql input
18
18
  config.sign_up_input = Ibrain::Auth::Types::Input::SignInInput
19
+
20
+ # firebase private json path
21
+ config.firebase_private_key_path = Rails.root.join('static/firebase.json')
22
+
23
+ # firebase aud url
24
+ config.firebase_auth_url = "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit"
25
+
26
+ # firebase owner email
27
+ config.firebase_owner_email = nil
19
28
  end
@@ -4,14 +4,14 @@ module Ibrain
4
4
  # frozen_string_literal: true
5
5
 
6
6
  module Auth
7
- VERSION = '0.2.5'
7
+ VERSION = '0.2.7'
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.2.4'
14
+ '0.2.6'
15
15
  end
16
16
 
17
17
  def self.ibrain_auth_gem_version
@@ -17,5 +17,14 @@ module Ibrain
17
17
 
18
18
  # sign_in input
19
19
  preference :sign_in_input, :class, default: Ibrain::Auth::Types::Input::SignInInput
20
+
21
+ # firebase private json path
22
+ preference :firebase_private_key_path, :string, default: Rails.root.join('static/firebase.json')
23
+
24
+ # firebase aud url
25
+ preference :firebase_auth_url, :string, default: "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit"
26
+
27
+ # firebase owner email
28
+ preference :firebase_owner_email, :string, default: nil
20
29
  end
21
30
  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.2.5
4
+ version: 0.2.7
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-09-13 00:00:00.000000000 Z
11
+ date: 2022-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: devise-encryptable
@@ -194,16 +194,18 @@ files:
194
194
  - app/controllers/ibrain/auth/sessions_controller.rb
195
195
  - app/controllers/ibrain/auth/unlocks_controller.rb
196
196
  - app/graphql/ibrain/auth/mutations/base_mutation.rb
197
+ - app/graphql/ibrain/auth/mutations/generate_firebase_token_mutation.rb
197
198
  - app/graphql/ibrain/auth/mutations/sign_in_mutation.rb
198
199
  - app/graphql/ibrain/auth/mutations/sign_out_mutation.rb
199
200
  - app/graphql/ibrain/auth/mutations/sign_up_mutation.rb
200
- - app/graphql/ibrain/auth/mutations/sso_sign_in_mutation.rb
201
- - app/graphql/ibrain/auth/mutations/sso_sign_up_mutation.rb
201
+ - app/graphql/ibrain/auth/mutations/social_sign_in_mutation.rb
202
+ - app/graphql/ibrain/auth/types/input/generate_firebase_token_input.rb
202
203
  - app/graphql/ibrain/auth/types/input/sign_in_input.rb
203
204
  - app/graphql/ibrain/auth/types/input/sign_up_input.rb
204
205
  - app/models/ibrain/auth/user.rb
205
206
  - app/repositories/apple_repository.rb
206
207
  - app/repositories/auth_repository.rb
208
+ - app/repositories/firebase_repository.rb
207
209
  - app/repositories/twitter_repository.rb
208
210
  - config/initializers/devise.rb
209
211
  - config/locales/en.yml
@@ -246,7 +248,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
246
248
  - !ruby/object:Gem::Version
247
249
  version: '0'
248
250
  requirements: []
249
- rubygems_version: 3.3.20
251
+ rubygems_version: 3.2.22
250
252
  signing_key:
251
253
  specification_version: 4
252
254
  summary: Its Auth is an sso authen gem for Ruby on Rails.
@@ -1,57 +0,0 @@
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(args.as_json)
49
- rescue StandardError
50
- ActionController::Parameters.new({})
51
- end
52
-
53
- def auth_options
54
- { scope: resource_name }
55
- end
56
- end
57
- end