Skip to content
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

GT-1839, implement facebook auth #1228

Merged
merged 51 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
67823be
implement facebook auth
andrewroth Mar 8, 2023
9832d44
standardrb fixes, rack update
andrewroth Mar 8, 2023
a492c19
match users based on facebook user id
andrewroth Mar 9, 2023
d36f9c5
remove rescue for bad request, it's not an exception but a reponse type
andrewroth Mar 9, 2023
9b95d15
move facebook code to service to match okta code style
andrewroth Mar 9, 2023
9385ce1
standardrb fixes
andrewroth Mar 9, 2023
5b7f6a4
handle json parse error instead of jwt decode which never happens in …
andrewroth Mar 9, 2023
2b515c5
add test for json error
andrewroth Mar 9, 2023
4b67af7
remove useless assignment variable
andrewroth Mar 9, 2023
c25b937
implement facebook user delete
andrewroth Mar 10, 2023
039f596
standardrb fixes
andrewroth Mar 10, 2023
3d9d46f
switch to account prefix url
andrewroth Mar 10, 2023
45a31f1
track gr_master_person_id from okta
andrewroth Mar 13, 2023
60f691b
use proper query escaping
andrewroth Mar 15, 2023
5685516
security upgrades
andrewroth Mar 15, 2023
1f868c7
Merge branch 'master' into GT-1839-support-facebook-logins
andrewroth Mar 15, 2023
a330241
Update app/controllers/deletion_requests_controller.rb
andrewroth Mar 16, 2023
843cf69
Update app/services/facebook.rb
andrewroth Mar 16, 2023
7131cdb
various tweaks as per code review
andrewroth Mar 16, 2023
07aa6c1
add google auth
andrewroth Mar 16, 2023
630a73f
style fix
andrewroth Mar 16, 2023
fb2fed1
implement apple token verify
andrewroth Mar 20, 2023
5f766c4
Reference the PR that needs to be released for apple_auth
frett Mar 20, 2023
bc06ad5
use the validation from the apple library for the token issuer
andrewroth Mar 20, 2023
0d00606
Merge branch 'GT-1839-support-facebook-logins' of github.com:CruGloba…
andrewroth Mar 20, 2023
6c4a50c
refactor auth services to a base for common patterns
andrewroth Mar 21, 2023
9d744fd
standardrb fixes
andrewroth Mar 21, 2023
3ef394a
implement GT-1871, tracking name and using given_name & family_name
andrewroth Mar 21, 2023
00b1bf3
standardrb fixes
andrewroth Mar 21, 2023
7a69e27
Merge branch 'master' into GT-1839-support-facebook-logins
andrewroth Mar 21, 2023
979619d
fix validate line and fix test failing because of expired token
andrewroth Mar 22, 2023
c7beec2
style fixes
andrewroth Mar 22, 2023
b5f54be
rename apple and google user identifiers to id_token
andrewroth Mar 23, 2023
0c822e3
rename some methods to reflect token from google and apple is id_token
andrewroth Mar 23, 2023
ea4b609
handle apple and google passing in *_id_token instead of access_token
andrewroth Mar 23, 2023
102f39b
rename auth service classes to be consistent
andrewroth Mar 23, 2023
e08df12
style fix
andrewroth Mar 23, 2023
003a14c
fix tests
andrewroth Mar 23, 2023
1b4ab2f
try to incrase coverage; don't need interface error messages now
andrewroth Mar 23, 2023
4137c79
trying to get coverage up
andrewroth Mar 23, 2023
4eb63a7
JWT::ExpiredSignature is a JWT::DecodeError, so it's redundant to cap…
andrewroth Mar 23, 2023
d41b11b
styling fixes
andrewroth Mar 23, 2023
ab42ea8
update schema
andrewroth Mar 27, 2023
d987a99
pull body from response
andrewroth Mar 28, 2023
1ffdcb9
Merge branch 'GT-1839-support-facebook-logins' of github.com:CruGloba…
andrewroth Mar 28, 2023
2ba0255
remove email unique requirement
andrewroth Mar 28, 2023
5e2052f
Update app/services/okta_auth_service.rb
andrewroth Mar 29, 2023
bbbd3a2
tweak okta_auth_service to be more consistent on using primary_key
andrewroth Mar 29, 2023
51f0667
don't need google service_name, now with the new class default is ok
andrewroth Mar 29, 2023
ae392dc
rename AuthServiceBase to BaseAuthService
andrewroth Mar 29, 2023
f77477e
add back the raise stubs for implementing classes
andrewroth Mar 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ ADOBE_CAMPAIGN_SIGNED_JWT=asdf
OKTA_SERVER_URL=https://dev1-signon.okta.com
OKTA_SERVER_PATH=https://dev1-signon.okta.com
OKTA_SERVER_AUDIENCE=https://dev1-signon.okta.com

