Class: PadlockAuth::Http::ForbiddenTokenResponse

Inherits:
ErrorResponse show all
Defined in:
lib/padlock_auth/http/forbidden_token_response.rb

Overview

A response for a forbidden token.

A forbidden token response is returned when a token is valid, but does not have the required scopes.

Instance Attribute Summary collapse

Attributes inherited from ErrorResponse

#name, #status

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ErrorResponse

#body, #raise_exception!

Constructor Details

#initialize(attributes = {}) ⇒ ForbiddenTokenResponse

Create a new forbidden token response.

Parameters:

  • attributes (Hash) (defaults to: {})

    Attributes



31
32
33
34
35
# File 'lib/padlock_auth/http/forbidden_token_response.rb', line 31

def initialize(attributes = {})
  super(attributes.merge(name: :invalid_scope, status: :forbidden))
  @reason = attributes[:reason] || :unknown
  @scopes = attributes[:scopes]
end

Instance Attribute Details

#descriptionString (readonly)

Returns A translated description of the error.

Returns:

  • (String)

    A translated description of the error



40
41
42
43
44
45
46
47
48
# File 'lib/padlock_auth/http/forbidden_token_response.rb', line 40

def description
  @description ||=
    I18n.translate(
      @reason,
      scope: i[padlock_auth errors messages forbidden_token],
      oauth_scopes: @scopes.map(&:to_s).join(" "),
      default: :unknown
    )
end

#reasonObject (readonly)

Returns the value of attribute reason.



12
13
14
# File 'lib/padlock_auth/http/forbidden_token_response.rb', line 12

def reason
  @reason
end

Class Method Details

.from_access_token(access_token, scopes, attributes = {}) ⇒ Object

Create a new forbidden token response from an access token.

Parameters:

  • access_token (PadlockAuth::AbstractAccessToken)

    Access token

  • scopes (Array<String>)

    Required scopes

  • attributes (Hash) (defaults to: {})

    Additional attributes



23
24
25
# File 'lib/padlock_auth/http/forbidden_token_response.rb', line 23

def self.from_access_token(access_token, scopes, attributes = {})
  new(attributes.merge(reason: access_token&.forbidden_token_reason, scopes: scopes))
end

Instance Method Details

#headersHash

Returns HTTP headers.

Returns:

  • (Hash)

    HTTP headers



52
53
54
55
56
# File 'lib/padlock_auth/http/forbidden_token_response.rb', line 52

def headers
  headers = super
  headers.delete "WWW-Authenticate" # Authentication was successful, so no need to display auth error info
  headers
end