Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset User Account #228

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions backend/src/main/java/org/cryptomator/hub/api/UsersResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import jakarta.validation.Valid;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.cryptomator.hub.entities.AccessToken;
import org.cryptomator.hub.entities.Device;
import org.cryptomator.hub.entities.User;
import org.eclipse.microprofile.jwt.JsonWebToken;
Expand Down Expand Up @@ -76,6 +78,24 @@ public UserDto getMe(@QueryParam("withDevices") boolean withDevices) {
return new UserDto(user.id, user.name, user.pictureUrl, user.email, devices, user.publicKey, user.privateKey, user.setupCode);
}

@POST
@Path("/me/reset")
@RolesAllowed("user")
@NoCache
@Transactional
@Operation(summary = "resets the user account")
@APIResponse(responseCode = "204", description = "deleted keys, devices and access permissions")
public Response resetMe() {
User user = User.findById(jwt.getSubject());
user.publicKey = null;
user.privateKey = null;
user.setupCode = null;
user.persist();
Device.deleteByOwner(user.id);
AccessToken.deleteByUser(user.id);
return Response.noContent().build();
}

@GET
@Path("/")
@RolesAllowed("user")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cryptomator.hub.entities;

import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
import io.quarkus.panache.common.Parameters;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
Expand All @@ -9,6 +10,7 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapsId;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.NoResultException;
import jakarta.persistence.Table;

Expand All @@ -17,6 +19,7 @@
import java.util.UUID;

@Entity
@NamedQuery(name = "AccessToken.deleteByUser", query = "DELETE FROM AccessToken a WHERE a.id.userId = :userId")
@Table(name = "access_token")
public class AccessToken extends PanacheEntityBase {

Expand Down Expand Up @@ -81,6 +84,11 @@ public static AccessToken unlock(UUID vaultId, String userId) {
}
}


public static void deleteByUser(String userId) {
delete("#AccessToken.deleteByUser", Parameters.with("userId", userId));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@NamedQuery(name = "Device.findByIdAndOwner",
query = "SELECT d FROM Device d WHERE d.id = :deviceId AND d.owner.id = :userId"
)
@NamedQuery(name = "Device.deleteByOwner", query = "DELETE FROM Device d WHERE d.owner.id = :userId")
@NamedQuery(name = "Device.allInList",
query = """
SELECT d
Expand Down Expand Up @@ -99,4 +100,9 @@ public static Device findByIdAndUser(String deviceId, String userId) throws NoRe
public static Stream<Device> findAllInList(List<String> ids) {
return find("#Device.allInList", Parameters.with("ids", ids)).stream();
}

public static void deleteByOwner(String userId) {
delete("#Device.deleteByOwner", Parameters.with("userId", userId));
}

}
4 changes: 4 additions & 0 deletions frontend/src/common/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ class UserService {
return axiosAuth.get<UserDto>(`/users/me?withDevices=${withDevices}`).then(response => UserDto.copy(response.data));
}

public async resetMe(): Promise<void> {
return axiosAuth.post('/users/me/reset');
}

public async listAll(): Promise<UserDto[]> {
return axiosAuth.get<UserDto[]>('/users/').then(response => {
return response.data.map(dto => UserDto.copy(dto));
Expand Down
22 changes: 22 additions & 0 deletions frontend/src/common/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,28 @@ export class BrowserKeys {
});
}

/**
* Deletes the key pair for the given user.
* @returns a promise resolving on success
*/
public static async delete(userId: string): Promise<void> {
const db = await new Promise<IDBDatabase>((resolve, reject) => {
const req = indexedDB.open('hub');
req.onsuccess = evt => { resolve(req.result); };
req.onerror = evt => { reject(req.error); };
req.onupgradeneeded = evt => { req.result.createObjectStore('keys'); };
});
return new Promise<void>((resolve, reject) => {
const transaction = db.transaction('keys', 'readwrite');
const keyStore = transaction.objectStore('keys');
const query = keyStore.delete(userId);
query.onsuccess = evt => { resolve(); };
query.onerror = evt => { reject(query.error); };
}).finally(() => {
db.close();
});
}

/**
* Stores the key pair in the browser's IndexedDB. See https://www.w3.org/TR/WebCryptoAPI/#concepts-key-storage
* @returns a promise that will resolve if the key pair has been saved
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/components/ResetUserAccountDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ import { Dialog, DialogOverlay, DialogPanel, DialogTitle, TransitionChild, Trans
import { ExclamationTriangleIcon } from '@heroicons/vue/24/outline';
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import backend, { UserDto } from '../common/backend';
import { BrowserKeys } from '../common/crypto';
import router from '../router';

const { t } = useI18n({ useScope: 'global' });

Expand All @@ -69,6 +72,10 @@ defineExpose({
show
});

const props = defineProps<{
me : UserDto
}>();

async function show() {
open.value = true;
}
Expand All @@ -77,7 +84,9 @@ async function resetUserAccount() {
onResetError.value = null;
try {
processing.value = true;
// TODO
await backend.users.resetMe();
await BrowserKeys.delete(props.me.id);
router.go(0); // reload current page
} catch (error) {
console.error('Resetting user account failed.', error);
onResetError.value = error instanceof Error ? error : new Error('Unknown Error');
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/SetupUserKey.vue
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
</div>
</div>

<ResetUserAccountDialog v-if="resettingUserAccount" ref="resetUserAccountDialog" @close="resettingUserAccount = false" />
<ResetUserAccountDialog v-if="resettingUserAccount" ref="resetUserAccountDialog" :me="user" @close="resettingUserAccount = false" />
</template>

<script setup lang="ts">
Expand Down
Loading