You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@teaclave.apache.org by ms...@apache.org on 2020/01/14 02:29:47 UTC

[incubator-teaclave] 02/02: [services] Add user_authorize rpc in authentication service

This is an automated email from the ASF dual-hosted git repository.

mssun pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-teaclave.git

commit 696b676757dd55c42b151b556059b0dd1bc3bee5
Author: Mingshen Sun <bo...@mssun.me>
AuthorDate: Mon Jan 13 18:20:07 2020 -0800

    [services] Add user_authorize rpc in authentication service
---
 services/authentication/enclave/src/lib.rs         |  5 ++-
 services/authentication/enclave/src/service.rs     | 33 ++++++++++++++-
 services/proto/src/lib.rs                          |  1 +
 .../proto/teaclave_authentication_service.proto    | 11 +++++
 .../proto/src/teaclave_authentication_service.rs   | 46 +++++++++++++++++++++
 services/proto/src/teaclave_common.rs              | 34 +++++++++++++++
 .../enclave/src/teaclave_authentication_service.rs | 48 +++++++++++++++++++---
 7 files changed, 171 insertions(+), 7 deletions(-)

diff --git a/services/authentication/enclave/src/lib.rs b/services/authentication/enclave/src/lib.rs
index e8bf5de..b75dc8c 100644
--- a/services/authentication/enclave/src/lib.rs
+++ b/services/authentication/enclave/src/lib.rs
@@ -100,6 +100,9 @@ pub mod tests {
     use sgx_tunittest::*;
 
     pub fn run_tests() -> usize {
-        rsgx_unit_tests!(service::tests::test_user_login)
+        rsgx_unit_tests!(
+            service::tests::test_user_login,
+            service::tests::test_user_authorize,
+        )
     }
 }
diff --git a/services/authentication/enclave/src/service.rs b/services/authentication/enclave/src/service.rs
index 206842c..34f066f 100644
--- a/services/authentication/enclave/src/service.rs
+++ b/services/authentication/enclave/src/service.rs
@@ -1,6 +1,7 @@
 use std::prelude::v1::*;
 use teaclave_proto::teaclave_authentication_service::{
-    self, TeaclaveAuthentication, UserLoginRequest, UserLoginResponse,
+    self, TeaclaveAuthentication, UserAuthorizeRequest, UserAuthorizeResponse, UserLoginRequest,
+    UserLoginResponse,
 };
 use teaclave_service_enclave_utils::teaclave_service;
 use teaclave_types::{TeaclaveServiceResponseError, TeaclaveServiceResponseResult};
@@ -33,6 +34,15 @@ impl TeaclaveAuthentication for TeaclaveAuthenticationService {
 
         Err(TeaclaveAuthenticationError::PermissionDenied.into())
     }
+
+    fn user_authorize(
+        _request: UserAuthorizeRequest,
+    ) -> TeaclaveServiceResponseResult<UserAuthorizeResponse> {
+        #[cfg(test_mode)]
+        return test_mode::mock_user_authorize(_request);
+
+        Err(TeaclaveAuthenticationError::PermissionDenied.into())
+    }
 }
 
 #[cfg(test_mode)]
@@ -49,11 +59,22 @@ mod test_mode {
         }
         Err(TeaclaveAuthenticationError::PermissionDenied.into())
     }
