Skip to content

Commit

Permalink
moving forward on oauth2 session management
Browse files Browse the repository at this point in the history
  • Loading branch information
mose committed Jan 4, 2016
1 parent 4400514 commit 857fa4e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/config/hieraviz.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ app_name: HieraViz
basepath: "../puppet"
classpath: "farm_modules/%s/manifests/init.pp"
hierafile: "dev/hiera-local.yaml"
session_seed: "xxx"
usedb: true
puppetdb:
usessl: false
Expand Down
4 changes: 2 additions & 2 deletions app/views/home.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="meat text">
Welcome to hieraviz
<%= session[:access_token] %>
Welcome to hieraviz<br>
<%= @sess %>
</div>
15 changes: 13 additions & 2 deletions app/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Web < Common
helpers Sinatra::ContentFor

configure do
set :session_secret, settings.configdata['session_seed']
set :public_folder, Proc.new { File.join(root, "public") }
set :views_folder, Proc.new { File.join(root, "views") }
set :erb, layout: :_layout
Expand All @@ -36,14 +37,21 @@ def oauth_client
end
def get_response(url)
access_token = OAuth2::AccessToken.new(oauth_client, session[:access_token])
JSON.parse(access_token.get("/api/v1/#{url}").body)
JSON.parse(access_token.get("/api/v3/#{url}").body)
end
def redirect_uri
uri = URI.parse(request.url)
uri.path = '/logged-in'
uri.query = nil
uri.to_s
end
def authorize
if settings.configdata['auth_method'] == 'oauth2'
resp = get_response('projects/devops/puppet')
# logger.info resp
redirect '/'
end
end
end

case settings.configdata['auth_method']
Expand All @@ -57,13 +65,13 @@ def redirect_uri
when 'oauth2'

get '/login' do
logger.info oauth_client
redirect oauth_client.auth_code.authorize_url(:redirect_uri => redirect_uri)
end

get '/logged-in' do
access_token = oauth_client.auth_code.get_token(params[:code], :redirect_uri => redirect_uri)
session[:access_token] = access_token.token
# logger.info session['access_token']
@message = "Successfully authenticated with the server"
redirect '/'
end
Expand All @@ -73,10 +81,13 @@ def redirect_uri


get '/' do
@sess = session['access_token']
# logger.info @sess
erb :home
end

get '/nodes' do
authorize
@nodes = Hieracles::Registry.nodes(settings.config)
erb :nodes
end
Expand Down
1 change: 1 addition & 0 deletions spec/files/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
basepath: "spec/files/puppet"
classpath: "farm_modules/%s/manifests/init.pp"
hierafile: "hiera.yml"
session_seed: "toto"
usedb: false
puppetdb:
usessl: false
Expand Down

0 comments on commit 857fa4e

Please sign in to comment.