Skip to content

Commit

Permalink
chore: refactor magic values
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Jan 5, 2024
1 parent 0248875 commit 4511fb7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/notify_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn sign_message(
exp: add_ttl(now, NOTIFY_MESSAGE_TTL).timestamp(),
iss: decoded_client_id.to_did_key(),
// no `aud` because any client can receive this message
act: NOTIFY_MESSAGE_ACT.to_string(),
act: NOTIFY_MESSAGE_ACT.to_owned(),
sub: account.to_did_pkh(),
app: app.clone(),
msg,
Expand Down
9 changes: 6 additions & 3 deletions src/services/websocket_server/handlers/notify_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use {
handlers::{decrypt_message, notify_watch_subscriptions::update_subscription_watchers},
NotifyDelete, NotifyRequest, NotifyResponse, ResponseAuth,
},
spec::{NOTIFY_DELETE_RESPONSE_TAG, NOTIFY_DELETE_RESPONSE_TTL},
spec::{
NOTIFY_DELETE_ACT, NOTIFY_DELETE_RESPONSE_ACT, NOTIFY_DELETE_RESPONSE_TAG,
NOTIFY_DELETE_RESPONSE_TTL,
},
state::{AppState, WebhookNotificationEvent},
types::{Envelope, EnvelopeType0},
utils::topic_from_key,
Expand Down Expand Up @@ -71,7 +74,7 @@ pub async fn handle(msg: PublishedMessage, state: &AppState, client: &Client) ->
}

let (account, siwe_domain) = {
if sub_auth.shared_claims.act != "notify_delete" {
if sub_auth.shared_claims.act != NOTIFY_DELETE_ACT {
return Err(AuthError::InvalidAct)?;
}

Expand Down Expand Up @@ -137,7 +140,7 @@ pub async fn handle(msg: PublishedMessage, state: &AppState, client: &Client) ->
exp: add_ttl(now, NOTIFY_DELETE_RESPONSE_TTL).timestamp() as u64,
iss: identity.to_did_key(),
aud: sub_auth.shared_claims.iss,
act: "notify_delete_response".to_string(),
act: NOTIFY_DELETE_RESPONSE_ACT.to_owned(),
mjv: "1".to_owned(),
},
sub: account.to_did_pkh(),
Expand Down
22 changes: 16 additions & 6 deletions src/services/websocket_server/handlers/notify_subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use {
handlers::{decrypt_message, notify_watch_subscriptions::update_subscription_watchers},
NotifyRequest, NotifyResponse, NotifySubscribe, ResponseAuth,
},
spec::{NOTIFY_NOOP_TAG, NOTIFY_SUBSCRIBE_RESPONSE_TAG, NOTIFY_SUBSCRIBE_RESPONSE_TTL},
spec::{
NOTIFY_NOOP_TAG, NOTIFY_NOOP_TTL, NOTIFY_SUBSCRIBE_ACT, NOTIFY_SUBSCRIBE_RESPONSE_ACT,
NOTIFY_SUBSCRIBE_RESPONSE_TAG, NOTIFY_SUBSCRIBE_RESPONSE_TTL,
},
state::{AppState, WebhookNotificationEvent},
types::{parse_scope, Envelope, EnvelopeType0, EnvelopeType1},
utils::topic_from_key,
Expand All @@ -28,7 +31,10 @@ use {
domain::{DecodedClientId, Topic},
rpc::Publish,
},
std::{collections::HashSet, sync::Arc},
std::{
collections::HashSet,
sync::{Arc, OnceLock},
},
tracing::{info, instrument},
x25519_dalek::{PublicKey, StaticSecret},
};
Expand Down Expand Up @@ -83,7 +89,7 @@ pub async fn handle(msg: PublishedMessage, state: &AppState) -> Result<()> {
}

