Class: Hitch::DeviceAuthorizationsController

Inherits:
PublicEndpointController
  • Object
show all
Includes:
ClientResolution, CorsSupport, DeviceAuthorizationGate, OauthFormAdmission, UriValidation
Defined in:
app/controllers/hitch/device_authorizations_controller.rb

Overview

POST /oauth/device_authorization — RFC 8628 §3.1: mint a device grant and hand back the code pair a headless client shows its human.

Public endpoint, like tokens: the caller is a machine with no session. One thing it deliberately never does is fetch client metadata — every CIMD fetch in this gem has a signed-in, rate-limited actor, and this endpoint has neither, so a CIMD-shaped client_id is accepted as a string here and resolved on /activate, where the approving person is the actor.

Constant Summary collapse

DEVICE_PARAMETER_NAMES =
%i[client_id client_secret scope resource].freeze

Constants included from RequestAdmission

RequestAdmission::MAX_REQUEST_BODY_BYTES

Instance Method Summary collapse

Instance Method Details

#createObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/hitch/device_authorizations_controller.rb', line 22

def create
  unless request.media_type == Hitch::OauthRequestParameters::FORM_MEDIA_TYPE
    return oauth_error("invalid_request", "device authorization requests must use application/x-www-form-urlencoded")
  end
  return unless admit_mint_rate!

  oauth = oauth_parameters(*DEVICE_PARAMETER_NAMES, form_only: true)
  resource = require_canonical_resource(oauth[:resource])
  return unless resource

  authentication = resolved_client_authentication(oauth)
  client_id = authentication.client_id
  # A device grant needs a client somebody real vouches for: a
  # metadata document (its host is earned by serving it), or a
  # confidential client the operator registered — whose row records that
  # provenance and which just authenticated by its secret. A client that
  # only vouched for itself via open registration is the §5.4 phishing
  # shape, whether or not DCR gave it a secret, and cannot mint. A
  # registered row decides first, exactly as /activate classifies, so
  # nothing can mint a grant the screen would never approve.
  live_client = Hitch::Client.find_by(client_id: client_id)
  voucher_auth_method = case authentication.token_endpoint_auth_method
  when "client_secret_basic"
    if authentication.operator_registered &&
        live_client&.operator_registered_confidential_client?
      "client_secret_basic"
    end
  when "none"
    if !authentication.registered_client && live_client.nil? &&
        Hitch::ClientIdMetadata.reference?(client_id)
      "none"
    end
  end
  unless voucher_auth_method == authentication.token_endpoint_auth_method
    return oauth_error(
      "invalid_client",
      "device authorization requires a client metadata document or an operator-registered client",
      :unauthorized
    )
  end

  grant = Hitch::DeviceGrant.mint!(
    client_id: client_id,
    scopes: oauth[:scope],
    resource_uri: resource,
    token_endpoint_auth_method: authentication.token_endpoint_auth_method
  )
  render_device_authorization(grant)
end