Skip to content

Commit

Permalink
feat: create rules for Authentication and Account modules (#658)
Browse files Browse the repository at this point in the history
* feat: create rules for Authentication and Account modules

* update response to handle thunder call failure
  • Loading branch information
maggie98choy authored Oct 25, 2024
1 parent c27cb22 commit ce96f61
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 28 deletions.
53 changes: 42 additions & 11 deletions core/main/src/firebolt/handlers/account_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ use jsonrpsee::{
};
use ripple_sdk::{
api::{
gateway::rpc_gateway_api::CallContext,
session::{AccountSessionRequest, AccountSessionTokenRequest},
gateway::rpc_gateway_api::{ApiProtocol, CallContext, RpcRequest, RpcStats},
session::AccountSessionTokenRequest,
},
log::error,
extn::extn_client_message::{ExtnMessage, ExtnResponse},
utils::error::RippleError,
};
use serde_json::json;

use crate::{
firebolt::rpc::RippleRPCProvider, state::platform_state::PlatformState,
Expand All @@ -52,17 +54,31 @@ pub struct AccountImpl {

#[async_trait]
impl AccountServer for AccountImpl {
async fn session(&self, _ctx: CallContext, a_t_r: AccountSessionTokenRequest) -> RpcResult<()> {
async fn session(
&self,
mut _ctx: CallContext,
a_t_r: AccountSessionTokenRequest,
) -> RpcResult<()> {
self.platform_state
.session_state
.insert_session_token(a_t_r.token.clone());
let resp = self
.platform_state
.get_client()
.send_extn_request(AccountSessionRequest::SetAccessToken(a_t_r))
.await;
if resp.is_err() {
error!("Error in session {:?}", resp);
_ctx.protocol = ApiProtocol::Extn;
let success = rpc_request_setter(
self.platform_state
.get_client()
.get_extn_client()
.main_internal_request(RpcRequest {
ctx: _ctx.clone(),
method: "account.setServiceAccessToken".into(),
params_json: RpcRequest::prepend_ctx(
Some(json!({"token": a_t_r.token, "expires": a_t_r.expires_in})),
&_ctx,
),
stats: RpcStats::default(),
})
.await,
);
if !success {
return Err(rpc_err("session error response TBD"));
}
Ok(())
Expand All @@ -87,6 +103,21 @@ impl AccountImpl {
}
}

fn rpc_request_setter(response: Result<ExtnMessage, RippleError>) -> bool {
if response.clone().is_ok() {
if let Ok(res) = response {
if let Some(ExtnResponse::Value(v)) = res.payload.extract::<ExtnResponse>() {
if v.is_boolean() {
if let Some(b) = v.as_bool() {
return b;
}
}
}
}
}
false
}

pub struct AccountRPCProvider;
impl RippleRPCProvider<AccountImpl> for AccountRPCProvider {
fn provide(platform_state: PlatformState) -> RpcModule<AccountImpl> {
Expand Down
92 changes: 75 additions & 17 deletions core/main/src/firebolt/handlers/device_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ use ripple_sdk::{
},
},
firebolt::fb_general::{ListenRequest, ListenerResponse},
gateway::rpc_gateway_api::CallContext,
session::{AccountSessionRequest, ProvisionRequest},
gateway::rpc_gateway_api::{ApiProtocol, CallContext, RpcRequest, RpcStats},
session::ProvisionRequest,
storage_property::{
StorageProperty, EVENT_DEVICE_DEVICE_NAME_CHANGED, EVENT_DEVICE_NAME_CHANGED,
},
},
extn::extn_client_message::ExtnResponse,
extn::extn_client_message::{ExtnMessage, ExtnResponse},
log::error,
tokio::time::timeout,
utils::error::RippleError,
};
use serde_json::json;

include!(concat!(env!("OUT_DIR"), "/version.rs"));

Expand Down Expand Up @@ -685,27 +687,68 @@ impl DeviceServer for DeviceImpl {

async fn provision(
&self,
_ctx: CallContext,
mut _ctx: CallContext,
provision_request: ProvisionRequest,
) -> RpcResult<()> {
// clear the cached distributor session
self.state
.session_state
.update_account_session(provision_request.clone());

let resp = self
.state
.get_client()
.send_extn_request(AccountSessionRequest::Provision(provision_request))
.await;
match resp {
Ok(payload) => match payload.payload.extract().unwrap() {
ExtnResponse::None(()) => Ok(()),
_ => Err(rpc_err("Provision Status error response TBD")),
},
Err(_e) => Err(jsonrpsee::core::Error::Custom(String::from(
"Provision Status error response TBD",
))),
if provision_request.distributor_id.is_none() {
return Err(rpc_err(
"set_provision: session.distributor_id is not set, cannot set provisioning",
));
};
_ctx.protocol = ApiProtocol::Extn;
let success = rpc_request_setter(
self.state
.get_client()
.get_extn_client()
.main_internal_request(RpcRequest {
ctx: _ctx.clone(),
method: "account.setServiceAccountId".into(),
params_json: RpcRequest::prepend_ctx(
Some(json!({"serviceAccountId": provision_request.account_id})),
&_ctx,
),
stats: RpcStats::default(),
})
.await,
) && rpc_request_setter(
self.state
.get_client()
.get_extn_client()
.main_internal_request(RpcRequest {
ctx: _ctx.clone(),
method: "account.setXDeviceId".into(),
params_json: RpcRequest::prepend_ctx(
Some(json!({"xDeviceId": provision_request.device_id})),
&_ctx,
),
stats: RpcStats::default(),
})
.await,
) && rpc_request_setter(
self.state
.get_client()
.get_extn_client()
.main_internal_request(RpcRequest {
ctx: _ctx.clone(),
method: "account.setPartnerId".into(),
params_json: RpcRequest::prepend_ctx(
Some(json!({"partnerId": provision_request.distributor_id })),
&_ctx,
),
stats: RpcStats::default(),
})
.await,
);

if success {
Ok(())
} else {
Err(rpc_err("Provision Status error response TBD"))
}
}

Expand All @@ -720,6 +763,21 @@ impl DeviceServer for DeviceImpl {
}
}

fn rpc_request_setter(response: Result<ExtnMessage, RippleError>) -> bool {
if response.clone().is_ok() {
if let Ok(res) = response {
if let Some(ExtnResponse::Value(v)) = res.payload.extract::<ExtnResponse>() {
if v.is_boolean() {
if let Some(b) = v.as_bool() {
return b;
}
}
}
}
}
false
}

pub struct DeviceRPCProvider;
impl RippleRPCProvider<DeviceImpl> for DeviceRPCProvider {
fn provide(state: PlatformState) -> RpcModule<DeviceImpl> {
Expand Down

0 comments on commit ce96f61

Please sign in to comment.