Esempio n. 1
0
        /// <summary>
        /// Performs authentication with an upstream update service.
        /// </summary>
        /// <returns>A new access token.</returns>
        public async Task <ServiceAccessToken> Authenticate()
        {
            ServiceAccessToken newAccessToken = new ServiceAccessToken();

            newAccessToken.AuthenticationInfo = (await GetAuthenticationInfo()).ToList();
            newAccessToken.AuthCookie         = await GetAuthorizationCookie(newAccessToken.AuthenticationInfo[0]);

            newAccessToken.AccessCookie = await GetServerAccessCookie(newAccessToken.AuthCookie);

            return(newAccessToken);
        }
Esempio n. 2
0
        /// <summary>
        /// Updates the access token of this client
        /// </summary>
        /// <returns>Nothing</returns>
        internal async Task RefreshAccessToken(string accountName, Guid?accountGuid)
        {
            var progress = new MetadataQueryProgress();

            progress.CurrentTask = MetadataQueryStage.AuthenticateStart;
            MetadataQueryProgress?.Invoke(this, progress);

            var authenticator = new ClientAuthenticator(UpstreamEndpoint, accountName, accountGuid.HasValue ? accountGuid.Value : new Guid());

            AccessToken = await authenticator.Authenticate(AccessToken);

            progress.CurrentTask = MetadataQueryStage.AuthenticateEnd;
            MetadataQueryProgress?.Invoke(this, progress);
        }
Esempio n. 3
0
        /// <summary>
        /// Performs authentication with an upstream update server, using a previously issued service access token.
        /// </summary>
        /// <remarks>
        /// Refreshing an old token with this method is faster than obtaining a new token as it requires fewer server roundtrips.
        ///
        /// If the access cookie does not expire within 30 minutes, the function succeeds and the old token is returned.
        /// </remarks>
        /// <param name="cachedAccessToken">The previously issued access token.</param>
        /// <returns>The new ServiceAccessToken</returns>
        public async Task <ServiceAccessToken> Authenticate(ServiceAccessToken cachedAccessToken)
        {
            if (cachedAccessToken == null)
            {
                return(await Authenticate());
            }

            ServiceAccessToken newAccessToken = new ServiceAccessToken()
            {
                AuthCookie         = cachedAccessToken.AuthCookie,
                AccessCookie       = cachedAccessToken.AccessCookie,
                AuthenticationInfo = cachedAccessToken.AuthenticationInfo
            };

            // Check if the cached access cookie expires in the next 30 minutes; if not, return the new token
            // with this cookie
            if (!newAccessToken.ExpiresIn(TimeSpan.FromMinutes(30)))
            {
                return(newAccessToken);
            }

            bool restartAuthenticationRequired = false;

            // Get a new access cookie
            try
            {
                newAccessToken.AccessCookie = await GetServerAccessCookie(newAccessToken.AuthCookie);
            }
            catch (UpstreamServerException ex)
            {
                if (ex.ErrorCode == UpstreamServerErrorCode.InvalidAuthorizationCookie)
                {
                    // The authorization cookie is expired or invalid. Restart the authentication protocol
                    restartAuthenticationRequired = true;
                }
                else
                {
                    throw ex;
                }
            }

            return(restartAuthenticationRequired ? await Authenticate() : newAccessToken);
        }