-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a service to call the DSI API for user roles
This DSI API endpoint will respond with roles belonging to the user.
- Loading branch information
Showing
5 changed files
with
116 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require "jwt" | ||
|
||
module DfESignInApi | ||
module Client | ||
TIMEOUT_IN_SECONDS = 5 | ||
|
||
def client | ||
@client ||= | ||
Faraday.new( | ||
url: ENV.fetch("DFE_SIGN_IN_API_BASE_URL"), | ||
request: { | ||
timeout: TIMEOUT_IN_SECONDS | ||
} | ||
) do |faraday| | ||
faraday.request :authorization, "Bearer", jwt | ||
faraday.request :json | ||
faraday.response :json | ||
faraday.adapter Faraday.default_adapter | ||
end | ||
end | ||
|
||
private | ||
|
||
def jwt | ||
@jwt ||= JWT.encode( | ||
{ | ||
iss: ENV.fetch("DFE_SIGN_IN_CLIENT_ID"), | ||
aud: ENV.fetch("DFE_SIGN_IN_API_AUDIENCE"), | ||
}, | ||
ENV.fetch("DFE_SIGN_IN_API_SECRET"), | ||
"HS256", | ||
) | ||
end | ||
end | ||
end |
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,34 @@ | ||
module DfESignInApi | ||
class GetUserAccessToService | ||
include Client | ||
|
||
attr_reader :org_id, :user_id | ||
|
||
def initialize(org_id:, user_id:) | ||
@org_id = org_id | ||
@user_id = user_id | ||
end | ||
|
||
def call | ||
response = client.get(endpoint) | ||
|
||
if response.success? && response.body.key?("roles") | ||
response.body["roles"].find { |role| authorised_role_codes.include?(role["code"]) } | ||
end | ||
end | ||
|
||
private | ||
|
||
def endpoint | ||
"/services/#{service_id}/organisations/#{org_id}/users/#{user_id}" | ||
end | ||
|
||
def service_id | ||
ENV["DFE_SIGN_IN_CLIENT_ID"] | ||
end | ||
|
||
def authorised_role_codes | ||
ENV.fetch("DFE_SIGN_IN_API_ROLE_CODES").split(",") | ||
end | ||
end | ||
end |
39 changes: 39 additions & 0 deletions
39
spec/lib/dfe_sign_in_api/get_user_access_to_service_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,39 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe DfESignInApi::GetUserAccessToService do | ||
describe "#call" do | ||
let(:org_id) { "123" } | ||
let(:user_id) { "456" } | ||
let(:role_id) { "789" } | ||
let(:role_code) { ENV.fetch("DFE_SIGN_IN_API_ROLE_CODES").split(",").first } | ||
let(:endpoint) do | ||
"#{ENV.fetch("DFE_SIGN_IN_API_BASE_URL")}/services/checkrecordteacher/organisations/#{org_id}/users/#{user_id}" | ||
end | ||
|
||
subject { described_class.new(org_id:, user_id:).call } | ||
|
||
context "when the user is authorised" do | ||
before do | ||
stub_request(:get, endpoint) | ||
.to_return_json( | ||
status: 200, | ||
body: { "roles" => [{ "id" => role_id, "code" => role_code }] }, | ||
) | ||
end | ||
|
||
it { is_expected.to eq({ "id" => role_id, "code" => role_code }) } | ||
end | ||
|
||
context "when the user is not authorised" do | ||
before do | ||
stub_request(:get, endpoint) | ||
.to_return_json( | ||
status: 200, | ||
body: { "roles" => [{ "id" => role_id, "code" => "Unauthorised_Role" }] }, | ||
) | ||
end | ||
|
||
it { is_expected.to be_nil } | ||
end | ||
end | ||
end |