Class: GdsApi::AccountApi

Inherits:
Base
  • Object
show all
Defined in:
lib/gds_api/account_api.rb

Overview

Adapter for the Account API

Constant Summary collapse

AUTH_HEADER_NAME =
"GOVUK-Account-Session".freeze

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#client, #create_client, #get_list, #initialize, #url_for_slug

Constructor Details

This class inherits a constructor from GdsApi::Base

Instance Method Details

#delete_email_subscription(name:, govuk_account_session:) ⇒ Object

Unsubscribe and delete an account-linked email subscription.

Parameters:

  • name (String)

    Name of the subscription

  • govuk_account_session (String)

    Value of the session header



126
127
128
# File 'lib/gds_api/account_api.rb', line 126

def delete_email_subscription(name:, govuk_account_session:)
  delete_json("#{endpoint}/api/email-subscriptions/#{CGI.escape(name)}", {}, auth_headers())
end

#delete_saved_page(page_path:, govuk_account_session:) ⇒ GdsApi::Response

Delete a single saved page entry from a users account

Parameters:

  • the (String)

    path of a page to check

  • govuk_account_session (String)

    Value of the session header

Returns:

  • (GdsApi::Response)

    A status code of 204 indicates the saved page has been successfully deleted. A status code of 404 indicates there is no saved page with this path.



166
167
168
# File 'lib/gds_api/account_api.rb', line 166

def delete_saved_page(page_path:, govuk_account_session:)
  delete_json("#{endpoint}/api/saved-pages/#{CGI.escape(page_path)}", {}, auth_headers())
end

#delete_user_by_subject_identifier(subject_identifier:) ⇒ Object

Delete a users account

Parameters:

  • subject_identifier (String)

    The identifier of the user, shared between the auth service and GOV.UK.



58
59
60
# File 'lib/gds_api/account_api.rb', line 58

def delete_user_by_subject_identifier(subject_identifier:)
  delete_json("#{endpoint}/api/oidc-users/#{subject_identifier}")
end

#get_attributes(attributes:, govuk_account_session:) ⇒ Hash

Look up the values of a user’s attributes

Parameters:

  • attributes (String)

    Names of the attributes to check

  • govuk_account_session (String)

    Value of the session header

Returns:

  • (Hash)

    The attribute values (if present), and a new session header



86
87
88
89
# File 'lib/gds_api/account_api.rb', line 86

def get_attributes(attributes:, govuk_account_session:)
  querystring = nested_query_string({ attributes: attributes }.compact)
  get_json("#{endpoint}/api/attributes?#{querystring}", auth_headers())
end

#get_email_subscription(name:, govuk_account_session:) ⇒ Hash

Get the details of an account-linked email subscription.

Parameters:

  • name (String)

    Name of the subscription

  • govuk_account_session (String)

    Value of the session header

Returns:

  • (Hash)

    Details of the subscription, if it exists.



107
108
109
# File 'lib/gds_api/account_api.rb', line 107

def get_email_subscription(name:, govuk_account_session:)
  get_json("#{endpoint}/api/email-subscriptions/#{CGI.escape(name)}", auth_headers())
end

#get_end_session_url(govuk_account_session: nil) ⇒ Hash

Get an OIDC end-session URL to redirect the user to

Parameters:

  • govuk_account_session (String, nil) (defaults to: nil)

    Value of the session header

Returns:

  • (Hash)

    An end-session URL



42
43
44
# File 'lib/gds_api/account_api.rb', line 42

def get_end_session_url(govuk_account_session: nil)
  get_json("#{endpoint}/api/oauth2/end-session", auth_headers())
end

#get_saved_page(page_path:, govuk_account_session:) ⇒ Hash

Return a single page by unique URL

Parameters:

  • the (String)

    path of a page to check

  • govuk_account_session (String)

    Value of the session header

Returns:

  • (Hash)

    containing :saved_page, a hash of a single saved page value



145
146
147
# File 'lib/gds_api/account_api.rb', line 145

def get_saved_page(page_path:, govuk_account_session:)
  get_json("#{endpoint}/api/saved-pages/#{CGI.escape(page_path)}", auth_headers())
end

#get_saved_pages(govuk_account_session:) ⇒ Hash

Look up all pages saved by a user in their Account

Parameters:

  • govuk_account_session (String)

    Value of the session header

Returns:

  • (Hash)

    containing :saved_pages, an array of single saved page hashes



135
136
137
# File 'lib/gds_api/account_api.rb', line 135

def get_saved_pages(govuk_account_session:)
  get_json("#{endpoint}/api/saved-pages", auth_headers())
end