FACEBOOK_APP_ID=facebook_app_id
FACEBOOK_APP_SECRET=facebook_app_secret
128 changes: 64 additions & 64 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@ GEM
specs:
action-cable-testing (0.6.1)
actioncable (>= 5.0)
actioncable (6.1.7.1)
actionpack (= 6.1.7.1)
activesupport (= 6.1.7.1)
actioncable (6.1.7.3)
actionpack (= 6.1.7.3)
activesupport (= 6.1.7.3)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.1.7.1)
actionpack (= 6.1.7.1)
activejob (= 6.1.7.1)
activerecord (= 6.1.7.1)
activestorage (= 6.1.7.1)
activesupport (= 6.1.7.1)
actionmailbox (6.1.7.3)
actionpack (= 6.1.7.3)
activejob (= 6.1.7.3)
activerecord (= 6.1.7.3)
activestorage (= 6.1.7.3)
activesupport (= 6.1.7.3)
mail (>= 2.7.1)
actionmailer (6.1.7.1)
actionpack (= 6.1.7.1)
actionview (= 6.1.7.1)
activejob (= 6.1.7.1)
activesupport (= 6.1.7.1)
actionmailer (6.1.7.3)
actionpack (= 6.1.7.3)
actionview (= 6.1.7.3)
activejob (= 6.1.7.3)
activesupport (= 6.1.7.3)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.1.7.1)
actionview (= 6.1.7.1)
activesupport (= 6.1.7.1)
actionpack (6.1.7.3)
actionview (= 6.1.7.3)
activesupport (= 6.1.7.3)
rack (~> 2.0, >= 2.0.9)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.1.7.1)
actionpack (= 6.1.7.1)
activerecord (= 6.1.7.1)
activestorage (= 6.1.7.1)
activesupport (= 6.1.7.1)
actiontext (6.1.7.3)
actionpack (= 6.1.7.3)
activerecord (= 6.1.7.3)
activestorage (= 6.1.7.3)
activesupport (= 6.1.7.3)
nokogiri (>= 1.8.5)
actionview (6.1.7.1)
activesupport (= 6.1.7.1)
actionview (6.1.7.3)
activesupport (= 6.1.7.3)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
Expand All @@ -51,22 +51,22 @@ GEM
activemodel (>= 5.2.0)
activestorage (>= 5.2.0)
activesupport (>= 5.2.0)
activejob (6.1.7.1)
activesupport (= 6.1.7.1)
activejob (6.1.7.3)
activesupport (= 6.1.7.3)
globalid (>= 0.3.6)
activemodel (6.1.7.1)
activesupport (= 6.1.7.1)
activerecord (6.1.7.1)
activemodel (= 6.1.7.1)
activesupport (= 6.1.7.1)
activestorage (6.1.7.1)
actionpack (= 6.1.7.1)
activejob (= 6.1.7.1)
activerecord (= 6.1.7.1)
activesupport (= 6.1.7.1)
activemodel (6.1.7.3)
activesupport (= 6.1.7.3)
activerecord (6.1.7.3)
activemodel (= 6.1.7.3)
activesupport (= 6.1.7.3)
activestorage (6.1.7.3)
actionpack (= 6.1.7.3)
activejob (= 6.1.7.3)
activerecord (= 6.1.7.3)
activesupport (= 6.1.7.3)
marcel (~> 1.0)
mini_mime (>= 1.1.0)
activesupport (6.1.7.1)
activesupport (6.1.7.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand Down Expand Up @@ -118,7 +118,7 @@ GEM
case_transform (0.2)
activesupport
coderay (1.1.3)
concurrent-ruby (1.1.10)
concurrent-ruby (1.2.2)
crack (0.4.5)
rexml
crass (1.0.6)
Expand Down Expand Up @@ -159,7 +159,7 @@ GEM
mime-types (>= 1.0)
formatador (0.3.0)
gems (1.2.0)
globalid (1.0.1)
globalid (1.1.0)
activesupport (>= 5.0)
google-api-client (0.53.0)
google-apis-core (~> 0.1)
Expand Down Expand Up @@ -241,7 +241,7 @@ GEM
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
lumberjack (1.2.8)
mail (2.8.0.1)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
net-pop
Expand All @@ -256,7 +256,7 @@ GEM
mini_magick (4.11.0)
mini_mime (1.1.2)
mini_portile2 (2.8.1)
minitest (5.17.0)
minitest (5.18.0)
msgpack (1.6.1)
multi_json (1.15.0)
multi_xml (0.6.0)
Expand Down Expand Up @@ -300,42 +300,42 @@ GEM
puma (5.6.5)
nio4r (~> 2.0)
racc (1.6.2)
rack (2.2.6.3)
rack (2.2.6.4)
rack-cors (2.0.0)
rack (>= 2.0.0)
rack-mini-profiler (3.0.0)
rack (>= 1.2.0)
rack-protection (2.2.3)
rack
rack-test (2.0.2)
rack-test (2.1.0)
rack (>= 1.3)
raddocs (2.2.0)
haml (>= 4.0)
json
sinatra (~> 2.0)
rails (6.1.7.1)
actioncable (= 6.1.7.1)
actionmailbox (= 6.1.7.1)
actionmailer (= 6.1.7.1)
actionpack (= 6.1.7.1)
actiontext (= 6.1.7.1)
actionview (= 6.1.7.1)
activejob (= 6.1.7.1)
activemodel (= 6.1.7.1)
activerecord (= 6.1.7.1)
activestorage (= 6.1.7.1)
activesupport (= 6.1.7.1)
rails (6.1.7.3)
actioncable (= 6.1.7.3)
actionmailbox (= 6.1.7.3)
actionmailer (= 6.1.7.3)
actionpack (= 6.1.7.3)
actiontext (= 6.1.7.3)
actionview (= 6.1.7.3)
activejob (= 6.1.7.3)
activemodel (= 6.1.7.3)
activerecord (= 6.1.7.3)
activestorage (= 6.1.7.3)
activesupport (= 6.1.7.3)
bundler (>= 1.15.0)
railties (= 6.1.7.1)
railties (= 6.1.7.3)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.4.4)
rails-html-sanitizer (1.5.0)
loofah (~> 2.19, >= 2.19.1)
railties (6.1.7.1)
actionpack (= 6.1.7.1)
activesupport (= 6.1.7.1)
railties (6.1.7.3)
actionpack (= 6.1.7.3)
activesupport (= 6.1.7.3)
method_source
rake (>= 12.2)
thor (~> 1.0)
Expand Down Expand Up @@ -451,8 +451,8 @@ GEM
temple (0.8.0)
thor (1.2.1)
tilt (2.0.11)
timeout (0.3.1)
tzinfo (2.0.5)
timeout (0.3.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
uber (0.1.0)
unf (0.1.4)
Expand Down Expand Up @@ -480,7 +480,7 @@ GEM
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.6.6)
zeitwerk (2.6.7)

