Class: Course::Assessment::SessionAuthenticationService

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

Overview

Authenticate the assessment and update the session_id in submission.

Instance Method Summary collapse

Constructor Details

#initialize(assessment, session, submission = nil) ⇒ SessionAuthenticationService

Returns a new instance of SessionAuthenticationService

Parameters:

  • assessment (Course::Assessment)

    The password protected assessment.

  • session (ActionDispatch::Request::Session)

    The current session.

  • submission (Course::Assessment::Submission|nil) (defaults to: nil)

    The session id will be stored if the submission is given.


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

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

Instance Method Details

#authenticate(password) ⇒ Boolean

Check if the password from user input matches the assessment password. Further stores the session_id in submission, this ensures that current_user is the only one that can access the submission.

Parameters:

  • password (String)

Returns:

  • (Boolean)

    true if matches


20
21
22
23
24
25
26
27
28
29
# File 'app/services/course/assessment/session_authentication_service.rb', line 20

def authenticate(password)
  return true unless @assessment.session_password_protected?

  if password == @assessment.session_password
    update_session_id if @submission
    true
  else
    false
  end
end

#authenticated?Boolean

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

Returns:

  • (Boolean)

43
44
45
# File 'app/services/course/assessment/session_authentication_service.rb', line 43

def authenticated?
  current_authentication_token && current_authentication_token == @submission.session_id
end

#generate_authentication_token!String

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

Returns:

  • (String)

    the new authentication token.


34
35
36
37
38
# File 'app/services/course/assessment/session_authentication_service.rb', line 34

def generate_authentication_token!
  new_id = SecureRandom.hex(8)
  @session[session_key] = new_id
  new_id
end