Example #1
0
        /// <summary>
        /// Creates an Api client which uses a username and password for Basic authorization. This method carries security risks and will not be available to JustGiving users with different user account types (such as Facebook).
        /// </summary>
        /// <param name="appId">The Application ID you received when you registered your app on the JustGiving developer portal.</param>
        /// <param name="applicationKey">If you created a Application Key for this app in the Developer Portal, you must include it here for your app to work.</param>
        /// <param name="basicAuthCredential">The username and password of a registered JustGiving user.</param>
        public JustGivingApiClient(string appId, string applicationKey, BasicCredential basicAuthCredential)
        {
            if (string.IsNullOrWhiteSpace(appId))
            {
                throw new ArgumentException("You must provide an appId");
            }

            if (string.IsNullOrWhiteSpace(applicationKey))
            {
                throw new ArgumentException("Application Key cannot be empty. If you have not created an Application Key for your application, use the overloaded version of this constructor.");
            }

            if (basicAuthCredential == null)
            {
                throw new ArgumentException("You must provide a credential");
            }

            _options = new ClientOptions(appId, applicationKey)
            {
                AuthorizationMode = AuthorizationMode.Basic,
                Endpoint          = Endpoints.Production,
                BasicAuthSettings = basicAuthCredential,
                LoggingOptions    = LoggingOptions.Default
            };
        }
Example #2
0
        /// <summary>
        /// Creates an Api client which uses a username and password for Basic authorization. This method carries security risks and will not be available to JustGiving users with different user account types (such as Facebook).
        /// </summary>
        /// <param name="appId">The Application ID you received when you registered your app on the JustGiving developer portal.</param>
        /// <param name="basicAuthCredential">The username and password of a registered JustGiving user.</param>
        public JustGivingApiClient(string appId, BasicCredential basicAuthCredential)
        {
            if (string.IsNullOrWhiteSpace(appId))
            {
                throw new ArgumentException("You must provide an appId");
            }

            if (basicAuthCredential == null)
            {
                throw new ArgumentException("You must provide a credential");
            }

            _options = new ClientOptions(appId)
            {
                AuthorizationMode = AuthorizationMode.Basic,
                Endpoint          = Endpoints.Production,
                BasicAuthSettings = basicAuthCredential,
                LoggingOptions    = LoggingOptions.Default
            };
        }
Example #3
0
        /// <remarks>
        /// Note that Dispose() may be called during running this method.
        /// That is, system shutdown or suspend may cause Dispose() call
        /// while it executes runner.GetCredential(), which may be opening a CredentialDialog.
        /// In this case, runner.GetCredential() will return in the next turn
        /// on the GUI thread cycle after Dispose() is called.
        /// </remarks>
        public BasicCredential GetServerBasicCredentials(string endPoint, string realm, bool firstRequest, BasicCredential oldBasicCredentials)
        {
            // argument checks
            if (endPoint == null)
            {
                throw new ArgumentNullException(nameof(endPoint));
            }
            // realm can be null
            // oldBasicCredentials can be null

            BasicCredential basicCredential;
            IDictionary <string, BasicCredential> basicCredentialCache;
            IProxyRunner runner;

            // the value of the this.serverBasicCredentialCache field is synchronized by locking this
            lock (this) {
                // state checks
                basicCredentialCache = this.serverBasicCredentialCache;
                if (basicCredentialCache == null)
                {
                    throw new ObjectDisposedException(this.ComponentName);
                }
                runner = this.Runner;
            }

            // the contents of the this.serverBasicCredentialCache are synchronized by locking this.credentialCacheLocker
            lock (this.credentialCacheLocker) {
                // get value from the cache
                basicCredentialCache.TryGetValue(endPoint, out basicCredential);

                // need a new credential?
                bool needGetCredential;
                if (basicCredential == null)
                {
                    needGetCredential = !firstRequest;
                }
                else
                {
                    // try the current credential if the current revision is newer than the caller's.
                    needGetCredential = ((oldBasicCredentials != null) && (basicCredential.Revision <= oldBasicCredentials.Revision));
                }

                // get the credential from the runner
                if (needGetCredential)
                {
                    // figure out the next revision number
                    int revision = 1;
                    if (basicCredential != null)
                    {
                        revision = basicCredential.Revision;
                        if (int.MaxValue <= revision)
                        {
                            throw new Exception("An internal counter was overflowed.");
                        }
                        ++revision;
                    }

                    // get the credential from the runner
                    CredentialSettings credential = null;
                    if (runner != null)
                    {
                        credential = runner.GetCredential(endPoint, realm, needUpdate: (basicCredential != null));
                    }
                    if (credential == null)
                    {
                        // maybe user cancel entering a credential
                        basicCredential = null;
                    }
                    else
                    {
                        basicCredential = new BasicCredential(revision, credential.EnableAssumptionMode, CreateBasicProxyAuthorizationBytes(credential.GetNetworkCredential()));
                    }

                    // update the cache
                    if (basicCredential == null || credential.Persistence == CredentialPersistence.Session)
                    {
                        basicCredentialCache.Remove(endPoint);
                    }
                    else
                    {
                        basicCredentialCache[endPoint] = basicCredential;
                    }
                }
            }

            // adjust for first time
            if (firstRequest && basicCredential != null && basicCredential.EnableAssumptionMode == false)
            {
                // In the first request, the basic credential is returned only if the AssumptionMode is enabled
                basicCredential = null;
            }

            return(basicCredential);
        }