Skip to content

Commit

Permalink
Merge pull request #545 from /issues/544-extract-methods
Browse files Browse the repository at this point in the history
Fix #544: Extract methods commitActivation and fetchUserClaimsForUserId in ActivationService
  • Loading branch information
banterCZ authored Aug 19, 2024
2 parents d4f9ad0 + bf309b6 commit e3fb545
Showing 1 changed file with 42 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,13 @@ public ActivationLayer1Response createActivation(ActivationLayer1Request request
final String activationId = response.getActivationId();
final String applicationId = response.getApplicationId();

// Process user info
Map<String, Object> userInfo = null;
if (userInfoProvider != null) {
final UserInfoContext userInfoContext = UserInfoContext.builder()
.stage(UserInfoStage.ACTIVATION_PROCESS_ACTIVATION_CODE)
.userId(userId)
.activationId(activationId)
.applicationId(applicationId)
.build();
if (userInfoProvider.shouldReturnUserInfo(userInfoContext)) {
userInfo = userInfoProvider.fetchUserClaimsForUserId(userInfoContext);
}
}
final UserInfoContext userInfoContext = UserInfoContext.builder()
.stage(UserInfoStage.ACTIVATION_PROCESS_ACTIVATION_CODE)
.userId(userId)
.activationId(activationId)
.applicationId(applicationId)
.build();
final Map<String, Object> userInfo = processUserInfo(userInfoContext);

Map<String, Object> processedCustomAttributes = customAttributes;
// In case a custom activation provider is enabled, process custom attributes and save any flags
Expand All @@ -215,15 +209,7 @@ public ActivationLayer1Response createActivation(ActivationLayer1Request request
} else {
// Otherwise check if activation should be committed instantly and if yes, perform commit.
if (activationProvider != null && activationProvider.shouldAutoCommitActivation(identity, customAttributes, activationId, userId, applicationId, ActivationType.CODE, context)) {
final CommitActivationRequest commitRequest = new CommitActivationRequest();
commitRequest.setActivationId(activationId);
commitRequest.setExternalUserId(null);
final CommitActivationResponse commitResponse = powerAuthClient.commitActivation(
commitRequest,
httpCustomizationService.getQueryParams(),
httpCustomizationService.getHttpHeaders()
);

final CommitActivationResponse commitResponse = commitActivation(activationId);
notifyActivationCommit = commitResponse.isActivated();
}
}
Expand Down Expand Up @@ -300,19 +286,13 @@ public ActivationLayer1Response createActivation(ActivationLayer1Request request
final String activationId = response.getActivationId();
final String applicationId = response.getApplicationId();

// Process user info
Map<String, Object> userInfo = null;
if (userInfoProvider != null) {
final UserInfoContext userInfoContext = UserInfoContext.builder()
.stage(UserInfoStage.ACTIVATION_PROCESS_CUSTOM)
.userId(userId)
.activationId(activationId)
.applicationId(applicationId)
.build();
if (userInfoProvider.shouldReturnUserInfo(userInfoContext)) {
userInfo = userInfoProvider.fetchUserClaimsForUserId(userInfoContext);
}
}
final UserInfoContext userInfoContext = UserInfoContext.builder()
.stage(UserInfoStage.ACTIVATION_PROCESS_CUSTOM)
.userId(userId)
.activationId(activationId)
.applicationId(applicationId)
.build();
final Map<String, Object> userInfo = processUserInfo(userInfoContext);

// Process custom attributes using a custom logic
final Map<String, Object> processedCustomAttributes = activationProvider.processCustomActivationAttributes(customAttributes, activationId, userId, applicationId, ActivationType.CUSTOM, context);
Expand All @@ -332,14 +312,7 @@ public ActivationLayer1Response createActivation(ActivationLayer1Request request

// Check if activation should be committed instantly and if yes, perform commit
if (activationProvider.shouldAutoCommitActivation(identity, customAttributes, activationId, userId, applicationId, ActivationType.CUSTOM, context)) {
final CommitActivationRequest commitRequest = new CommitActivationRequest();
commitRequest.setActivationId(activationId);
commitRequest.setExternalUserId(null);
final CommitActivationResponse commitResponse = powerAuthClient.commitActivation(
commitRequest,
httpCustomizationService.getQueryParams(),
httpCustomizationService.getHttpHeaders()
);
final CommitActivationResponse commitResponse = commitActivation(activationId);
if (commitResponse.isActivated()) {
activationProvider.activationWasCommitted(identity, customAttributes, activationId, userId, applicationId, ActivationType.CUSTOM, context);
}
Expand Down Expand Up @@ -409,19 +382,13 @@ public ActivationLayer1Response createActivation(ActivationLayer1Request request
final String activationId = response.getActivationId();
final String applicationId = response.getApplicationId();

// Process user info
Map<String, Object> userInfo = null;
if (userInfoProvider != null) {
final UserInfoContext userInfoContext = UserInfoContext.builder()
.stage(UserInfoStage.ACTIVATION_PROCESS_RECOVERY)
.userId(userId)
.activationId(activationId)
.applicationId(applicationId)
.build();
if (userInfoProvider.shouldReturnUserInfo(userInfoContext)) {
userInfo = userInfoProvider.fetchUserClaimsForUserId(userInfoContext);
}
}
final UserInfoContext userInfoContext = UserInfoContext.builder()
.stage(UserInfoStage.ACTIVATION_PROCESS_RECOVERY)
.userId(userId)
.activationId(activationId)
.applicationId(applicationId)
.build();
final Map<String, Object> userInfo = processUserInfo(userInfoContext);

Map<String, Object> processedCustomAttributes = customAttributes;
// In case a custom activation provider is enabled, process custom attributes and save any flags
Expand All @@ -442,14 +409,7 @@ public ActivationLayer1Response createActivation(ActivationLayer1Request request

// Automatically commit activation by default, the optional activation provider can override automatic commit
if (activationProvider == null || activationProvider.shouldAutoCommitActivation(identity, customAttributes, activationId, userId, applicationId, ActivationType.RECOVERY, context)) {
final CommitActivationRequest commitRequest = new CommitActivationRequest();
commitRequest.setActivationId(activationId);
commitRequest.setExternalUserId(null);
final CommitActivationResponse commitResponse = powerAuthClient.commitActivation(
commitRequest,
httpCustomizationService.getQueryParams(),
httpCustomizationService.getHttpHeaders()
);
final CommitActivationResponse commitResponse = commitActivation(activationId);
if (activationProvider != null && commitResponse.isActivated()) {
activationProvider.activationWasCommitted(identity, customAttributes, activationId, userId, applicationId, ActivationType.RECOVERY, context);
}
Expand Down Expand Up @@ -484,6 +444,24 @@ public ActivationLayer1Response createActivation(ActivationLayer1Request request
}
}

private CommitActivationResponse commitActivation(final String activationId) throws PowerAuthClientException {
final CommitActivationRequest commitRequest = new CommitActivationRequest();
commitRequest.setActivationId(activationId);
commitRequest.setExternalUserId(null);
return powerAuthClient.commitActivation(
commitRequest,
httpCustomizationService.getQueryParams(),
httpCustomizationService.getHttpHeaders()
);
}

private Map<String, Object> processUserInfo(final UserInfoContext userInfoContext) {
if (userInfoProvider != null && userInfoProvider.shouldReturnUserInfo(userInfoContext)) {
return userInfoProvider.fetchUserClaimsForUserId(userInfoContext);
}
return null;
}

private boolean shouldGenerateRecoveryCodes(final Map<String, String> identity, final Map<String, Object> customAttributes, final Map<String, Object> context) throws PowerAuthActivationException {
if (activationProvider == null) {
return true;
Expand Down

0 comments on commit e3fb545

Please sign in to comment.