예제 #1
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;
 }
예제 #2
0
        public async Task Invoke(HttpContext context)
        {
            if (!context.WebSockets.IsWebSocketRequest)
            {
                return;
            }

            BasicAuthenticator basicAuthn = new BasicAuthenticator();
            SecurityTokenType  tokenType  = Enum.Parse <SecurityTokenType>(config.ClientTokenType, true);

            basicAuthn.Add(tokenType, config.ClientSymmetricKey, config.ClientIssuer, config.ClientAudience, context);
            IAuthenticator authn = basicAuthn;

            WebSocket socket = await context.WebSockets.AcceptWebSocketAsync();

            source = new CancellationTokenSource();
            ProtocolAdapter adapter =
                ProtocolAdapterFactory.Create(config, graphManager, context, socket, logger, authn, source.Token);

            container.Add(adapter.Channel.Id, adapter);
            adapter.OnClose += Adapter_OnClose;
            adapter.OnError += Adapter_OnError;
            adapter.Init();

            await adapter.Channel.OpenAsync();

            await next(context);

            await logger.LogInformationAsync("Exiting Web socket invoke.");
        }
예제 #3
0
 public void Add(SecurityTokenType type, string signingKey, string issuer = null, string audience = null, HttpContext context = null)
 {
     this.context = context;
     if (!container.ContainsKey(type.ToString()))
     {
         Tuple <string, string, string> tuple = new Tuple <string, string, string>(signingKey, issuer, audience);
         container.Add(type.ToString(), tuple);
     }
 }
예제 #4
0
        public bool Authenticate(SecurityTokenType type, byte[] token)
        {
            if (token != null)
            {
                return Authenticate(type,
                    type == SecurityTokenType.X509 ? Convert.ToBase64String(token) : Encoding.UTF8.GetString(token));
            }

            return Authenticate(type, token);
        }
예제 #5
0
        public bool Authenticate(string tokenType, string token)
        {
            SecurityTokenType tt = (SecurityTokenType)Enum.Parse(typeof(SecurityTokenType), tokenType, true);

            bootstrapTokenType = tt;
            bootstrapToken     = token;
            HasBootstrapToken  = true;

            IsAuthenticated = Config.Authenticator.Authenticate(tt, token);
            return(IsAuthenticated);
        }
예제 #6
0
        public ConnectController(PiraeusConfig config, IClusterClient client, ILog logger)
        {
            this.config = config;
            BasicAuthenticator basicAuthn = new BasicAuthenticator();

            SecurityTokenType tokenType = Enum.Parse <SecurityTokenType>(config.ClientTokenType, true);

            basicAuthn.Add(tokenType, config.ClientSymmetricKey, config.ClientIssuer, config.ClientAudience);
            authn = basicAuthn;

            graphManager = new GraphManager(client);
            this.logger  = logger;
        }
예제 #7
0
        public UdpServerListener(PiraeusConfig config, IPEndPoint localEP, CancellationToken token)
        {
            this.config  = config;
            this.localEP = localEP;
            this.token   = token;
            cache        = new Dictionary <string, Tuple <ProtocolAdapter, CancellationTokenSource> >();
            container    = new Dictionary <string, string>();

            SecurityTokenType  stt    = System.Enum.Parse <SecurityTokenType>(config.ClientTokenType, true);
            BasicAuthenticator bauthn = new BasicAuthenticator();

            bauthn.Add(stt, config.ClientSymmetricKey, config.ClientIssuer, config.ClientAudience);
            authn = bauthn;
        }
예제 #8
0
        public bool Authenticate(SecurityTokenType type, string token)
        {
            if (container.ContainsKey(SecurityTokenType.NONE.ToString()) && type == SecurityTokenType.NONE)
            {
                return true;
            }

            if (token != null && container.ContainsKey(type.ToString()))
            {
                Tuple<string, string, string> tuple = container[type.ToString()];
                return SecurityTokenValidator.Validate(token, type, tuple.Item1, tuple.Item2, tuple.Item3, context);
            }

            return false;
        }
예제 #9
0
        public TcpServerListener(IPEndPoint localEP, PiraeusConfig config, ILogger logger = null, CancellationToken token = default(CancellationToken))
        {
            serverIP    = localEP.Address;
            serverPort  = localEP.Port;
            listener    = new TcpListener(localEP);
            this.token  = token;
            dict        = new Dictionary <string, ProtocolAdapter>();
            this.config = config;
            this.logger = logger;

            if (config.ClientTokenType != null && config.ClientSymmetricKey != null)
            {
                SecurityTokenType  stt    = Enum.Parse <SecurityTokenType>(config.ClientTokenType, true);
                BasicAuthenticator bauthn = new BasicAuthenticator();
                bauthn.Add(stt, config.ClientSymmetricKey, config.ClientIssuer, config.ClientAudience);
                this.authn = bauthn;
            }
        }
