lra_client 0.1.0 → 1.0.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
  SHA1:
3
- metadata.gz: 35298c5706f221732cdb53f372d95ffc76cc7721
4
- data.tar.gz: ffa9118cf81704ea7a8c295b2cb23983ee85ef7d
3
+ metadata.gz: d375d12b47958dbd15927d665ccab0e38de53fac
4
+ data.tar.gz: 17b1d9835198ab8cc9d379a69e61ad691f00bc07
5
5
  SHA512:
6
- metadata.gz: b9c4eb67fce91a7cfab85f1e85e8f5ddfe816dd4947dc2d451928778312953ee6be0743de402431422d01a7fec2e6bed874674755af3d335eed554b3720789ae
7
- data.tar.gz: 39f75a64010e37d909293b3fc4b699408c7fbca2ad9f01b0b2f208a9488bddba0514fac1430bd657dc32bf4318cd2506425e54f5e0f1a27c7a33ef051b180cc3
6
+ metadata.gz: 150cf5b14c2988a67eb92e332e0463eac66ee23123cfdea0feb4f1c89ebdd49c8e52724f0ffb111e1de16038f254e2b9ffbebc2e1e88ff264e730efcb5840df4
7
+ data.tar.gz: 2def0facde50210e369b6bc2b96c55d5fd87bbaf56ce5a52c15b4abb367d1a6335c4a6e3c05a010da3a4f7092d5ffccd72466d93b977690b3af2b56925e68296
data/README.md CHANGED
@@ -1,9 +1,3 @@
1
- # LraClient
2
- Short description and motivation.
3
-
4
- ## Usage
5
- How to use my plugin.
6
-
7
1
  ## Installation
8
2
  Add this line to your application's Gemfile:
9
3
 
@@ -11,18 +5,52 @@ Add this line to your application's Gemfile:
11
5
  gem 'lra_client'
12
6
  ```
13
7
 
14
- And then execute:
8
+ And then execute the following to setup:
15
9
  ```bash
16
- $ bundle
10
+ $ bundle install
11
+ $ bundle exec rake lra_client:install:migrations
12
+ $ bundle exec rake db:migrate
17
13
  ```
18
14
 
19
- Or install it yourself as:
20
- ```bash
21
- $ gem install lra_client
15
+ To configure the client, add a new initializer to your project with the following:
16
+ ```ruby
17
+ LraClient.app_id = [your app ID]
18
+ LraClient.key = '[your app key]'
19
+ LraClient.domain_namespace = 'https://my.website.com/[mountpoint]'
20
+ LraClient.login_completion_url = '[where to redirect after login attempt]'
22
21
  ```
23
22
 
24
- ## Contributing
25
- Contribution directions go here.
23
+ Finally, mount the engine into your `config/routes.rb` file:
24
+ ```ruby
25
+ mount LraClient::Engine, at: '[mountpoint]'
26
+ ```
27
+
28
+ (A good example mountpoint for the above might be simply `/lra`)
29
+
30
+ ## Usage
31
+
32
+ Assuming you have mounted the engine at `/lra`, direct your users to `/lra/login` in order to
33
+ start the login process. After a login attempt, your user will be redirected back to the endpoint
34
+ you have specified by `LraClient.login_completion_url`. If the login was successful, a record
35
+ in the `lra_client_users` table will have been created with some basic details. In addition,
36
+ two session variables will have been set to identify the user:
37
+ ```ruby
38
+ session[:current_user_id] # the ID of a LraClient::User
39
+ session[:current_user_type] # always set to the string "LraClient::User"
40
+ ```
41
+
42
+ At any point, you can validate that the current user has a valid LRA session by calling
43
+ `LraClient.validate_session(session)`. The `LraClient::User` object will be returned if
44
+ the session is valid, and `nil` otherwise. This can be used to restrict access to pages
45
+ by doing something similar to:
46
+ ```ruby
47
+ before_filter :validate_session
48
+
49
+ def validate_session
50
+ @current_user = LraClient.validate_session
51
+ return true if @current_user.present?
52
+ redirect_to '/lra/login'
53
+ false
54
+ end
55
+ ```
26
56
 
