forest_liana 2.0.4 → 2.1.0

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
  SHA1:
3
- metadata.gz: c8f25a263a71b665bac3123e7095e02231fcd773
4
- data.tar.gz: ca4ec89ea6c0d1721f09786fe17e45bbcc5725e0
3
+ metadata.gz: a4db7e02922415856e04fdc54fb7b4557853d253
4
+ data.tar.gz: 392d8c62c9766398b3373b530a77877ee195a78f
5
5
  SHA512:
6
- metadata.gz: da2a3d664e0beae4a6a90238977490ace3153ba02f7daa8efafab3d77c0cb78ec319de95cec4e145444d24265dffafbec5440c8f2f1a20cd25bbedc95ffec990
7
- data.tar.gz: 9053106fdbc35a96b0acd060bb0403efaa253d992ade6a7a27e37b9851083c2a887e5edb11d97a0318696f14bb103d5126cf270fa5f77f0926792233b1459876
6
+ metadata.gz: 2e330eafe8a7a2c9e81fd37037ea2703bf671821de2161bccb8e903807028f0aa01299bc0cf3edea500bccb3b6ecd984f33e5d3d4cd69922659fe6394b7e9b57
7
+ data.tar.gz: 03d746ad20c557fcd7ee569c62ab7983103703b38edfff8bce6a968b471dfb49a73a7cd1745eb50c521f9c355bba027438378155a872232b42caf6544d8795d4
@@ -24,6 +24,27 @@ module ForestLiana
24
24
  end
25
25
  end
26
26
 
27
+ def create_with_google
28
+ @error_message = nil
29
+
30
+ rendering_id = params['renderingId']
31
+ forest_token = params['forestToken']
32
+
33
+ user = GoogleAuthorizedUserGetter.new(rendering_id, forest_token).perform()
34
+ token = encode_token(user) if user
35
+
36
+ if token
37
+ render json: { token: token }, serializer: nil
38
+ else
39
+ if @error_message
40
+ render serializer: nil, json: JSONAPI::Serializer.serialize_errors(
41
+ [{ detail: @error_message }]), status: :unauthorized
42
+ else
43
+ head :unauthorized
44
+ end
45
+ end
46
+ end
47
+
27
48
  private
28
49
 
29
50
  def check_user
@@ -43,7 +64,7 @@ module ForestLiana
43
64
  end
44
65
 
45
66
  def fetch_allowed_users
46
- AllowedUsersGetter.new.perform(params['renderingId'])
67
+ AllowedUsersGetter.new(params['renderingId']).perform()
47
68
  end
48
69
 
49
70
  def has_internal_authentication?
@@ -1,45 +1,27 @@
1
1
  module ForestLiana
2
- class AllowedUsersGetter
3
- def perform(renderingId)
4
- uri = URI.parse("#{forest_url}/forest/renderings/#{renderingId}/allowed-users")
5
- http = Net::HTTP.new(uri.host, uri.port)
6
- http.use_ssl = true if forest_url.start_with?('https')
7
-
8
- begin
9
- http.start do |client|
10
- request = Net::HTTP::Get.new(uri.path)
11
- request['Content-Type'] = 'application/json'
12
- request['forest-secret-key'] = ForestLiana.env_secret
13
- response = client.request(request)
2
+ class AllowedUsersGetter < UsersGetter
3
+ def initialize(rendering_id)
4
+ super('allowed-users', rendering_id)
5
+ end
14
6
 
15
- if response.is_a?(Net::HTTPOK)
16
- body = JSON.parse(response.body)
17
- ForestLiana.allowed_users = body['data'].map do |d|
18
- user = d['attributes']
19
- user['id'] = d['id']
7
+ def handle_service_response(response)
8
+ if response.is_a?(Net::HTTPOK)
9
+ body = JSON.parse(response.body)
10
+ ForestLiana.allowed_users = body['data'].map do |d|
11
+ user = d['attributes']
12
+ user['id'] = d['id']
20
13
 
21
- user
22
- end
23
- elsif response.is_a?(Net::HTTPNotFound)
24
- FOREST_LOGGER.error "Cannot retrieve the project you\'re trying " \
25
- "to unlock. Can you check that you properly copied the Forest " \
26
- "env_secret in the forest_liana initializer?"
27
- else
28
- FOREST_LOGGER.error "Cannot retrieve any users for the project " \
29
- "you\'re trying to unlock. An error occured in Forest API."
30
- []
31
- end
14
+ user
32
15
  end
33
- rescue => exception
16
+ elsif response.is_a?(Net::HTTPNotFound)
17
+ FOREST_LOGGER.error "Cannot retrieve the project you\'re trying " \
18
+ "to unlock. Can you check that you properly copied the Forest " \
19
+ "env_secret in the forest_liana initializer?"
20
+ else
34
21
  FOREST_LOGGER.error "Cannot retrieve any users for the project " \
