Commit 3874d609 by John Doe

basic interface proxy works

parent 91a5fad1
......@@ -2,7 +2,7 @@ source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '5.1.5'
gem 'rails', '5.2.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
......@@ -59,6 +59,7 @@ group :development, :test do
gem 'capistrano-rails', '~> 1.2'
gem 'capistrano-rbenv', '~> 2.0'
gem 'capistrano-passenger'
gem 'thin'
end
group :local, :development, :test do
gem 'factory_bot_rails'
......
GEM
remote: https://rubygems.org/
specs:
actioncable (5.1.5)
actionpack (= 5.1.5)
actioncable (5.2.0)
actionpack (= 5.2.0)
nio4r (~> 2.0)
websocket-driver (~> 0.6.1)
actionmailer (5.1.5)
actionpack (= 5.1.5)
actionview (= 5.1.5)
activejob (= 5.1.5)
websocket-driver (>= 0.6.1)
actionmailer (5.2.0)
actionpack (= 5.2.0)
actionview (= 5.2.0)
activejob (= 5.2.0)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.1.5)
actionview (= 5.1.5)
activesupport (= 5.1.5)
actionpack (5.2.0)
actionview (= 5.2.0)
activesupport (= 5.2.0)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.1.5)
activesupport (= 5.1.5)
actionview (5.2.0)
activesupport (= 5.2.0)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (5.1.5)
activesupport (= 5.1.5)
activejob (5.2.0)
activesupport (= 5.2.0)
globalid (>= 0.3.6)
activemodel (5.1.5)
activesupport (= 5.1.5)
activerecord (5.1.5)
activemodel (= 5.1.5)
activesupport (= 5.1.5)
arel (~> 8.0)
activesupport (5.1.5)
activemodel (5.2.0)
activesupport (= 5.2.0)
activerecord (5.2.0)
activemodel (= 5.2.0)
activesupport (= 5.2.0)
arel (>= 9.0)
activestorage (5.2.0)
actionpack (= 5.2.0)
activerecord (= 5.2.0)
marcel (~> 0.3.1)
activesupport (5.2.0)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
airbrussh (1.3.0)
sshkit (>= 1.6.1, != 1.7.0)
arel (8.0.0)
arel (9.0.0)
ast (2.4.0)
backports (3.11.3)
bcrypt (3.1.11)
bcrypt (3.1.11-x64-mingw32)
bcrypt (3.1.12)
bcrypt (3.1.12-x64-mingw32)
bindex (0.5.0)
builder (3.2.3)
byebug (10.0.2)
......@@ -66,7 +70,7 @@ GEM
capistrano-rbenv (2.1.3)
capistrano (~> 3.1)
sshkit (~> 1.3)
capybara (3.0.3)
capybara (3.1.0)
addressable
mini_mime (>= 0.1.3)
nokogiri (~> 1.8)
......@@ -100,6 +104,7 @@ GEM
railties (>= 4, < 6)
cucumber-tag_expressions (1.1.1)
cucumber-wire (0.0.1)
daemons (1.2.6)
database_cleaner (1.7.0)
devise (4.4.3)
bcrypt (~> 3.0)
......@@ -116,6 +121,8 @@ GEM
diff-lcs (1.3)
docile (1.3.0)
erubi (1.7.1)
eventmachine (1.2.7)
eventmachine (1.2.7-x64-mingw32)
execjs (2.7.0)
factory_bot (4.8.2)
activesupport (>= 3.0.0)
......@@ -151,7 +158,7 @@ GEM
hashie (3.5.7)
httparty (0.16.2)
multi_xml (>= 0.5.2)
i18n (0.9.5)
i18n (1.0.1)
concurrent-ruby (~> 1.0)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
......@@ -171,10 +178,13 @@ GEM
lumberjack (1.0.13)
mail (2.7.0)
mini_mime (>= 0.1.1)
marcel (0.3.2)
mimemagic (~> 0.3.2)
method_source (0.9.0)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mimemagic (0.3.2)
mini_magick (4.8.0)
mini_mime (1.0.0)
mini_portile2 (2.3.0)
......@@ -213,26 +223,27 @@ GEM
rack-cors (1.0.2)
rack-test (1.0.0)
rack (>= 1.0, < 3)
rails (5.1.5)
actioncable (= 5.1.5)
actionmailer (= 5.1.5)
actionpack (= 5.1.5)
actionview (= 5.1.5)
activejob (= 5.1.5)
activemodel (= 5.1.5)
activerecord (= 5.1.5)
activesupport (= 5.1.5)
rails (5.2.0)
actioncable (= 5.2.0)
actionmailer (= 5.2.0)
actionpack (= 5.2.0)
actionview (= 5.2.0)
activejob (= 5.2.0)
activemodel (= 5.2.0)
activerecord (= 5.2.0)
activestorage (= 5.2.0)
activesupport (= 5.2.0)
bundler (>= 1.3.0)
railties (= 5.1.5)
railties (= 5.2.0)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.4)
loofah (~> 2.2, >= 2.2.2)
railties (5.1.5)
actionpack (= 5.1.5)
activesupport (= 5.1.5)
railties (5.2.0)
actionpack (= 5.2.0)
activesupport (= 5.2.0)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
......@@ -267,7 +278,7 @@ GEM
rspec-mocks (~> 3.7.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
rubocop (0.55.0)
rubocop (0.56.0)
parallel (~> 1.10)
parser (>= 2.5)
powerpack (~> 0.1)
......@@ -315,6 +326,10 @@ GEM
net-ssh (>= 2.8.0)
terminal-notifier (2.0.0)
terminal-notifier-guard (1.7.0)
thin (1.7.2)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
rack (>= 1, < 3)
thor (0.20.0)
thread_safe (0.3.6)
tilt (2.0.8)
......@@ -340,7 +355,7 @@ GEM
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff
websocket-driver (0.6.5)
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
xpath (3.0.0)
......@@ -373,7 +388,7 @@ DEPENDENCIES
omniauth
pg
rack-cors
rails (= 5.1.5)
rails (= 5.2.0)
rspec-json_expectations
rspec-rails (~> 3.1)
rubocop
......@@ -385,6 +400,7 @@ DEPENDENCIES
sqlite3
terminal-notifier
terminal-notifier-guard
thin
turbolinks
tzinfo-data
uglifier (>= 1.3.0)
......@@ -393,4 +409,4 @@ DEPENDENCIES
webmock
BUNDLED WITH
1.12.5
1.16.1
......@@ -30,4 +30,8 @@ class JouleAdapter
end
return items
end
def module_interface(joule_module, req)
self.class.get("#{@url}/module/#{joule_module.joule_id}/#{req}")
end
end
class InterfacesController < ActionController::Base
before_action :authenticate_interface_user!, except: [:authenticate]
after_action :allow_wattsworth_iframe
#GET /authenticate
def authenticate
puts "here we go!"
reset_session
token = InterfaceAuthToken.find_by_id(params[:token])
token = InterfaceAuthToken.find_by_value(params[:token])
render :unauthorized and return if token.nil?
render :unauthorized and return if token.expiration < Time.now
token.destroy
session[:user_id]=token.user.id
session[:interface_id]=token.joule_module.id
render plain: "welcome #{token.user.email}"
redirect_to '/'
end
#GET /logout
def logout
reset_session
redirect '/'
redirect_to '/'
end
#everything else is proxied
def get
render 'ok, you got it'
path = params[:path] || ''
req = path +"?"+request.query_string
adapter = JouleAdapter.new(@joule_module.nilm.url)
render plain: adapter.module_interface(@joule_module,req)
end
def put
......@@ -36,10 +40,18 @@ class InterfacesController < ActionController::Base
private
def authenticate_interface_user!
puts "trying to figure out the users..."
@current_user = User.find_by_id(session[:user_id])
render :unauthorized if @current_user.nil?
@joule_module = JouleModule.find_by_id(session[:interface_id])
render :unauthorized if (@current_user.nil? || @joule_module.nil?)
#verify the session matches the URL
#verify the user has permissions on this module
end
def allow_wattsworth_iframe
urls = Rails.application.config_for(:urls)
# TODO: check if this does anything...
response.headers['X-Frame-Options'] = "ALLOW-FROM #{urls['frontend']}"
end
end
......@@ -8,11 +8,20 @@ class JouleModulesController < ApplicationController
head :unauthorized and return unless current_user.views_nilm?(@nilm)
if(@joule_module.web_interface)
@auth_token = InterfaceAuthToken.create(joule_module: @joule_module,
token = InterfaceAuthToken.create(joule_module: @joule_module,
user: current_user, expiration: 5.minutes.from_now)
@module_url = _interface_authentication_url(token)
end
render and return
end
private
def _interface_authentication_url(token)
urls = Rails.application.config_for(:urls)
#eg: http://3.interfaces.wattsworth.net/authenticate?token=1234
urls["interfaces"].gsub("XX",token.joule_module.id.to_s)+
"/authenticate?token="+token.value
end
end
......@@ -5,8 +5,4 @@ class InterfaceAuthToken < ApplicationRecord
after_initialize do |auth_token|
auth_token.value ||= SecureRandom.hex
end
def url
"http://localhost:3000/interfaces/#{joule_module.id}/authenticate?token=#{value}"
end
end
<h1>You are not logged in.
Use the Data Explorer to access this interface.</h1>
<%= image_tag "robot_stop_707x707.jpg" %>
<% url="http://wattsworth.local/api#{image_path 'robot_stop_707x707.jpg'}"%>
<img src='<%= url %>'>
......@@ -2,5 +2,5 @@
json.extract! @joule_module, *JouleModule.json_keys
json.nilm_id @nilm.id
if @joule_module.web_interface
json.url @auth_token.url
json.url @module_url
end
......@@ -7,7 +7,7 @@ Rails.application.routes.draw do
end
resources :joule_modules, only: [:show]
resources :data_views do
collection do
get 'home' #retrieve a user's home data view
......@@ -48,6 +48,7 @@ Rails.application.routes.draw do
end
end
get 'interfaces/authenticate', to: 'interfaces#authenticate'
get 'interfaces/*path', to: 'interfaces#get'
get 'interfaces/:id/authenticate', to: 'interfaces#authenticate'
get 'interfaces/:id', to: 'interfaces#get'
get 'interfaces/:id/*path', to: 'interfaces#get'
end
test:
api: http://wattsworth.local/api
interfaces: http://XX.interfaces.wattsworth.local
development:
api: http://wattsworth.local/api
interfaces: http://XX.interfaces.wattsworth.local
frontend: http://wattsworth.net
aws:
api: https://wattsworth.net/api
interfaces: https://XX.interfaces.wattsworth.net
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment