Class: Locomotive::Steam::AuthService

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/steam/services/auth_service.rb

Defined Under Namespace

Modules: ContentEntryAuth

Constant Summary collapse

MIN_PASSWORD_LENGTH =
6
RESET_TOKEN_LIFETIME =

6 hours in seconds

1 * 3600

Instance Method Summary collapse

Instance Method Details

#find_authenticated_resource(type, id) ⇒ Object



11
12
13
# File 'lib/locomotive/steam/services/auth_service.rb', line 11

def find_authenticated_resource(type, id)
  entries.find(type, id)
end

#forgot_password(options, context) ⇒ Object

options is an instance of the AuthOptions class



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/locomotive/steam/services/auth_service.rb', line 54

def forgot_password(options, context)
  entry = entries.all(options.type, options.id_field => options.id).first

  if entry.nil?
    :"wrong_#{options.id_field}"
  else
    entries.update_decorated_entry(entry, {
      '_auth_reset_token'   => SecureRandom.hex,
      '_auth_reset_sent_at' => Time.zone.now.iso8601
    })

    context['reset_password_url'] = options.reset_password_url + '?auth_reset_token=' + entry['_auth_reset_token']
    context[options.type.singularize] = entry

    send_reset_password_instructions(options, context)

    :"reset_#{options.password_field}_instructions_sent"
  end
end

#notify(action, entry, request) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/locomotive/steam/services/auth_service.rb', line 99

def notify(action, entry, request)
  ActiveSupport::Notifications.instrument("steam.auth.#{action}",
    site:     site,
    entry:    entry,
    locale:   entries.locale,
    request:  request
  )
end

#reset_password(options, request) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/locomotive/steam/services/auth_service.rb', line 74

def reset_password(options, request)
  return :invalid_token       if options.reset_token.blank?
  return :password_too_short  if options.password.to_s.size < MIN_PASSWORD_LENGTH

  entry = entries.all(options.type, '_auth_reset_token' => options.reset_token).first

  if entry
    sent_at = Time.parse(entry[:_auth_reset_sent_at]).to_i
    now = Time.zone.now.to_i - RESET_TOKEN_LIFETIME

    if sent_at >= now
      entries.update_decorated_entry(entry, {
        "#{options.password_field}_hash" => BCrypt::Password.create(options.password),
        '_auth_reset_token'   => nil,
        '_auth_reset_sent_at' => nil
      })
      notify(:reset_password, entry, request)

      return [:"#{options.password_field}_reset", entry]
    end
  end

  :invalid_token
end

#sign_in(options, request) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/locomotive/steam/services/auth_service.rb', line 30

def (options, request)
  entry = entries.all(options.type, options.id_field => options.id).first

  if entry && (entry.send(options.password_field).present?)
    hashed_password = entry[:"#{options.password_field}_hash"]
    password        = ::BCrypt::Engine.hash_secret(options.password, entry.send(options.password_field).try(:salt))
    same_password   = secure_compare(password, hashed_password)

    if same_password
      notify(:signed_in, entry, request)
      return [:signed_in, entry]
    end
  end

  :wrong_credentials
end

#sign_out(entry, request) ⇒ Object



47
48
49
50
51
# File 'lib/locomotive/steam/services/auth_service.rb', line 47

def sign_out(entry, request)
  notify(:signed_out, entry, request)

  :signed_out
end

#sign_up(options, context, request = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/locomotive/steam/services/auth_service.rb', line 15

def (options, context, request = nil)
  entry = entries.create(options.type, options.entry) do |_entry|
    _entry.extend(ContentEntryAuth)
    _entry[:_password_field] = options.password_field.to_sym
  end

  if entry.errors.empty?
    notify(:signed_up, entry, request)
    context[options.type.singularize] = entry
    send_welcome_email(options, context)
  end

  [entry.errors.empty? ? :entry_created : :invalid_entry, entry]
end