PLATFORMS
ruby
Expand Down Expand Up @@ -544,4 +544,4 @@ RUBY VERSION
ruby 3.0.5p211

BUNDLED WITH
2.3.11
2.3.12
16 changes: 15 additions & 1 deletion app/controllers/auth_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

class AuthController < ApplicationController
def create
token = data_attrs[:okta_access_token] ? auth_with_okta : auth_with_code
token = if data_attrs[:okta_access_token]
auth_with_okta
elsif data_attrs[:facebook_access_token]
auth_with_facebook
else
auth_with_code
end
render json: token, status: :created if token
end

Expand Down Expand Up @@ -30,4 +36,12 @@ def auth_with_okta
render_bad_request e.message
nil
end

def auth_with_facebook
user = Facebook.find_user_by_access_token(data_attrs[:facebook_access_token])
AuthToken.new(user: user)
rescue Facebook::FailedAuthentication => e
render_bad_request e.message
nil
end
end
25 changes: 25 additions & 0 deletions app/controllers/deletion_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class DeletionRequestsController < ApplicationController
# disable CSRF protection, as it doesn't make sense in this case
protect_from_forgery with: :null_session

def facebook
begin
dr = DeletionRequest.from_signed_fb(params["signed_request"])
rescue DeletionRequest::FailedAuthentication => e
render json: {"error" => e.to_s}
return
end

