Esempio n. 1
0
        public static CoapMessageHandler Create(CoapSession session, CoapMessage message,
                                                ICoapRequestDispatch dispatcher = null)
        {
            if (message.Code == CodeType.EmptyMessage && message.MessageType == CoapMessageType.Confirmable)
            {
                return(new CoapPingHandler(session, message));
            }

            if (message.Code == CodeType.POST)
            {
                return(new CoapPostHandler(session, message, dispatcher));
            }

            if (message.Code == CodeType.PUT)
            {
                return(new CoapPutHandler(session, message, dispatcher));
            }

            if (message.Code == CodeType.GET)
            {
                return(new CoapObserveHandler(session, message, dispatcher));
            }

            if (message.Code == CodeType.DELETE)
            {
                return(new CoapDeleteHandler(session, message, dispatcher));
            }

            if (message.MessageType == CoapMessageType.Reset)
            {
                return(new CoapRstHandler(session, message));
            }

            return(new CoapResponseHandler(session, message));
        }
Esempio n. 2
0
 public PiraeusCoapClient(CoapConfig config, IChannel channel, SecurityTokenType tokenType, string securityToken,
                          ICoapRequestDispatch dispatcher = null)
     : this(config, channel, dispatcher)
 {
     this.tokenType     = tokenType;
     this.securityToken = securityToken;
     usedToken          = false;
 }
Esempio n. 3
0
        private void Channel_OnOpen(object sender, ChannelOpenEventArgs e)
        {
            session.IsAuthenticated = Channel.IsAuthenticated;
            logger?.LogDebugAsync(
                $"CoAP protocol channel opening with session authenticated '{session.IsAuthenticated}'.").GetAwaiter();

            try
            {
                if (!Channel.IsAuthenticated && e.Message != null)
                {
                    CoapMessage msg     = CoapMessage.DecodeMessage(e.Message);
                    CoapUri     coapUri = new CoapUri(msg.ResourceUri.ToString());
                    session.IsAuthenticated = session.Authenticate(coapUri.TokenType, coapUri.SecurityToken);
                    logger?.LogDebugAsync(
                        $"CoAP protocol channel opening session authenticated '{session.IsAuthenticated}' by authenticator.")
                    .GetAwaiter();
                }

                if (session.IsAuthenticated)
                {
                    IdentityDecoder decoder = new IdentityDecoder(session.Config.IdentityClaimType, context,
                                                                  session.Config.Indexes);
                    session.Identity = decoder.Id;
                    session.Indexes  = decoder.Indexes;
                    logger?.LogDebugAsync($"CoAP protocol channel opening with session identity '{session.Identity}'.")
                    .GetAwaiter();

                    UserAuditRecord record = new UserAuditRecord(Channel.Id, session.Identity,
                                                                 session.Config.IdentityClaimType, Channel.TypeId, "COAP", "Granted", DateTime.UtcNow);
                    userAuditor?.WriteAuditRecordAsync(record).Ignore();
                }
            }
            catch (Exception ex)
            {
                logger?.LogErrorAsync(ex, $"CoAP adapter opening channel '{Channel.Id}'.").GetAwaiter();
                OnError?.Invoke(this, new ProtocolAdapterErrorEventArgs(Channel.Id, ex));
            }

            if (!session.IsAuthenticated && e.Message != null)
            {
                logger?.LogWarningAsync("CoAP adpater closing due to unauthenticated user.");
                Channel.CloseAsync().Ignore();
            }
            else
            {
                dispatcher = new CoapRequestDispatcher(session, Channel, config, graphManager, logger);
            }
        }
Esempio n. 4
0
        public PiraeusCoapClient(CoapConfig config, IChannel channel, ICoapRequestDispatch dispatcher = null)
        {
            this.config = config;
            pingId      = new List <ushort>();
            session     = new CoapSession(config);

            observers                   = new Dictionary <string, string>();
            this.dispatcher             = dispatcher;
            this.channel                = channel;
            this.channel.OnClose       += Channel_OnClose;
            this.channel.OnError       += Channel_OnError;
            this.channel.OnOpen        += Channel_OnOpen;
            this.channel.OnStateChange += Channel_OnStateChange;
            this.channel.OnReceive     += Channel_OnReceive;
            session.OnRetry            += Session_OnRetry;
            session.OnKeepAlive        += Session_OnKeepAlive;
            session.IsAuthenticated     = true;
            usedToken                   = true;
            queue = new Queue <byte[]>();
        }
Esempio n. 5
0
        private void Channel_OnOpen(object sender, ChannelOpenEventArgs e)
        {
            session.IsAuthenticated = Channel.IsAuthenticated;

            try
            {
                if (!Channel.IsAuthenticated && e.Message != null)
                {
                    CoapMessage msg     = CoapMessage.DecodeMessage(e.Message);
                    CoapUri     coapUri = new CoapUri(msg.ResourceUri.ToString());
                    session.IsAuthenticated = session.Authenticate(coapUri.TokenType, coapUri.SecurityToken);
                }

                if (session.IsAuthenticated)
                {
                    IdentityDecoder decoder = new IdentityDecoder(session.Config.IdentityClaimType, context, session.Config.Indexes);
                    session.Identity = decoder.Id;
                    session.Indexes  = decoder.Indexes;

                    UserAuditRecord record = new UserAuditRecord(Channel.Id, session.Identity, session.Config.IdentityClaimType, Channel.TypeId, "COAP", "Granted", DateTime.UtcNow);
                    userAuditor?.WriteAuditRecordAsync(record).Ignore();
                }
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, $"CoAP adapter opening channel '{Channel.Id}'.");
                OnError?.Invoke(this, new ProtocolAdapterErrorEventArgs(Channel.Id, ex));
            }

            if (!session.IsAuthenticated && e.Message != null)
            {
                //close the channel
                logger?.LogInformation($"CoAP adapter user not authenticated; must close channel '{Channel.Id}'.");
                Channel.CloseAsync().Ignore();
            }
            else
            {
                dispatcher = new CoapRequestDispatcher(session, Channel);
            }
        }
Esempio n. 6
0
 public CoapObserveHandler(CoapSession session, CoapMessage message, ICoapRequestDispatch dispatcher = null)
     : base(session, message, dispatcher)
 {
     session.EnsureAuthentication(message);
 }
Esempio n. 7
0
 protected CoapMessageHandler(CoapSession session, CoapMessage message, ICoapRequestDispatch dispatcher = null)
 {
     Session    = session;
     Message    = message;
     Dispatcher = dispatcher;
 }