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

feat: create rules for Authentication and Account modules #658

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions core/main/src/firebolt/handlers/account_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ use jsonrpsee::{
};
use ripple_sdk::{
api::{
gateway::rpc_gateway_api::CallContext,
session::{AccountSessionRequest, AccountSessionTokenRequest},
gateway::rpc_gateway_api::{ApiProtocol, CallContext, RpcRequest},
session::AccountSessionTokenRequest,
},
log::error,
};
use serde_json::json;

use crate::{
firebolt::rpc::RippleRPCProvider, state::platform_state::PlatformState,
Expand All @@ -52,14 +53,27 @@ 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());
_ctx.protocol = ApiProtocol::Extn;
let resp = self
.platform_state
.get_client()
.send_extn_request(AccountSessionRequest::SetAccessToken(a_t_r))
.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,
),
})
.await;
if resp.is_err() {
error!("Error in session {:?}", resp);
Expand Down
89 changes: 72 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},
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,65 @@ 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,
),
})
.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,
),
})
.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,
),
})
.await,
);

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

Expand All @@ -720,6 +760,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
Loading