-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/943_tenant_via_iss' into multitenancy_main
- Loading branch information
Showing
11 changed files
with
4,684 additions
and
19 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
51 changes: 51 additions & 0 deletions
51
backend/src/main/java/ch/puzzle/okr/security/helper/ClaimHelper.java
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,51 @@ | ||
package ch.puzzle.okr.security.helper; | ||
|
||
import com.nimbusds.jwt.JWTClaimsSet; | ||
|
||
import java.text.ParseException; | ||
import java.util.Optional; | ||
|
||
import static ch.puzzle.okr.security.JwtHelper.CLAIM_ISS; | ||
import static ch.puzzle.okr.security.JwtHelper.CLAIM_TENANT; | ||
import static ch.puzzle.okr.security.helper.JwtStatusLogger.logStatus; | ||
import static ch.puzzle.okr.security.helper.UrlHelper.extractTenantFromIssUrl; | ||
|
||
public class ClaimHelper { | ||
|
||
public Optional<String> getTenantFromClaimsSetUsingClaimTenant(JWTClaimsSet claimSet) { | ||
try { | ||
return getTenant(claimSet); | ||
} catch (ParseException e) { | ||
logStatus(CLAIM_TENANT, claimSet, e); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private Optional<String> getTenant(JWTClaimsSet claimSet) throws ParseException { | ||
String tenant = claimSet.getStringClaim(CLAIM_TENANT); | ||
logStatus(CLAIM_TENANT, claimSet, tenant); | ||
return Optional.ofNullable(tenant); | ||
} | ||
|
||
public Optional<String> getTenantFromClaimsSetUsingClaimIss(JWTClaimsSet claimSet) { | ||
try { | ||
return getIssUrl(claimSet).flatMap(url -> getTenant(claimSet, url)); | ||
} catch (ParseException e) { | ||
logStatus(CLAIM_ISS, claimSet, e); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private Optional<String> getIssUrl(JWTClaimsSet claimSet) throws ParseException { | ||
String issUrl = claimSet.getStringClaim(CLAIM_ISS); | ||
logStatus(CLAIM_ISS, claimSet, issUrl); | ||
return Optional.ofNullable(issUrl); | ||
} | ||
|
||
private Optional<String> getTenant(JWTClaimsSet claimSet, String issUrl) { | ||
Optional<String> tenant = extractTenantFromIssUrl(issUrl); | ||
logStatus(CLAIM_ISS, claimSet, tenant.isPresent()); | ||
return tenant; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
backend/src/main/java/ch/puzzle/okr/security/helper/JwtStatusLogger.java
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 @@ | ||
package ch.puzzle.okr.security.helper; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.text.ParseException; | ||
|
||
public class JwtStatusLogger { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ClaimHelper.class); | ||
|
||
public static void logStatus(String claim, Object context, String result) { | ||
logStatus(claim, context, result != null); | ||
} | ||
|
||
public static void logStatus(String claim, Object context, boolean isOk) { | ||
if (isOk) { | ||
logger.info("Tenant: get claim '{}' from {}{}", claim, context.getClass().getSimpleName(), | ||
statusToSymbol(isOk)); | ||
} else { | ||
logger.warn("Tenant: get claim '{}' from {}{}", claim, context.getClass().getSimpleName(), | ||
statusToSymbol(isOk)); | ||
} | ||
} | ||
|
||
public static void logStatus(String claim, Object context, ParseException e) { | ||
logger.warn("Tenant: get claim '{}' from {}{}", claim, context.getClass().getSimpleName(), | ||
statusToSymbol(false), e); | ||
} | ||
|
||
private static String statusToSymbol(boolean isOk) { | ||
return isOk ? " | OK" : " | FAILED"; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
backend/src/main/java/ch/puzzle/okr/security/helper/TokenHelper.java
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 @@ | ||
package ch.puzzle.okr.security.helper; | ||
|
||
import org.springframework.security.oauth2.jwt.Jwt; | ||
|
||
import java.util.Optional; | ||
|
||
import static ch.puzzle.okr.security.JwtHelper.CLAIM_ISS; | ||
import static ch.puzzle.okr.security.JwtHelper.CLAIM_TENANT; | ||
import static ch.puzzle.okr.security.helper.JwtStatusLogger.logStatus; | ||
import static ch.puzzle.okr.security.helper.UrlHelper.extractTenantFromIssUrl; | ||
|
||
public class TokenHelper { | ||
|
||
public Optional<String> getTenantFromTokenUsingClaimTenant(Jwt token) { | ||
return getTenant(token); | ||
} | ||
|
||
private Optional<String> getTenant(Jwt token) { | ||
String tenant = token.getClaimAsString(CLAIM_TENANT); // can return null | ||
logStatus(CLAIM_TENANT, token, tenant); | ||
return Optional.ofNullable(tenant); | ||
} | ||
|
||
public Optional<String> getTenantFromTokenUsingClaimIss(Jwt token) { | ||
return getIssUrl(token).flatMap(url -> getTenant(token, url)); | ||
} | ||
|
||
private Optional<String> getIssUrl(Jwt token) { | ||
String issUrl = token.getClaimAsString(CLAIM_ISS); // can return null | ||
logStatus(CLAIM_ISS, token, issUrl); | ||
return Optional.ofNullable(issUrl); | ||
} | ||
|
||
private Optional<String> getTenant(Jwt token, String issUrl) { | ||
Optional<String> tenant = extractTenantFromIssUrl(issUrl); | ||
logStatus(CLAIM_ISS, token, tenant.isPresent()); | ||
return tenant; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/ch/puzzle/okr/security/helper/UrlHelper.java
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,14 @@ | ||
package ch.puzzle.okr.security.helper; | ||
|
||
import java.util.Optional; | ||
|
||
public class UrlHelper { | ||
|
||
public static Optional<String> extractTenantFromIssUrl(String issUrl) { | ||
if (issUrl == null) | ||
return Optional.empty(); | ||
String[] issUrlParts = issUrl.split("/"); | ||
String tenant = issUrlParts[issUrlParts.length - 1]; | ||
return Optional.of(tenant); | ||
} | ||
} |
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
Oops, something went wrong.