dr.run

render json: {
url: deletion_request_url(dr.pid),
confirmation_code: dr.pid
}
end

def show
dr = DeletionRequest.find_by_pid!(params[:id])
andrewroth marked this conversation as resolved.
Show resolved Hide resolved
render json: {"data" => dr.deleted? ? "Your data has been completely deleted" : "Your deletion request is still in progress"}
end
end
2 changes: 0 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ def show
end

def destroy
@user.user_counters.destroy_all
@user.favorite_tools.destroy_all
@user.destroy!
render json: "", status: 204
end
Expand Down
57 changes: 57 additions & 0 deletions app/models/deletion_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class DeletionRequest < ApplicationRecord
validates_presence_of :uid, :provider, :pid

# there can only be one entry with given provider + uid
validates_uniqueness_of :uid, scope: :provider

before_validation :set_pid

def run
associated_user&.destroy!
end

def deleted?
associated_user.nil?
end

def self.from_signed_fb(req)
data = DeletionRequest.parse_fb_request(req)
return unless data
DeletionRequest.create(provider: "facebook", uid: data["user_id"])
end

def self.parse_fb_request(req)
knutsenm marked this conversation as resolved.
Show resolved Hide resolved
encoded, payload = req.split(".", 2)
decoded = Base64.urlsafe_decode64(encoded)
data = JSON.parse(Base64.urlsafe_decode64(payload))

# we need to verify the digest is the same
exp = OpenSSL::HMAC.digest("SHA256", ENV["FACEBOOK_APP_SECRET"], payload)
raise FailedAuthentication, "FB deletion callback called with invalid data" if decoded != exp

data
end

private

def associated_user
# more providers will be added
knutsenm marked this conversation as resolved.
Show resolved Hide resolved
case provider
when "facebook"
User.find_by(facebook_user_id: uid)
end
end

def set_pid
if pid.blank?
self.pid = random_pid
end
end

def random_pid
knutsenm marked this conversation as resolved.
Show resolved Hide resolved
SecureRandom.hex(4)
end

class FailedAuthentication < StandardError
end
end
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class User < ApplicationRecord
has_many :user_counters
has_many :favorite_tools
has_many :user_counters, dependent: :destroy
has_many :favorite_tools, dependent: :destroy
has_many :tools, through: :favorite_tools

validates :sso_guid, uniqueness: true, presence: true
validates :sso_guid, uniqueness: true, presence: {unless: -> { facebook_user_id.present? }}

# while the email needs to be validated case-insensitively, we'll
# let Rails pass the insensitive check down to postgres's citext type
Expand Down
Loading