Search in sources :

Example 1 with InvalidPoPTokenException

use of com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException in project popstellar by dedis.

the class RollCallHandler method handleCloseRollCall.

/**
 * Process a CloseRollCall message.
 *
 * @param context the HandlerContext of the message
 * @param closeRollCall the message that was received
 */
public static void handleCloseRollCall(HandlerContext context, CloseRollCall closeRollCall) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Log.d(TAG, "handleCloseRollCall: " + channel);
    String updateId = closeRollCall.getUpdateId();
    String closes = closeRollCall.getCloses();
    Optional<RollCall> rollCallOptional = lao.getRollCall(closes);
    if (!rollCallOptional.isPresent()) {
        Log.w(TAG, "Cannot find roll call to close : " + closes);
        throw new InvalidDataException(closeRollCall, "close id", closes);
    }
    RollCall rollCall = rollCallOptional.get();
    rollCall.setEnd(closeRollCall.getClosedAt());
    rollCall.setId(updateId);
    rollCall.getAttendees().addAll(closeRollCall.getAttendees());
    rollCall.setState(EventState.CLOSED);
    lao.updateRollCall(closes, rollCall);
    lao.updateWitnessMessage(messageId, closeRollCallWitnessMessage(messageId, rollCall));
    // Subscribe to the social media channels
    try {
        PoPToken token = context.getKeyManager().getValidPoPToken(lao, rollCall);
        context.getMessageSender().subscribe(channel.subChannel("social").subChannel(token.getPublicKey().getEncoded())).subscribe();
    } catch (InvalidPoPTokenException e) {
        Log.i(TAG, "Received a close roll-call that you did not attend");
    } catch (KeyException e) {
        Log.e(TAG, "Could not retrieve your PoP Token to subscribe you to your social media channel", e);
    }
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) InvalidPoPTokenException(com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidDataException(com.github.dedis.popstellar.utility.error.InvalidDataException) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) Lao(com.github.dedis.popstellar.model.objects.Lao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 2 with InvalidPoPTokenException

use of com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException in project popstellar by dedis.

the class KeyManagerTest method popTokenRetrievingFailsWhenWalletFails.

@Test
public void popTokenRetrievingFailsWhenWalletFails() throws KeyException {
    PoPToken token = Base64DataUtils.generatePoPToken();
    // create LAO and RollCall
    Lao lao = new Lao("lao", Base64DataUtils.generatePublicKey(), 54213424);
    RollCall rollCall = new RollCall(lao.getId(), 5421364, "rollcall");
    lao.updateRollCall(rollCall.getId(), rollCall);
    KeyManager manager = new KeyManager(androidKeysetManager, wallet);
    // Test with every possible errors
    when(wallet.recoverKey(any(), any(), any())).thenThrow(new KeyGenerationException(new GeneralSecurityException()));
    assertThrows(KeyGenerationException.class, () -> manager.getValidPoPToken(lao));
    verify(wallet, times(1)).recoverKey(eq(lao.getId()), eq(rollCall.getId()), any());
    reset(wallet);
    when(wallet.recoverKey(any(), any(), any())).thenThrow(new UninitializedWalletException());
    assertThrows(UninitializedWalletException.class, () -> manager.getValidPoPToken(lao));
    verify(wallet, times(1)).recoverKey(eq(lao.getId()), eq(rollCall.getId()), any());
    reset(wallet);
    when(wallet.recoverKey(any(), any(), any())).thenThrow(new InvalidPoPTokenException(token));
    assertThrows(InvalidPoPTokenException.class, () -> manager.getValidPoPToken(lao));
    verify(wallet, times(1)).recoverKey(eq(lao.getId()), eq(rollCall.getId()), any());
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) InvalidPoPTokenException(com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException) GeneralSecurityException(java.security.GeneralSecurityException) RollCall(com.github.dedis.popstellar.model.objects.RollCall) KeyGenerationException(com.github.dedis.popstellar.utility.error.keys.KeyGenerationException) Lao(com.github.dedis.popstellar.model.objects.Lao) UninitializedWalletException(com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException) Test(org.junit.Test) HiltAndroidTest(dagger.hilt.android.testing.HiltAndroidTest)

Aggregations

Lao (com.github.dedis.popstellar.model.objects.Lao)2 RollCall (com.github.dedis.popstellar.model.objects.RollCall)2 PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)2 InvalidPoPTokenException (com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException)2 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)1 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)1 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)1 Channel (com.github.dedis.popstellar.model.objects.Channel)1 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)1 LAORepository (com.github.dedis.popstellar.repository.LAORepository)1 InvalidDataException (com.github.dedis.popstellar.utility.error.InvalidDataException)1 KeyException (com.github.dedis.popstellar.utility.error.keys.KeyException)1 KeyGenerationException (com.github.dedis.popstellar.utility.error.keys.KeyGenerationException)1 UninitializedWalletException (com.github.dedis.popstellar.utility.error.keys.UninitializedWalletException)1 HiltAndroidTest (dagger.hilt.android.testing.HiltAndroidTest)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Test (org.junit.Test)1