-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use global signed ID for magic link auth
- Handle valid and invalid sgid in magic link - Handle expired resource so that magic link user can request a new link if previous link has expired - Add magic link mailer for consultee
- Loading branch information
1 parent
7cb871a
commit 9418c1b
Showing
10 changed files
with
212 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
37 changes: 37 additions & 0 deletions
37
engines/bops_core/app/controllers/concerns/bops_core/magic_link_authenticatable.rb
This file contains 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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
module MagicLinkAuthenticatable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
rescue_from ActionController::ParameterMissing do |exception| | ||
render plain: "Not Found", status: :not_found | ||
end | ||
end | ||
|
||
def authenticate_with_sgid! | ||
resource = sgid_authentication_service.locate_resource | ||
|
||
handle_expired_or_invalid_sgid if resource.nil? | ||
end | ||
|
||
private | ||
|
||
def sgid_authentication_service | ||
@sgid_authentication_service ||= SgidAuthenticationService.new(sgid) | ||
end | ||
|
||
def sgid | ||
params.require(:sgid) | ||
end | ||
|
||
def handle_expired_or_invalid_sgid | ||
if sgid_authentication_service.expired_resource | ||
render plain: "Magic link expired", status: :unprocessable_entity | ||
else | ||
render plain: "Forbidden", status: :forbidden | ||
end | ||
end | ||
end | ||
end |
30 changes: 30 additions & 0 deletions
30
engines/bops_core/app/mailers/concerns/bops_core/magic_link_mailer.rb
This file contains 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 | ||
|
||
module BopsCore | ||
class MagicLinkMailer < ApplicationMailer | ||
def magic_link_mail(resource:, subdomain:, subject: "Your magic link") | ||
@resource = resource | ||
@sgid = resource.sgid | ||
@subdomain = subdomain | ||
@url = magic_link_url | ||
|
||
mail( | ||
to: resource.email_address, | ||
subject: | ||
) | ||
end | ||
|
||
private | ||
|
||
attr_reader :resource, :sgid, :subdomain | ||
|
||
def magic_link_url | ||
case resource | ||
when Consultee | ||
bops_consultees.dashboard_url(sgid:, subdomain:) | ||
else | ||
main_app.root_url | ||
end | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
engines/bops_core/app/models/concerns/bops_core/magic_linkable.rb
This file contains 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,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
module MagicLinkable | ||
extend ActiveSupport::Concern | ||
include GlobalID::Identification | ||
|
||
def sgid(expires_in: 48.hours, for: "magic_link") | ||
to_sgid(expires_in:, for:).to_s | ||
end | ||
end | ||
end |
39 changes: 39 additions & 0 deletions
39
engines/bops_core/app/services/bops_core/sgid_authentication_service.rb
This file contains 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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
class SgidAuthenticationService | ||
attr_reader :sgid, :purpose | ||
|
||
def initialize(sgid, purpose: "magic_link") | ||
@sgid = sgid | ||
@purpose = purpose | ||
end | ||
|
||
def locate_resource | ||
GlobalID::Locator.locate_signed(sgid, for: purpose) | ||
end | ||
|
||
def expired_resource | ||
gid = parse_global_id | ||
return nil unless gid | ||
|
||
gid.model_class.find_by(id: gid.model_id) | ||
rescue ActiveRecord::RecordNotFound | ||
nil | ||
end | ||
|
||
private | ||
|
||
def parse_global_id | ||
encoded, = sgid.split("--") | ||
decoded = Base64.urlsafe_decode64(CGI.unescape(encoded)) | ||
parsed = JSON.parse(decoded) | ||
|
||
return nil unless parsed.dig("_rails", "pur") == purpose | ||
|
||
GlobalID.parse(parsed.dig("_rails", "data")) | ||
rescue JSON::ParserError, TypeError, ArgumentError | ||
nil | ||
end | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
engines/bops_core/app/views/bops_core/magic_link_mailer/magic_link_mail.text.erb
This file contains 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,7 @@ | ||
Hello <%= @resource.name %> | ||
|
||
Click the link below to access your dashboard: | ||
|
||
<%= link_to "Access Dashboard", @url %> | ||
|
||
This link will expire in 48 hours. |
31 changes: 31 additions & 0 deletions
31
engines/bops_core/spec/models/bops_core/magic_linkable_spec.rb
This file contains 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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bops_core_helper" | ||
|
||
RSpec.describe BopsCore::MagicLinkable, type: :model do | ||
let(:consultation) { create(:consultation) } | ||
let(:consultee) { create(:consultee, consultation:) } | ||
|
||
describe "#sgid" do | ||
it "generates a SGID with expiration time" do | ||
sgid = consultee.sgid(expires_in: 1.day, for: "magic_link") | ||
decoded = GlobalID::Locator.locate_signed(sgid, for: "magic_link") | ||
|
||
expect(decoded).to eq(consultee) | ||
end | ||
|
||
it "returns nil if SGID has expired" do | ||
sgid = consultee.sgid(expires_in: 1.second, for: "magic_link") | ||
travel 1.minute | ||
|
||
expect(GlobalID::Locator.locate_signed(sgid, for: "magic_link")).to be_nil | ||
end | ||
|
||
it "returns nil if SGID is invalid" do | ||
sgid = consultee.sgid(expires_in: 1.minute, for: "other_link") | ||
travel 1.minute | ||
|
||
expect(GlobalID::Locator.locate_signed(sgid, for: "magic_link")).to be_nil | ||
end | ||
end | ||
end |