Skip to main content

User-restricted NHS login separate authentication and authorisation - PHP tutorial

Connect to a user-restricted REST API using NHS login separate authentication and authorisation and the PHP programming language.

Overview

This tutorial shows you how to connect to a user-restricted REST API using NHS login separate authentication and authorisation and the PHP programming language. It uses Symfony to create a simple web application which authenticates the end user using our sandbox NHS login environment, receives an access token from our authorisation server and calls the user restricted endpoint of our Hello World API.

To call a user-restricted API, the end user must be authenticated. NHS login is used to authenticate when the end user is a patient. With the separate authentication and authorisation pattern, authentication is done by NHS login. In exchange, you receive an access ID token which you need to exchange it with an access token. You need to include this access token in the API request.


Setting up your environment

This example project was developed using Symfony 6.1.4 and PHP 8.1.9 so you need to have these installed.


Check out the GitHub repository

You can find the code for this PHP user-restricted REST API NHS login separate authentication and authorisation tutorial in our GitHub repository.

Implementation details

This project contains:

  • a HelloAuthController file. This contains the routes of the web application startup code and configures the OAuth flow required to authenticate. It uses the OAuth2 client provided by league/oauth2-client.

To follow this tutorial download or clone this repository.


Create an application and generate a key pair

You need to create an application using our Developer portal.

This gives you access to your application ID and API key which you need to generate a JWT.

You also need to create a public and private key pair.

You register your public key with our authentication server and sign your JWT using your private key.

Create an application

To do this, follow Step 1 'Create an application'.

Notes:

  • when creating a new app, you need to select the 'Environment'. For this tutorial select 'Sandbox'
  • when editing your application details and selecting the API you want to use, select 'Hello World (Sandbox)'. You might be prompted for a callback URL which is not required for the signed JWT authentication method, so you can enter a dummy value such as https://www.example.com.
  • make a note of your API Key 

Generate a key pair

To do this, follow Step 2 'Generate a key pair'.

Make a note of the Key Identifier (KID) you have chosen.

Register your public key

To do this, follow Step 3 'Register your public key with us'.


Populate the project's environment variables

You should now have:

  • your application's API Key
  • a KID that you have chosen
  • your private key

To run the example tutorial, you need to set the following environment variables.

Variable name Description
CLIENT_ID Your application's API Key
CLIENT_SECRET Your application's API Secret

You can set your environment variables in a file named .env. This project contains a sample env file to use:

  • rename env.sample to .env and modify it
  • source it by running source .env

Run the code

Once you set the environment variables, you are ready to run the project.

Run using Makefile

Alternatively you can set your environment variables in a file named .env. Then use the make command:  make run. See the README for more info.


Using the application

When you run the code, you should be able to load the application at http://localhost:8000.

  1. Click the button 'Login with NHS login' to be directed to our mock NHS login authorisation service
  2. Sign in by selecting the highest NHS login authentication level: P9.
  3. You are be redirected back to the application. The access token you have received is used to make a request to the Hello World API.
  4. The response from the API should read:

{
  "message": "Hello User!"
}

Create a developer account

To get started with our tutorials and APIs, you need to create a developer account.

Last edited: 12 October 2022 4:21 pm