#get_sign_in_url(redirect_path: nil, mfa: false) ⇒ Hash

Get an OAuth sign-in URL to redirect the user to

Parameters:

  • redirect_path (String, nil) (defaults to: nil)

    path on GOV.UK to send the user to after authentication

  • mfa (Boolean, nil) (defaults to: false)

    whether to authenticate the user with MFA or not

Returns:

  • (Hash)

    An authentication URL and the OAuth state parameter (for CSRF protection)



17
18
19
20
21
22
23
24
25
# File 'lib/gds_api/account_api.rb', line 17

def (redirect_path: nil, mfa: false)
  querystring = nested_query_string(
    {
      redirect_path: redirect_path,
      mfa: mfa,
    }.compact,
  )
  get_json("#{endpoint}/api/oauth2/sign-in?#{querystring}")
end

#get_user(govuk_account_session:) ⇒ Hash

Get all the information about a user needed to render the account home page

Parameters:

  • govuk_account_session (String)

    Value of the session header

Returns:

  • (Hash)

    Information about the user and the services they’ve used, and a new session header



51
52
53
# File 'lib/gds_api/account_api.rb', line 51

def get_user(govuk_account_session:)
  get_json("#{endpoint}/api/user", auth_headers())
end

#put_email_subscription(name:, topic_slug:, govuk_account_session:) ⇒ Hash

Create or update an account-linked email subscription.

Parameters:

  • name (String)

    Name of the subscription

  • topic_slug (String)

    The email-alert-api topic slug to subscribe to

  • govuk_account_session (String)

    Value of the session header

Returns:

  • (Hash)

    Details of the newly created subscription.



118
119
120
# File 'lib/gds_api/account_api.rb', line 118

def put_email_subscription(name:, topic_slug:, govuk_account_session:)
  put_json("#{endpoint}/api/email-subscriptions/#{CGI.escape(name)}", { topic_slug: topic_slug }, auth_headers())
end

#save_page(page_path:, govuk_account_session:) ⇒ Hash

Upsert a single saved page entry in a users account

Parameters:

  • the (String)

    path of a page to check

  • govuk_account_session (String)

    Value of the session header

Returns:

  • (Hash)

    A single saved page value (if sucessful)



155
156
157
# File 'lib/gds_api/account_api.rb', line 155

def save_page(page_path:, govuk_account_session:)
  put_json("#{endpoint}/api/saved-pages/#{CGI.escape(page_path)}", {}, auth_headers())
end

#set_attributes(attributes:, govuk_account_session:) ⇒ Hash

Create or update attributes for a user

Parameters:

  • attributes (String)

    Hash of new attribute values

  • govuk_account_session (String)

    Value of the session header

Returns:

  • (Hash)

    A new session header



97
98
99
# File 'lib/gds_api/account_api.rb', line 97

def set_attributes(attributes:, govuk_account_session:)
  patch_json("#{endpoint}/api/attributes", { attributes: attributes }, auth_headers())
end

#update_user_by_subject_identifier(subject_identifier:, email: nil, email_verified: nil, has_unconfirmed_email: nil) ⇒ Hash

Update the user record with privileged information from the auth service. Only the auth service will call this.

Parameters:

  • subject_identifier (String)

    The identifier of the user, shared between the auth service and GOV.UK.

  • email (String, nil) (defaults to: nil)

    The user’s current

  • email_verified (Boolean, nil) (defaults to: nil)

    Whether the user’s current email address is verified

  • has_unconfirmed_email (Boolean, nil) (defaults to: nil)

    Whether the user has a new, pending, email address

Returns:

  • (Hash)

    The user’s subject identifier and email attributes



70
71
72
73
74
75
76
77
78
# File 'lib/gds_api/account_api.rb', line 70

def update_user_by_subject_identifier(subject_identifier:, email: nil, email_verified: nil, has_unconfirmed_email: nil)
  params = {
    email: email,
    email_verified: email_verified,
    has_unconfirmed_email: has_unconfirmed_email,
  }.compact

  patch_json("#{endpoint}/api/oidc-users/#{subject_identifier}", params)
end

#validate_auth_response(code:, state:) ⇒ Hash

Validate an OAuth authentication response

Parameters:

  • code (String)

    The OAuth code parameter, from the auth server.

  • state (String)

    The OAuth state parameter, from the auth server.

Returns:

  • (Hash)

    The value for the govuk_account_session header, the path to redirect the user to, and the GA client ID (if there is one)



33
34
35
# File 'lib/gds_api/account_api.rb', line 33

def validate_auth_response(code:, state:)
  post_json("#{endpoint}/api/oauth2/callback", code: code, state: state)
end