Class: Course::Assessment::AuthenticationService

Inherits:
Object
  • Object
show all
Defined in:
app/services/course/assessment/authentication_service.rb

Overview

Authenticate the assessment and stores the authentication token in the given session. Token generation is based on the assessment password, so that if the password changes, the token automatically becomes invalid.

Instance Method Summary collapse

Constructor Details

#initialize(assessment, session) ⇒ AuthenticationService


9
10
11
12
# File 'app/services/course/assessment/authentication_service.rb', line 9

def initialize(assessment, session)
  @assessment = assessment
  @session = session
end

Instance Method Details

#authenticate(password_input) ⇒ Boolean

Check if the password from user input matches the assessment password.


18
19
20
21
22
23
24
25
26
27
# File 'app/services/course/assessment/authentication_service.rb', line 18

def authenticate(password_input)
  return true unless @assessment.view_password_protected?

  if password_input == @assessment.view_password
    set_session_token!
    true
  else
    false
  end
end

#authenticated?Boolean

Check whether current session is the same session that created the submission or not.


37
38
39
40
41
# File 'app/services/course/assessment/authentication_service.rb', line 37

def authenticated?
  return true unless @session

  @session[session_key] == password_token
end

#set_session_token!Object

Generates a new authentication token and stores it in current session.


30
31
32
# File 'app/services/course/assessment/authentication_service.rb', line 30

def set_session_token!
  @session[session_key] = password_token
end