Example #1
0
        public void GetToken(X509Certificate2 Certificate, TokenCallback Callback, object State)
#endif
        {
            if (!Certificate.HasPrivateKey)
            {
                throw new ArgumentException("Certificate must have private key.", nameof(Certificate));
            }

#if WINDOWS_UWP
            IBuffer Buffer = Certificate.GetCertificateBlob();

            CryptographicBuffer.CopyToByteArray(Buffer, out byte[] Bin);
            string Base64 = System.Convert.ToBase64String(Bin);
#else
            byte[] Bin    = Certificate.Export(X509ContentType.Cert);
            string Base64 = System.Convert.ToBase64String(Bin);
#endif
            this.client.SendIqGet(this.provisioningServerAddress, "<getToken xmlns='" + NamespaceProvisioningToken + "'>" + Base64 + "</getToken>",
                                  this.GetTokenResponse, new object[] { Certificate, Callback, State });
        }
Example #2
0
        private void GetTokenChallengeResponse(object Sender, IqResultEventArgs e)
        {
            object[] P = (object[])e.State;
#if WINDOWS_UWP
            Certificate Certificate = (Certificate)P[0];
#else
            X509Certificate2 Certificate = (X509Certificate2)P[0];
#endif
            TokenCallback Callback = (TokenCallback)P[1];
            object        State    = P[2];
            XmlElement    E        = e.FirstElement;
            string        Token;

            if (e.Ok && E != null && E.LocalName == "getTokenResponse" && E.NamespaceURI == NamespaceProvisioningToken)
            {
                Token = XML.Attribute(E, "token");

                lock (this.certificates)
                {
                    this.certificates[Token] = new CertificateUse(Token, Certificate);
                }
            }
            else
            {
                Token = null;
            }

            TokenEventArgs e2 = new TokenEventArgs(e, State, Token);
            try
            {
                Callback(this, e2);
            }
            catch (Exception ex)
            {
                Log.Critical(ex);
            }
        }
 public void AddTokenDefinition(TTokenType type, string regex, RegexOptions regexOptions = RegexOptions.None, TokenCallback tokenCallback = null)
 {
     if (string.IsNullOrEmpty(regex))
     {
         throw new Exception("Regex cannot be null or empty");
     }
     if (finalized)
     {
         unfinalize();
     }
     if (tokenDefinitions.ContainsKey(type))
     {
         throw new Exception("A previous definition for the given token type already exists");
     }
     tokenDefinitions.Add(type, new TokenDefinition(regex, regexOptions, tokenCallback));
 }
 public TokenDefinition(string regex, RegexOptions regexOptions, TokenCallback tokenCallback)
 {
     Regex         = regex;
     RegexOptions  = regexOptions;
     TokenCallback = tokenCallback;
 }
Example #5
0
        public void GetToken(string frob, TokenCallback callback)
        {
            if (!Syncing)
            {
                User user = new User("rtm.is.unsynced", "IronCow is currently disabled");
                callback(string.Empty, user);
            }
            else
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("frob", frob);

                GetResponse("rtm.auth.getToken", parameters, (response) =>
                {
                    AuthToken = response.Authentication.Token;
                    User user = new User(response.Authentication.User);
                    callback(response.Authentication.Token, user);
                });
            }
        }
Example #6
0
        public void GetToken(string frob, TokenCallback callback)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("frob", frob);

            GetResponse("rtm.auth.getToken", parameters, (response) =>
            {
                AuthToken = response.Authentication.Token;
                callback(response.Authentication.Token, response.Authentication.User);
            });
        }
        private static IConfiguration BuildConfiguration(
            IServiceProvider serviceProvider)
        {
            IConfiguration toReturn = null;

            ConfigurationBuilder configurationBuilder =
                new ConfigurationBuilder();

            // Go to environment variables first...
            configurationBuilder.AddEnvironmentVariables();

            IStartupSettingsProvider startupSettingsProvider =
                serviceProvider.GetService <IStartupSettingsProvider>();

            string keyVaultInstanceName =
                startupSettingsProvider.KeyVaultInstanceName;

            if (!string.IsNullOrEmpty(keyVaultInstanceName))
            {
                string vault =
                    $"https://{keyVaultInstanceName}.vault.azure.net/";

                AzureServiceTokenProvider azureServiceTokenProvider =
                    new AzureServiceTokenProvider();

                TokenCallback keyVaultTokenCallback =
                    azureServiceTokenProvider.KeyVaultTokenCallback;

                KeyVaultClient.AuthenticationCallback authenticationCallback =
                    new KeyVaultClient.AuthenticationCallback(
                        keyVaultTokenCallback);

                KeyVaultClient keyVaultClient = new KeyVaultClient(
                    authenticationCallback);

                DefaultKeyVaultSecretManager defaultKeyVaultSecretManager =
                    new DefaultKeyVaultSecretManager();

                // Otherwise, KeyVault.
                configurationBuilder.AddAzureKeyVault(
                    vault,
                    keyVaultClient,
                    defaultKeyVaultSecretManager);
            }

            try
            {
                toReturn = configurationBuilder.Build();
            }
            catch (AzureServiceTokenProviderException azureServiceTokenProviderException)
            {
                throw new Exception(
                          $"This is likely happening because you're debugging, " +
                          $"and you haven't used the Azure CLI 2.0 tools to 'az " +
                          $"login'. Because KeyVault uses Managed Service " +
                          $"Identities, you need to do this first. If you'd " +
                          $"rather fall back to environment variables only, make " +
                          $"the setting value for " +
                          $"{nameof(IStartupSettingsProvider.KeyVaultInstanceName)} " +
                          $"null (or just omit it completely).",
                          azureServiceTokenProviderException);
            }

            return(toReturn);
        }
Example #8
0
        /// <summary>
        /// Gets a token for a certicate. This token can be used to identify services, devices or users. The provisioning server will
        /// challenge the request, and may choose to challenge it further when it is used, to make sure the sender is the correct holder
        /// of the private certificate.
        /// </summary>
        /// <param name="Certificate">Private certificate. Only the public part will be sent to the provisioning server. But the private
        /// part is required in order to be able to respond to challenges sent by the provisioning server.</param>
        /// <param name="Callback">Callback method called, when token is available.</param>
        /// <param name="State">State object that will be passed on to the callback method.</param>
#if WINDOWS_UWP
        public void GetToken(Certificate Certificate, TokenCallback Callback, object State)