Search in sources :

Example 1 with NoPrivateKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey in project TumCampusApp by TCA-Team.

the class AuthenticationManager method generatePrivateKey.

/**
 * Gets private key from preferences or generates one.
 *
 * @return true if a private key is present
 */
public boolean generatePrivateKey(ChatMember member) {
    // Try to retrieve private key
    try {
        // Try to get the private key
        this.getPrivateKeyString();
        // Reupload it in the case it was not yet transmitted to the server
        this.uploadKey(this.getPublicKeyString(), member);
        // If we already have one don't create a new one
        return true;
    } catch (NoPrivateKey | NoPublicKey e) {
    // NOPMD
    // Otherwise catch a not existing private key exception and proceed generation
    }
    // Something went wrong, generate a new pair
    this.clearKeys();
    // If the key is not in shared preferences, a new generate key-pair
    KeyPair keyPair = generateKeyPair();
    // In order to store the preferences we need to encode them as base64 string
    String publicKeyString = keyToBase64(keyPair.getPublic().getEncoded());
    String privateKeyString = keyToBase64(keyPair.getPrivate().getEncoded());
    this.saveKeys(privateKeyString, publicKeyString);
    // New keys, need to re-upload
    this.uploadKey(publicKeyString, member);
    return true;
}
Also used : KeyPair(java.security.KeyPair) NoPublicKey(de.tum.in.tumcampusapp.api.app.exception.NoPublicKey) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey)

Example 2 with NoPrivateKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey in project TumCampusApp by TCA-Team.

the class GcmIdentificationService method sendTokenToBackend.

/**
 * Sends the registration ID to your server over HTTP, so it can use GCM/HTTP
 * or CCS to send messages to your app. Not needed for this demo since the
 * device sends upstream messages to a server that echoes back the message
 * using the 'from' address in the message.
 */
private void sendTokenToBackend(String token) {
    // Check if all parameters are present
    if (token == null || token.isEmpty()) {
        Utils.logv("Parameter missing for sending reg id");
        return;
    }
    // Try to create the message
    DeviceUploadGcmToken dgcm;
    try {
        dgcm = DeviceUploadGcmToken.Companion.getDeviceUploadGcmToken(mContext, token);
    } catch (NoPrivateKey noPrivateKey) {
        return;
    }
    TUMCabeClient.getInstance(mContext).deviceUploadGcmToken(dgcm, new Callback<TUMCabeStatus>() {

        @Override
        public void onResponse(@NonNull Call<TUMCabeStatus> call, @NonNull Response<TUMCabeStatus> response) {
            TUMCabeStatus s = response.body();
            if (response.isSuccessful() && s != null) {
                Utils.logv("Success uploading GCM registration id: " + s.getStatus());
                // Store in shared preferences the information that the GCM registration id
                // was sent to the TCA server successfully
                Utils.setSetting(mContext, Const.GCM_REG_ID_SENT_TO_SERVER, true);
            } else {
                Utils.logv("Uploading GCM registration failed...");
            }
        }

        @Override
        public void onFailure(@NonNull Call<TUMCabeStatus> call, @NonNull Throwable t) {
            Utils.log(t, "Failure uploading GCM registration id");
            Utils.setSetting(mContext, Const.GCM_REG_ID_SENT_TO_SERVER, false);
        }
    });
}
Also used : DeviceUploadGcmToken(de.tum.in.tumcampusapp.api.app.model.DeviceUploadGcmToken) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) TUMCabeStatus(de.tum.in.tumcampusapp.api.app.model.TUMCabeStatus)

Example 3 with NoPrivateKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey in project TumCampusApp by TCA-Team.

the class ChatRoomController method onRequestCard.

@Override
public void onRequestCard(Context context) {
    // Get all of the users lectures and save them as possible chat rooms
    TUMOnlineRequest<LecturesSearchRowSet> requestHandler = new TUMOnlineRequest<>(TUMOnlineConst.Companion.getLECTURES_PERSONAL(), context, true);
    Optional<LecturesSearchRowSet> lecturesList = requestHandler.fetch();
    if (lecturesList.isPresent()) {
        List<LecturesSearchRow> lectures = lecturesList.get().getLehrveranstaltungen();
        this.createLectureRooms(lectures);
    }
    // Join all new chat rooms
    if (Utils.getSettingBool(context, Const.AUTO_JOIN_NEW_ROOMS, false)) {
        List<String> newRooms = this.getNewUnjoined();
        ChatMember currentChatMember = Utils.getSetting(context, Const.CHAT_MEMBER, ChatMember.class);
        for (String roomId : newRooms) {
            // Join chat room
            try {
                ChatRoom currentChatRoom = new ChatRoom(roomId);
                currentChatRoom = TUMCabeClient.getInstance(context).createRoom(currentChatRoom, ChatVerification.Companion.getChatVerification(context, currentChatMember));
                if (currentChatRoom != null) {
                    this.join(currentChatRoom);
                }
            } catch (IOException e) {
                Utils.log(e, " - error occured while creating the room!");
            } catch (NoPrivateKey noPrivateKey) {
                return;
            }
        }
    }
    // Get all rooms that have unread messages
    List<ChatRoomDbRow> rooms = chatRoomDao.getUnreadRooms();
    if (!rooms.isEmpty()) {
        for (ChatRoomDbRow room : rooms) {
            ChatMessagesCard card = new ChatMessagesCard(context, room);
            card.apply();
        }
    }
}
Also used : ChatMember(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember) TUMOnlineRequest(de.tum.in.tumcampusapp.api.tumonline.TUMOnlineRequest) LecturesSearchRowSet(de.tum.in.tumcampusapp.component.tumui.lectures.model.LecturesSearchRowSet) LecturesSearchRow(de.tum.in.tumcampusapp.component.tumui.lectures.model.LecturesSearchRow) IOException(java.io.IOException) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatRoom(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom) ChatRoomDbRow(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoomDbRow)

