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

#check_for_email_subscription(govuk_account_session:) ⇒ Hash

Check if a user has an email subscription for the Transition Checker

Parameters:

  • govuk_account_session (String)

    Value of the session header

Returns:

  • (Hash)

    Whether the user has a subscription, and a new session header



53
54
55
# File 'lib/gds_api/account_api.rb', line 53

def check_for_email_subscription(govuk_account_session:)
  get_json("#{endpoint}/api/transition-checker-email-subscription", auth_headers())
end

#create_registration_state(attributes:) ⇒ Hash

Register some initial state, to pass to get_sign_in_url, which is used to initialise the account if the user signs up

Parameters:

  • attributes (Hash, nil)

    Initial attributes to store

Returns:

  • (Hash)

    The state ID to pass to get_sign_in_url



44
45
46
# File 'lib/gds_api/account_api.rb', line 44

def create_registration_state(attributes:)
  post_json("#{endpoint}/api/oauth2/state", attributes: attributes)
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



73
74
75
76
# File 'lib/gds_api/account_api.rb', line 73

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

#get_attributes_names(attributes:, govuk_account_session:) ⇒ Hash

Look up the names 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 names (if present), and a new session header



94
95
96
97
# File 'lib/gds_api/account_api.rb', line 94

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

#get_sign_in_url(redirect_path: nil, state_id: nil, level_of_authentication: nil) ⇒ 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

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

    identifier originally returned by #create_registration_state

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

    either “level1” (require MFA) or “level0” (do not require MFA)

Returns:

  • (Hash)

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



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

def (redirect_path: nil, state_id: nil, level_of_authentication: nil)
  querystring = nested_query_string(
    {
      redirect_path: redirect_path,
      state_id: state_id,
      level_of_authentication: level_of_authentication,
    }.compact,
  )
  get_json("#{endpoint}/api/oauth2/sign-in?#{querystring}")
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



84
85
86
# File 'lib/gds_api/account_api.rb', line 84

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

#set_email_subscription(govuk_account_session:, slug:) ⇒ Hash

Create or update a user’s email subscription for the Transition Checker

Parameters:

  • govuk_account_session (String)

    Value of the session header

  • slug (String)

    The email topic slug

Returns:

  • (Hash)

    Whether the user has a subscription, and a new session header



63
64
65
# File 'lib/gds_api/account_api.rb', line 63

def set_email_subscription(govuk_account_session:, slug:)
  post_json("#{endpoint}/api/transition-checker-email-subscription", { slug: slug }, auth_headers())
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)



35
36
37
# File 'lib/gds_api/account_api.rb', line 35

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