+
+    pub fn mock_user_authorize(
+        request: UserAuthorizeRequest,
+    ) -> TeaclaveServiceResponseResult<UserAuthorizeResponse> {
+        if request.credential.id == "test_id" && request.credential.token == "test_token" {
+            let response = UserAuthorizeResponse { accept: true };
+            return Ok(response);
+        }
+        Err(TeaclaveAuthenticationError::PermissionDenied.into())
+    }
 }
 
 #[cfg(feature = "enclave_unit_test")]
 pub mod tests {
     use super::*;
+    use teaclave_proto::teaclave_common::UserCredential;
 
     pub fn test_user_login() {
         let request = UserLoginRequest {
@@ -62,4 +83,14 @@ pub mod tests {
         };
         assert!(TeaclaveAuthenticationService::user_login(request).is_ok());
     }
+
+    pub fn test_user_authorize() {
+        let credential = UserCredential {
+            id: "test_id".to_string(),
+            token: "test_token".to_string(),
+        };
+
+        let request = UserAuthorizeRequest { credential };
+        assert!(TeaclaveAuthenticationService::user_authorize(request).is_ok());
+    }
 }
diff --git a/services/proto/src/lib.rs b/services/proto/src/lib.rs
index 2c08f82..247455f 100644
--- a/services/proto/src/lib.rs
+++ b/services/proto/src/lib.rs
@@ -20,3 +20,4 @@
 extern crate sgx_tstd as std;
 
 pub mod teaclave_authentication_service;
+pub mod teaclave_common;
diff --git a/services/proto/src/proto/teaclave_authentication_service.proto b/services/proto/src/proto/teaclave_authentication_service.proto
index 4d26e18..f8bf946 100644
--- a/services/proto/src/proto/teaclave_authentication_service.proto
+++ b/services/proto/src/proto/teaclave_authentication_service.proto
@@ -1,6 +1,8 @@
 syntax = "proto3";
 package teaclave_authentication_service_proto;
 
+import "common.proto";
+
 message UserLoginRequest {
   string id = 1;
   string password = 2;
@@ -10,6 +12,15 @@ message UserLoginResponse {
   string token = 1;
 }
 
+message UserAuthorizeRequest {
+  teaclave_common_proto.UserCredential credential = 1;
+}
+
+message UserAuthorizeResponse {
+  bool accept = 1;
+}
+
 service TeaclaveAuthentication {
   rpc UserLogin (UserLoginRequest) returns (UserLoginResponse);
+  rpc UserAuthorize (UserAuthorizeRequest) returns (UserAuthorizeResponse);
 }
diff --git a/services/proto/src/teaclave_authentication_service.rs b/services/proto/src/teaclave_authentication_service.rs
index cddb914..cb9a3e2 100644
--- a/services/proto/src/teaclave_authentication_service.rs
+++ b/services/proto/src/teaclave_authentication_service.rs
@@ -1,4 +1,8 @@
+use crate::teaclave_common;
+use crate::teaclave_common::proto as teaclave_common_proto;
+use anyhow::anyhow;
 use anyhow::{Error, Result};
+use core::convert::TryInto;
 
 pub mod proto {
     #![allow(clippy::all)]
@@ -9,6 +13,7 @@ pub mod proto {
 }
 
 pub use proto::TeaclaveAuthentication;
+pub use proto::TeaclaveAuthenticationClient;
 pub use proto::TeaclaveAuthenticationRequest;
 pub use proto::TeaclaveAuthenticationResponse;
 
@@ -23,6 +28,16 @@ pub struct UserLoginResponse {
     pub token: std::string::String,
 }
 
+#[derive(serde_derive::Serialize, serde_derive::Deserialize, Debug)]
+pub struct UserAuthorizeRequest {
+    pub credential: teaclave_common::UserCredential,
+}
+
+#[derive(serde_derive::Serialize, serde_derive::Deserialize, Debug)]
+pub struct UserAuthorizeResponse {
+    pub accept: bool,
+}
+
 impl std::convert::TryFrom<proto::UserLoginRequest> for UserLoginRequest {
     type Error = Error;
 
@@ -52,3 +67,34 @@ impl From<UserLoginResponse> for proto::UserLoginResponse {
         }
     }
 }
+
+impl std::convert::TryFrom<proto::UserAuthorizeRequest> for UserAuthorizeRequest {
+    type Error = Error;
+
+    fn try_from(proto: proto::UserAuthorizeRequest) -> Result<Self> {
+        let ret = Self {
+            credential: proto
+                .credential
+                .ok_or_else(|| anyhow!("Missing credential"))?
+                .try_into()?,
+        };
+
+        Ok(ret)
+    }
+}
+
+impl From<UserAuthorizeRequest> for proto::UserAuthorizeRequest {
+    fn from(request: UserAuthorizeRequest) -> Self {
+        Self {
+            credential: Some(request.credential.into()),
+        }
+    }
+}
+
+impl From<UserAuthorizeResponse> for proto::UserAuthorizeResponse {
+    fn from(response: UserAuthorizeResponse) -> Self {
+        Self {
+            accept: response.accept,
+        }
+    }
+}
diff --git a/services/proto/src/teaclave_common.rs b/services/proto/src/teaclave_common.rs
new file mode 100644
index 0000000..9f3fdc5
--- /dev/null
+++ b/services/proto/src/teaclave_common.rs
@@ -0,0 +1,34 @@
+use anyhow::{Error, Result};
+
+pub mod proto {
+    #![allow(clippy::all)]
+    include!(concat!(env!("OUT_DIR"), "/teaclave_common_proto.rs"));
+}
+
+#[derive(serde_derive::Serialize, serde_derive::Deserialize, Debug)]
+pub struct UserCredential {
+    pub id: std::string::String,
+    pub token: std::string::String,
+}
+
+impl std::convert::TryFrom<proto::UserCredential> for UserCredential {
+    type Error = Error;
+
+    fn try_from(proto: proto::UserCredential) -> Result<Self> {
+        let ret = Self {
+            id: proto.id,
+            token: proto.token,
+        };
+
+        Ok(ret)
+    }
+}
+
+impl From<UserCredential> for proto::UserCredential {
+    fn from(request: UserCredential) -> Self {
+        Self {
+            id: request.id,
+            token: request.token,
+        }
+    }
+}
diff --git a/testsuite/service_functional_test/enclave/src/teaclave_authentication_service.rs b/testsuite/service_functional_test/enclave/src/teaclave_authentication_service.rs
index 9b73b59..6db1081 100644
--- a/testsuite/service_functional_test/enclave/src/teaclave_authentication_service.rs
+++ b/testsuite/service_functional_test/enclave/src/teaclave_authentication_service.rs
@@ -1,6 +1,7 @@
 use sgx_tunittest::*;
 use std::prelude::v1::*;
-use teaclave_proto::teaclave_authentication_service::proto::*;
+use teaclave_proto::teaclave_authentication_service::*;
+use teaclave_proto::teaclave_common::*;
 use teaclave_rpc::channel::SgxTrustedTlsChannel;
 use teaclave_rpc::config::SgxTrustedTlsClientConfig;
 use teaclave_types::TeaclaveServiceResponseResult;
@@ -9,10 +10,15 @@ type U = TeaclaveAuthenticationRequest;
 type V = TeaclaveServiceResponseResult<TeaclaveAuthenticationResponse>;
 
 pub fn run_functional_tests() {
-    rsgx_unit_tests!(test_login_success, test_login_failed,);
+    rsgx_unit_tests!(
+        test_login_successful,
+        test_login_failed,
+        test_authorize_successful,
+        test_authorize_failed,
+    );
 }
 
-fn test_login_success() {
+fn test_login_successful() {
     let client_config = SgxTrustedTlsClientConfig::new_without_verifier();
     let channel =
         SgxTrustedTlsChannel::<U, V>::new("127.0.0.1:7776", "localhost", &client_config).unwrap();
@@ -20,7 +26,8 @@ fn test_login_success() {
     let request = UserLoginRequest {
         id: "test_id".to_string(),
         password: "test_password".to_string(),
-    };
+    }
+    .into();
     let response_result = client.user_login(request);
     info!("{:?}", response_result);
     assert!(response_result.is_ok());
@@ -34,8 +41,39 @@ fn test_login_failed() {
     let request = UserLoginRequest {
         id: "".to_string(),
         password: "".to_string(),
-    };
+    }
+    .into();
     let response_result = client.user_login(request);
     info!("{:?}", response_result);
     assert!(response_result.is_err());
 }
+
+fn test_authorize_successful() {
+    let client_config = SgxTrustedTlsClientConfig::new_without_verifier();
+    let channel =
+        SgxTrustedTlsChannel::<U, V>::new("127.0.0.1:7776", "localhost", &client_config).unwrap();
+    let mut client = TeaclaveAuthenticationClient::new(channel).unwrap();
+    let credential = UserCredential {
+        id: "test_id".to_string(),
+        token: "test_token".to_string(),
+    };
+    let request = UserAuthorizeRequest { credential }.into();
+    let response_result = client.user_authorize(request);
+    info!("{:?}", response_result);
+    assert!(response_result.is_ok());
+}
+
+fn test_authorize_failed() {
+    let client_config = SgxTrustedTlsClientConfig::new_without_verifier();
+    let channel =
+        SgxTrustedTlsChannel::<U, V>::new("127.0.0.1:7776", "localhost", &client_config).unwrap();
+    let mut client = TeaclaveAuthenticationClient::new(channel).unwrap();
+    let credential = UserCredential {
+        id: "".to_string(),
+        token: "".to_string(),
+    };
+    let request = UserAuthorizeRequest { credential }.into();
+    let response_result = client.user_authorize(request);
+    info!("{:?}", response_result);
+    assert!(response_result.is_err());
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@teaclave.apache.org
For additional commands, e-mail: commits-help@teaclave.apache.org