Example 4 with NoPrivateKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey in project TumCampusApp by TCA-Team.

the class ChatActivity method onClick.

/**
 * When user confirms the leave dialog send the request to the server.
 *
 * @param dialog Dialog handle
 * @param which  The users choice (ignored because this is only called when the user confirms)
 */
@Override
public void onClick(DialogInterface dialog, int which) {
    // Send request to the server to remove the user from this room
    ChatVerification verification;
    try {
        verification = ChatVerification.Companion.getChatVerification(this, currentChatMember);
    } catch (NoPrivateKey noPrivateKey) {
        return;
    }
    TUMCabeClient.getInstance(this).leaveChatRoom(currentChatRoom, verification, new Callback<ChatRoom>() {

        @Override
        public void onResponse(Call<ChatRoom> call, Response<ChatRoom> room) {
            Utils.logv("Success leaving chat room: " + room.body().getName());
            new ChatRoomController(ChatActivity.this).leave(currentChatRoom);
            // Move back to ChatRoomsActivity
            Intent intent = new Intent(ChatActivity.this, ChatRoomsActivity.class);
            startActivity(intent);
        }

        @Override
        public void onFailure(Call<ChatRoom> call, Throwable t) {
            Utils.log(t, "Failure leaving chat room");
        }
    });
}
Also used : NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatRoom(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom) ChatVerification(de.tum.in.tumcampusapp.component.ui.chat.model.ChatVerification) Intent(android.content.Intent) ChatRoomController(de.tum.in.tumcampusapp.component.ui.chat.ChatRoomController)

Example 5 with NoPrivateKey

use of de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey in project TumCampusApp by TCA-Team.

the class ChatRoomsActivity method createOrJoinChatRoom.

/**
 * Creates a given chat room if it does not exist and joins it
 * Works asynchronously.
 */
private void createOrJoinChatRoom(String name) {
    if (this.currentChatMember == null) {
        Utils.showToast(this, getString(R.string.chat_not_setup));
        return;
    }
    Utils.logv("create or join chat room " + name);
    currentChatRoom = new ChatRoom(name);
    ChatVerification verification;
    try {
        verification = ChatVerification.Companion.getChatVerification(this, this.currentChatMember);
    } catch (NoPrivateKey noPrivateKey) {
        this.finish();
        return;
    }
    Callback callback = new Callback<ChatRoom>() {

        @Override
        public void onResponse(@NonNull Call<ChatRoom> call, @NonNull Response<ChatRoom> response) {
            if (!response.isSuccessful()) {
                Utils.logv("Error creating&joining chat room: " + response.message());
                return;
            }
            // The POST request is successful: go to room. API should have auto joined it
            Utils.logv("Success creating&joining chat room: " + response.body());
            currentChatRoom = response.body();
            manager.join(currentChatRoom);
            // When we show joined chat rooms open chat room directly
            if (mCurrentMode == 1) {
                moveToChatActivity();
            } else {
                // Otherwise show a nice information, that we added the room
                final List<ChatRoomAndLastMessage> rooms = manager.getAllByStatus(mCurrentMode);
                runOnUiThread(() -> {
                    chatRoomAdapter.updateRooms(rooms);
                    Utils.showToast(ChatRoomsActivity.this, R.string.joined_chat_room);
                });
            }
        }

        @Override
        public void onFailure(@NonNull Call<ChatRoom> call, @NonNull Throwable t) {
            Utils.log(t, "Failure creating/joining chat room - trying to GET it from the server");
            Utils.showToastOnUIThread(ChatRoomsActivity.this, R.string.activate_key);
        }
    };
    TUMCabeClient.getInstance(this).createRoom(currentChatRoom, verification, callback);
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatRoom(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom) ChatRoomAndLastMessage(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoomAndLastMessage) NonNull(android.support.annotation.NonNull) ChatVerification(de.tum.in.tumcampusapp.component.ui.chat.model.ChatVerification)

Aggregations

NoPrivateKey (de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey)10 ChatMember (de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember)4 ChatRoom (de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom)4 ChatVerification (de.tum.in.tumcampusapp.component.ui.chat.model.ChatVerification)3 IOException (java.io.IOException)3 AuthenticationManager (de.tum.in.tumcampusapp.api.app.AuthenticationManager)2 TUMCabeStatus (de.tum.in.tumcampusapp.api.app.model.TUMCabeStatus)2 ChatRoomController (de.tum.in.tumcampusapp.component.ui.chat.ChatRoomController)2 Context (android.content.Context)1 Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)1 NoPublicKey (de.tum.in.tumcampusapp.api.app.exception.NoPublicKey)1 DeviceRegister (de.tum.in.tumcampusapp.api.app.model.DeviceRegister)1 DeviceUploadGcmToken (de.tum.in.tumcampusapp.api.app.model.DeviceUploadGcmToken)1 TUMOnlineRequest (de.tum.in.tumcampusapp.api.tumonline.TUMOnlineRequest)1 LecturesSearchRow (de.tum.in.tumcampusapp.component.tumui.lectures.model.LecturesSearchRow)1 LecturesSearchRowSet (de.tum.in.tumcampusapp.component.tumui.lectures.model.LecturesSearchRowSet)1 ChatMessageViewModel (de.tum.in.tumcampusapp.component.ui.chat.ChatMessageViewModel)1 ChatMessage (de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage)1 ChatRoomAndLastMessage (de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoomAndLastMessage)1