Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
wattsworth
/
lumen-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c087a93c
authored
Sep 08, 2019
by
source_reader
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
refactored data app proxy to better support nginx
parent
0202fd3d
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
36 additions
and
90 deletions
app/controllers/data_app_controller.rb
app/controllers/joule_modules_controller.rb
app/controllers/nilms_controller.rb
app/controllers/proxy_controller.rb
app/views/nilms/show.json.jbuilder
config/environments/development.rb
config/environments/production.rb
config/environments/test.rb
config/routes.rb
app/controllers/data_app_controller.rb
View file @
c087a93c
...
...
@@ -15,10 +15,10 @@ class DataAppController < ApplicationController
private
def
_app_auth_url
(
token
)
#urls = Rails.application.config_for(:urls)
#eg: http://3.interfaces.wattsworth.net/authenticate?token=1234
Rails
.
configuration
.
app_auth_url
.
call
(
token
.
data_app
.
id
)
+
"?token="
+
token
.
value
return
"#"
unless
request
.
headers
.
key?
(
"HTTP_X_APP_BASE_URI"
)
# apps not enabled
base
=
request
.
headers
[
"HTTP_X_APP_BASE_URI"
]
"
#{
base
}
/
#{
token
.
data_app
.
id
}
/?auth_token=
#{
token
.
value
}
"
end
def
authenticate_interface_user
...
...
app/controllers/joule_modules_controller.rb
deleted
100644 → 0
View file @
0202fd3d
class
JouleModulesController
<
ApplicationController
before_action
:authenticate_user!
def
show
@joule_module
=
JouleModule
.
find
(
params
[
:id
])
@nilm
=
@joule_module
.
nilm
head
:unauthorized
and
return
unless
current_user
.
views_nilm?
(
@nilm
)
if
@joule_module
.
web_interface
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
Rails
.
configuration
.
interface_url_template
.
call
(
token
.
joule_module
.
id
)
+
"/authenticate?token="
+
token
.
value
end
end
app/controllers/nilms_controller.rb
View file @
c087a93c
...
...
@@ -26,6 +26,12 @@ class NilmsController < ApplicationController
else
@service
=
StubService
.
new
end
# url's for data apps
@apps_available
=
request
.
headers
.
key?
(
"HTTP_X_APP_BASE_URI"
)
if
@apps_available
@base_url
=
request
.
headers
[
"HTTP_X_APP_BASE_URI"
]
end
end
# POST /nilms.json
...
...
app/controllers/proxy_controller.rb
View file @
c087a93c
...
...
@@ -9,40 +9,42 @@ class ProxyController < ActionController::Base
#GET /app/:id/auth
def
authenticate
#if the user is already authenticated return the proxy url
if
params
[
:token
].
nil?
# first try to authenticate the user
if
authenticate_interface_user
response
.
set_header
(
'X-PROXY-URL'
,
@app
.
url
)
response
.
set_header
(
'X-JOULE-KEY'
,
@nilm
.
key
)
head
:ok
and
return
else
head
:forbidden
and
return
end
end
# otherwise log them in and redirect to /proxy
#reset_session
token
=
InterfaceAuthToken
.
find_by_value
(
params
[
:token
])
render
:unauthorized
and
return
if
token
.
nil?
render
:unauthorized
and
return
if
token
.
expiration
<
Time
.
now
(
head
:unauthorized
and
return
)
unless
request
.
headers
.
key?
(
"HTTP_X_ORIGINAL_URI"
)
orig_query
=
URI
.
parse
(
request
.
headers
[
"HTTP_X_ORIGINAL_URI"
]).
query
head
:unauthorized
and
return
if
orig_query
.
nil?
params
=
CGI
.
parse
(
orig_query
)
head
:unathorized
and
return
unless
params
.
key?
(
"auth_token"
)
token_value
=
params
[
"auth_token"
][
0
]
token
=
InterfaceAuthToken
.
find_by_value
(
token_value
)
head
:unauthorized
and
return
if
token
.
nil?
head
:unauthorized
and
return
if
token
.
expiration
<
Time
.
now
token
.
destroy
session
[
:user_id
]
=
token
.
user
.
id
response
.
set_header
(
'X-JOULE-KEY'
,
token
.
data_app
.
nilm
.
key
)
# if the app_ids key does not exist initialize it to this app
session
[
:app_ids
]
=
session
[
:app_ids
]
||
[
@app
.
id
]
# if it does exist append this app if it is not already in the array
session
[
:app_ids
]
|=
[
@app
.
id
]
redirect_to
_app_proxy_url
(
token
)
and
return
response
.
set_header
(
'X-PROXY-URL'
,
@app
.
url
)
response
.
set_header
(
'X-JOULE-KEY'
,
token
.
data_app
.
nilm
.
key
)
head
:ok
and
return
end
private
def
_app_proxy_url
(
token
)
#urls = Rails.application.config_for(:urls)
#eg: http://3.interfaces.wattsworth.net/authenticate?token=1234
Rails
.
configuration
.
app_proxy_url
.
call
(
token
.
data_app
.
id
)
end
def
authenticate_interface_user
@current_user
=
User
.
find_by_id
(
session
[
:user_id
])
@app
=
DataApp
.
find_by_id
(
params
[
:id
])
# make sure the app is authorized by the cookie
return
false
unless
session
.
include?
(
:app_ids
)
return
false
unless
session
[
:app_ids
].
include?
(
@app
.
id
)
@nilm
=
@app
.
nilm
return
false
if
@current_user
.
nil?
||
@app
.
nil?
return
false
unless
@current_user
.
views_nilm?
(
@nilm
)
...
...
app/views/nilms/show.json.jbuilder
View file @
c087a93c
...
...
@@ -13,11 +13,16 @@ json.data do
end
end
end
if @apps_available
json.data_apps(@nilm.data_apps) do |app|
json.id app.id
json.name app.name
json.url Rails.configuration.app_proxy_url.call(app.id)
json.url "#{@base_url}/#{app.id}/"
json.nilm_id @nilm.id
end
else
json.data_apps = []
end
end
json.partial! 'helpers/messages', service: @service
config/environments/development.rb
View file @
c087a93c
...
...
@@ -78,20 +78,5 @@ Rails.application.configure do
#
config
.
send_emails
=
true
config
.
app_auth_url
=
lambda
do
|
id
|
# change to subdomains for additional security
# NOTE: this requires a DNS server
# return "http://#{id}.data_app.wattsworth.local"
#
return
"http://127.0.0.1:3001/api/app/
#{
id
}
/auth"
end
config
.
app_proxy_url
=
lambda
do
|
id
|
# change to subdomains for additional security
# NOTE: this requires a DNS server
# return "http://#{id}.data_app.wattsworth.local"
#
return
"http://127.0.0.1:3001/app/
#{
id
}
/"
end
end
config/environments/production.rb
View file @
c087a93c
...
...
@@ -95,12 +95,5 @@ Rails.application.configure do
#
config
.
send_emails
=
false
config
.
interface_url_template
=
lambda
do
|
id
|
# change to subdomains for additional security
# NOTE: this requires a DNS server
# return "http://#{id}.data_app.wattsworth.local"
#
return
"/api/data_app/
#{
id
}
/"
end
end
config/environments/test.rb
View file @
c087a93c
...
...
@@ -40,20 +40,4 @@ Rails.application.configure do
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# set up interface subdomain
config
.
app_auth_url
=
lambda
do
|
id
|
# change to subdomains for additional security
# NOTE: this requires a DNS server
# return "http://#{id}.data_app.wattsworth.local"
#
return
"http://127.0.0.1:3001/api/app/
#{
id
}
/auth"
end
config
.
app_proxy_url
=
lambda
do
|
id
|
# change to subdomains for additional security
# NOTE: this requires a DNS server
# return "http://#{id}.data_app.wattsworth.local"
#
return
"http://127.0.0.1:3001/app/
#{
id
}
/"
end
end
config/routes.rb
View file @
c087a93c
...
...
@@ -6,8 +6,6 @@ Rails.application.routes.draw do
end
end
resources
:joule_modules
,
only:
[
:show
]
resources
:data_views
do
collection
do
get
'home'
#retrieve a user's home data view
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment