Skip to content

Commit

Permalink
Bridge+Dart: implement resetCredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
inetic committed Nov 15, 2024
1 parent c169baf commit c594c45
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
20 changes: 15 additions & 5 deletions bindings/dart/lib/ouisync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class Session {
// native code.
// [channelName] is the name of the MethodChannel to be used, equally named channel
// must be created and set up to listen to the commands in the native code.
static Future<Session> createChanneled(String channelName, [void Function()? onClose]) async {
static Future<Session> createChanneled(String channelName,
[void Function()? onClose]) async {
final client = ChannelClient(channelName, onClose);
await client.initialized;
return Session._(client);
Expand Down Expand Up @@ -238,9 +239,9 @@ class Session {
void closeSync() {
final client = _client;
if (client is DirectClient) {
client.closeSync();
client.closeSync();
} else {
throw "closeSync is currently only implemented for DirectClient";
throw "closeSync is currently only implemented for DirectClient";
}
}
}
Expand Down Expand Up @@ -431,6 +432,15 @@ class Repository {
'credentials': credentials,
});

/// Like `setCredentials` but use the `ShareToken` and reset any values
/// encrypted with local secrets to random values. Currently that is only the
/// writer ID.
Future<void> resetCredentials(ShareToken token) =>
_client.invoke<void>('repository_reset_credentials', {
'repository': _handle,
'token': token.toString(),
});

Future<AccessMode> get accessMode {
return _client
.invoke<int>('repository_access_mode', _handle)
Expand Down Expand Up @@ -937,9 +947,9 @@ class File {
final client = _client;

if (client is DirectClient) {
await client.copyToRawFd(_handle, fd);
await client.copyToRawFd(_handle, fd);
} else {
throw "copyToRawFd is currently implemented only for DirectClient";
throw "copyToRawFd is currently implemented only for DirectClient";
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions ffi/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};
use async_trait::async_trait;
use ouisync_bridge::transport::SessionContext;
use ouisync_lib::{crypto::cipher::SecretKey, PeerAddr};
use ouisync_lib::{crypto::cipher::SecretKey, Credentials, PeerAddr};
use std::{net::SocketAddr, sync::Arc};

#[derive(Clone)]
Expand Down Expand Up @@ -106,9 +106,18 @@ impl ouisync_bridge::transport::Handler for Handler {
Request::RepositorySetCredentials {
repository,
credentials,
} => repository::set_credentials(&self.state, repository, credentials.into())
.await?
.into(),
} => {
let cv: Vec<u8> = credentials.into();
repository::set_credentials(&self.state, repository, Credentials::decode(&cv)?)
.await?
.into()
}
Request::RepositoryResetCredentials { repository, token } => {
let new_credentials = Credentials::with_random_writer_id(token.into_secrets());
repository::set_credentials(&self.state, repository, new_credentials)
.await?
.into()
}
Request::RepositorySetAccessMode {
repository,
access_mode,
Expand Down
4 changes: 4 additions & 0 deletions ffi/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ pub(crate) enum Request {
repository: RepositoryHandle,
credentials: Bytes,
},
RepositoryResetCredentials {
repository: RepositoryHandle,
token: ShareToken,
},
RepositoryAccessMode(RepositoryHandle),
RepositorySetAccessMode {
repository: RepositoryHandle,
Expand Down
4 changes: 2 additions & 2 deletions ffi/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ pub(crate) fn credentials(state: &State, handle: RepositoryHandle) -> Result<Vec
pub(crate) async fn set_credentials(
state: &State,
handle: RepositoryHandle,
credentials: Vec<u8>,
credentials: Credentials,
) -> Result<(), Error> {
state
.repositories
.get(handle)?
.repository
.set_credentials(Credentials::decode(&credentials)?)
.set_credentials(credentials)
.await?;

Ok(())
Expand Down

0 comments on commit c594c45

Please sign in to comment.