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); } }
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); } }