35
- "you\'re trying to unlock. Forest API seems to be down right now."
22
+ "you\'re trying to unlock. An error occured in Forest API."
23
+ []
36
24
  end
37
25
  end
38
-
39
- private
40
-
41
- def forest_url
42
- ENV['FOREST_URL'] || 'https://forestadmin-server.herokuapp.com';
43
- end
44
26
  end
45
27
  end
@@ -0,0 +1,28 @@
1
+ module ForestLiana
2
+ class GoogleAuthorizedUserGetter < UsersGetter
3
+ def initialize(rendering_id, forest_token)
4
+ @forest_token = forest_token
5
+ super('google-authorization', rendering_id)
6
+ end
7
+
8
+ def handle_service_response(response)
9
+ if response.is_a?(Net::HTTPOK)
10
+ body = JSON.parse(response.body)
11
+ body['data']['attributes']
12
+ elsif response.is_a?(Net::HTTPNotFound)
13
+ FOREST_LOGGER.error "Cannot retrieve the project you\'re trying " \
14
+ "to unlock. Can you check that you properly copied the Forest " \
15
+ "env_secret in the forest_liana initializer?"
16
+ nil
17
+ elsif response.is_a?(Net::HTTPUnauthorized)
18
+ FOREST_LOGGER.error "Cannot retrieve the user for the project " \
19
+ "you\'re trying to unlock. The google user account seems invalid."
20
+ nil
21
+ else
22
+ FOREST_LOGGER.error "Cannot retrieve the user for the project " \
23
+ "you\'re trying to unlock. An error occured in Forest API."
24
+ nil
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,39 @@
1
+ module ForestLiana
2
+ class UsersGetter
3
+ def initialize(endpoint, rendering_id)
4
+ @uri = URI.parse("#{forest_url}/forest/renderings/#{rendering_id}/#{endpoint}")
5
+ end
6
+
7
+ def perform
8
+ http = Net::HTTP.new(@uri.host, @uri.port)
9
+ http.use_ssl = true if forest_url.start_with?('https')
10
+
11
+ begin
12
+ http.start do |client|
13
+ request = Net::HTTP::Get.new(@uri.path)
14
+ request['Content-Type'] = 'application/json'
15
+ request['forest-secret-key'] = ForestLiana.env_secret
16
+ request['forest-token'] = @forest_token if @forest_token
17
+ response = client.request(request)
18
+
19
+ handle_service_response(response)
20
+ end
21
+ rescue => exception
22
+ puts exception
23
+ FOREST_LOGGER.error "Cannot retrieve any users for the project " \
24
+ "you\'re trying to unlock. Forest API seems to be down right now."
25
+ nil
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def forest_url
32
+ ENV['FOREST_URL'] || 'https://api.forestadmin.com';
33
+ end
34
+
35
+ def handle_service_response
36
+ raise 'Abstract class method, this method must be implemented.'
37
+ end
38
+ end
39
+ end
data/config/routes.rb CHANGED
@@ -6,6 +6,7 @@ ForestLiana::Engine.routes.draw do
6
6
 
7
7
  # Session
8
8
  post 'sessions' => 'sessions#create'
9
+ post 'sessions-google' => 'sessions#create_with_google'
9
10
 
10
11
  # Associations
11
12
  get ':collection/:id/relationships/:association_name' => 'associations#index'
@@ -1,3 +1,3 @@
1
1
  module ForestLiana
2
- VERSION = "2.0.4"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -20,6 +20,11 @@ module ForestLiana
20
20
  }, {
21
21
  controller: 'forest_liana/sessions', action: 'create'
22
22
  })
23
+ assert_routing({
24
+ method: 'post', path: 'sessions-google'
25
+ }, {
26
+ controller: 'forest_liana/sessions', action: 'create_with_google'
27
+ })
23
28
 
24
29
  # Associations
25
30
  assert_routing({
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_liana
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sandro Munda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-08 00:00:00.000000000 Z
11
+ date: 2018-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -171,6 +171,7 @@ files:
171
171
  - app/services/forest_liana/base_getter.rb
172
172
  - app/services/forest_liana/belongs_to_updater.rb
173
173
  - app/services/forest_liana/controller_factory.rb
174
+ - app/services/forest_liana/google_authorized_user_getter.rb
174
175
  - app/services/forest_liana/has_many_associator.rb
175
176
  - app/services/forest_liana/has_many_dissociator.rb
176
177
  - app/services/forest_liana/has_many_getter.rb
@@ -200,6 +201,7 @@ files:
200
201
  - app/services/forest_liana/stripe_sources_getter.rb
201
202
  - app/services/forest_liana/stripe_subscription_getter.rb
202
203
  - app/services/forest_liana/stripe_subscriptions_getter.rb
204
+ - app/services/forest_liana/users_getter.rb
203
205
  - app/services/forest_liana/value_stat_getter.rb
204
206
  - app/views/layouts/forest_liana/application.html.erb
205
207
  - config/initializers/arel-helpers.rb