Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release/1.7.x' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	proj-xcode/PowerAuth2IntegrationTests/PowerAuthTokenTests.m

Fix #471
  • Loading branch information
hvge committed Aug 17, 2022
2 parents 6e68363 + 20cf249 commit 5e65b93
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.test.platform.app.InstrumentationRegistry;

import io.getlime.security.powerauth.integration.support.client.PowerAuthClientFactory;
Expand All @@ -45,7 +46,7 @@ public class PowerAuthTestHelper {
private final @NonNull PowerAuthServerApi serverApi;
private final @NonNull RandomGenerator randomGenerator;

private final @NonNull PowerAuthSDK sharedSdk;
private @NonNull PowerAuthSDK sharedSdk;
private final @NonNull PowerAuthConfiguration sharedConfiguration;
private final @NonNull PowerAuthKeychainConfiguration sharedKeychainConfiguration;
private final @NonNull PowerAuthClientConfiguration sharedClientConfiguration;
Expand Down Expand Up @@ -439,6 +440,29 @@ private PowerAuthTestHelper(
return sdk;
}

/**
* Re-create a new instance of shared {@link PowerAuthSDK} with provided configurations.
* @param configuration If null, then shared configuration will be used.
* @param clientConfiguration If null, then shared client configuration will be used.
* @param keychainConfiguration If null, then shared keychain configuration will be used.
* @return New instance of {@link PowerAuthSDK} that will be also used as new shared instance.
* @throws Exception In case that instance creation failed.
*/
public @NonNull PowerAuthSDK reCreateSdk(
@Nullable PowerAuthConfiguration configuration,
@Nullable PowerAuthClientConfiguration clientConfiguration,
@Nullable PowerAuthKeychainConfiguration keychainConfiguration) throws Exception {
final PowerAuthConfiguration newConfiguration = configuration != null ? configuration : getSharedPowerAuthConfiguration();
final PowerAuthClientConfiguration newClientConfiguration = clientConfiguration != null ? clientConfiguration : getSharedPowerAuthClientConfiguration();
final PowerAuthKeychainConfiguration newKeychainConfiguration = keychainConfiguration != null ? keychainConfiguration : getSharedPowerAuthKeychainConfiguration();
final PowerAuthSDK sdk = new PowerAuthSDK.Builder(newConfiguration)
.clientConfiguration(newClientConfiguration)
.keychainConfiguration(newKeychainConfiguration)
.build(getContext());
sharedSdk = sdk;
return sdk;
}

/**
* @return Expected protocol version for HTTP headers.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ public void testCreateAndRemoveToken() throws Exception {

assertTrue(calculateAndValidateTokenDigest(token2, SignatureType.POSSESSION_KNOWLEDGE));

// Try to re-create SDK. This simulates application restart.
powerAuthSDK = testHelper.reCreateSdk(null, null, null);
tokenStore = powerAuthSDK.getTokenStore();

// Now ask for the same tokens
assertTrue(tokenStore.hasLocalToken(context, TOKEN_NAME_POSSESSION));
token1 = requestAccessToken(TOKEN_NAME_POSSESSION, activationHelper.getPossessionAuthentication(), true);
token2 = requestAccessToken(TOKEN_NAME_POSSESSION_KNOWLEDGE, activationHelper.getValidAuthentication(), true);
assertNotNull(token1);
assertNotNull(token2);
assertTrue(calculateAndValidateTokenDigest(token1, SignatureType.POSSESSION));
assertTrue(calculateAndValidateTokenDigest(token2, SignatureType.POSSESSION_KNOWLEDGE));

// Invalid password
assertFalse(tokenStore.hasLocalToken(context, TOKEN_NAME_OTHER));
assertNull(requestAccessToken(TOKEN_NAME_OTHER, activationHelper.getInvalidAuthentication(), false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ public boolean equals(Object anObject) {
final byte[] secret = Base64.decode(components[1], Base64.NO_WRAP);
final String name = new String(Base64.decode(components[2], Base64.NO_WRAP));
final String activationId;
if (components.length == 4) {
if (components.length >= 4) {
activationId = components[3];
} else {
activationId = null;
}
final int authenticationFactors;
if (components.length == 5) {
authenticationFactors = Integer.parseInt(components[0]);
if (components.length >= 5) {
authenticationFactors = Integer.parseInt(components[4]);
} else {
authenticationFactors = 0;
}
Expand Down
12 changes: 12 additions & 0 deletions proj-xcode/PowerAuth2IntegrationTests/PowerAuthTokenTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ - (void) testBasicTokenOperations
result = [_helper validateTokenHeader:header activationId:activationData.activationId expectedResult:YES];
XCTAssertTrue(result);

// Now ask for the same token
XCTAssertTrue([tokenStore hasLocalTokenWithName:@"MyPreciousToken"]);
PowerAuthToken * tokenAfterRestart = [AsyncHelper synchronizeAsynchronousBlock:^(AsyncHelper *waiting) {
[tokenStore requestAccessTokenWithName:@"MyPreciousToken" authentication:possession completion:^(PowerAuthToken * token, NSError * error) {
[waiting reportCompletion:token];
}];
}];
// And try to generate header
header = [tokenAfterRestart generateHeader];
result = [_helper validateTokenHeader:header activationId:activationData.activationId expectedResult:YES];
XCTAssertTrue(result);

// Remove token
BOOL tokenRemoved = [[AsyncHelper synchronizeAsynchronousBlock:^(AsyncHelper *waiting) {
[tokenStore removeAccessTokenWithName:@"MyPreciousToken" completion:^(BOOL removed, NSError * _Nullable error) {
Expand Down

0 comments on commit 5e65b93

Please sign in to comment.