1 package org.iotivity.cloud.ciserver.protocols;
3 import java.net.InetSocketAddress;
4 import java.nio.charset.StandardCharsets;
5 import java.util.HashMap;
8 import org.iotivity.cloud.base.CoapClient;
9 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
10 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
11 import org.iotivity.cloud.base.protocols.coap.enums.CoapStatus;
12 import org.iotivity.cloud.ciserver.Constants;
13 import org.iotivity.cloud.util.Cbor;
14 import org.iotivity.cloud.util.JSONUtil;
15 import org.iotivity.cloud.util.Logger;
17 import io.netty.channel.ChannelDuplexHandler;
18 import io.netty.channel.ChannelHandler.Sharable;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.channel.SimpleChannelInboundHandler;
21 import io.netty.util.AttributeKey;
24 public class CoapAuthHandler extends ChannelDuplexHandler {
26 private static final AttributeKey<ChannelHandlerContext> keyAuthClient = AttributeKey
27 .newInstance("authCtx");
29 private class AccountHandler
30 extends SimpleChannelInboundHandler<CoapResponse> {
33 public void channelRead0(ChannelHandlerContext ctx, CoapResponse msg)
35 Logger.d("Receive response from account, forward to client");
37 ChannelHandlerContext ctxToDevice = ctx.channel()
38 .attr(keyAuthClient).get();
40 if (msg.getResponseCode() == CoapStatus.CREATED) {
41 Map<String, String> response = JSONUtil
42 .parseJSON(new String(msg.getPayload(), StandardCharsets.UTF_8));
44 String userId = response.get("userid");
46 ctxToDevice.channel().attr(Constants.Attribute_UserId)
49 msg.setPayload(cbor.encodingPayloadToCbor(response));
51 CoapAuthHandler authHandler = ctxToDevice.channel().pipeline()
52 .get(CoapAuthHandler.class);
54 ctxToDevice.channel().pipeline().remove(authHandler);
57 ctxToDevice.writeAndFlush(msg);
59 if (msg.getResponseCode() != CoapStatus.CREATED)
64 public void exceptionCaught(ChannelHandlerContext ctx,
66 cause.printStackTrace();
71 private CoapClient accountClient = new CoapClient();
73 public CoapAuthHandler(String accountAddress, int accountPort) {
75 accountClient.addHandler(new AccountHandler());
77 accountClient.startClient(
78 new InetSocketAddress(accountAddress, accountPort));
79 } catch (InterruptedException e) {
80 // TODO Auto-generated catch block
85 private Cbor<HashMap<Object, Object>> cbor = new Cbor<HashMap<Object, Object>>();
88 public void channelRead(ChannelHandlerContext ctx, Object msg)
91 if (msg instanceof CoapRequest) {
92 CoapRequest request = (CoapRequest) msg;
93 switch (request.getUriPath()) {
94 // This handler only used for initial handshake
95 case Constants.AUTH_URI:
96 HashMap<Object, Object> payloadData = cbor
97 .parsePayloadFromCbor(request.getPayload(),
100 JSONUtil.writeJSON(payloadData).getBytes(StandardCharsets.UTF_8));
101 accountClient.getChannelFuture().channel()
102 .attr(keyAuthClient).set(ctx);
103 accountClient.sendRequest(request);
106 case Constants.KEEP_ALIVE_URI:
107 super.channelRead(ctx, msg);
111 CoapResponse response = new CoapResponse(
112 CoapStatus.UNAUTHORIZED);
113 Logger.e("Sending UNAUTHORIZED to client");
114 ctx.writeAndFlush(response);
119 Logger.d("Invalid packet for authenticating");