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:
parent
7cd1005559
commit
1ade55b03b
@ -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]
|
||||||
|
|||||||
@ -1,11 +1,19 @@
|
|||||||
class SessionsController < ApplicationController
|
class SessionsController < ApplicationController
|
||||||
def create
|
def create
|
||||||
user = User.find_or_create_from_auth(auth)
|
if org.nil? || in_organization?(org)
|
||||||
session[:current_user_id] = user.id
|
session[:token] = auth.credentials.token
|
||||||
redirect_to root_path
|
user = User.find_or_create_from_auth(auth)
|
||||||
|
session[:current_user_id] = user.id
|
||||||
|
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
|
||||||
|
|||||||
@ -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>
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user