-
Notifications
You must be signed in to change notification settings - Fork 5
Feature/oauth integration #1095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rsmithlal
wants to merge
18
commits into
main
Choose a base branch
from
feature/oauth-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7cf30fe
WIP: test devise github oauth integration
rsmithlal b89a658
Reorder dependencies in Gemfile.lock to maintain consistency
rsmithlal 9787161
Update omniauth routes
rsmithlal 7103abc
update github oauth action
rsmithlal 7a31505
WIP: github oauth
rsmithlal a601e1d
WIP: github oauth use correct user class reference
rsmithlal d4bb07b
WIP: github oauth use correct user method to process auth env data
rsmithlal 5b94626
Only show oauth login links if param oauth_login=true
rsmithlal 855c2fb
WIP: initial foundation to integrate GitHub API using Octokit gem
rsmithlal 390dcfc
WIP: Initial scaffolding of PersonPlatformIntegrations
rsmithlal d6f7f7e
WIP: Flesh out PersonPlatformIntegration
rsmithlal 15ca1a1
Fix primary community description fallback logic
rsmithlal 23f9855
WIP: attempt to correctly sign_in the user after oauth
rsmithlal 6c800ce
Add comprehensive tests for OAuth integration and user creation
rsmithlal 8973482
Refactor GitHub authentication flow and update PersonPlatformIntegrat…
rsmithlal bf3fcf0
Configure OmniAuth for test mode and enhance OAuth test helpers
rsmithlal 0b792f3
Merge branch 'main' into feature/oauth-integration
rsmithlal 5134ce3
Merge branch 'main' into feature/oauth-integration
rsmithlal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
# app/builders/better_together/external_platform_builder.rb | ||
|
||
module BetterTogether | ||
# Builder to create external OAuth provider platforms | ||
class ExternalPlatformBuilder < Builder | ||
OAUTH_PROVIDERS = [ | ||
{ | ||
name: 'GitHub', | ||
url: 'https://github.com', | ||
identifier: 'github', | ||
description: 'GitHub OAuth Provider', | ||
time_zone: 'UTC' | ||
}, | ||
{ | ||
name: 'Google', | ||
url: 'https://accounts.google.com', | ||
identifier: 'google', | ||
description: 'Google OAuth Provider', | ||
time_zone: 'UTC' | ||
}, | ||
{ | ||
name: 'Facebook', | ||
url: 'https://www.facebook.com', | ||
identifier: 'facebook', | ||
description: 'Facebook OAuth Provider', | ||
time_zone: 'UTC' | ||
}, | ||
{ | ||
name: 'LinkedIn', | ||
url: 'https://linkedin.com', | ||
identifier: 'linkedin', | ||
description: 'LinkedIn OAuth Provider', | ||
time_zone: 'UTC' | ||
} | ||
].freeze | ||
|
||
class << self | ||
def seed_data | ||
puts "Creating #{OAUTH_PROVIDERS.length} external OAuth provider platforms..." | ||
|
||
OAUTH_PROVIDERS.each do |provider_attrs| | ||
create_external_platform(provider_attrs) | ||
end | ||
|
||
puts '✓ Successfully processed all OAuth providers' | ||
end | ||
|
||
# Clear existing external platforms - Use with caution! | ||
def clear_existing | ||
count = Platform.external.count | ||
Platform.external.delete_all | ||
puts "✓ Cleared #{count} existing external platforms" | ||
end | ||
|
||
private | ||
|
||
def create_external_platform(provider_attrs) | ||
platform = Platform.find_or_initialize_by( | ||
identifier: provider_attrs[:identifier], | ||
external: true | ||
) | ||
|
||
if platform.new_record? | ||
platform.assign_attributes( | ||
name: provider_attrs[:name], | ||
url: provider_attrs[:url], | ||
description: provider_attrs[:description], | ||
time_zone: provider_attrs[:time_zone], | ||
external: true, | ||
host: false, | ||
privacy: 'public' | ||
) | ||
|
||
platform.save! | ||
puts " ✓ Created external platform: #{platform.name}" | ||
else | ||
puts " - External platform already exists: #{platform.name}" | ||
end | ||
|
||
platform | ||
end | ||
end | ||
end | ||
end |
59 changes: 59 additions & 0 deletions
59
app/controllers/better_together/omniauth_callbacks_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class BetterTogether::OmniauthCallbacksController < Devise::OmniauthCallbacksController | ||
# See https://github.com/omniauth/omniauth/wiki/FAQ#rails-session-is-clobbered-after-callback-on-developer-strategy | ||
skip_before_action :verify_authenticity_token, only: %i[github] | ||
|
||
before_action :set_person_platform_integration, except: [:failure] | ||
before_action :set_user, except: [:failure] | ||
|
||
attr_reader :person_platform_integration, :user | ||
|
||
# def facebook | ||
# handle_auth "Facebook" | ||
# end | ||
|
||
def github | ||
handle_auth 'Github' | ||
end | ||
|
||
private | ||
|
||
def handle_auth(kind) | ||
if user.present? | ||
flash[:success] = t 'devise_omniauth_callbacks.success', kind: kind if is_navigational_format? | ||
sign_in_and_redirect user, event: :authentication # This handles the redirect | ||
else | ||
flash[:alert] = | ||
t 'devise_omniauth_callbacks.failure', kind:, reason: "#{auth.info.email} is not authorized" | ||
redirect_to new_user_registration_path | ||
end | ||
end | ||
|
||
def auth | ||
request.env['omniauth.auth'] | ||
end | ||
|
||
def set_person_platform_integration | ||
@person_platform_integration = BetterTogether::PersonPlatformIntegration.find_by(provider: auth.provider, | ||
uid: auth.uid) | ||
end | ||
|
||
def set_user | ||
@user = ::BetterTogether.user_class.from_omniauth(person_platform_integration:, auth:, current_user:) | ||
end | ||
|
||
# def github | ||
# @user = ::BetterTogether.user_class.from_omniauth(request.env['omniauth.auth']) | ||
# if @user.persisted? | ||
# sign_in_and_redirect @user | ||
# set_flash_message(:notice, :success, kind: 'Github') if is_navigational_format? | ||
# else | ||
# flash[:error] = 'There was a problem signing you in through Github. Please register or try signing in later.' | ||
# redirect_to new_user_registration_url | ||
# end | ||
# end | ||
|
||
def failure | ||
flash[:error] = 'There was a problem signing you in. Please register or try signing in later.' | ||
redirect_to helpers.base_url | ||
end | ||
end |
63 changes: 63 additions & 0 deletions
63
app/controllers/better_together/person_platform_integrations_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module BetterTogether | ||
class PersonPlatformIntegrationsController < ApplicationController | ||
before_action :set_person_platform_integration, only: %i[show edit update destroy] | ||
|
||
# GET /better_together/person_platform_integrations | ||
def index | ||
@person_platform_integrations = BetterTogether::PersonPlatformIntegration.all | ||
end | ||
|
||
# GET /better_together/person_platform_integrations/1 | ||
def show; end | ||
|
||
# GET /better_together/person_platform_integrations/new | ||
def new | ||
@person_platform_integration = BetterTogether::PersonPlatformIntegration.new | ||
end | ||
|
||
# GET /better_together/person_platform_integrations/1/edit | ||
def edit; end | ||
|
||
# POST /better_together/person_platform_integrations | ||
def create | ||
@better_together_person_platform_integration = BetterTogether::PersonPlatformIntegration.new(person_platform_integration_params) | ||
|
||
if @person_platform_integration.save | ||
redirect_to @person_platform_integration, notice: 'PersonPlatformIntegration was successfully created.' | ||
else | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# PATCH/PUT /better_together/person_platform_integrations/1 | ||
def update | ||
if @person_platform_integration.update(person_platform_integration_params) | ||
redirect_to @person_platform_integration, notice: 'PersonPlatformIntegration was successfully updated.', | ||
status: :see_other | ||
else | ||
render :edit, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /better_together/person_platform_integrations/1 | ||
def destroy | ||
@person_platform_integration.destroy! | ||
redirect_to person_platform_integrations_url, notice: 'PersonPlatformIntegration was successfully destroyed.', | ||
status: :see_other | ||
end | ||
|
||
private | ||
|
||
# Use callbacks to share common setup or constraints between actions. | ||
def set_person_platform_integration | ||
@person_platform_integration = BetterTogether::PersonPlatformIntegration.find(params[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def person_platform_integration_params | ||
params.require(:person_platform_integration).permit(:provider, :uid, :token, :secret, :profile_url, :user_id) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
app/helpers/better_together/person_platform_integrations_helper.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module BetterTogether | ||
module PersonPlatformIntegrationsHelper | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'octokit' | ||
|
||
module BetterTogether | ||
class Github | ||
def access_token | ||
Octokit::Client.new(bearer_token: jwt).create_app_installation_access_token(Rails.application.credentials.dig( | ||
:github, :installation_id | ||
))[:token] | ||
end | ||
|
||
def jwt | ||
payload = { | ||
iat: Time.now.to_i - 60, # issued at time | ||
exp: Time.now.to_i + (10 * 60), | ||
iss: Rails.application.credentials.dig(:github, :app_id) | ||
} | ||
JWT.encode(payload, private_key, 'RS256') | ||
end | ||
|
||
def private_key | ||
@private_key ||= OpenSSL::PKey::RSA.new(private_pem) | ||
end | ||
|
||
def private_pem | ||
@private_pem ||= Rails.application.credentials.dig(:github, :private_pem) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
CSRF protection weakened or disabled High
Copilot Autofix
AI 27 days ago
To fix this issue, re-enable CSRF protection for the
github
callback by removing or altering the line that skips CSRF verification for it. The best, most minimal fix is to delete or comment out theskip_before_action :verify_authenticity_token, only: %i[github]
line (line 3 in the given code). This restores Rails' default (and secure) behaviour: requests made to thegithub
callback will require the authenticity token, mitigating CSRF risk. No additional code or imports are needed—just remove that single line.