示例#1
0
        private bool IsExemptFromRoles(AuthorizationInfo auth, IAuthenticationAttributes authAttribtues, AuthenticationInfo tokenInfo)
        {
            if (!_config.Configuration.IsStartupWizardCompleted &&
                authAttribtues.AllowBeforeStartupWizard)
            {
                return true;
            }

            if (string.IsNullOrWhiteSpace(auth.Token))
            {
                return true;
            }

            if (tokenInfo != null && string.IsNullOrWhiteSpace(tokenInfo.UserId))
            {
                return true;
            }

            return false;
        }
示例#2
0
        private async Task<string> GetAuthorizationToken(string userId, string deviceId, string app, string deviceName)
        {
            var existing = _authRepo.Get(new AuthenticationInfoQuery
            {
                DeviceId = deviceId,
                IsActive = true,
                UserId = userId,
                Limit = 1
            });

            if (existing.Items.Length > 0)
            {
                _logger.Debug("Reissuing access token");
                return existing.Items[0].AccessToken;
            }

            var newToken = new AuthenticationInfo
            {
                AppName = app,
                DateCreated = DateTime.UtcNow,
                DeviceId = deviceId,
                DeviceName = deviceName,
                UserId = userId,
                IsActive = true,
                AccessToken = Guid.NewGuid().ToString("N")
            };

            _logger.Debug("Creating new access token for user {0}", userId);
            await _authRepo.Create(newToken, CancellationToken.None).ConfigureAwait(false);

            return newToken.AccessToken;
        }
        public Task Create(AuthenticationInfo info, CancellationToken cancellationToken)
        {
            info.Id = Guid.NewGuid().ToString("N");

            return Update(info, cancellationToken);
        }
        public async Task Update(AuthenticationInfo info, CancellationToken cancellationToken)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }

            cancellationToken.ThrowIfCancellationRequested();

            await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);

            IDbTransaction transaction = null;

            try
            {
                transaction = _connection.BeginTransaction();

                var index = 0;

                _saveInfoCommand.GetParameter(index++).Value = new Guid(info.Id);
                _saveInfoCommand.GetParameter(index++).Value = info.AccessToken;
                _saveInfoCommand.GetParameter(index++).Value = info.DeviceId;
                _saveInfoCommand.GetParameter(index++).Value = info.AppName;
                _saveInfoCommand.GetParameter(index++).Value = info.AppVersion;
                _saveInfoCommand.GetParameter(index++).Value = info.DeviceName;
                _saveInfoCommand.GetParameter(index++).Value = info.UserId;
                _saveInfoCommand.GetParameter(index++).Value = info.IsActive;
                _saveInfoCommand.GetParameter(index++).Value = info.DateCreated;
                _saveInfoCommand.GetParameter(index++).Value = info.DateRevoked;

                _saveInfoCommand.Transaction = transaction;

                _saveInfoCommand.ExecuteNonQuery();

                transaction.Commit();
            }
            catch (OperationCanceledException)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }

                throw;
            }
            catch (Exception e)
            {
                Logger.ErrorException("Failed to save record:", e);

                if (transaction != null)
                {
                    transaction.Rollback();
                }

                throw;
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Dispose();
                }

                WriteLock.Release();
            }
        }
        private AuthenticationInfo Get(IDataReader reader)
        {
            var info = new AuthenticationInfo
            {
                Id = reader.GetGuid(0).ToString("N"),
                AccessToken = reader.GetString(1)
            };

            if (!reader.IsDBNull(2))
            {
                info.DeviceId = reader.GetString(2);
            }

            if (!reader.IsDBNull(3))
            {
                info.AppName = reader.GetString(3);
            }

            if (!reader.IsDBNull(4))
            {
                info.AppVersion = reader.GetString(4);
            }

            if (!reader.IsDBNull(5))
            {
                info.DeviceName = reader.GetString(5);
            }

            if (!reader.IsDBNull(6))
            {
                info.UserId = reader.GetString(6);
            }

            info.IsActive = reader.GetBoolean(7);
            info.DateCreated = reader.GetDateTime(8).ToUniversalTime();

            if (!reader.IsDBNull(9))
            {
                info.DateRevoked = reader.GetDateTime(9).ToUniversalTime();
            }

            return info;
        }