ibrain-auth 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d92db7ce276ebc1d2452aa711b72a0c655a339bfabced5c67f864f1a65eaba0d
4
- data.tar.gz: 7b8691f0dd5e97027d2b65914e4350bbb9ebf226f255d40c657f1697df16aa60
3
+ metadata.gz: fbd79a8f9a3704ee91db11e78b78894991b517d86956a8787b1b494ed8b0c21b
4
+ data.tar.gz: a781b4afeda9073308eff768fa8fe9401dd14670c019a8b7ee5fc5ba36b796b5
5
5
  SHA512:
6
- metadata.gz: 68cd07f1bc0c0541388bdfa5da98b50368ec129cbc2941b0b73fcecd04424d107d676548143b0f1303dcd9f64296bfa2bf0f5a1adb5259a0285036f79d6b6ea1
7
- data.tar.gz: 12c8c892bbd2bb9673aa8b3a98204c624e3c00b673627d72f438697ec4887c8e49642ad66fd8fc4fcc5e9cc79435a6b2b4b9c6d8f7dee343008bc51c110b2d11
6
+ metadata.gz: 87a2246dc66f90bdba08265ca39e2d496c404ddeea69751dcd6a9d8ca30d1bc94938ad427fc5e1ab680b395f8385695fa7d481f1a484364d20f53950037699f5
7
+ data.tar.gz: eca7f37db46b319ccb3d31857739597b0a4685938197401bb9f272d93dff9d1b14d6b41781d37a89eb20c01bda5347574006169202247910833a9974d5f386f0
@@ -41,4 +41,16 @@ class Ibrain::Auth::SessionsController < Devise::SessionsController
41
41
  def repo
42
42
  AuthRepository.new(resource, params)
43
43
  end
44
+
45
+ def twitter_repo
46
+ TwitterRepository.new(resource, request.env['omniauth.auth'])
47
+ end
48
+
49
+ def apple_repo
50
+ AppleRepository.new(resource, request.env['omniauth.auth'])
51
+ end
52
+
53
+ def line_repo
54
+ LineRepository.new(resource, request.env['omniauth.auth'])
55
+ end
44
56
  end
@@ -11,6 +11,13 @@ module Ibrain::Auth::Mutations
11
11
 
12
12
  helper_method(*helpers)
13
13
 
14
+ def ready?(args)
15
+ super(args)
16
+
17
+ @auth_resource = load_resource
18
+ true
19
+ end
20
+
14
21
  # Override prefixes to consider the scoped view.
15
22
  # Notice we need to check for the request due to a bug in
16
23
  # Action Controller tests that forces _prefixes to be
@@ -28,6 +35,8 @@ module Ibrain::Auth::Mutations
28
35
 
29
36
  protected
30
37
 
38
+ attr_reader :auth_resource
39
+
31
40
  def auth_headers(headers, user, scope: nil, aud: nil)
32
41
  scope ||= Devise::Mapping.find_scope!(user)
33
42
  aud ||= headers[Warden::JWTAuth.config.aud_header]
@@ -198,6 +207,10 @@ module Ibrain::Auth::Mutations
198
207
  params.fetch(resource_name, {})
199
208
  end
200
209
 
210
+ def repo; end
211
+ def load_resource; end
212
+ def normalize_parameters; end
213
+
201
214
  ActiveSupport.run_load_hooks(:devise_controller, self)
202
215
  end
203
216
  end
@@ -6,44 +6,46 @@ module Ibrain::Auth::Mutations
6
6
  field :token, String, null: true
7
7
  field :result, Boolean, null: true
8
8
 
9
- argument :auth, Ibrain::Auth::Config.sign_in_input, required: true
9
+ argument :attributes, Ibrain::Auth::Config.sign_in_input, required: true
10
10
  argument :device_token, String, description: 'Device token for notification', required: false
11
11
 
12
- def resolve(args)
13
- # TODO: define logic inside repository
14
- repo = ::AuthRepository.new(nil, normalize_params(args))
15
- user = repo.sign_in
12
+ def resolve(_args)
13
+ raise ActionController::InvalidAuthenticityToken, I18n.t('ibrain.errors.account.incorrect') if auth_resource.blank?
16
14
 
17
- raise ActionController::InvalidAuthenticityToken, I18n.t('ibrain.errors.account.incorrect') if user.blank?
18
-
19
- sign_in(resource_name, user)
15
+ sign_in(resource_name, auth_resource)
20
16
  @current_user = warden.authenticate!(auth_options)
21
17
 
22
18
  warden.set_user(current_user)
23
- current_user.jwt_token, jti = auth_headers(request, user)
24
-
19
+ current_user.jwt_token, jti = auth_headers(request, auth_resource)
25
20
  current_user.jti = jti
26
21
  current_user.save!
27
22
 
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?
23
+ if params[:device_token].present?
24
+ device_token = current_user.device_tokens.find_by(token: params[:device_token])
25
+ current_user.device_tokens.create!({ token: params[:device_token] }) if device_token.blank?
32
26
  end
