Exemplo n.º 1
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                User user = await _userManager.Users.Include(x => x.RefreshTokens)
                            .SingleOrDefaultAsync(x => x.RefreshTokens.Any(y => y.Token == request.RefreshToken && y.UserId == x.Id), cancellationToken);

                if (user == null)
                {
                    throw new ValidationException(new[] { new ValidationFailure(nameof(request.RefreshToken), "No user found for supplied refresh token") });
                }

                RefreshToken existingToken = user.RefreshTokens.FirstOrDefault(x => x.Token == request.RefreshToken);

                existingToken.Revoke(request, _dateTimeProvider.GetUtcNow());

                _arpaContext.Set <RefreshToken>().Update(existingToken);

                if (await _arpaContext.SaveChangesAsync(cancellationToken) > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception($"Problem updating {existingToken.GetType().Name}");
            }
Exemplo n.º 2
0
        private async Task CreateRefreshTokenAsync(User user, string remoteIpAddress)
        {
            RefreshToken refreshToken = GernerateRefreshToken(user, remoteIpAddress);

            var cookieOptions = new CookieOptions
            {
                HttpOnly    = true,
                Expires     = refreshToken.ExpiryOn,
                IsEssential = true,
                SameSite    = SameSiteMode.None,
                Secure      = true
            };

            _httpContextAccessor.HttpContext.Response.Cookies.Append("refreshToken", refreshToken.Token, cookieOptions);

            user.RefreshTokens.Add(refreshToken);
            _arpaContext.Add(refreshToken);

            if (!(await _arpaContext.SaveChangesAsync(new CancellationToken()) > 0))
            {
                throw new Exception($"Problem creating {refreshToken.GetType().Name}");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Refreshes access token from refresh token.
        /// </summary>
        /// <exception cref="VMware.Horizon.RESTAPI.Client.ApiException">Thrown when fails to make API call</exception>
        /// <param name="body">Refresh token needed to generate new Access Token</param>
        /// <returns>Task of ApiResponse (AccessToken)</returns>
        public async System.Threading.Tasks.Task <ApiResponse <AccessToken> > RefreshAccessTokenAsyncWithHttpInfo(RefreshToken body)
        {
            // verify the required parameter 'body' is set
            if (body == null)
            {
                throw new ApiException(400, "Missing required parameter 'body' when calling AuthApi->RefreshAccessToken");
            }

            var    localVarPath         = "./refresh";
            var    localVarPathParams   = new Dictionary <String, String>();
            var    localVarQueryParams  = new List <KeyValuePair <String, String> >();
            var    localVarHeaderParams = new Dictionary <String, String>(this.Configuration.DefaultHeader);
            var    localVarFormParams   = new Dictionary <String, String>();
            var    localVarFileParams   = new Dictionary <String, FileParameter>();
            Object localVarPostBody     = null;

            // to determine the Content-Type header
            String[] localVarHttpContentTypes = new String[] {
                "application/json"
            };
            String localVarHttpContentType = this.Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes);

            // to determine the Accept header
            String[] localVarHttpHeaderAccepts = new String[] {
                "*/*"
            };
            String localVarHttpHeaderAccept = this.Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts);

            if (localVarHttpHeaderAccept != null)
            {
                localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept);
            }

            if (body != null && body.GetType() != typeof(byte[]))
            {
                localVarPostBody = this.Configuration.ApiClient.Serialize(body); // http body (model) parameter
            }
            else
            {
                localVarPostBody = body; // byte array
            }


            // make the HTTP request
            IRestResponse localVarResponse = (IRestResponse)await this.Configuration.ApiClient.CallApiAsync(localVarPath,
                                                                                                            Method.POST, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams,
                                                                                                            localVarPathParams, localVarHttpContentType);

            int localVarStatusCode = (int)localVarResponse.StatusCode;

            if (ExceptionFactory != null)
            {
                Exception exception = ExceptionFactory("RefreshAccessToken", localVarResponse);
                if (exception != null)
                {
                    throw exception;
                }
            }

            return(new ApiResponse <AccessToken>(localVarStatusCode,
                                                 localVarResponse.Headers.ToDictionary(x => x.Key, x => x.Value.ToString()),
                                                 (AccessToken)this.Configuration.ApiClient.Deserialize(localVarResponse, typeof(AccessToken))));
        }