Skip to content

fix: did:key app_authentication_key #153

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

Merged
merged 2 commits into from
Oct 27, 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
19 changes: 10 additions & 9 deletions src/handlers/subscribe_topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,28 @@ pub async fn handler(
});
let signing_public = PublicKey::from(&signing_secret);
let topic: Topic = sha256::digest(signing_public.as_bytes()).into();
let signing_public = hex::encode(signing_public);
let signing_secret = hex::encode(signing_secret.to_bytes());
let subscribe_public_key = hex::encode(signing_public);
let subscribe_private_key = hex::encode(signing_secret.to_bytes());

let identity_secret = ed25519_dalek::SigningKey::generate(&mut rng);
let identity_public = hex::encode(ed25519_dalek::VerifyingKey::from(&identity_secret));
let identity_secret = hex::encode(identity_secret.to_bytes());
let authentication_public_key =
hex::encode(ed25519_dalek::VerifyingKey::from(&identity_secret));
let authentication_private_key = hex::encode(identity_secret.to_bytes());

info!(
"Saving project_info to database for project: {project_id} and app_domain {app_domain} \
with signing pubkey: {signing_public} and identity pubkey: {identity_public}, topic: \
with subscribe_public_key: {subscribe_public_key} and authentication_public_key: {authentication_public_key}, topic: \
{topic}"
);

let project = upsert_project(
project_id,
&app_domain,
topic.clone(),
identity_public,
identity_secret,
signing_public,
signing_secret,
authentication_public_key,
authentication_private_key,
subscribe_public_key,
subscribe_private_key,
&state.postgres,
)
.await?;
Expand Down
16 changes: 8 additions & 8 deletions src/model/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub async fn upsert_project(
project_id: ProjectId,
app_domain: &str,
topic: Topic,
identity_public: String,
identity_secret: String,
signing_public: String,
signing_secret: String,
authentication_public_key: String,
authentication_private_key: String,
subscribe_public_key: String,
subscribe_private_key: String,
postgres: &PgPool,
) -> Result<ProjectWithPublicKeys, sqlx::error::Error> {
let query = "
Expand All @@ -48,10 +48,10 @@ pub async fn upsert_project(
.bind(project_id.as_ref())
.bind(app_domain)
.bind(topic.as_ref())
.bind(identity_public)
.bind(identity_secret)
.bind(signing_public)
.bind(signing_secret)
.bind(authentication_public_key)
.bind(authentication_private_key)
.bind(subscribe_public_key)
.bind(subscribe_private_key)
.fetch_one(postgres)
.await
}
Expand Down
39 changes: 27 additions & 12 deletions src/websocket_service/handlers/notify_watch_subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
helpers::{
get_project_by_app_domain, get_subscription_watchers_for_account_by_app_or_all_app,
get_subscriptions_by_account, get_subscriptions_by_account_and_app,
upsert_subscription_watcher,
upsert_subscription_watcher, SubscriberWithProject,
},
types::AccountId,
},
Expand Down Expand Up @@ -180,17 +180,32 @@ pub async fn collect_subscriptions(
get_subscriptions_by_account(account, postgres).await?
};

let subscriptions = subscriptions
.into_iter()
.map(|sub| NotifyServerSubscription {
app_domain: sub.app_domain,
app_authentication_key: sub.authentication_public_key,
sym_key: sub.sym_key,
account: sub.account,
scope: sub.scope.into_iter().collect(),
expiry: sub.expiry.timestamp() as u64,
})
.collect::<Vec<_>>();
let subscriptions = {
let try_subscriptions = subscriptions
.into_iter()
.map(|sub| {
fn wrap(sub: SubscriberWithProject) -> Result<NotifyServerSubscription> {
Ok(NotifyServerSubscription {
app_domain: sub.app_domain,
app_authentication_key: format!(
"did:key:{}",
DecodedClientId(decode_key(&sub.authentication_public_key)?)
Copy link

@Elyniss Elyniss Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something smells. It's not DecodedClientId, It's base58 encoded ED25519 key. Do we have tests for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same code for the other did:keys. DecodedClientId is a wrapper type containing the decoded public key. I'm then formatting it as string, which encodes it.

Added a test in 81e0c05

),
sym_key: sub.sym_key,
account: sub.account,
scope: sub.scope.into_iter().collect(),
expiry: sub.expiry.timestamp() as u64,
})
}
wrap(sub)
})
.collect::<Vec<_>>();
let mut subscriptions = Vec::with_capacity(try_subscriptions.len());
for result in try_subscriptions {
subscriptions.push(result?);
}
subscriptions
};

Ok(subscriptions)
}
Expand Down
7 changes: 7 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,13 @@ async fn run_test(statement: String) {
);
assert_eq!(sub.account, account);
assert_eq!(sub.app_domain, app_domain);
assert_eq!(&sub.app_authentication_key, &dapp_did_key);
assert_eq!(
DecodedClientId::try_from_did_key(&sub.app_authentication_key)
.unwrap()
.0,
decode_key(dapp_identity_pubkey).unwrap()
);
assert_eq!(
sub.scope,
HashSet::from(["test".to_owned(), "test1".to_owned()]),
Expand Down