Exception: Mongo::Auth::Unauthorized

Inherits:
Error::AuthError show all
Includes:
Error::Labelable, Error::ReadWriteRetryable
Defined in:
lib/mongo/auth.rb

Overview

Raised when a user is not authorized on a database.

Since:

  • 2.0.0

Constant Summary

Constants included from Error::ReadWriteRetryable

Error::ReadWriteRetryable::RETRY_MESSAGES, Error::ReadWriteRetryable::WRITE_RETRY_ERRORS, Error::ReadWriteRetryable::WRITE_RETRY_MESSAGES

Instance Attribute Summary collapse

Attributes included from Error::Notable

#connection_global_id, #generation, #service_id

Instance Method Summary collapse

Methods included from Error::Labelable

#add_label, #label?, #labels

Methods included from Error::ReadWriteRetryable

#retryable?, #write_retryable?

Methods included from Error::Notable

#add_note, #add_notes, #notes, #to_s

Constructor Details

#initialize(user, used_mechanism: nil, message: nil, server: nil, code: nil) ⇒ Unauthorized

Instantiate the new error.

Examples:

Instantiate the error.

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

Parameters:

  • user (Mongo::Auth::User)

    The unauthorized user.

  • used_mechanism (String) (defaults to: nil)

    Auth mechanism actually used for authentication. This is a full string like SCRAM-SHA-256.

  • message (String) (defaults to: nil)

    The error message returned by the server.

  • server (Server) (defaults to: nil)

    The server instance that authentication was attempted against.

  • The (Integer)

    error code.

Since:

  • 2.0.0



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/mongo/auth.rb', line 150

def initialize(user, used_mechanism: nil, message: nil,
               server: nil, code: nil)
  @code = code

  configured_bits = []
  used_bits = [
    "auth source: #{user.auth_source}",
  ]

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

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

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

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

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

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

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

Instance Attribute Details

#codeInteger (readonly)

Returns The error code.

Returns:

  • (Integer)

    The error code.

Since:

  • 2.0.0



134
135
136
# File 'lib/mongo/auth.rb', line 134

def code
  @code
end