public static final String KEYFIELD_DID = "did";
+ public static final String KEYFIELD_UID = "uid";
+
+ public static final String KEYFIELD_OID = "oid";
+
public static final String KEYFIELD_ACLID = "aclid";
public static final String KEYFIELD_DI = "di";
import org.iotivity.cloud.accountserver.db.UserTable;
import org.iotivity.cloud.accountserver.oauth.OAuthProviderFactory;
import org.iotivity.cloud.accountserver.resources.acl.group.GroupManager;
+import org.iotivity.cloud.accountserver.resources.acl.id.AclResource;
import org.iotivity.cloud.accountserver.util.TypeCastingManager;
import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException;
// store token information and user information to the DB
// private group creation and store group information to the DB
- storeUserTokenInfo(userUuid, userInfo, tokenInfo, did);
+ userUuid = storeUserTokenInfo(userUuid, userInfo, tokenInfo, did);
+
+ AclResource.getInstance().createAcl(userUuid, did);
// make response
HashMap<String, Object> response = makeSignUpResponse(tokenInfo);
return response;
}
- private void storeUserTokenInfo(String userUuid, UserTable userInfo,
+ private String storeUserTokenInfo(String userUuid, UserTable userInfo,
TokenTable tokenInfo, String did) {
// store db
// the user table is created
tokenInfo.setUuid(userUuid);
AccountDBManager.getInstance().insertAndReplaceRecord(
Constants.TOKEN_TABLE, castTokenTableToMap(tokenInfo));
+ return userUuid;
}
private String checkAuthProviderName(String authProvider) {
GroupManager.getInstance().removeGroupDeviceinEveryGroup(uid, di);
// TODO remove device record from the ACL table
+ HashMap<String, Object> getAcl = new HashMap<>();
+
+ getAcl = AclResource.getInstance().getAclid(di);
+ if (getAcl == null || getAcl.containsKey(Constants.KEYFIELD_ACLID)) {
+ throw new BadRequestException("getAcl is invalid");
+ }
+
+ if (getAcl.get(Constants.KEYFIELD_ACLID) == null) {
+ throw new BadRequestException("getAcl is null");
+ }
+
+ AclResource.getInstance()
+ .deleteAcl((String) getAcl.get(Constants.KEYFIELD_ACLID));
}
}
import java.util.List;
import org.iotivity.cloud.accountserver.Constants;
+import org.iotivity.cloud.accountserver.resources.acl.id.AclManager;
import org.iotivity.cloud.base.device.Device;
import org.iotivity.cloud.base.exception.ServerException;
import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
public static final String REQ_INVITE = "invite";
public static final String REQ_PING_ARRAY = "inarray";
public static final String REQ_PING = "in";
+ public static final String REQ_SEARCH_USER_ID = "sid";
+ public static final String REQ_REQUEST_METHOD = "rm";
+ public static final String REQ_REQUEST_URI = "uri";
+
+ public static final String RESP_GRANT_POLICY = "gp";
+ public static final String RESP_ACL_ALLOWED = "Allowed";
+ public static final String RESP_ACL_DENIED = "Denied";
public static final String REQ_LINKS = "links";
public static final String REQ_HREF = "href";
import java.util.HashMap;
import java.util.List;
+import org.iotivity.cloud.base.OICConstants;
+import org.iotivity.cloud.base.connector.ConnectorPool;
import org.iotivity.cloud.base.device.CoapDevice;
import org.iotivity.cloud.base.device.Device;
import org.iotivity.cloud.base.device.IRequestChannel;
import org.iotivity.cloud.base.protocols.MessageBuilder;
import org.iotivity.cloud.base.protocols.coap.CoapResponse;
import org.iotivity.cloud.base.protocols.enums.ContentFormat;
+import org.iotivity.cloud.base.protocols.enums.RequestMethod;
import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
import org.iotivity.cloud.base.resource.Resource;
import org.iotivity.cloud.ciserver.Constants;
*/
public class DiResource extends Resource {
- private CoapDevicePool mDevicePool = null;
+ private CoapDevicePool mDevicePool = null;
+ private IRequestChannel mASServer = null;
+ private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
public DiResource(CoapDevicePool devicePool) {
super(Arrays.asList(Constants.REQ_DEVICE_ID));
+ mASServer = ConnectorPool.getConnection("account");
mDevicePool = devicePool;
-
- addQueryHandler(
- Arrays.asList(Constants.RS_INTERFACE + "="
- + Constants.LINK_INTERFACE),
- this::onLinkInterfaceRequestReceived);
}
private IRequestChannel getTargetDeviceChannel(IRequest request)
}
}
- /**
- * API for handling optional method for handling packet contains link
- * interface.
- *
- * @param srcDevice
- * device information contains response channel
- * @param request
- * received request to relay
- */
- public void onLinkInterfaceRequestReceived(Device srcDevice,
- IRequest request) throws ServerException {
- IRequestChannel requestChannel = getTargetDeviceChannel(request);
-
- if (requestChannel == null) {
- throw new NotFoundException();
- }
-
- String deviceId = request.getUriPathSegments().get(1);
-
- requestChannel.sendRequest(
- MessageBuilder.modifyRequest(request,
- extractTargetUriPath(request), null, null, null),
- new LinkInterfaceHandler(deviceId, srcDevice));
- }
-
class DefaultResponseHandler implements IResponseEventHandler {
private String mTargetDI = null;
private Device mSrcDevice = null;
}
}
- @Override
- public void onDefaultRequestReceived(Device srcDevice, IRequest request)
- throws ServerException {
- // Find proper request channel using di in URI path field.
- IRequestChannel requestChannel = getTargetDeviceChannel(request);
+ class AccountReceiveHandler implements IResponseEventHandler {
+ private IRequest mRequest = null;
+ private Device mSrcDevice = null;
- if (requestChannel == null) {
- throw new NotFoundException();
+ public AccountReceiveHandler(Device srcDevice, IRequest request) {
+ mRequest = request;
+ mSrcDevice = srcDevice;
}
- String deviceId = request.getUriPathSegments().get(1);
+ @Override
+ public void onResponseReceived(IResponse response) {
+ switch (response.getStatus()) {
+ case CONTENT:
+ HashMap<String, Object> payloadData = mCbor
+ .parsePayloadFromCbor(response.getPayload(),
+ HashMap.class);
+ checkPayloadException(Constants.RESP_GRANT_POLICY,
+ payloadData);
+ String gp = (String) payloadData
+ .get(Constants.RESP_GRANT_POLICY);
+ verifyRequest(mSrcDevice, mRequest, gp);
+ break;
+ default:
+ mSrcDevice.sendResponse(MessageBuilder.createResponse(
+ mRequest, ResponseStatus.BAD_REQUEST));
+ }
+
+ }
+ }
+
+ private void verifyRequest(Device srcDevice, IRequest request,
+ String grantPermisson) {
+ switch (grantPermisson) {
+ case Constants.RESP_ACL_ALLOWED:
+ IRequestChannel requestChannel = getTargetDeviceChannel(
+ request);
+
+ if (requestChannel == null) {
+ throw new NotFoundException();
+ }
+
+ String deviceId = request.getUriPathSegments().get(1);
+
+ IResponseEventHandler responseHandler = null;
+ if (request.getUriQuery() != null && checkQueryException(
+ Constants.RS_INTERFACE, request.getUriQueryMap())) {
+ boolean hasLinkInterface = request.getUriQuery()
+ .contains(Constants.LINK_INTERFACE);
+ if (hasLinkInterface) {
+ responseHandler = new LinkInterfaceHandler(deviceId,
+ srcDevice);
+ }
+ } else {
+ responseHandler = new DefaultResponseHandler(deviceId,
+ srcDevice);
+ }
- requestChannel.sendRequest(
- MessageBuilder.modifyRequest(request,
- extractTargetUriPath(request), null, null, null),
- new DefaultResponseHandler(deviceId, srcDevice));
+ String uriPath = extractTargetUriPath(request);
+ IRequest requestToResource = MessageBuilder
+ .modifyRequest(request, uriPath, null, null, null);
+ requestChannel.sendRequest(requestToResource, responseHandler);
+ break;
+ case Constants.RESP_ACL_DENIED:
+ srcDevice.sendResponse(MessageBuilder.createResponse(request,
+ ResponseStatus.UNAUTHORIZED));
+ break;
+ default:
+ srcDevice.sendResponse(MessageBuilder.createResponse(request,
+ ResponseStatus.BAD_REQUEST));
+ }
}
+ @Override
+ public void onDefaultRequestReceived(Device srcDevice, IRequest request)
+ throws ServerException {
+ // verify Permission
+ StringBuffer uriQuery = new StringBuffer();
+ uriQuery.append(Constants.REQ_SEARCH_USER_ID + "="
+ + srcDevice.getUserId() + ";");
+ uriQuery.append(Constants.REQ_DEVICE_ID + "="
+ + request.getUriPathSegments().get(1) + ";");
+ uriQuery.append(
+ Constants.REQ_REQUEST_METHOD + "=" + request.getMethod() + ";");
+ uriQuery.append(Constants.REQ_REQUEST_URI + "="
+ + extractTargetUriPath(request));
+
+ IRequest verifyRequest = MessageBuilder.createRequest(RequestMethod.GET,
+ OICConstants.ACL_VERIFY_FULL_URI, uriQuery.toString());
+
+ mASServer.sendRequest(verifyRequest,
+ new AccountReceiveHandler(srcDevice, request));
+ }
}
\ No newline at end of file
import org.iotivity.cloud.ciserver.Constants;
import org.iotivity.cloud.ciserver.DeviceServerSystem;
import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool;
+import org.iotivity.cloud.ciserver.resources.DiResource.AccountReceiveHandler;
import org.iotivity.cloud.ciserver.resources.DiResource.DefaultResponseHandler;
import org.iotivity.cloud.ciserver.resources.DiResource.LinkInterfaceHandler;
import org.iotivity.cloud.util.Cbor;
import org.mockito.stubbing.Answer;
public class DiResourceTest {
- private static final String RELAY_URI = "/di";
- private static final String RESOURCE_URI = "/a/light/0";
- private String mDiServer = "resourceServerId";
- private CoapDevice mSourceDevice = mock(
+ private static final String RELAY_URI = "/di";
+ private static final String RESOURCE_URI = "/a/light/0";
+ private static final String VERIFY_URI = "/oic/acl/verify";
+ private String mDiServer = "resourceServerId";
+ private CoapDevice mSourceDevice = mock(
CoapDevice.class);
- private CoapDevice mTargetDevice = mock(
+ private CoapDevice mTargetDevice = mock(
CoapDevice.class);
- private IResponse mRes = null;
- private IRequest mReq = null;
- private DeviceServerSystem mDeviceServerSystem = new DeviceServerSystem();
- private final CountDownLatch mLatch = new CountDownLatch(
+ private IResponse mRes = null;
+ private IRequest mReq = null;
+ private DeviceServerSystem mDeviceServerSystem = new DeviceServerSystem();
+ private final CountDownLatch mLatch = new CountDownLatch(
1);
- private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
- private IRequestChannel mTargetChannel = mock(
+ private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
+ private IRequestChannel mTargetChannel = mock(
IRequestChannel.class);
+ @Mock(name = "mASServer")
+ IRequestChannel mRequestChannelASServer;
+
@Mock
CoapDevicePool coapDevicePool;
IRequestChannel requestChannel;
@InjectMocks
- DiResource diHandler = new DiResource(
+ DiResource diHandler = new DiResource(
coapDevicePool);
@InjectMocks
- LinkInterfaceHandler linkInterfaceHandler = diHandler.new LinkInterfaceHandler(
+ LinkInterfaceHandler linkInterfaceHandler = diHandler.new LinkInterfaceHandler(
"targetDeviceId", mSourceDevice);
@InjectMocks
- DefaultResponseHandler defaultResponseHandler = diHandler.new DefaultResponseHandler(
+ DefaultResponseHandler defaultResponseHandler = diHandler.new DefaultResponseHandler(
"targetDeviceId", mSourceDevice);
+ IRequest requestDefault = makePutRequest();
+ IRequest requestLinkInterface = makePutLinkInterfaceRequest();
+
+ @InjectMocks
+ AccountReceiveHandler accountDefaultReceiveHandler = diHandler.new AccountReceiveHandler(
+ mSourceDevice, requestDefault);
+
+ @InjectMocks
+ AccountReceiveHandler accountLinkInterfaceReceiveHandler = diHandler.new AccountReceiveHandler(
+ mSourceDevice, requestLinkInterface);
+
@Before
public void setUp() throws Exception {
mRes = null;
}
}).when(mTargetChannel).sendRequest(Mockito.any(IRequest.class),
Mockito.any(CoapDevice.class));
+ Mockito.doAnswer(new Answer<Object>() {
+ @Override
+ public CoapRequest answer(InvocationOnMock invocation)
+ throws Throwable {
+ Object[] args = invocation.getArguments();
+ CoapRequest request = (CoapRequest) args[0];
+ System.out.println(
+ "\t----------payload : " + request.getPayloadString());
+ System.out.println(
+ "\t----------uripath : " + request.getUriPath());
+ System.out.println(
+ "\t---------uriquery : " + request.getUriQuery());
+ mReq = request;
+ mLatch.countDown();
+ return null;
+ }
+ }).when(mRequestChannelASServer).sendRequest(
+ Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
}
@Test
public void testOnDefaultRequestReceived() throws InterruptedException {
IRequest request = makePutRequest();
diHandler.onDefaultRequestReceived(mSourceDevice, request);
- assertTrue(mReq.getMethod().equals(RequestMethod.PUT));
- assertTrue(mReq.getUriPath().equals(RESOURCE_URI));
+ assertTrue(mReq.getMethod().equals(RequestMethod.GET));
+ assertTrue(mReq.getUriPath().equals(VERIFY_URI));
assertTrue(mLatch.await(1L, SECONDS));
}
@Test
- public void testOnLinkInterfaceRequestReceived()
+ public void testOnDefaultResponseHandleronResponseReceived()
throws InterruptedException {
- IRequest request = makePutLinkInterfaceRequest();
- diHandler.onLinkInterfaceRequestReceived(mSourceDevice, request);
- assertTrue(mReq.getMethod().equals(RequestMethod.PUT));
- assertTrue(mReq.getUriPath().equals(RESOURCE_URI));
+ IResponse response = makeContentResponse();
+ defaultResponseHandler.onResponseReceived(response);
+ assertEquals(mRes, response);
assertTrue(mLatch.await(1L, SECONDS));
}
@Test
- public void testOnDefaultResponseHandleronResponseReceived()
+ public void testOnAccountReceiveHandlerDeniedonResponseReceived()
throws InterruptedException {
- IResponse response = makeContentResponse();
- defaultResponseHandler.onResponseReceived(response);
- assertEquals(mRes, response);
+ IResponse response = makeVerifyDeniedContentResponse();
+ accountDefaultReceiveHandler.onResponseReceived(response);
+ }
+
+ @Test
+ public void testOnAccountReceiveHandlerDefaultonResponseReceived()
+ throws InterruptedException {
+ IResponse response = makeVerifyAllowedContentResponse();
+ accountDefaultReceiveHandler.onResponseReceived(response);
+ assertEquals(mReq, requestDefault);
assertTrue(mLatch.await(1L, SECONDS));
}
@Test
public void testOnLinkInterfaceResponseHandleronResponseReceived()
throws InterruptedException {
+ IResponse response = makeVerifyAllowedContentResponse();
+ accountLinkInterfaceReceiveHandler.onResponseReceived(response);
+ assertEquals(mReq, requestLinkInterface);
+ assertTrue(mLatch.await(1L, SECONDS));
+ }
+
+ @Test
+ public void testOnAccountReceiveHandlerLinkInterfaceonResponseReceived()
+ throws InterruptedException {
IResponse response = makeContentLinkResponse();
linkInterfaceHandler.onResponseReceived(response);
assertEquals(mRes, response);
return response;
}
+ private IResponse makeVerifyAllowedContentResponse() {
+
+ HashMap<String, Object> payloadData = new HashMap<>();
+ payloadData.put("gp", "Allowed");
+ IResponse response = MessageBuilder.createResponse(makeGetRequest(),
+ ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
+ mCbor.encodingPayloadToCbor(payloadData));
+ return response;
+ }
+
+ private IResponse makeVerifyDeniedContentResponse() {
+
+ HashMap<String, Object> payloadData = new HashMap<>();
+ payloadData.put("gp", "Denied");
+ IResponse response = MessageBuilder.createResponse(makeGetRequest(),
+ ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
+ mCbor.encodingPayloadToCbor(payloadData));
+ return response;
+ }
+
private IResponse makeContentLinkResponse() {
HashMap<String, Object> payloadData = new HashMap<>();
ArrayList<HashMap<String, Object>> linkPayload = new ArrayList<>();
+ OICConstants.PREFIX_OIC + "/" + OICConstants.ACL_URI + "/"
+ OICConstants.INVITE_URI;
+ public static final String ACL_VERIFY_FULL_URI = "/"
+ + OICConstants.PREFIX_OIC + "/" + OICConstants.ACL_URI + "/"
+ + OICConstants.VERIFY_URI;
+
public static final String KEEP_ALIVE_FULL_URI = "/" + PREFIX_OIC
+ "/" + OICConstants.KEEP_ALIVE_URI;