class Mongo::Auth::Unauthorized

Raised when a user is not authorized on a database.

@since 2.0.0

Public Class Methods

new(user, used_mechanism: nil, message: nil, server: nil ) click to toggle source

Instantiate the new error.

@example Instantiate the error.

Mongo::Auth::Unauthorized.new(user)

@param [ Mongo::Auth::User ] user The unauthorized user. @param [ String ] used_mechanism Auth mechanism actually used for

authentication. This is a full string like SCRAM-SHA-256

@param [ String ] message The error message returned by the server. @param [ Server ] server The server instance that authentication

was attempted against.

@since 2.0.0

Calls superclass method
# File lib/mongo/auth.rb, line 117
def initialize(user, used_mechanism: nil, message: nil,
  server: nil
)
  configured_bits = []
  used_bits = [
    "auth source: #{user.auth_source}",
  ]

  if user.mechanism
    configured_bits << "mechanism: #{user.mechanism}"
  end

  if used_mechanism
    used_bits << "used mechanism: #{used_mechanism}"
  end

  if server
    used_bits << "used server: #{server.address} (#{server.status})"
  end

  used_user = if user.mechanism == :mongodb_x509
    'Client certificate'
  else
    "User #{user.name}"
  end

  if configured_bits.empty?
    configured_bits = ''
  else
    configured_bits = " (#{configured_bits.join(', ')})"
  end

  used_bits = " (#{used_bits.join(', ')})"

  msg = "#{used_user}#{configured_bits} is not authorized to access #{user.database}#{used_bits}"
  if message
    msg += ': ' + message
  end
  super(msg)
end