27
- ## License
28
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,46 @@
1
+ require_dependency "lra_client/application_controller"
2
+
3
+ module LraClient
4
+ class LraController < ApplicationController
5
+
6
+ def login
7
+ result = send_post_request(
8
+ LraClient.request_challenge_url,
9
+ app_id: LraClient.app_id,
10
+ key: LraClient.key,
11
+ return_url: LraClient.return_url
12
+ )
13
+
14
+ redirect_to result['login_url']
15
+ end
16
+
17
+ def return
18
+ result = send_post_request(
19
+ LraClient.load_details_url,
20
+ app_id: LraClient.app_id,
21
+ key: LraClient.key,
22
+ answer: params[:answer]
23
+ )
24
+
25
+ if result['success']
26
+ user = LraClient::User.create_or_update_from_loft(result)
27
+ session[:current_user_id] = user.id
28
+ session[:current_user_type] = 'LraClient::User'
29
+ end
30
+
31
+ redirect_to LraClient.login_completion_url
32
+ end
33
+
34
+ private
35
+
36
+ def send_post_request(url, data)
37
+ uri = URI.parse(url)
38
+ req = Net::HTTP::Post.new(uri.path)
39
+ req.form_data = data
40
+ con = Net::HTTP.new(uri.host, uri.port)
41
+ con.use_ssl = true
42
+ result = con.start { |https| https.request(req) }
43
+ JSON.parse(result.body)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,4 @@
1
+ module LraClient
2
+ module LraHelper
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ module LraClient
2
+ class User < ApplicationRecord
3
+ def self.create_or_update_from_loft(params)
4
+ user = User.find_by(loft_id: params['id'])
5
+ user = User.new if user.nil?
6
+ user.loft_id = params['id']
7
+ user.name = params['name']
8
+ user.avatar = params['avatar']
9
+ user.email = params['email']
10
+ user.save!
11
+ user
12
+ end
13
+ end
14
+ end
data/config/routes.rb CHANGED
@@ -1,2 +1,4 @@
1
1
  LraClient::Engine.routes.draw do
2
+ get 'login', to: 'lra#login'
3
+ get 'return', to: 'lra#return'
2
4
  end
@@ -0,0 +1,11 @@
1
+ class CreateLraClientUsers < ActiveRecord::Migration[5.0]
2
+ def change
3
+ create_table :lra_client_users do |t|
4
+ t.integer :loft_id
5
+ t.string :name
6
+ t.string :avatar
7
+ t.string :email
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module LraClient
2
- VERSION = '0.1.0'
2
+ VERSION = '1.0.0'
3
3
  end
data/lib/lra_client.rb CHANGED
@@ -1,5 +1,20 @@
1
1
  require "lra_client/engine"
2
2
 
3
3
  module LraClient
4
- # Your code goes here...
4
+ class << self
5
+ attr_accessor :app_id, :key, :domain_namespace, :login_completion_url
6
+ attr_accessor :request_challenge_url, :load_details_url
7
+
8
+ def return_url
9
+ self.domain_namespace + '/return'
10
+ end
11
+
12
+ def validate_session(session)
13
+ return nil if session[:current_user_type] != 'LraClient::User'
14
+ User::find_by(id: session[:current_user_id])
15
+ end
16
+ end
17
+
18
+ self.request_challenge_url = 'https://lifemakesuslaugh.com/api/lra/request_challenge.json'
19
+ self.load_details_url = 'https://lifemakesuslaugh.com/api/lra/load_details.json'
5
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lra_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Simpson
@@ -56,14 +56,20 @@ files:
56
56
  - Rakefile
57
57
  - app/assets/config/lra_client_manifest.js
58
58
  - app/assets/javascripts/lra_client/application.js
59
+ - app/assets/javascripts/lra_client/lra.js
59
60
  - app/assets/stylesheets/lra_client/application.css
61
+ - app/assets/stylesheets/lra_client/lra.css
60
62
  - app/controllers/lra_client/application_controller.rb
63
+ - app/controllers/lra_client/lra_controller.rb
61
64
  - app/helpers/lra_client/application_helper.rb
65
+ - app/helpers/lra_client/lra_helper.rb
62
66
  - app/jobs/lra_client/application_job.rb
63
67
  - app/mailers/lra_client/application_mailer.rb
64
68
  - app/models/lra_client/application_record.rb
69
+ - app/models/lra_client/user.rb
65
70
  - app/views/layouts/lra_client/application.html.erb
66
71
  - config/routes.rb
72
+ - db/migrate/20161219184020_create_lra_client_users.rb
67
73
  - lib/lra_client.rb
68
74
  - lib/lra_client/engine.rb
69
75
  - lib/lra_client/version.rb