33
27
 
34
28
  context[:current_user] = current_user
35
29
 
36
- OpenStruct.new(
37
- user: user_signed_in? ? current_user : nil,
38
- token: current_user.try(:jwt_token),
39
- result: user_signed_in?
30
+ graphql_returning(
31
+ user_signed_in?,
32
+ user_signed_in? ? current_user : nil,
33
+ current_user.try(:jwt_token)
40
34
  )
41
35
  end
42
36
 
43
37
  private
44
38
 
45
- def normalize_params(args)
46
- args[:auth].to_params
39
+ def load_resource
40
+ repo.sign_in
41
+ end
42
+
43
+ def repo
44
+ ::AuthRepository.new(nil, normalize_parameters)
45
+ end
46
+
47
+ def normalize_parameters
48
+ attribute_params
47
49
  rescue StandardError
48
50
  ActionController::Parameters.new({})
49
51
  end
@@ -51,5 +53,13 @@ module Ibrain::Auth::Mutations
51
53
  def auth_options
52
54
  { scope: resource_name }
53
55
  end
56
+
57
+ def graphql_returning(result, user = nil, token = nil)
58
+ OpenStruct.new(
59
+ user: user,
60
+ token: token,
61
+ result: result
62
+ )
63
+ end
54
64
  end
55
65
  end
@@ -6,21 +6,18 @@ module Ibrain::Auth::Mutations
6
6
  field :token, String, null: true
7
7
  field :result, Boolean, null: true
8
8
 
9
- argument :user, Ibrain::Auth::Config.sign_up_input, required: true
9
+ argument :attributes, Ibrain::Auth::Config.sign_up_input, required: true
10
10
  argument :device_token, String, description: 'Device token for notificaiton', required: false
11
11
 
12
12
  def resolve(args)
13
13
  # TODO: define logic inside repository
14
- repo = ::AuthRepository.new(nil, normalize_params(args))
15
- user = repo.sign_up
14
+ return graphql_returning(false, false) if auth_resource.blank?
16
15
 
17
- return OpenStruct.new({ user: nil, token: nil, result: false, is_verified: false }) if user.blank?
18
-
19
- sign_in(resource_name, user)
16
+ sign_in(resource_name, auth_resource)
20
17
  @current_user = warden.authenticate!(auth_options)
21
18
 
22
19
  warden.set_user(current_user)
23
- current_user.jwt_token, jti = auth_headers(request, user)
20
+ current_user.jwt_token, jti = auth_headers(request, auth_resource)
24
21
 
25
22
  current_user.jti = jti
26
23
  current_user.save!
@@ -33,18 +30,26 @@ module Ibrain::Auth::Mutations
33
30
 
34
31
  context[:current_user] = current_user
35
32
 
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
33
+ graphql_returning(
34
+ user_signed_in?,
35
+ true,
36
+ user_signed_in? ? current_user : nil,
37
+ current_user.try(:jwt_token),
41
38
  )
42
39
  end
43
40
 
44
41
  private
45
42
 
46
- def normalize_params(args)
47
- args[:user].to_params
43
+ def load_resource
44
+ repo.sign_up
45
+ end
46
+
47
+ def repo
48
+ ::AuthRepository.new(nil, normalize_parameters)
49
+ end
50
+
51
+ def normalize_parameters
52
+ attribute_params
48
53
  rescue StandardError
49
54
  ActionController::Parameters.new({})
50
55
  end
@@ -52,5 +57,14 @@ module Ibrain::Auth::Mutations
52
57
  def auth_options
53
58
  { scope: resource_name }
54
59
  end
60
+
61
+ def graphql_returning(result, is_verified, user = nil, token = nil)
62
+ OpenStruct.new(
63
+ user: user,
64
+ token: token,
65
+ result: result,
66
+ is_verified: is_verified
67
+ )
68
+ end
55
69
  end
56
70
  end
@@ -0,0 +1,8 @@
1
+ class AppleRepository < Ibrain::BaseRepository
2
+ def initialize(record, params)
3
+ super(nil, record)
4
+
5
+ @params = params
6
+ @collection = Ibrain.user_class
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ class TwitterRepository < Ibrain::BaseRepository
2
+ def initialize(record, params)
3
+ super(nil, record)
4
+
5
+ @params = params
6
+ @collection = Ibrain.user_class
7
+ end
8
+ end
data/config/routes.rb CHANGED
@@ -3,7 +3,8 @@
3
3
  Ibrain::Core::Engine.routes.draw do
4
4
  devise_for :users, controllers: {
5
5
  sessions: "ibrain/auth/sessions",
6
- registrations: "ibrain/auth/registrations"
6
+ registrations: "ibrain/auth/registrations",
7
+ callback: "ibrain/auth/sessions/callback"
7
8
  },
8
9
  path: "api/#{Ibrain::Config.api_version}/users",
9
10
  defaults: { format: :json }
@@ -6,6 +6,7 @@ module Ibrain
6
6
  module Auth
7
7
  class InstallGenerator < Rails::Generators::Base
8
8
  class_option :with_ridgepole, type: :boolean, default: true
9
+ class_option :with_social, type: :boolean, default: false
9
10
 
10
11
  def self.source_paths
11
12
  paths = superclass.source_paths
@@ -17,6 +18,10 @@ module Ibrain
17
18
  template 'config/initializers/devise.rb.tt', 'config/initializers/devise.rb', { skip: true }
18
19
  template 'config/initializers/ibrain_auth.rb.tt', 'config/initializers/ibrain_auth.rb', { skip: true }
19
20
  template 'config/initializers/ibrain_jwt.rb.tt', 'config/initializers/ibrain_jwt.rb', { skip: true }
21
+
22
+ if options[:with_social]
23
+ template 'config/initializers/omniauth.rb.tt', 'config/initializers/omniauth.rb', { skip: true }
24
+ end
20
25
 
21
26
  if options[:with_ridgepole]
22
27
  template 'db/schemas/users_schema.erb', 'db/schemas/users.schema', { skip: true }
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ Rails.application.config.middleware.use OmniAuth::Builder do
4
+ # For twitter authenticate
5
+ # provider :twitter, ENV['TWITTER_CLIENT_ID'], ENV['TWITTER_CLIENT_SECRET']
6
+
7
+ # For line authenticate
8
+ # provider :line, ENV['LINE_CLIENT_ID'], ENV['LINE_CLIENT_SECRET']
9
+
10
+ # For apple authenticate
11
+ # provider :apple, ENV['APPLE_CLIENT_ID'], ENV['APPLE_CLIENT_SECRET']
12
+ end
@@ -4,14 +4,14 @@ module Ibrain
4
4
  # frozen_string_literal: true
5
5
 
6
6
  module Auth
7
- VERSION = '0.2.3'
7
+ VERSION = '0.2.4'
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.2'
14
+ '0.2.3'
15
15
  end
16
16
 
17
17
  def self.ibrain_auth_gem_version
data/lib/ibrain/auth.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  require 'devise'
4
4
  require 'devise-encryptable'
5
5
  require 'devise/jwt'
6
+ require 'omniauth'
7
+ require 'omniauth-twitter'
6
8
  require 'ibrain_core'
7
9
 
8
10
  require 'ibrain/auth/devise'
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative '../../app/graphql/ibrain/auth/types/input/sign_in_input.rb'
4
+ require_relative '../../app/graphql/ibrain/auth/types/input/sign_up_input.rb'
5
+
3
6
  module Ibrain
4
7
  class AuthConfiguration < Preferences::Configuration
5
8
  preference :api_version, :string, default: 'v1'
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.3
4
+ version: 0.2.4
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-07-24 00:00:00.000000000 Z
11
+ date: 2022-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: devise
@@ -108,6 +108,76 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: omniauth-twitter
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: omniauth-line
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: omniauth-facebook
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: omniauth-google-oauth2
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: omniauth-apple
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
111
181
  description: Its Auth is an sso authen gem for Ruby on Rails.
112
182
  email:
113
183
  - john@techfox.io
@@ -132,7 +202,9 @@ files:
132
202
  - app/graphql/ibrain/auth/types/input/sign_in_input.rb
133
203
  - app/graphql/ibrain/auth/types/input/sign_up_input.rb
134
204
  - app/models/ibrain/auth/user.rb
205
+ - app/repositories/apple_repository.rb
135
206
  - app/repositories/auth_repository.rb
207
+ - app/repositories/twitter_repository.rb
136
208
  - config/initializers/devise.rb
137
209
  - config/locales/en.yml
138
210
  - config/locales/jp.yml
@@ -142,6 +214,7 @@ files:
142
214
  - lib/generators/ibrain/auth/install/templates/config/initializers/devise.rb.tt
143
215
  - lib/generators/ibrain/auth/install/templates/config/initializers/ibrain_auth.rb.tt
144
216
  - lib/generators/ibrain/auth/install/templates/config/initializers/ibrain_jwt.rb.tt
217
+ - lib/generators/ibrain/auth/install/templates/config/initializers/omniauth.rb.tt
145
218
  - lib/generators/ibrain/auth/install/templates/db/schemas/users_migrate.erb
146
219
  - lib/generators/ibrain/auth/install/templates/db/schemas/users_schema.erb
147
220
  - lib/ibrain/auth.rb
@@ -173,7 +246,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
246
  - !ruby/object:Gem::Version
174
247
  version: '0'
175
248
  requirements: []
176
- rubygems_version: 3.3.7
249
+ rubygems_version: 3.3.20
177
250
  signing_key:
178
251
  specification_version: 4
179
252
  summary: Its Auth is an sso authen gem for Ruby on Rails.