예제 #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);
        }
예제 #2
0
 /// <summary>
 /// Registers the specified client in the application.
 /// </summary>
 /// <param name="client">The client instance.</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
 /// (
 ///   new GoogleClient
 ///   (
 ///     "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com",
 ///     "AeEbEGQqoKgOZb41JUVLvEJL"
 ///   )
 /// );
 ///
 /// OAuthManager.RegisterClient
 /// (
 ///   new FacebookClient
 ///   (
 ///     "1435890426686808",
 ///     "c6057dfae399beee9e8dc46a4182e8fd"
 ///   )
 /// );
 /// </code>
 /// <code lang="VB">
 /// OAuthManager.RegisterClient _
 /// (
 ///   New GoogleClient _
 ///   (
 ///     "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com",
 ///     "AeEbEGQqoKgOZb41JUVLvEJL"
 ///   )
 /// )
 ///
 /// OAuthManager.RegisterClient _
 /// (
 ///   New FacebookClient _
 ///   (
 ///     "1435890426686808",
 ///     "c6057dfae399beee9e8dc46a4182e8fd"
 ///   )
 /// )
 /// </code>
 /// </example>
 public static void RegisterClient(OAuthBase client)
 {
     if (client == null)
     {
         throw new ArgumentNullException("client");
     }
     if (_RegisteredClients.ContainsKey(client.ProviderName))
     {
         throw new DuplicateProviderException(client.ProviderName);
     }
     // add client
     _RegisteredClients.Add(client.ProviderName, client);
     // remove from watching
     OAuthManager.RemoveRequet(client.State.ToString());
 }
예제 #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);
        }
예제 #4
0
        /// <summary>
        /// Returns the authorization URL of the specified provider, query parameters and return URL.
        /// </summary>
        /// <param name="clientName">
        /// The provider name, through which it is necessary to authorize the current user; or the name of the registered client.
        /// </param>
        /// <param name="parameters">Additional parameters to be passed to the authorization URL.</param>
        /// <param name="returnUrl">The address to which the user is redirected after the authorization.</param>
        /// <param name="state">Custom state associated with authorization request.</param>
        /// <exception cref="NullHttpContextException">
        /// The exception that is thrown when you try to access methods that are designed for web projects.
        /// </exception>
        public static string GetAuthorizationUrl(ClientName clientName, NameValueCollection parameters, string returnUrl, object state)
        {
            if (!OAuthManager.RegisteredClients.ContainsKey(clientName))
            {
                throw new ClientIsNotRegisteredException();
            }

            // get normal client name
            clientName = OAuthManager.RegisteredClients.Keys.First(k => k == clientName);

            // create new instance of the client
            var client = OAuthManager.RegisteredClients[clientName].Clone(parameters, returnUrl);

            // add request
            OAuthManager.AddRequest(client.State, clientName, client, state);

            // return url
            return(client.AuthorizationUrl);
        }
예제 #5
0
        /// <summary>
        /// Initializes the <see cref="OAuthManager"/>.
        /// </summary>
        static OAuthManager()
        {
            OAuthManager.RegisteredClients = new Dictionary <ClientName, OAuthBase>();
            OAuthManager.AllClients        = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase);

            // get all clients
            var types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes().Where
                        (
                itm =>
                itm.BaseType != null &&
                (itm.BaseType == typeof(OAuthClient) || itm.BaseType == typeof(OAuth2Client))
                        );

            // creating clients list
            foreach (var t in types)
            {
                // var ppp = t.GetProperty("ProviderName").GetGetMethod();
                var param = new ArrayList();
                foreach (var p in t.GetConstructors().First().GetParameters())
                {
                    if (p.ParameterType == typeof(string))
                    {
                        param.Add("1");
                    }
                    else
                    {
                        throw new NotSupportedException(String.Format("Supported only string data types for parameters constructors of providers class. Please, check {0} class.", t.Name));
                    }
                }
                var client = Activator.CreateInstance(t, param.ToArray()) as OAuthBase;
                OAuthManager.AllClients.Add(client.ProviderName, t);
                // OAuthManager.RemoveRequest(client.State);
            }
            // --

            // create request provider
            OAuthManager.SetAuthRequestsProvider(typeof(DefaultOAuthRequestsProvider));
        }
예제 #6
0
 /// <summary>
 /// Registers the specified client in the application.
 /// </summary>
 /// <param name="providerName">The provider name.</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="scope">List of scope that will be requested from the provider. Only for OAuth 2.0.</param>
 /// <param name="initArgs">Additional parameters to be passed to the constructor of the client class.</param>
 /// <param name="parameters">Additional parameters that will be transferred to the provider website.</param>
 /// <exception cref="ArgumentNullException"><paramref name="providerName"/>, <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="providerName"/>.</exception>
 /// <exception cref="NotSupportedException">The <paramref name="providerName"/> 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>
 /// </example>
 public static void RegisterClient(string providerName, string clientId, string clientSecret, string scope = null, NameValueCollection parameters = null, object[] initArgs = null)
 {
     OAuthManager.RegisterClient(ClientName.Parse(providerName), clientId, clientSecret, scope, parameters, initArgs);
 }
예제 #7
0
 /// <summary>
 /// Registers the specified client in the application.
 /// </summary>
 /// <param name="providerName">The provider name.</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>
 /// <exception cref="ArgumentNullException"><paramref name="providerName"/>, <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="providerName"/>.</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>
 /// </example>
 public static void RegisterClient(string providerName, string clientId, string clientSecret)
 {
     OAuthManager.RegisterClient(ClientName.Parse(providerName), clientId, clientSecret, null, null, null);
 }
예제 #8
0
 /// <summary>
 /// Registers the specified client in the application.
 /// </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(ClientName clientName, OAuthBase client)
 {
     OAuthManager.RegisterClient(clientName.Key, client);
 }
예제 #9
0
 /// <summary>
 /// Registers the specified client in the application.
 /// </summary>
 /// <param name="client">The client instance.</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
 /// (
 ///   new GoogleClient
 ///   (
 ///     "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com",
 ///     "AeEbEGQqoKgOZb41JUVLvEJL"
 ///   )
 /// );
 ///
 /// OAuthManager.RegisterClient
 /// (
 ///   new FacebookClient
 ///   (
 ///     "1435890426686808",
 ///     "c6057dfae399beee9e8dc46a4182e8fd"
 ///   )
 /// );
 /// </code>
 /// <code lang="VB">
 /// OAuthManager.RegisterClient _
 /// (
 ///   New GoogleClient _
 ///   (
 ///     "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com",
 ///     "AeEbEGQqoKgOZb41JUVLvEJL"
 ///   )
 /// )
 ///
 /// OAuthManager.RegisterClient _
 /// (
 ///   New FacebookClient _
 ///   (
 ///     "1435890426686808",
 ///     "c6057dfae399beee9e8dc46a4182e8fd"
 ///   )
 /// )
 /// </code>
 /// </example>
 public static void RegisterClient(OAuthBase client)
 {
     OAuthManager.RegisterClient(String.Empty, client);
 }
예제 #10
0
 /// <summary>
 /// Adds the specified request to the collection.
 /// </summary>
 /// <param name="key">The unique request key.</param>
 /// <param name="clientName">The client name.</param>
 /// <param name="client">The client instance.</param>
 internal static void AddRequest(string key, ClientName clientName, OAuthBase client)
 {
     OAuthManager.AddRequest(key, clientName, client, null);
 }