Class: PasskeysRails::PasskeysController

Inherits:
ApplicationController show all
Defined in:
app/controllers/passkeys_rails/passkeys_controller.rb

Instance Method Summary collapse

Instance Method Details

#authenticateObject



33
34
35
36
37
38
39
40
41
# File 'app/controllers/passkeys_rails/passkeys_controller.rb', line 33

def authenticate
  cookie_data = JSON.parse(cookies.signed["passkeys_rails"] || "{}")
  result = PasskeysRails::FinishAuthentication.call!(credential: authentication_params.to_h,
                                                     challenge: cookie_data["challenge"])

  broadcast(:did_authenticate, agent: result.agent)

  render json: auth_response(result)
end

#challengeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/passkeys_rails/passkeys_controller.rb', line 6

def challenge
  result = PasskeysRails::BeginChallenge.call!(username: challenge_params[:username])

  # Store the challenge so we can verify the future register or authentication request
  cookies.signed[:passkeys_rails] = {
    value: result.cookie_data.to_json,
    expire: Time.now.utc + (result.response.timeout / 1000),
    secure: true,
    httponly: true,
    same_site: :strict
  }

  render json: result.response.as_json
end

#debug_loginObject

This action exists to allow easier mobile app debugging as it may not be possible to acess Passkey functionality in mobile simulators. It is only routable if DEBUG_LOGIN_REGEX is set in the server environment. CAUTION: It is very insecure to set DEBUG_LOGIN_REGEX in a production environment.



55
56
57
58
59
60
61
# File 'app/controllers/passkeys_rails/passkeys_controller.rb', line 55

def 
  result = PasskeysRails::DebugLogin.call!(username: [:username])

  broadcast(:did_authenticate, agent: result.agent)

  render json: auth_response(result)
end

#debug_registerObject

This action exists to allow easier mobile app debugging as it may not be possible to acess Passkey functionality in mobile simulators. It is only routable if DEBUG_LOGIN_REGEX is set in the server environment. CAUTION: It is very insecure to set DEBUG_LOGIN_REGEX in a production environment.



67
68
69
70
71
72
73
74
# File 'app/controllers/passkeys_rails/passkeys_controller.rb', line 67

def debug_register
  result = PasskeysRails::DebugRegister.call!(username: [:username],
                                              authenticatable_info: authenticatable_params&.to_h)

  broadcast(:did_register, agent: result.agent)

  render json: auth_response(result)
end

#refreshObject



43
44
45
46
47
48
49
# File 'app/controllers/passkeys_rails/passkeys_controller.rb', line 43

def refresh
  result = PasskeysRails::RefreshToken.call!(token: refresh_params[:auth_token])

  broadcast(:did_refresh, agent: result.agent)

  render json: auth_response(result)
end

#registerObject



21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/passkeys_rails/passkeys_controller.rb', line 21

def register
  cookie_data = JSON.parse(cookies.signed["passkeys_rails"] || "{}")
  result = PasskeysRails::FinishRegistration.call!(credential: attestation_credential_params.to_h,
                                                   authenticatable_info: authenticatable_params&.to_h,
                                                   username: cookie_data["username"],
                                                   challenge: cookie_data["challenge"])

  broadcast(:did_register, agent: result.agent)

  render json: auth_response(result)
end