Skip to content

Commit

Permalink
Using the user token to get user details (#352)
Browse files Browse the repository at this point in the history
* Using the user token to get user details

* Removing manage-users-api env variables

* Addressing comments
  • Loading branch information
brightonsbox authored May 13, 2024
1 parent 42a7626 commit 78e2040
Show file tree
Hide file tree
Showing 25 changed files with 132 additions and 232 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
# Change log

**May 10th 2024** – Derive user details from the `authorization_code` "user" token instead of making an API call to `hmpps-manage-users-api`, thereby removing an unnecessary dependency.

PR: [#352](https://github.com/ministryofjustice/hmpps-template-typescript/pull/352)

---

**February 29th 2024** – Use same node version for outdated check and security scan. This currently defaults to node 16

PR: [#321](https://github.com/ministryofjustice/hmpps-template-typescript/pull/321)

---

**February 15th 2024** – Move over to use Debian 12 based image (bookworm)

PR: [#316](https://github.com/ministryofjustice/hmpps-template-typescript/pull/316)

---

**January 9th 2024** – Move over to Gov UK Frontend 5.0 and MoJ Frontend 2.0

Note, this removed support for IE8,9,10 etc.

PR: [#297](https://github.com/ministryofjustice/hmpps-template-typescript/pull/297)

---

**November 29th 2023** – Remove getUserRoles as an api call and add as decoded from the token #274

This is to encourage services not to make additional calls to retrieve a user's role information.
Expand Down
2 changes: 0 additions & 2 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { defineConfig } from 'cypress'
import { resetStubs } from './integration_tests/mockApis/wiremock'
import auth from './integration_tests/mockApis/auth'
import manageUsersApi from './integration_tests/mockApis/manageUsersApi'
import tokenVerification from './integration_tests/mockApis/tokenVerification'

export default defineConfig({
Expand All @@ -19,7 +18,6 @@ export default defineConfig({
on('task', {
reset: resetStubs,
...auth,
...manageUsersApi,
...tokenVerification,
})
},
Expand Down
1 change: 0 additions & 1 deletion feature.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
PORT=3007
HMPPS_AUTH_URL=http://localhost:9091/auth
MANAGE_USERS_API_URL=http://localhost:9091/manage-users-api
TOKEN_VERIFICATION_API_URL=http://localhost:9091/verification
TOKEN_VERIFICATION_ENABLED=true
REDIS_ENABLED=false
Expand Down
1 change: 0 additions & 1 deletion helm_deploy/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ generic-service:
env:
INGRESS_URL: "https://hmpps-template-typescript-dev.hmpps.service.justice.gov.uk"
HMPPS_AUTH_URL: "https://sign-in-dev.hmpps.service.justice.gov.uk/auth"
MANAGE_USERS_API_URL: "https://manage-users-api-dev.hmpps.service.justice.gov.uk"
TOKEN_VERIFICATION_API_URL: "https://token-verification-api-dev.prison.service.justice.gov.uk"
ENVIRONMENT_NAME: DEV
AUDIT_ENABLED: "false"
Expand Down
1 change: 0 additions & 1 deletion helm_deploy/values-preprod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ generic-service:
env:
INGRESS_URL: "https://hmpps-template-typescript-preprod.hmpps.service.justice.gov.uk"
HMPPS_AUTH_URL: "https://sign-in-preprod.hmpps.service.justice.gov.uk/auth"
MANAGE_USERS_API_URL: "https://manage-users-api-preprod.hmpps.service.justice.gov.uk"
TOKEN_VERIFICATION_API_URL: "https://token-verification-api-preprod.prison.service.justice.gov.uk"
ENVIRONMENT_NAME: PRE-PRODUCTION
AUDIT_ENABLED: "false"
Expand Down
1 change: 0 additions & 1 deletion helm_deploy/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ generic-service:
env:
INGRESS_URL: "https://hmpps-template-typescript.hmpps.service.justice.gov.uk"
HMPPS_AUTH_URL: "https://sign-in.hmpps.service.justice.gov.uk/auth"
MANAGE_USERS_API_URL: "https://manage-users-api.hmpps.service.justice.gov.uk"
TOKEN_VERIFICATION_API_URL: "https://token-verification-api.prison.service.justice.gov.uk"
AUDIT_ENABLED: "false"

Expand Down
3 changes: 0 additions & 3 deletions integration_tests/e2e/health.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ context('Healthcheck', () => {
beforeEach(() => {
cy.task('reset')
cy.task('stubAuthPing')
cy.task('stubManageUsersPing')
cy.task('stubTokenVerificationPing')
})

Expand All @@ -24,14 +23,12 @@ context('Healthcheck', () => {
beforeEach(() => {
cy.task('reset')
cy.task('stubAuthPing')
cy.task('stubManageUsersPing')
cy.task('stubTokenVerificationPing', 500)
})

it('Reports correctly when token verification down', () => {
cy.request({ url: '/health', method: 'GET', failOnStatusCode: false }).then(response => {
expect(response.body.components.hmppsAuth.status).to.equal('UP')
expect(response.body.components.manageUsersApi.status).to.equal('UP')
expect(response.body.components.tokenVerification.status).to.equal('DOWN')
expect(response.body.components.tokenVerification.details).to.contain({ status: 500, retries: 2 })
})
Expand Down
4 changes: 2 additions & 2 deletions integration_tests/e2e/signIn.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ context('Sign In', () => {
beforeEach(() => {
cy.task('reset')
cy.task('stubSignIn')
cy.task('stubManageUser')
})

it('Unauthenticated user directed to auth', () => {
Expand Down Expand Up @@ -67,7 +66,8 @@ context('Sign In', () => {
cy.request('/').its('body').should('contain', 'Sign in')

cy.task('stubVerifyToken', true)
cy.task('stubManageUser', 'bobby brown')
cy.task('stubSignIn', { name: 'bobby brown' })

cy.signIn()

indexPage.headerUserName().contains('B. Brown')
Expand Down
19 changes: 13 additions & 6 deletions integration_tests/mockApis/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import { Response } from 'superagent'
import { stubFor, getMatchingRequests } from './wiremock'
import tokenVerification from './tokenVerification'

const createToken = (roles: string[] = []) => {
interface UserToken {
name?: string
roles?: string[]
}

const createToken = (userToken: UserToken) => {
// authorities in the session are always prefixed by ROLE.
const authorities = roles.map(role => (role.startsWith('ROLE_') ? role : `ROLE_${role}`))
const authorities = userToken.roles?.map(role => (role.startsWith('ROLE_') ? role : `ROLE_${role}`)) || []
const payload = {
name: userToken.name || 'john smith',
user_name: 'USER1',
scope: ['read'],
auth_source: 'nomis',
Expand Down Expand Up @@ -97,7 +103,7 @@ const manageDetails = () =>
},
})

const token = (roles: string[] = []) =>
const token = (userToken: UserToken) =>
stubFor({
request: {
method: 'POST',
Expand All @@ -110,7 +116,7 @@ const token = (roles: string[] = []) =>
Location: 'http://localhost:3007/sign-in/callback?code=codexxxx&state=stateyyyy',
},
jsonBody: {
access_token: createToken(roles),
access_token: createToken(userToken),
token_type: 'bearer',
user_name: 'USER1',
expires_in: 599,
Expand All @@ -119,10 +125,11 @@ const token = (roles: string[] = []) =>
},
},
})

export default {
getSignInUrl,
stubAuthPing: ping,
stubAuthManageDetails: manageDetails,
stubSignIn: (roles: string[]): Promise<[Response, Response, Response, Response, Response]> =>
Promise.all([favicon(), redirect(), signOut(), token(roles), tokenVerification.stubVerifyToken()]),
stubSignIn: (userToken: UserToken = {}): Promise<[Response, Response, Response, Response, Response]> =>
Promise.all([favicon(), redirect(), signOut(), token(userToken), tokenVerification.stubVerifyToken()]),
}
36 changes: 0 additions & 36 deletions integration_tests/mockApis/manageUsersApi.ts

This file was deleted.

11 changes: 5 additions & 6 deletions server/@types/express/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { UserDetails } from '../../services/userService'
import { HmppsUser } from '../../interfaces/hmppsUser'

export default {}

declare module 'express-session' {
export declare module 'express-session' {
// Declare that the session will potentially contain these additional fields
interface SessionData {
returnTo: string
Expand All @@ -12,7 +10,8 @@ declare module 'express-session' {

export declare global {
namespace Express {
interface User extends Partial<UserDetails> {
interface User {
username: string
token: string
authSource: string
}
Expand All @@ -24,7 +23,7 @@ export declare global {
}

interface Locals {
user: Express.User
user: HmppsUser
}
}
}
2 changes: 1 addition & 1 deletion server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function createApp(services: Services): express.Application {
app.use(setUpAuthentication())
app.use(authorisationMiddleware())
app.use(setUpCsrf())
app.use(setUpCurrentUser(services))
app.use(setUpCurrentUser())

app.use(routes(services))

Expand Down
8 changes: 0 additions & 8 deletions server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ export default {
systemClientId: get('SYSTEM_CLIENT_ID', 'clientid', requiredInProduction),
systemClientSecret: get('SYSTEM_CLIENT_SECRET', 'clientsecret', requiredInProduction),
},
manageUsersApi: {
url: get('MANAGE_USERS_API_URL', 'http://localhost:9091', requiredInProduction),
timeout: {
response: Number(get('MANAGE_USERS_API_TIMEOUT_RESPONSE', 10000)),
deadline: Number(get('MANAGE_USERS_API_TIMEOUT_DEADLINE', 10000)),
},
agent: new AgentConfig(Number(get('MANAGE_USERS_API_TIMEOUT_RESPONSE', 10000))),
},
tokenVerification: {
url: get('TOKEN_VERIFICATION_API_URL', 'http://localhost:8100', requiredInProduction),
timeout: {
Expand Down
4 changes: 1 addition & 3 deletions server/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ initialiseAppInsights()
buildAppInsightsClient(applicationInfo)

import HmppsAuthClient from './hmppsAuthClient'
import ManageUsersApiClient from './manageUsersApiClient'
import { createRedisClient } from './redisClient'
import RedisTokenStore from './tokenStore/redisTokenStore'
import InMemoryTokenStore from './tokenStore/inMemoryTokenStore'
Expand All @@ -25,10 +24,9 @@ export const dataAccess = () => ({
hmppsAuthClient: new HmppsAuthClient(
config.redis.enabled ? new RedisTokenStore(createRedisClient()) : new InMemoryTokenStore(),
),
manageUsersApiClient: new ManageUsersApiClient(),
hmppsAuditClient: new HmppsAuditClient(config.sqs.audit),
})

export type DataAccess = ReturnType<typeof dataAccess>

export { HmppsAuthClient, RestClientBuilder, ManageUsersApiClient, HmppsAuditClient }
export { HmppsAuthClient, RestClientBuilder, HmppsAuditClient }
37 changes: 0 additions & 37 deletions server/data/manageUsersApiClient.test.ts

This file was deleted.

30 changes: 0 additions & 30 deletions server/data/manageUsersApiClient.ts

This file was deleted.

Loading

0 comments on commit 78e2040

Please sign in to comment.