ibrain-auth 0.3.13 → 0.3.15

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: 8c4349772e38156afa066d4fda39aba790be46230aea760585ac2857fdbd1562
4
- data.tar.gz: a564f50845cdd14359f57a3ee0c1fde568adea39109c0330c60a5a5904b74723
3
+ metadata.gz: cede940ab1a8cb4dcf23bfef0f27135f108b06cd91c04a9f65a08b525581b369
4
+ data.tar.gz: bb24a581ed8195dea78d4497024665b93ae7e9e92654c6b199a157c9eb2967e0
5
5
  SHA512:
6
- metadata.gz: 248e52d90169fb7905e805556065cadab6c69ddfd6aa79edc0c90b0fb8261e25c1ad5ec2e4793af4b87f1c7978293f8d05cc626514ab6b2fb315ccd7d67e5250
7
- data.tar.gz: 0ca26294856bdac8bb949880dfcc06ce9e66c0ca809e720e3b3b72cd9b40c2e1a222867ebd077e3c9ac44bb3265e9155956e585adbbe98e50f314a97ff8fcc88
6
+ metadata.gz: acae84052ab448f7d874c14fc23adfc8116ceaed35c7f2f572ace7597ca675fd7e960128f7b2d51cc975690acec3c5b551e58b34aa57bf30bb8f7ba25a03c988
7
+ data.tar.gz: d5aa7e5f8ceea119e0721f909a73c1c13cd5d436e45ae357f70fba561b6eeb85f5907615eb21bf78fd39538da8f1cdb8f5d9580d80eb19787abb20947fb54a32
@@ -16,7 +16,7 @@ module Ibrain::Mutations
16
16
  private
17
17
 
18
18
  def normalize_parameters
19
- attribute_params.permit(:uid)
19
+ attribute_params.permit(:code, :redirect_uri)
20
20
  rescue StandardError
21
21
  ActionController::Parameters.new({})
22
22
  end
@@ -4,7 +4,8 @@ module Ibrain
4
4
  module Types
5
5
  module Input
6
6
  class GenerateFirebaseTokenInput < Ibrain::Types::BaseInputObject
7
- argument :uid, String, required: true
7
+ argument :code, String, required: true
8
+ argument :redirect_uri, String, required: true
8
9
  end
9
10
  end
10
11
  end
@@ -68,6 +68,10 @@ module Ibrain
68
68
  user.skip_confirmation! unless user&.confirmed?
69
69
  user
70
70
  end
71
+
72
+ def random_password
73
+ (('A'..'Z').to_a.sample(4) + ["~", "!", "@", "#", "$", "%", "^", "&", "*", "_", "-"].sample(1) + ('0'..'9').to_a.sample(2) + ('a'..'z').to_a.sample(4)).join
74
+ end
71
75
  end
72
76
  end
73
77
  end
@@ -68,12 +68,14 @@ class AuthRepository < Ibrain::BaseRepository
68
68
  try(:gsub, '.com', '')
69
69
  raise ActionController::InvalidAuthenticityToken, I18n.t('ibrain.errors.account.not_found') if uid.blank?
70
70
 
71
+ provider = 'custom' if user_information.try(:fetch, 'customAuth', false) && provider.blank?
72
+
71
73
  collection.social_find_or_initialize({
72
74
  uid: uid,
73
75
  provider: provider,
74
76
  remote_avatar_url: user_information.try(:fetch, 'photoUrl', nil),
75
77
  email: user_information.try(:fetch, 'email', nil),
76
- password: 'Eco@123456'
78
+ password: collection.random_password
77
79
  })
78
80
  end
79
81
 
@@ -12,15 +12,23 @@ class FirebaseRepository < Ibrain::BaseRepository
12
12
  end
13
13
 
14
14
  def generate_custom_token!
15
- now = Time.now.to_i
15
+ iat = Time.now.to_i
16
+ exp = 60.minutes.from_now.to_i
17
+
18
+ uid = LineRepository.singleton.retrieve_uid(
19
+ code: params[:code],
20
+ redirect_uri: params[:redirect_uri]
21
+ )
22
+
23
+ raise IbrainErrors::UnknownError.new I18n.t("ibrain.errors.custom_token.not_retrieve_uid") unless uid
16
24
 
17
25
  payload = {
18
26
  iss: firebase_owner_email,
19
27
  sub: firebase_owner_email,
20
28
  aud: Ibrain::Auth::Config.firebase_auth_url,
21
- iat: now,
22
- exp: now + 3600,
23
- uid: params[:uid],
29
+ iat: iat,
30
+ exp: exp,
31
+ uid: uid,
24
32
  claims: {}
25
33
  }
