Ejemplo n.º 1
0
        /// <summary>
        /// Registers the specified client in the application.
        /// </summary>
        /// <param name="clientName">The provider name. And may also contain any client name for for division into groups.</param>
        /// <param name="clientId">The application identifier obtained from the provider website.</param>
        /// <param name="clientSecret">The application secret key obtained from the provider website.</param>
        /// <param name="initArgs">Additional parameters to be passed to the constructor of the client class.</param>
        /// <param name="scope">List of scope that will be requested from the provider. Only for OAuth 2.0.</param>
        /// <param name="parameters">Additional parameters that will be transferred to the provider website.</param>
        /// <exception cref="ArgumentNullException"><paramref name="clientName"/>, <paramref name="clientId"/> or <paramref name="clientSecret"/> is <b>null</b> or <b>empty</b>.</exception>
        /// <exception cref="UnknownProviderException">Provider not found by <paramref name="clientName"/>.</exception>
        /// <exception cref="NotSupportedException">The <paramref name="clientName"/> not suppored <paramref name="scope"/>.</exception>
        /// <example>
        /// <code lang="C#">
        /// OAuthManager.RegisterClient
        /// (
        ///   "google",
        ///   "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com",
        ///   "AeEbEGQqoKgOZb41JUVLvEJL"
        /// );
        ///
        /// OAuthManager.RegisterClient
        /// (
        ///   "facebook"
        ///   "1435890426686808",
        ///   "c6057dfae399beee9e8dc46a4182e8fd"
        /// );
        /// </code>
        /// <code lang="VB">
        /// OAuthManager.RegisterClient _
        /// (
        ///   "google",
        ///   "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com",
        ///   "AeEbEGQqoKgOZb41JUVLvEJL"
        /// )
        ///
        /// OAuthManager.RegisterClient _
        /// (
        ///   "facebook",
        ///   "1435890426686808",
        ///   "c6057dfae399beee9e8dc46a4182e8fd"
        /// )
        /// </code>
        /// <para>
        /// You can register multiple clients to a single provider.
        /// The following example shows how to do it.
        /// </para>
        /// <code lang="C#">
        /// var clientName = ClientName.Create("Debug", "Facebook");
        ///
        /// OAuthManager.RegisterClient
        /// (
        ///   clientName
        ///   "000000000000000000",
        ///   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        /// );
        ///
        /// clientName = ClientName.Create("Any name", "Facebook");
        ///
        /// OAuthManager.RegisterClient
        /// (
        ///   clientName
        ///   "111111111111111111",
        ///   "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
        /// );
        /// </code>
        /// <code lang="VB">
        /// Dim name As ClientName = ClientName.Create("Debug", "Facebook")
        ///
        /// OAuthManager.RegisterClient _
        /// (
        ///   name,
        ///   "000000000000000000",
        ///   "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        /// )
        ///
        /// name As ClientName = ClientName.Create("Any name", "Facebook")
        ///
        /// OAuthManager.RegisterClient _
        /// (
        ///   name,
        ///   "111111111111111111",
        ///   "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
        /// )
        /// </code>
        /// </example>
        public static void RegisterClient(ClientName clientName, string clientId, string clientSecret, string scope = null, NameValueCollection parameters = null, object[] initArgs = null)
        {
            if (String.IsNullOrEmpty(clientName))
            {
                throw new ArgumentNullException("clientName");
            }
            if (String.IsNullOrEmpty(clientId))
            {
                throw new ArgumentNullException("clientId");
            }
            if (String.IsNullOrEmpty(clientSecret))
            {
                throw new ArgumentNullException("clientSecret");
            }

            // searching provider by name
            if (!OAuthManager.AllClients.ContainsKey(clientName.ProviderName))
            {
                throw new UnknownProviderException("Provider [{0}] not found. Please, check provider name.", clientName.ProviderName);
            }

            // init parameters
            var parm = new ArrayList();

            parm.Add(clientId);
            parm.Add(clientSecret);

            if (initArgs != null && initArgs.Length > 0)
            {
                parm.AddRange(initArgs);
            }

            // creating client instance
            OAuthBase client = Activator.CreateInstance(OAuthManager.AllClients[clientName.ProviderName], parm.ToArray()) as OAuthBase;

            if (!String.IsNullOrEmpty(scope))
            {
                if (client.GetType().BaseType != typeof(OAuth2Client))
                {
                    throw new NotSupportedException("The scope supported only for OAuth 2.0 clients.");
                }
                ((OAuth2Client)client).Scope = scope;
            }

            if (parameters != null)
            {
                client.Parameters = parameters;
            }

            // add client
            OAuthManager.RegisterClient(clientName.Key, client);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Registers the specified client in the application. (the main method)
        /// </summary>
        /// <param name="client">The client instance.</param>
        /// <param name="clientName">The any name of the client. For example: Test, Release, Project #1, Ku!, Example.org etc.</param>
        /// <exception cref="ArgumentNullException"><paramref name="client"/> is <b>null</b> or <b>empty</b>.</exception>
        /// <exception cref="DuplicateProviderException">If you attempt to register the already registered client.</exception>
        /// <example>
        /// <code lang="C#">
        /// OAuthManager.RegisterClient
        /// (
        ///   "Test",
        ///   new GoogleClient
        ///   (
        ///     "00000000000000.apps.googleusercontent.com",
        ///     "000000000000000000000000"
        ///   )
        /// );
        ///
        /// OAuthManager.RegisterClient
        /// (
        ///   "Test",
        ///   new FacebookClient
        ///   (
        ///     "00000000000000",
        ///     "000000000000000000000000"
        ///   )
        /// );
        ///
        /// OAuthManager.RegisterClient
        /// (
        ///   "Release",
        ///   new GoogleClient
        ///   (
        ///     "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com",
        ///     "AeEbEGQqoKgOZb41JUVLvEJL"
        ///   )
        /// );
        ///
        /// OAuthManager.RegisterClient
        /// (
        ///   "Release",
        ///   new FacebookClient
        ///   (
        ///     "1435890426686808",
        ///     "c6057dfae399beee9e8dc46a4182e8fd"
        ///   )
        /// );
        /// </code>
        /// <code lang="VB">
        /// OAuthManager.RegisterClient _
        /// (
        ///   "Test",
        ///   New GoogleClient _
        ///   (
        ///     "00000000000000.apps.googleusercontent.com",
        ///     "000000000000000000000000"
        ///   )
        /// )
        ///
        /// OAuthManager.RegisterClient _
        /// (
        ///   "Test",
        ///   New FacebookClient _
        ///   (
        ///     "00000000000000",
        ///     "000000000000000000000000"
        ///   )
        /// )
        ///
        /// OAuthManager.RegisterClient _
        /// (
        ///   "Release",
        ///   New GoogleClient _
        ///   (
        ///     "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com",
        ///     "AeEbEGQqoKgOZb41JUVLvEJL"
        ///   )
        /// )
        ///
        /// OAuthManager.RegisterClient _
        /// (
        ///   "Release",
        ///   New FacebookClient _
        ///   (
        ///     "1435890426686808",
        ///     "c6057dfae399beee9e8dc46a4182e8fd"
        ///   )
        /// )
        /// </code>
        /// </example>
        public static void RegisterClient(string clientName, OAuthBase client)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            /*if (client.ProviderName.Equals(ClientName.Parse(clientName).ProviderName, StringComparison.InvariantCultureIgnoreCase))
             * {
             * // contains provider name
             * clientName = ClientName.Escape(ClientName.Parse(clientName).Key);
             * }
             * else
             * {
             * clientName = ClientName.Escape(clientName);
             * }
             *
             * var name = ClientName.Create(ClientName.Parse(clientName).Key ?? ClientName.Unescape(clientName), client.ProviderName);
             */

            var name = ClientName.Create(clientName, client.ProviderName);

            if (OAuthManager.RegisteredClients.ContainsKey(name))
            {
                throw new DuplicateProviderException(name);
            }

            // add client
            OAuthManager.RegisteredClients.Add(name, client);

            // remove from watching
            // OAuthManager.RemoveRequest(client.State.ToString());
            // --

            // if this is a new client
            if (!OAuthManager.AllClients.ContainsKey(client.ProviderName))
            {
                // add to list
                OAuthManager.AllClients.Add(client.ProviderName, client.GetType());
            }

            /*else
             * {
             * // check namespace
             * if (!client.GetType().Namespace.Equals("Nemiro.OAuth.Clients", StringComparison.InvariantCultureIgnoreCase))
             * {
             *  // overwrite
             *  OAuthManager.AllClients[client.ProviderName] = client.GetType();
             * }
             * }*/
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a shallow copy of the current object.
        /// </summary>
        /// <returns>A shallow copy of the current object.</returns>
        /// <remarks>
        /// <para>Method creates a copy of the current object, removes tokens, change the return address, query parameters and state.</para>
        /// <para>Unfortunately, I made a mistake in architecture, so I had to make this method.</para>
        /// </remarks>
        /// <seealso cref="Clone(NameValueCollection, string)"/>
        public object Clone()
        {
            OAuthBase result = this.MemberwiseClone() as OAuthBase;

            result.State             = OAuthUtility.GetRandomKey();
            result.AccessToken       = null;
            result.AuthorizationCode = null;

            if (result.GetType().IsSubclassOf(typeof(OAuthClient)))
            {
                ((OAuthClient)result).RequestToken = null;
            }

            OAuthManager.AddRequet(result.State, result);

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a shallow copy of the current object.
        /// </summary>
        /// <returns>A shallow copy of the current object.</returns>
        /// <remarks>
        /// <para>Method creates a copy of the current object, removes tokens, change the return address, query parameters and state.</para>
        /// <para>Unfortunately, I made a mistake in architecture, so I had to make this method.</para>
        /// </remarks>
        /// <seealso cref="Clone(NameValueCollection, string)"/>
        public object Clone()
        {
            OAuthBase result = this.MemberwiseClone() as OAuthBase;

            result.State             = OAuthUtility.GetRandomKey();
            result.AccessToken       = null;
            result.AuthorizationCode = null;

            if (result.GetType().IsSubclassOf(typeof(OAuthClient)))
            {
                ((OAuthClient)result).RequestToken = null;
            }

            // I do not remember, why. I'll try to change it. // v1.8
            // OAuthManager.AddRequest(result.State, result.ProviderName, result);
            // --

            return(result);
        }
Ejemplo n.º 5
0
    /// <summary>
    /// Registers the specified client in the application. (the main method)
    /// </summary>
    /// <param name="client">The client instance.</param>
    /// <param name="clientName">The any name of the client. For example: Test, Release, Project #1, Ku!, Example.org etc.</param>
    /// <exception cref="ArgumentNullException"><paramref name="client"/> is <b>null</b> or <b>empty</b>.</exception>
    /// <exception cref="DuplicateProviderException">If you attempt to register the already registered client.</exception>
    /// <example>
    /// <code lang="C#">
    /// OAuthManager.RegisterClient
    /// (
    ///   "Test",
    ///   new GoogleClient
    ///   (
    ///     "00000000000000.apps.googleusercontent.com", 
    ///     "000000000000000000000000"
    ///   )
    /// );
    /// 
    /// OAuthManager.RegisterClient
    /// (
    ///   "Test",
    ///   new FacebookClient
    ///   (
    ///     "00000000000000", 
    ///     "000000000000000000000000"
    ///   )
    /// );
    /// 
    /// OAuthManager.RegisterClient
    /// (
    ///   "Release",
    ///   new GoogleClient
    ///   (
    ///     "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com", 
    ///     "AeEbEGQqoKgOZb41JUVLvEJL"
    ///   )
    /// );
    /// 
    /// OAuthManager.RegisterClient
    /// (
    ///   "Release",
    ///   new FacebookClient
    ///   (
    ///     "1435890426686808", 
    ///     "c6057dfae399beee9e8dc46a4182e8fd"
    ///   )
    /// );
    /// </code>
    /// <code lang="VB">
    /// OAuthManager.RegisterClient _
    /// (
    ///   "Test",
    ///   New GoogleClient _
    ///   (
    ///     "00000000000000.apps.googleusercontent.com", 
    ///     "000000000000000000000000"
    ///   )
    /// )
    /// 
    /// OAuthManager.RegisterClient _
    /// (
    ///   "Test",
    ///   New FacebookClient _
    ///   (
    ///     "00000000000000", 
    ///     "000000000000000000000000"
    ///   )
    /// )
    /// 
    /// OAuthManager.RegisterClient _
    /// (
    ///   "Release",
    ///   New GoogleClient _
    ///   (
    ///     "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com", 
    ///     "AeEbEGQqoKgOZb41JUVLvEJL"
    ///   )
    /// )
    /// 
    /// OAuthManager.RegisterClient _
    /// (
    ///   "Release",
    ///   New FacebookClient _
    ///   (
    ///     "1435890426686808", 
    ///     "c6057dfae399beee9e8dc46a4182e8fd"
    ///   )
    /// )
    /// </code>
    /// </example>
    public static void RegisterClient(string clientName, OAuthBase client)
    {
      if (client == null) { throw new ArgumentNullException("client"); }

      /*if (client.ProviderName.Equals(ClientName.Parse(clientName).ProviderName, StringComparison.InvariantCultureIgnoreCase))
      {
        // contains provider name
        clientName = ClientName.Escape(ClientName.Parse(clientName).Key);
      }
      else
      {
        clientName = ClientName.Escape(clientName);
      }

      var name = ClientName.Create(ClientName.Parse(clientName).Key ?? ClientName.Unescape(clientName), client.ProviderName);
      */

      var name = ClientName.Create(clientName, client.ProviderName);

      if (_RegisteredClients.ContainsKey(name))
      {
        throw new DuplicateProviderException(name);
      }

      // add client
      _RegisteredClients.Add(name, client);

      // remove from watching 
      // OAuthManager.RemoveRequest(client.State.ToString());
      // --

      // if this is a new client
      if (!OAuthManager.AllClients.ContainsKey(client.ProviderName))
      {
        // add to list
        OAuthManager.AllClients.Add(client.ProviderName, client.GetType());
      }
      /*else
      {
        // check namespace
        if (!client.GetType().Namespace.Equals("Nemiro.OAuth.Clients", StringComparison.InvariantCultureIgnoreCase))
        {
          // overwrite
          OAuthManager.AllClients[client.ProviderName] = client.GetType();
        }
      }*/
    }