Skip to content

Commit

Permalink
Skipped org validation for service accounts. (#195)
Browse files Browse the repository at this point in the history
* Skipped org validation for service accounts.

(cherry picked from commit 708c1c6)

* Added path params to SecurityConfig.

---------

Co-authored-by: weskubo-cgi <Wesley.Kubo@gov.bc.ca>
  • Loading branch information
weskubo-cgi and weskubo-cgi authored May 19, 2023
1 parent 0a11809 commit 9d7afef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public OAuth2TokenValidatorResult validate(Jwt jwt) {

String org = jwt.getClaim(SecurityUtil.CLAIM_ORGANIZATION);

if (StringUtils.isNotEmpty(org)) {
// Service accounts won't have an Organization so don't validate
if (SecurityUtil.isServiceAccount(jwt) || StringUtils.isNotEmpty(org)) {
return OAuth2TokenValidatorResult.success();
} else {
// Audit the failure. Only users with defined Organization should be considered as authorized and able to access MSP Direct.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ protected void configure(HttpSecurity http) throws Exception {
.mvcMatchers(HttpMethod.POST, "/msp-contracts/update-contract-address").hasRole("UpdateContractAddress")
.mvcMatchers(HttpMethod.POST, "/msp-contracts/inquire-contract").hasAnyRole("ContractInquiry", "GetContractAddress") //inquire-contract endpoint will require this multi role as it is used by both R40 and R37 transactions
.mvcMatchers(HttpMethod.POST, "/patient-registration/get-patient-registration").hasRole("PatientRegistration")
.mvcMatchers(HttpMethod.GET, "/payee-mapping/").hasAnyRole("PatientRegistration", "ManageMSPPayeeNumber")
.mvcMatchers(HttpMethod.GET, "/payee-mapping/{id}").hasAnyRole("PatientRegistration", "ManageMSPPayeeNumber")
.mvcMatchers(HttpMethod.POST, "/payee-mapping").hasRole("ManageMSPPayeeNumber")
.mvcMatchers(HttpMethod.PUT, "/payee-mapping/").hasRole("ManageMSPPayeeNumber")
.mvcMatchers(HttpMethod.DELETE, "/payee-mapping/").hasRole("ManageMSPPayeeNumber")
.mvcMatchers(HttpMethod.PUT, "/payee-mapping/{id}").hasRole("ManageMSPPayeeNumber")
.mvcMatchers(HttpMethod.DELETE, "/payee-mapping/{id}").hasRole("ManageMSPPayeeNumber")
.mvcMatchers(HttpMethod.GET, "/user/**").fullyAuthenticated()
.mvcMatchers("/*").denyAll()
.and()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -28,6 +29,7 @@ public class SecurityUtil {
public static final String CLAIM_USERNAME = "preferred_username";
private static final String CLAIM_SUB = "sub"; // the Subject claim identifies the principal that is the subject of the JWT
public static final String CLAIM_ORGANIZATION = "org_details";
public static final String CLAIM_CLIENT_ID = "clientId";

private static final String ORGANIZATION_ID = "id";

Expand All @@ -37,6 +39,8 @@ public class SecurityUtil {

private static final String UNKNOWN_ROLE = "UNKNOWN";

private static final String SERVICE_SUFFIX = "-SERVICE";

private static String KEYCLOAK_CLIENT;

private static SecurityProperties securityProperties;
Expand Down Expand Up @@ -167,5 +171,17 @@ public static List<String> loadPermissions(Jwt jwt, Map<String, List<String>> ro

return permissions;
}

/**
* Checks if the jwt is generated for a service account.
* @param jwt
* @return
*/
public static Boolean isServiceAccount(Jwt jwt) {
// This is just a rough check to see if the client is a service account
// It's good enough to detect any current MSP Direct API clients
return StringUtils.endsWith(jwt.getClaim(SecurityUtil.CLAIM_CLIENT_ID), SERVICE_SUFFIX);

}

}

0 comments on commit 9d7afef

Please sign in to comment.