Skip to content

Commit 864333e

Browse files
authored
Basic SSO login using rubybench discourse forums (#264)
* Basic SSO login using rubybench discourse forums * Fix rubocop
1 parent e66594d commit 864333e

File tree

12 files changed

+117
-6
lines changed

12 files changed

+117
-6
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ gem 'msgpack'
2222
gem 'octokit'
2323
gem 'bootstrap_sb_admin_base_v2'
2424
gem 'activerecord-import'
25+
gem 'discourse_api'
2526

2627
group :development do
2728
gem 'spring'

Gemfile.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ GEM
9191
crass (1.0.3)
9292
database_cleaner (1.5.3)
9393
debug_inspector (0.0.2)
94+
discourse_api (0.35.0)
95+
faraday (~> 0.9)
96+
faraday_middleware (~> 0.10)
97+
rack (>= 1.6)
9498
erubis (2.7.0)
9599
execjs (2.7.0)
96100
factory_girl (4.7.0)
@@ -100,6 +104,8 @@ GEM
100104
railties (>= 3.0.0)
101105
faraday (0.12.1)
102106
multipart-post (>= 1.2, < 3)
107+
faraday_middleware (0.13.1)
108+
faraday (>= 0.7.4, < 1.0)
103109
ffi (1.9.14)
104110
font-awesome-rails (4.7.0.2)
105111
railties (>= 3.2, < 5.2)
@@ -124,7 +130,7 @@ GEM
124130
kgio (2.10.0)
125131
launchy (2.4.3)
126132
addressable (~> 2.3)
127-
logster (1.2.6)
133+
logster (2.3.0)
128134
loofah (2.2.2)
129135
crass (~> 1.0.2)
130136
nokogiri (>= 1.5.9)
@@ -320,6 +326,7 @@ DEPENDENCIES
320326
capybara-screenshot
321327
coffee-rails
322328
database_cleaner
329+
discourse_api
323330
factory_girl_rails
324331
haml
325332
highstock-rails

app/assets/stylesheets/modules/_navbar.scss

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,25 @@
99
width: 160px;
1010
}
1111

12-
.navbar-header {
13-
margin: 10px 0;
14-
}
15-
1612
.navbar-nav {
1713
.current {
1814
border-top: 4px solid $brand-primary;
1915
padding-top: 11px;
2016
}
2117
}
2218

19+
.navbar {
20+
.current-user {
21+
float: right;
22+
margin-right: 15px;
23+
}
24+
.container {
25+
.navbar-header {
26+
margin: 10px 0;
27+
}
28+
}
29+
}
30+
2331
.navbar-toggle:hover .icon-bar {
2432
background-color: #fff;
2533
}

app/controllers/application_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class ApplicationController < ActionController::Base
2+
include ApplicationHelper
3+
24
# Prevent CSRF attacks by raising an exception.
35
# For APIs, you may want to use :null_session instead.
46
protect_from_forgery with: :exception

app/controllers/session_controller.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class SessionController < ApplicationController
2+
def sso
3+
sso = DiscourseApi::SingleSignOn.parse(request.query_string, Rails.application.secrets.sso_secret)
4+
return_url = $redis.get(sso.nonce)
5+
if return_url.present?
6+
session[:user] = {
7+
username: sso.username,
8+
email: sso.email,
9+
external_id: sso.external_id
10+
}
11+
redirect_to return_url
12+
else
13+
render plain: "Couldn't authenticate via SSO.", status: 422
14+
end
15+
end
16+
17+
def login
18+
sso = DiscourseApi::SingleSignOn.new
19+
sso.sso_secret = Rails.application.secrets.sso_secret
20+
sso.return_sso_url = "#{request.base_url}/sso"
21+
sso.nonce = SecureRandom.hex
22+
sso.sso_url = "#{AppSettings.forum_url}/session/sso_provider"
23+
$redis.setex(sso.nonce, 10.minutes.to_i, '/')
24+
redirect_to sso.to_url
25+
end
26+
end

app/helpers/application_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
module ApplicationHelper
2+
def current_user
3+
return @current_user if @current_user
4+
user = session[:user] || {}
5+
if user[:external_id].present? || user['external_id'].present?
6+
@current_user = OpenStruct.new(user)
7+
end
8+
end
29
end

app/views/layouts/_top_nav.html.haml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020
= link_to t('top_nav.sponsors'), sponsors_path
2121
%li
2222
= link_to t('top_nav.discuss'), AppSettings.forum_url, target: '_blank'
23+
- if current_user
24+
.current-user
25+
= current_user.username

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
get ':organization_name/:repo_name/commits' => 'repos#commits', as: :commits
2222
get ':organization_name/:repo_name/releases' => 'repos#releases', as: :releases
2323

24+
get 'login' => 'session#login'
25+
get 'sso' => 'session#sso'
26+
2427
namespace :admin do
2528
resources :groups, except: [:show]
2629

config/secrets.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ development:
2323
admin_password: '12345'
2424
github_api_token: <%= ENV["GITHUB_API_TOKEN"] %>
2525
secret_key_base: 836fa3665997a860728bcb9e9a1e704d427cfc920e79d847d79c8a9a907b9e965defa4154b2b86bdec6930adbe33f21364523a6f6ce363865724549fdfc08553
26+
sso_secret: '0123456789abc'
2627

2728
test:
2829
<<: *shared
2930
api_name: 'test'
3031
api_password: '12345'
3132
github_api_token: <%= ENV["GITHUB_API_TOKEN"] %>
3233
secret_key_base: 5a37811464e7d378488b0f073e2193b093682e4e21f5d6f3ae0a4e1781e61a351fdc878a843424e81c73fb484a40d23f92c8dafac4870e74ede6e5e174423010
34+
sso_secret: '0123456789abc'
3335

3436
# Do not keep production secrets in the repository,
3537
# instead read values from the environment.
@@ -41,3 +43,4 @@ production:
4143
ga: <%= ENV["GA"] %>
4244
admin_password: <%= ENV["ADMIN_PASSWORD"] %>
4345
github_api_token: <%= ENV["GITHUB_API_TOKEN"] %>
46+
sso_secret: <%= ENV["RUBYBENCH_SSO_SECRET"] %>

config/settings.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ test:
55
<<: *shared
66

77
development:
8-
<<: *shared
8+
# change to match your local discourse instance
9+
forum_url: 'http://l.discourse'
910

1011
production:
1112
<<: *shared

0 commit comments

Comments
 (0)