Exemple #1
0
        public async Task <InvokeResult <AuthResponse> > RefreshTokenGrantAsync(AuthRequest authRequest)
        {
            var requestValidationResult = _authRequestValidators.ValidateAuthRequest(authRequest);

            if (!requestValidationResult.Successful)
            {
                return(InvokeResult <AuthResponse> .FromInvokeResult(requestValidationResult));
            }

            var refreshTokenRequestValidationResult = _authRequestValidators.ValidateRefreshTokenGrant(authRequest);

            if (!refreshTokenRequestValidationResult.Successful)
            {
                return(InvokeResult <AuthResponse> .FromInvokeResult(refreshTokenRequestValidationResult));
            }

            var appUser = await _userManager.FindByNameAsync(authRequest.UserName);

            if (appUser == null)
            {
                _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Error, "AuthTokenManager_RefreshTokenGrantAsync", UserAdminErrorCodes.AuthCouldNotFindUserAccount.Message, new KeyValuePair <string, string>("id", authRequest.UserName));
                return(InvokeResult <AuthResponse> .FromErrors(UserAdminErrorCodes.AuthCouldNotFindUserAccount.ToErrorMessage()));
            }

            if (!String.IsNullOrEmpty(authRequest.OrgId) && (appUser.CurrentOrganization == null || authRequest.OrgId != appUser.CurrentOrganization.Id))
            {
                var changeOrgResult = await _orgHelper.SetUserOrgAsync(authRequest, appUser);

                if (!changeOrgResult.Successful)
                {
                    return(InvokeResult <AuthResponse> .FromInvokeResult(changeOrgResult));
                }
            }

            var updateLastRefreshTokenResult = (await _appInstanceManager.UpdateLastAccessTokenRefreshAsync(appUser.Id, authRequest));

            if (updateLastRefreshTokenResult.Successful)
            {
                authRequest.AppInstanceId = updateLastRefreshTokenResult.Result.RowKey;
                var refreshTokenResponse = await _refreshTokenManager.RenewRefreshTokenAsync(authRequest.RefreshToken, appUser.Id);

                _adminLogger.LogInvokeResult("AuthTokenManager_RefreshTokenGrantAsync", refreshTokenResponse);
                return(_tokenHelper.GenerateAuthResponse(appUser, authRequest, refreshTokenResponse));
            }
            else
            {
                return(InvokeResult <AuthResponse> .FromInvokeResult(updateLastRefreshTokenResult.ToInvokeResult()));
            }
        }