1
0
mirror of https://github.com/danbee/my-images synced 2025-03-04 08:49:05 +00:00

Ability to restrict authentication to GitHub Org

This commit is contained in:
Daniel Barber 2018-08-09 20:23:03 -04:00
parent 7cd1005559
commit 1ade55b03b
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
4 changed files with 44 additions and 4 deletions

View File

@ -1,2 +1,3 @@
export GITHUB_KEY=[key] export GITHUB_KEY=[key]
export GITHUB_SECRET=[secret] export GITHUB_SECRET=[secret]
export GITHUB_TEAM_ID=[team_id]

View File

@ -1,11 +1,19 @@
class SessionsController < ApplicationController class SessionsController < ApplicationController
def create def create
if org.nil? || in_organization?(org)
session[:token] = auth.credentials.token
user = User.find_or_create_from_auth(auth) user = User.find_or_create_from_auth(auth)
session[:current_user_id] = user.id session[:current_user_id] = user.id
redirect_to root_path redirect_to root_path
else
flash[:error] = "You must be in the #{org} organization "\
"to access that page"
redirect_to new_session_path
end
end end
def destroy def destroy
session[:token] = nil
session[:current_user_id] = nil session[:current_user_id] = nil
@current_user = nil @current_user = nil
redirect_to root_path redirect_to root_path
@ -13,6 +21,30 @@ class SessionsController < ApplicationController
private private
def org
ENV["GITHUB_ORG"]
end
def in_organization?(org_name)
organizations.select do |organization|
organization["login"] == org_name
end.any?
end
def organizations
JSON.parse(get_organizations.body)
end
def get_organizations
HTTP.
auth("token #{auth.credentials.token}").
get(organizations_url)
end
def organizations_url
"https://api.github.com/user/orgs"
end
def auth def auth
request.env["omniauth.auth"] request.env["omniauth.auth"]
end end

View File

@ -20,6 +20,10 @@
<main> <main>
<div class="container"> <div class="container">
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, class: "alert alert-info" %>
<% end %>
<%= yield %> <%= yield %>
</div> </div>
</main> </main>

View File

@ -1,3 +1,6 @@
Rails.application.config.middleware.use OmniAuth::Builder do Rails.application.config.middleware.use OmniAuth::Builder do
provider :github, ENV["GITHUB_KEY"], ENV["GITHUB_SECRET"] provider :github,
ENV["GITHUB_KEY"],
ENV["GITHUB_SECRET"],
scope: "read:org"
end end