예제 #10
0
        public static bool Validate(string tokenString, SecurityTokenType tokenType, string securityKey,
                                    string issuer = null, string audience = null, HttpContext context = null)
        {
            if (tokenType == SecurityTokenType.NONE)
            {
                return(false);
            }

            if (tokenType == SecurityTokenType.JWT)
            {
                return(ValidateJwt(tokenString, securityKey, issuer, audience, context));
            }

            byte[]           certBytes = Convert.FromBase64String(tokenString);
            X509Certificate2 cert      = new X509Certificate2(certBytes);

            return(ValidateCertificate(cert, context));
        }
예제 #11
0
        public static SecurityTokenReference Get(XmlDocument envelopeDocument, SecurityTokenType type, ICertificateRepository certificateRepository)
        {
            XmlElement keyInfoElement;

            switch (type)
            {
            case SecurityTokenType.Signing:
                keyInfoElement =
                    envelopeDocument.SelectSingleNode(
                        @"//*[local-name()='Header']/*[local-name()='Security']/*[local-name()='Signature']/*[local-name()='KeyInfo']/*[local-name()='SecurityTokenReference']") as XmlElement;
                break;

            case SecurityTokenType.Encryption:
                keyInfoElement =
                    envelopeDocument.SelectSingleNode(
                        @"//*[local-name()='Header']/*[local-name()='Security']/*[local-name()='EncryptedKey']/*[local-name()='KeyInfo']/*[local-name()='SecurityTokenReference']") as XmlElement;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(type));
            }

            if (keyInfoElement == null)
            {
                return(null);
            }

            if (HasEnvelopeTag(keyInfoElement, securityTokenNodeName: "Reference"))
            {
                return(new BinarySecurityTokenReference(keyInfoElement));
            }

            if (HasEnvelopeTag(keyInfoElement, securityTokenNodeName: "KeyIdentifier"))
            {
                return(new KeyIdentifierSecurityTokenReference(keyInfoElement, certificateRepository));
            }

            if (HasEnvelopeTag(keyInfoElement, securityTokenNodeName: "X509Data"))
            {
                return(new IssuerSecurityTokenReference(keyInfoElement, certificateRepository));
            }

            throw new NotSupportedException("Unable to retrieve SecurityTokenReference of type " + keyInfoElement.OuterXml);
        }
예제 #12
0
        public TcpServerListener(IPAddress address, int port, PiraeusConfig config, ILogger logger = null, CancellationToken token = default(CancellationToken))
        {
            serverIP   = address;
            serverPort = port;
            listener   = new TcpListener(address, port);
            listener.ExclusiveAddressUse = false;
            this.token  = token;
            dict        = new Dictionary <string, ProtocolAdapter>();
            this.config = config;
            this.logger = logger;


            if (config.ClientTokenType != null && config.ClientSymmetricKey != null)
            {
                SecurityTokenType  stt    = (SecurityTokenType)System.Enum.Parse(typeof(SecurityTokenType), config.ClientTokenType, true);
                BasicAuthenticator bauthn = new BasicAuthenticator();
                bauthn.Add(stt, config.ClientSymmetricKey, config.ClientIssuer, config.ClientAudience);
                this.authn = bauthn;
            }
        }
예제 #13
0
        public UdpServerListener(IPEndPoint localEP, PiraeusConfig config, OrleansConfig orleansConfig,
                                 ILog logger = null, CancellationToken token = default)
        {
            this.localEP = localEP;
            listener     = new UdpClient();
            this.token   = token;
            dict         = new Dictionary <string, ProtocolAdapter>();
            this.config  = config;
            this.logger  = logger;
            graphManager = new GraphManager(orleansConfig);

            if (config.ClientTokenType != null && config.ClientSymmetricKey != null)
            {
                SecurityTokenType  stt    = Enum.Parse <SecurityTokenType>(config.ClientTokenType, true);
                BasicAuthenticator bauthn = new BasicAuthenticator();
                bauthn.Add(stt, config.ClientSymmetricKey, config.ClientIssuer, config.ClientAudience);
                authn = bauthn;
            }

            cache = MemoryCache.Default;
        }
예제 #14
0
 public static SecurityTokenType ToSecurityTokenType(this SecurityTokenType type, string tokenType)
 {
     return((SecurityTokenType)Enum.Parse(typeof(SecurityTokenType), tokenType, true));
 }
예제 #15
0
 public bool Remove(SecurityTokenType type)
 {
     return(container.Remove(type.ToString()));
 }