let (account, siwe_domain) = {
if sub_auth.shared_claims.act != "notify_subscription" {
if sub_auth.shared_claims.act != NOTIFY_SUBSCRIBE_ACT {
return Err(AuthError::InvalidAct)?;
}

Expand Down Expand Up @@ -125,7 +131,7 @@ pub async fn handle(msg: PublishedMessage, state: &AppState) -> Result<()> {
exp: add_ttl(now, NOTIFY_SUBSCRIBE_RESPONSE_TTL).timestamp() as u64,
iss: identity.to_did_key(),
aud: sub_auth.shared_claims.iss.clone(),
act: "notify_subscription_response".to_string(),
act: NOTIFY_SUBSCRIBE_RESPONSE_ACT.to_owned(),
mjv: "1".to_owned(),
},
sub: account.to_did_pkh(),
Expand Down Expand Up @@ -209,9 +215,13 @@ pub async fn handle(msg: PublishedMessage, state: &AppState) -> Result<()> {
&state.relay_http_client,
&Publish {
topic: notify_topic,
message: "".into(),
message: {
// Extremely minor performance optimization with OnceLock to avoid allocating the same empty string everytime
static LOCK: OnceLock<Arc<str>> = OnceLock::new();
LOCK.get_or_init(|| "".into()).clone()
},
tag: NOTIFY_NOOP_TAG,
ttl_secs: 300,
ttl_secs: NOTIFY_NOOP_TTL.as_secs() as u32,
prompt: false,
},
state.metrics.as_ref(),
Expand Down
9 changes: 6 additions & 3 deletions src/services/websocket_server/handlers/notify_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use {
decode_key, handlers::decrypt_message, NotifyRequest, NotifyResponse, NotifyUpdate,
ResponseAuth,
},
spec::{NOTIFY_UPDATE_RESPONSE_TAG, NOTIFY_UPDATE_RESPONSE_TTL},
spec::{
NOTIFY_UPDATE_ACT, NOTIFY_UPDATE_RESPONSE_ACT, NOTIFY_UPDATE_RESPONSE_TAG,
NOTIFY_UPDATE_RESPONSE_TTL,
},
state::AppState,
types::{parse_scope, Envelope, EnvelopeType0},
utils::topic_from_key,
Expand Down Expand Up @@ -70,7 +73,7 @@ pub async fn handle(msg: PublishedMessage, state: &AppState) -> Result<()> {
}

let (account, siwe_domain) = {
if sub_auth.shared_claims.act != "notify_update" {
if sub_auth.shared_claims.act != NOTIFY_UPDATE_ACT {
return Err(AuthError::InvalidAct)?;
}

Expand Down Expand Up @@ -142,7 +145,7 @@ pub async fn handle(msg: PublishedMessage, state: &AppState) -> Result<()> {
exp: add_ttl(now, NOTIFY_UPDATE_RESPONSE_TTL).timestamp() as u64,
iss: identity.to_did_key(),
aud: sub_auth.shared_claims.iss,
act: "notify_update_response".to_string(),
act: NOTIFY_UPDATE_RESPONSE_ACT.to_owned(),
mjv: "1".to_owned(),
},
sub: account.to_did_pkh(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ use {
NotifySubscriptionsChanged, NotifyWatchSubscriptions,
},
spec::{
NOTIFY_SUBSCRIPTIONS_CHANGED_METHOD, NOTIFY_SUBSCRIPTIONS_CHANGED_TAG,
NOTIFY_SUBSCRIPTIONS_CHANGED_TTL, NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TAG,
NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TTL,
NOTIFY_SUBSCRIPTIONS_CHANGED_ACT, NOTIFY_SUBSCRIPTIONS_CHANGED_METHOD,
NOTIFY_SUBSCRIPTIONS_CHANGED_TAG, NOTIFY_SUBSCRIPTIONS_CHANGED_TTL,
NOTIFY_WATCH_SUBSCRIPTIONS_ACT, NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_ACT,
NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TAG, NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TTL,
},
state::AppState,
types::{Envelope, EnvelopeType0, EnvelopeType1},
Expand Down Expand Up @@ -80,7 +81,7 @@ pub async fn handle(msg: PublishedMessage, state: &AppState) -> Result<()> {

// Verify request
let authorization = {
if request_auth.shared_claims.act != "notify_watch_subscriptions" {
if request_auth.shared_claims.act != NOTIFY_WATCH_SUBSCRIPTIONS_ACT {
return Err(AuthError::InvalidAct)?;
}

Expand Down Expand Up @@ -151,7 +152,7 @@ pub async fn handle(msg: PublishedMessage, state: &AppState) -> Result<()> {
exp: add_ttl(now, NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TTL).timestamp() as u64,
iss: identity.to_did_key(),
aud: request_auth.shared_claims.iss,
act: "notify_watch_subscriptions_response".to_string(),
act: NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_ACT.to_owned(),
mjv: "1".to_owned(),
},
sub: request_auth.sub,
Expand Down Expand Up @@ -287,7 +288,7 @@ pub async fn update_subscription_watchers(
exp: add_ttl(now, NOTIFY_SUBSCRIPTIONS_CHANGED_TTL).timestamp() as u64,
iss: notify_did_key.clone(),
aud,
act: "notify_subscriptions_changed".to_string(),
act: NOTIFY_SUBSCRIPTIONS_CHANGED_ACT.to_owned(),
mjv: "1".to_owned(),
},
sub: did_pkh,
Expand Down
1 change: 1 addition & 0 deletions src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub const NOTIFY_WATCH_SUBSCRIPTIONS_TTL: Duration = T300;
pub const NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TTL: Duration = T300;
pub const NOTIFY_SUBSCRIPTIONS_CHANGED_TTL: Duration = T300;
pub const NOTIFY_SUBSCRIPTIONS_CHANGED_RESPONSE_TTL: Duration = T300;
pub const NOTIFY_NOOP_TTL: Duration = T300;

// acts
// https://specs.walletconnect.com/2.0/specs/clients/notify/notify-authentication
Expand Down
41 changes: 22 additions & 19 deletions tests/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ use {
},
},
spec::{
NOTIFY_DELETE_METHOD, NOTIFY_DELETE_RESPONSE_TAG, NOTIFY_DELETE_TAG, NOTIFY_DELETE_TTL,
NOTIFY_MESSAGE_TAG, NOTIFY_NOOP_TAG, NOTIFY_SUBSCRIBE_METHOD,
NOTIFY_SUBSCRIBE_RESPONSE_TAG, NOTIFY_SUBSCRIBE_TAG, NOTIFY_SUBSCRIBE_TTL,
NOTIFY_SUBSCRIPTIONS_CHANGED_TAG, NOTIFY_UPDATE_METHOD, NOTIFY_UPDATE_RESPONSE_TAG,
NOTIFY_UPDATE_TAG, NOTIFY_UPDATE_TTL, NOTIFY_WATCH_SUBSCRIPTIONS_METHOD,
NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TAG, NOTIFY_WATCH_SUBSCRIPTIONS_TAG,
NOTIFY_WATCH_SUBSCRIPTIONS_TTL,
NOTIFY_DELETE_ACT, NOTIFY_DELETE_METHOD, NOTIFY_DELETE_RESPONSE_ACT,
NOTIFY_DELETE_RESPONSE_TAG, NOTIFY_DELETE_TAG, NOTIFY_DELETE_TTL, NOTIFY_MESSAGE_ACT,
NOTIFY_MESSAGE_TAG, NOTIFY_NOOP_TAG, NOTIFY_SUBSCRIBE_ACT, NOTIFY_SUBSCRIBE_METHOD,
NOTIFY_SUBSCRIBE_RESPONSE_ACT, NOTIFY_SUBSCRIBE_RESPONSE_TAG, NOTIFY_SUBSCRIBE_TAG,
NOTIFY_SUBSCRIBE_TTL, NOTIFY_SUBSCRIPTIONS_CHANGED_ACT,
NOTIFY_SUBSCRIPTIONS_CHANGED_TAG, NOTIFY_UPDATE_ACT, NOTIFY_UPDATE_METHOD,
NOTIFY_UPDATE_RESPONSE_ACT, NOTIFY_UPDATE_RESPONSE_TAG, NOTIFY_UPDATE_TAG,
NOTIFY_UPDATE_TTL, NOTIFY_WATCH_SUBSCRIPTIONS_ACT, NOTIFY_WATCH_SUBSCRIPTIONS_METHOD,
NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_ACT, NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TAG,
NOTIFY_WATCH_SUBSCRIPTIONS_TAG, NOTIFY_WATCH_SUBSCRIPTIONS_TTL,
},
types::{encode_scope, Envelope, EnvelopeType0, EnvelopeType1, Notification},
utils::topic_from_key,
Expand Down Expand Up @@ -236,7 +239,7 @@ async fn watch_subscriptions(
iat: now.timestamp() as u64,
exp: add_ttl(now, NOTIFY_SUBSCRIBE_TTL).timestamp() as u64,
iss: identity_did_key.to_owned(),
act: "notify_watch_subscriptions".to_owned(),
act: NOTIFY_WATCH_SUBSCRIPTIONS_ACT.to_owned(),
aud: DecodedClientId(authentication_key).to_did_key(),
mjv: "0".to_owned(),
},
Expand Down Expand Up @@ -318,7 +321,7 @@ async fn watch_subscriptions(
let auth = from_jwt::<WatchSubscriptionsResponseAuth>(response_auth).unwrap();
assert_eq!(
auth.shared_claims.act,
"notify_watch_subscriptions_response"
NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_ACT
);
assert_eq!(
auth.shared_claims.iss,
Expand Down Expand Up @@ -486,7 +489,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
iat: now.timestamp() as u64,
exp: add_ttl(now, NOTIFY_SUBSCRIBE_TTL).timestamp() as u64,
iss: identity_did_key.clone(),
act: "notify_subscription".to_owned(),
act: NOTIFY_SUBSCRIBE_ACT.to_owned(),
aud: dapp_did_key.clone(),
mjv: "0".to_owned(),
},
Expand Down Expand Up @@ -605,7 +608,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
let subscribe_response_auth = from_jwt::<SubscriptionResponseAuth>(response_auth).unwrap();
assert_eq!(
subscribe_response_auth.shared_claims.act,
"notify_subscription_response"
NOTIFY_SUBSCRIBE_RESPONSE_ACT
);

let notify_key = {
Expand Down Expand Up @@ -637,7 +640,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
.as_str()
.unwrap();
let auth = from_jwt::<WatchSubscriptionsChangedRequestAuth>(response_auth).unwrap();
assert_eq!(auth.shared_claims.act, "notify_subscriptions_changed");
assert_eq!(auth.shared_claims.act, NOTIFY_SUBSCRIPTIONS_CHANGED_ACT);
assert_eq!(auth.sbs.len(), 1);
let sub = &auth.sbs[0];
assert_eq!(sub.scope, notification_types);
Expand Down Expand Up @@ -736,7 +739,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
assert!(claims.exp > chrono::Utc::now().timestamp() - JWT_LEEWAY); // TODO remove leeway
assert_eq!(claims.app.as_ref(), app_domain);
assert_eq!(claims.sub, did_pkh);
assert_eq!(claims.act, "notify_message");
assert_eq!(claims.act, NOTIFY_MESSAGE_ACT);

// TODO Notify receipt?
// https://github.com/WalletConnect/walletconnect-docs/blob/main/docs/specs/clients/notify/notify-authentication.md#notify-receipt
Expand All @@ -753,7 +756,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
iat: now.timestamp() as u64,
exp: add_ttl(now, NOTIFY_UPDATE_TTL).timestamp() as u64,
iss: identity_did_key.clone(),
act: "notify_update".to_owned(),
act: NOTIFY_UPDATE_ACT.to_owned(),
aud: dapp_did_key.clone(),
mjv: "0".to_owned(),
},
Expand Down Expand Up @@ -821,7 +824,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
assert!((claims.shared_claims.exp as i64) > chrono::Utc::now().timestamp() - JWT_LEEWAY); // TODO remove leeway
assert_eq!(claims.app, DidWeb::from_domain(app_domain.clone()));
assert_eq!(claims.shared_claims.aud, identity_did_key);
assert_eq!(claims.shared_claims.act, "notify_update_response");
assert_eq!(claims.shared_claims.act, NOTIFY_UPDATE_RESPONSE_ACT);

{
let resp = rx.recv().await.unwrap();
Expand Down Expand Up @@ -852,7 +855,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
.as_str()
.unwrap();
let auth = from_jwt::<WatchSubscriptionsChangedRequestAuth>(response_auth).unwrap();
assert_eq!(auth.shared_claims.act, "notify_subscriptions_changed");
assert_eq!(auth.shared_claims.act, NOTIFY_SUBSCRIPTIONS_CHANGED_ACT);
assert_eq!(auth.sbs.len(), 1);
let subs = &auth.sbs[0];
assert_eq!(subs.scope, notification_types);
Expand All @@ -867,7 +870,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
exp: add_ttl(now, NOTIFY_DELETE_TTL).timestamp() as u64,
iss: identity_did_key.clone(),
aud: dapp_did_key.clone(),
act: "notify_delete".to_owned(),
act: NOTIFY_DELETE_ACT.to_owned(),
mjv: "0".to_owned(),
},
ksu: vars.keys_server_url.to_string(),
Expand Down Expand Up @@ -933,7 +936,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
assert!((claims.shared_claims.exp as i64) > chrono::Utc::now().timestamp() - JWT_LEEWAY); // TODO remove leeway
assert_eq!(claims.app, DidWeb::from_domain(app_domain));
assert_eq!(claims.shared_claims.aud, identity_did_key);
assert_eq!(claims.shared_claims.act, "notify_delete_response");
assert_eq!(claims.shared_claims.act, NOTIFY_DELETE_RESPONSE_ACT);

{
let resp = rx.recv().await.unwrap();
Expand Down Expand Up @@ -964,7 +967,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
.as_str()
.unwrap();
let auth = from_jwt::<WatchSubscriptionsChangedRequestAuth>(response_auth).unwrap();
assert_eq!(auth.shared_claims.act, "notify_subscriptions_changed");
assert_eq!(auth.shared_claims.act, NOTIFY_SUBSCRIPTIONS_CHANGED_ACT);
assert!(auth.sbs.is_empty());
}

Expand Down

0 comments on commit 4511fb7

Please sign in to comment.