Class: Cassette::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/cassette/authentication.rb,
lib/cassette/authentication/user.rb,
lib/cassette/authentication/cache.rb,
lib/cassette/authentication/filter.rb,
lib/cassette/authentication/authorities.rb

Defined Under Namespace

Modules: Filter Classes: Authorities, Cache, User

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Authentication

Returns a new instance of Authentication


10
11
12
13
14
15
# File 'lib/cassette/authentication.rb', line 10

def initialize(opts = {})
  self.config = opts.fetch(:config, Cassette.config)
  self.logger = opts.fetch(:logger, Cassette.logger)
  self.http   = opts.fetch(:http_client, Cassette::Http::Request.new(config))
  self.cache  = opts.fetch(:cache, Cassette::Authentication::Cache.new(logger))
end

Class Method Details

.method_missing(name, *args) ⇒ Object


5
6
7
8
# File 'lib/cassette/authentication.rb', line 5

def self.method_missing(name, *args)
  @default_authentication ||= new
  @default_authentication.send(name, *args)
end

Instance Method Details

#ticket_user(ticket, service = config.service) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cassette/authentication.rb', line 29

def ticket_user(ticket, service = config.service)
  cache.fetch_authentication(ticket, service) do
    begin
      logger.info("Validating #{ticket} on #{validate_path}")

      response = http.post(validate_path, ticket: ticket, service: service).body
      ticket_response = Http::TicketResponse.new(response)

      logger.info("Validation resut: #{response.inspect}")

      Cassette::Authentication::User.new(
        login: ticket_response.,
        name: ticket_response.name,
        authorities: ticket_response.authorities,
        ticket: ticket,
        config: config
      ) if ticket_response.
    rescue => exception
      logger.error "Error while authenticating ticket #{ticket}: #{exception.message}"
      raise Cassette::Errors::Forbidden, exception.message
    end
  end
end

#validate_ticket(ticket, service = config.service) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cassette/authentication.rb', line 17

def validate_ticket(ticket, service = config.service)
  logger.debug "Cassette::Authentication validating ticket: #{ticket}, #{service}"
  fail Cassette::Errors::AuthorizationRequired if ticket.blank?

  user = ticket_user(ticket, service)
  logger.info "Cassette::Authentication user: #{user.inspect}"

  fail Cassette::Errors::Forbidden unless user

  user
end