-
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.
Call DSI API for user roles on successful authentication
DfE SignIn API will return 404 if the user isn't found for the given API call. Checks the roles in the response for a valid authorised role code.
- Loading branch information
Showing
8 changed files
with
130 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require "jwt" | ||
|
||
module DfESignInApi | ||
class 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 | ||
|
||
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,47 @@ | ||
module DfESignInApi | ||
class GetUserAccessToService | ||
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) | ||
|
||
case response.status | ||
when 200 | ||
if response.body.key?("roles") | ||
response.body["roles"].map { |role| role["code"] }.include?(authorised_role_code) | ||
else | ||
raise DfESignInApi::UserNotAuthorisedError, "Response does not contain appropriate role" | ||
end | ||
when 404 | ||
raise DfESignInApi::UserNotAuthorisedError | ||
when 401 | ||
raise DfESignInApi::InvalidTokenError | ||
when 500 | ||
raise StandardError, "Internal Server Error" | ||
end | ||
end | ||
|
||
private | ||
|
||
def client | ||
@client ||= Client.new.client | ||
end | ||
|
||
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_code | ||
ENV["DFE_SIGN_IN_API_ROLE_CODE"] | ||
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,3 @@ | ||
module DfESignInApi | ||
class InvalidTokenError < StandardError; 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,3 @@ | ||
module DfESignInApi | ||
class UserNotAuthorisedError < StandardError; 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 |
---|---|---|
|
@@ -16,9 +16,24 @@ def given_dsi_auth_is_mocked | |
email: "[email protected]", | ||
first_name: "Test", | ||
last_name: "User" | ||
}, | ||
extra: { | ||
raw_info: { | ||
organisation: { | ||
id: org_id, | ||
} | ||
} | ||
} | ||
} | ||
) | ||
|
||
stub_request( | ||
:get, | ||
"#{ENV.fetch("DFE_SIGN_IN_API_BASE_URL")}/services/checkrecordteacher/organisations/#{org_id}/users/123456", | ||
).to_return_json( | ||
status: 200, | ||
body: { "roles" => [{ "code" => ENV["DFE_SIGN_IN_API_ROLE_CODE"] }] }, | ||
) | ||
end | ||
|
||
def when_i_visit_the_sign_in_page | ||
|
@@ -28,5 +43,9 @@ def when_i_visit_the_sign_in_page | |
def and_click_the_dsi_sign_in_button | ||
click_button "Sign in with DSI" | ||
end | ||
|
||
def org_id | ||
"12345678-1234-1234-1234-123456789012" | ||
end | ||
end | ||
end |