26
34
 
@@ -1,18 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class LineRepository < Ibrain::BaseRepository
4
- def initialize(record, params)
5
- super(nil, record)
3
+ class LineRepository
4
+ LINE_BASE_HEADERS = {
5
+ 'Content-Type': "application/x-www-form-urlencoded"
6
+ }.freeze
6
7
 
7
- @params = params
8
- @collection = Ibrain.user_class
8
+ LINE_TOKEN_URL = "https://api.line.me/oauth2/v2.1/token"
9
+ LINE_INFORMATION_URL = "https://api.line.me/v2/profile"
10
+
11
+ def self.singleton
12
+ @singleton ||= new
13
+ end
14
+
15
+ def retrieve_access_token(redirect_uri:, code:)
16
+ response = HTTParty.post(
17
+ LINE_TOKEN_URL,
18
+ headers: LINE_BASE_HEADERS,
19
+ body: URI.encode_www_form({
20
+ grant_type: "authorization_code",
21
+ code: code,
22
+ redirect_uri: redirect_uri,
23
+ client_id: Ibrain::Auth::Config.line_client_id,
24
+ client_secret: Ibrain::Auth::Config.line_client_secret
25
+ })
26
+ )
27
+
28
+ response.try(:fetch, "access_token", nil)
9
29
  end
10
30
 
11
- def find_or_initialize!
12
- user = @collection.find_by_line(uid: params['uid'])
31
+ def retrieve_uid(redirect_uri:, code:)
32
+ token = retrieve_access_token(
33
+ redirect_uri: redirect_uri,
34
+ code: code
35
+ )
13
36
 
14
- return user if user.present?
37
+ response = HTTParty.get(
38
+ LINE_INFORMATION_URL,
39
+ headers: LINE_BASE_HEADERS.merge({
40
+ 'Authorization' => "Bearer #{token}"
41
+ })
42
+ )
15
43
 
16
- @collection.create_with_line!
44
+ response.try(:fetch, 'userId', nil)
17
45
  end
18
46
  end
@@ -11,4 +11,6 @@ en:
11
11
  account:
12
12
  not_found: Account not found
13
13
  incorrect: Username or Password is incorrect!
14
- not_verified: Please verify your account before login
14
+ not_verified: Please verify your account before login
15
+ custom_token:
16
+ not_retrieve_uid: Can not retrieve uid
@@ -11,4 +11,6 @@ ja:
11
11
  account:
12
12
  not_found: アカウントが見つかりません
13
13
  incorrect: ユーザー名またはパスワードが正しくありません!
14
- not_verified: ログインする前にアカウントを確認してください
14
+ not_verified: ログインする前にアカウントを確認してください
15
+ custom_token:
16
+ not_retrieve_uid: uid を取得できません
@@ -11,4 +11,6 @@ vi:
11
11
  account:
12
12
  not_found: Không tìm thấy tài khoản
13
13
  incorrect: Tài khoản hoặc mật khẩu không chính xác!
14
- not_verified: Vui lòng xác minh tài khoản của bạn trước khi đăng nhập
14
+ not_verified: Vui lòng xác minh tài khoản của bạn trước khi đăng nhập
15
+ custom_token:
16
+ not_retrieve_uid: Lấy thông tin uid của người dùng thất bại
@@ -34,4 +34,10 @@ Ibrain::Auth.config do |config|
34
34
 
35
35
  # devise social providers setting
36
36
  config.devise_omniauth_providers = %i[apple facebook twitter line]
37
+
38
+ # line client id
39
+ config.line_client_id = nil
40
+
41
+ # line client secret
42
+ config.line_client_secret = nil
37
43
  end
@@ -4,7 +4,7 @@ module Ibrain
4
4
  # frozen_string_literal: true
5
5
 
6
6
  module Auth
7
- VERSION = '0.3.13'
7
+ VERSION = '0.3.15'
8
8
 
9
9
  def self.ibrain_auth_version
10
10
  VERSION
@@ -35,5 +35,11 @@ module Ibrain
35
35
 
36
36
  # devise social providers setting
37
37
  preference :devise_omniauth_providers, :array, default: %i[apple facebook twitter line]
38
+
39
+ # line client id
40
+ preference :line_client_id, :string, default: nil
41
+
42
+ # line client secret
43
+ preference :line_client_secret, :string, default: nil
38
44
  end
39
45
  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.3.13
4
+ version: 0.3.15
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: 2023-01-04 00:00:00.000000000 Z
11
+ date: 2023-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: devise