Skip to content

Commit

Permalink
Made google version 2 optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Hein committed Feb 21, 2024
1 parent 8b3dc1c commit 664200c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ public interface AdministrationService {
*/
OrganisationData readOrganisation(TenantIdentifier identifier);



Optional<OrganisationData> readOrganisationDoesNotThrow(TenantIdentifier identifier);

/**
* Updates an organisation. Fails if organisation does not exist.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,6 @@ public OrganisationData readOrganisation(TenantIdentifier identifier) {
.findAny()
.orElseThrow(NoSuchOrganisation::new);
}
@Override
public Optional<OrganisationData> readOrganisationDoesNotThrow(TenantIdentifier identifier) {
IMap<String, OrganisationData> orgMap = hazelcastInstance.getMap(hazelcastConf.getPrefix() + ORG_POSTFIX);
return orgMap.values()
.stream()
.filter(entry -> identifier.matches(entry.getProviderConfiguration()))
.findAny();
}


@Override
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ services:
- MITID_AUTHORITY_URL=https://brokertest.signaturgruppen.dk/op
- KEYSTORE_PASS=fafnir
- KEY_PASS=fafnir
- GOOGLE_VERSION2_ENABLED=true
volumes:
- ./fafnir-storage:/var/lib/fafnir

Expand Down
25 changes: 13 additions & 12 deletions sso/src/main/java/dk/acto/fafnir/sso/provider/GoogleProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public String authenticate() {

@Override
public AuthenticationResult callback(TokenCredentials data) {
boolean version2Enabled = Boolean.parseBoolean(System.getenv("GOOGLE_VERSION2_ENABLED"));

var token = Try.of(() -> googleOauth.getAccessToken(data.getCode()))
.onFailure(x -> log.error("Authentication failed", x))
.getOrNull();
Expand All @@ -50,26 +52,25 @@ public AuthenticationResult callback(TokenCredentials data) {
.name(displayName)
.build();

var orgOptional = administrationService.readOrganisationDoesNotThrow(
test -> getMetaData().getProviderId().equals(test.getProviderId()) &&
(providerValue.equals(test.getValues().get("Organisation Domain")) || "true".equals(test.getValues().get("Catchall Organisation")))
);

if (orgOptional.isPresent()) {
var orgActual = orgOptional.get();
var claimsActual = ClaimData.empty();
var jwt = tokenFactory.generateToken(subjectActual, orgActual, claimsActual, getMetaData(), providerValue);
return AuthenticationResult.success(jwt);
} else {
if (version2Enabled) {
var fafnirUser = FafnirUser.builder()
.data(subjectActual)
.organisationId(providerValue)
.organisationName(displayName)
.provider("google")
.build();

var jwt = tokenFactory.generateToken(fafnirUser);
return AuthenticationResult.success(jwt);

} else {
var orgActual = administrationService.readOrganisation(
test -> getMetaData().getProviderId().equals(test.getProviderId()) &&
(providerValue.equals(test.getValues().get("Organisation Domain")) || "true".equals(test.getValues().get("Catchall Organisation")))
);
var claimsActual = ClaimData.empty();
var jwt = tokenFactory.generateToken(subjectActual, orgActual, claimsActual, getMetaData(), providerValue);

return AuthenticationResult.success(jwt);
}
}

Expand Down

0 comments on commit 664200c

Please sign in to comment.