Class: AtomicLti::Services::PlatformNotifications

Inherits:
Base
  • Object
show all
Defined in:
app/lib/atomic_lti/services/platform_notifications.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#get_next_url, #headers, #initialize, #logged_service_call, #service_delete, #service_get, #service_post, #service_put

Constructor Details

This class inherits a constructor from AtomicLti::Services::Base

Class Method Details

.enabled?(id_token_decoded) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'app/lib/atomic_lti/services/platform_notifications.rb', line 21

def self.enabled?(id_token_decoded)
  return false unless id_token_decoded&.dig(AtomicLti::Definitions::PLATFORM_NOTIFICATION_SERVICE_CLAIM)

  (AtomicLti::Definitions::PLATFORM_NOTIFICATION_SERVICE_VERSIONS &
    (id_token_decoded.dig(AtomicLti::Definitions::PLATFORM_NOTIFICATION_SERVICE_CLAIM, "service_versions") || [])).present?
end

.validate_notification(notification) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 'app/lib/atomic_lti/services/platform_notifications.rb', line 49

def self.validate_notification(notification)
  decoded_token = AtomicLti::Authorization.validate_token(notification)
  if decoded_token.blank?
    raise AtomicLti::Exceptions::InvalidPlatformNotification
  end

  errors = []

  if decoded_token["iss"].blank?
    errors.push("LTI token is missing required field iss")
  end

  if decoded_token["aud"].blank?
    errors.push("LTI token is missing required field aud")
  end

  if decoded_token["aud"].is_a?(Array) && decoded_token["aud"].length > 1
    # OpenID Connect spec specifies the AZP should exist and be an AUD
    if decoded_token["azp"].blank?
      errors.push("LTI token has multiple aud and is missing required field azp")
    elsif decoded_token["aud"].exclude?(decoded_token["azp"])
      errors.push("LTI token azp is not one of the aud's")
    end
  end

  if decoded_token[AtomicLti::Definitions::DEPLOYMENT_ID].blank?
    errors.push(
      "LTI token is missing required field #{AtomicLti::Definitions::DEPLOYMENT_ID}",
    )
  end

  if decoded_token[AtomicLti::Definitions::NOTICE_TYPE_CLAIM].blank?
    errors.push(
      "LTI token is missing required claim #{AtomicLti::Definitions::NOTICE_TYPE_CLAIM}",
    )
  end

  if errors.present?
    raise Exceptions::InvalidPlatformNotification.new(errors.join(" "))
  end

  if decoded_token[AtomicLti::Definitions::LTI_VERSION].blank?
    raise AtomicLti::Exceptions::NoLTIVersion
  end

  raise AtomicLti::Exceptions::InvalidLTIVersion unless AtomicLti::Lti.valid_version?(decoded_token)

  decoded_token
end

Instance Method Details

#endpointObject



8
9
10
11
12
13
# File 'app/lib/atomic_lti/services/platform_notifications.rb', line 8

def endpoint
  url = @id_token_decoded.dig(AtomicLti::Definitions::PLATFORM_NOTIFICATION_SERVICE_CLAIM, "platform_notification_service_url")
  raise AtomicLti::Exceptions::PlatformNotificationsError, "Unable to access platform notifications" if url.blank?

  url
end

#get(query: {}) ⇒ Object

Get platform notifications



33
34
35
36
37
38
39
40
# File 'app/lib/atomic_lti/services/platform_notifications.rb', line 33

def get(query: {})
  uri = Addressable::URI.parse(endpoint)
  uri.query_values = (uri.query_values || {}).merge(query)
  url = uri.to_str

  response, = service_get(url, headers:)
  response.parsed_response
end

#scopesObject



4
5
6
# File 'app/lib/atomic_lti/services/platform_notifications.rb', line 4

def scopes
  [AtomicLti::Definitions::PNS_SCOPE_NOTICEHANDLERS]
end

#update(notice_type, handler) ⇒ Object



42
43
44
45
46
47
# File 'app/lib/atomic_lti/services/platform_notifications.rb', line 42

def update(notice_type, handler)
  content_type = { "Content-Type" => "application/json" }
  payload = { notice_type:, handler: }
  response, = service_put(endpoint, body: JSON.dump(payload), headers: headers(content_type))
  response
end

#url_for(query = nil) ⇒ Object



15
16
17
18
19
# File 'app/lib/atomic_lti/services/platform_notifications.rb', line 15

def url_for(query = nil)
  url = endpoint.dup
  url << "?#{query}" if query.present?
  url
end

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/lib/atomic_lti/services/platform_notifications.rb', line 28

def valid?
  self.class.enabled?(@id_token_decoded)
end