Class: SkullIsland::Resources::BasicauthCredential

Inherits:
SkullIsland::Resource show all
Defined in:
lib/skull_island/resources/basicauth_credential.rb

Overview

The BasicauthCredential resource class

Instance Attribute Summary collapse

Attributes inherited from SkullIsland::Resource

#api_client, #entity, #errors

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SkullIsland::Resource

all, cleanup_except, find, from_hash, gen_getter_method, gen_property_methods, gen_setter_method, get, immutable, #initialize, property, where

Methods included from Helpers::ResourceClass

#determine_getter_names, #determine_setter_names, #human, #i18n_key, #immutable?, #param_key, #properties, #route_key

Methods included from Helpers::Resource

#datetime_from_params, #delayed_set, #destroy, #digest_properties, #find_by_digest, #fresh?, #host_regex, #id, #id_property, #immutable?, #import_update_or_skip, #lookup, #model_name, #new?, #persisted?, #postprocess_created_at, #postprocess_updated_at, #properties, #prune_for_save, #recursive_erubi, #reload, #required_properties, #save, #supports_meta?, #tainted?, #to_param, #to_s, #update

Methods included from Validations::Resource

#validate_id, #validate_mutability, #validate_required_properties, #validate_tags

Constructor Details

This class inherits a constructor from SkullIsland::Resource

Instance Attribute Details

#hashed_passwordObject

Returns the value of attribute hashed_password.



10
11
12
# File 'lib/skull_island/resources/basicauth_credential.rb', line 10

def hashed_password
  @hashed_password
end

Class Method Details

.batch_import(data, verbose: false, test: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/skull_island/resources/basicauth_credential.rb', line 20

def self.batch_import(data, verbose: false, test: false)
  raise(Exceptions::InvalidArguments) unless data.is_a?(Array)

  known_ids = []

  data.each_with_index do |resource_data, index|
    resource = new
    resource.delayed_set(:username, resource_data)
    resource.delayed_set(:password, resource_data) if resource_data['password']
    resource.delayed_set(:consumer, resource_data)
    resource.import_update_or_skip(index: index, verbose: verbose, test: test)
    known_ids << resource.id
  end

  known_ids
end

.relative_uriObject



37
38
39
# File 'lib/skull_island/resources/basicauth_credential.rb', line 37

def self.relative_uri
  'basic-auths'
end

Instance Method Details

#<=>(other) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/skull_island/resources/basicauth_credential.rb', line 91

def <=>(other)
  if id
    if id < other.id
      -1
    elsif id > other.id
      1
    elsif id == other.id
      0
    else
      raise Exceptions::InvalidArguments
    end
  else
    digest <=> other.digest
  end
end

#consumerObject

The consumer property



14
15
16
17
# File 'lib/skull_island/resources/basicauth_credential.rb', line 14

property(
  :consumer,
  required: true, validate: true, preprocess: true, postprocess: true
)

#created_atObject

The created_at property



18
# File 'lib/skull_island/resources/basicauth_credential.rb', line 18

property :created_at, read_only: true, postprocess: true

#digestObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/skull_island/resources/basicauth_credential.rb', line 49

def digest
  Digest::MD5.hexdigest(
    if new? && !password.match?(/^hash{.+}$/)
      hashed_pass = Digest::SHA1.hexdigest((password || '') + consumer.id)
      "#{username}:hash{#{hashed_pass}}"
    else
      "#{username}:#{password}"
    end
  )
end

#export(options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/skull_island/resources/basicauth_credential.rb', line 60

def export(options = {})
  hash = { 'username' => username, 'password' => password }
  hash['consumer'] = "<%= lookup :consumer, '#{consumer.username}' %>" if consumer
  [*options[:exclude]].each do |exclude|
    hash.delete(exclude.to_s)
  end
  [*options[:include]].each do |inc|
    hash[inc.to_s] = send(inc.to_sym)
  end
  hash.reject { |_, value| value.nil? }
end

#modified_existing?Boolean

Credentials can’t be updated, only deleted then created



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/skull_island/resources/basicauth_credential.rb', line 73

def modified_existing?
  return false unless new?

  # Find credentials of the same username
  basic_auths = consumer.credentials['basic-auth']
  return false unless basic_auths

  same_username = basic_auths.where(:username, username)

  existing = same_username.size == 1 ? same_username.first : nil
  # Need to destroy the old one then save the new one...
  existing ? existing.destroy && save : false
end

#passwordObject

The password property



13
# File 'lib/skull_island/resources/basicauth_credential.rb', line 13

property :password, validated: true, preprocess: true, postprocess: true

#projectObject



87
88
89
# File 'lib/skull_island/resources/basicauth_credential.rb', line 87

def project
  consumer ? consumer.project : nil
end

#relative_uriObject



41
42
43
# File 'lib/skull_island/resources/basicauth_credential.rb', line 41

def relative_uri
  consumer ? "#{consumer.relative_uri}/basic-auth/#{id}" : nil
end

#save_uriObject



45
46
47
# File 'lib/skull_island/resources/basicauth_credential.rb', line 45

def save_uri
  consumer ? "#{consumer.relative_uri}/basic-auth" : nil
end

#usernameObject

The username property



12
# File 'lib/skull_island/resources/basicauth_credential.rb', line 12

property :username, required: true, validate: true