@defra/lis-hubs-infra-access 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/src/krds-client.js +123 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-hubs-infra-access",
3
- "version": "0.18.0",
3
+ "version": "0.19.0",
4
4
  "description": "Shared hub access and capability resolution for the livestock platform.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -10,7 +10,8 @@
10
10
  "exports": {
11
11
  ".": "./src/module-access/index.js",
12
12
  "./auth": "./src/auth.js",
13
- "./ish-client": "./src/identity-service-helper-client.js"
13
+ "./ish-client": "./src/identity-service-helper-client.js",
14
+ "./krds-client": "./src/krds-client.js"
14
15
  },
15
16
  "files": [
16
17
  "src"
@@ -0,0 +1,123 @@
1
+ import Wreck from '@hapi/wreck'
2
+ import { requestContext } from '@defra/lis-hubs-infra-core'
3
+ import { statusCodes } from './constants/status-codes.js'
4
+
5
+ /**
6
+ * @typedef {object} CphAssociation
7
+ * @property {string} id
8
+ * @property {string} cphNumber
9
+ * @property {string} role
10
+ * @property {string | null} partyId
11
+ * @property {string | null} holdingId
12
+ * @property {string | null} holdingName
13
+ */
14
+
15
+ /**
16
+ * @typedef {object} UserAccount
17
+ * @property {string} id
18
+ * @property {string | null} subject
19
+ * @property {string} email
20
+ * @property {string | null} firstName
21
+ * @property {string | null} lastName
22
+ * @property {string | null} displayName
23
+ * @property {CphAssociation[]} cphAssociations
24
+ * @property {string | null} associationsRefreshedDate
25
+ * @property {string} lastUpdatedDate
26
+ */
27
+
28
+ export class KrdsClient {
29
+ #baseUrl
30
+ #clientId
31
+ #clientSecret
32
+
33
+ /**
34
+ * @param {string} baseUrl
35
+ * @param {string} clientId
36
+ * @param {string} clientSecret
37
+ */
38
+ constructor(baseUrl, clientId, clientSecret) {
39
+ this.#baseUrl = baseUrl
40
+ this.#clientId = clientId
41
+ this.#clientSecret = clientSecret
42
+ }
43
+
44
+ /**
45
+ * Ensures a user account exists for the supplied identity provider
46
+ * claims, called on every successful logon.
47
+ *
48
+ * @param {{ sub?: string | null, email: string, given_name?: string | null, family_name?: string | null }} claims
49
+ * @returns {Promise<UserAccount>}
50
+ */
51
+ async ensureAccount(claims) {
52
+ let result
53
+
54
+ try {
55
+ result = await Wreck.post('api/v2/user-accounts', {
56
+ baseUrl: this.#baseUrl,
57
+ json: true,
58
+ payload: claims,
59
+ headers: this.#getHeaders()
60
+ })
61
+ } catch (err) {
62
+ throw this.#parseError(err.output?.statusCode, err.data?.payload)
63
+ }
64
+
65
+ if (result.res.statusCode >= statusCodes.badRequest) {
66
+ throw this.#parseError(result.res.statusCode, result.payload)
67
+ }
68
+
69
+ return result.payload
70
+ }
71
+
72
+ /**
73
+ * Reads a user account by identity provider subject. Read only - no
74
+ * association refresh is performed.
75
+ *
76
+ * @param {string} subject
77
+ * @returns {Promise<UserAccount>}
78
+ */
79
+ async fetchUserAccount(subject) {
80
+ let result
81
+
82
+ try {
83
+ result = await Wreck.get(
84
+ `api/v2/user-accounts/${encodeURIComponent(subject)}`,
85
+ {
86
+ baseUrl: this.#baseUrl,
87
+ json: true,
88
+ headers: this.#getHeaders()
89
+ }
90
+ )
91
+ } catch (err) {
92
+ throw this.#parseError(err.output?.statusCode, err.data?.payload)
93
+ }
94
+
95
+ if (result.res.statusCode >= statusCodes.badRequest) {
96
+ throw this.#parseError(result.res.statusCode, result.payload)
97
+ }
98
+
99
+ return result.payload
100
+ }
101
+
102
+ #getHeaders() {
103
+ const credentials = Buffer.from(
104
+ `${this.#clientId}:${this.#clientSecret}`
105
+ ).toString('base64')
106
+
107
+ return {
108
+ authorization: `Basic ${credentials}`,
109
+ 'x-correlation-id': requestContext.get('correlation_id'),
110
+ 'x-cdp-request-id': requestContext.get('correlation_id')
111
+ }
112
+ }
113
+
114
+ #parseError(statusCode, payload) {
115
+ if (payload?.detail || payload?.title) {
116
+ return new Error(`${payload.status} - ${payload.detail || payload.title}`)
117
+ }
118
+ if (statusCode === statusCodes.unprocessableEntity) {
119
+ return new Error('Validation failed')
120
+ }
121
+ return new Error(`Request failed - ${statusCode}`)
122
+ }
123
+ }