@defra/lis-hubs-infra-access 0.1.0 → 0.2.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.
- package/package.json +7 -3
- package/src/identity-service-helper-client.js +116 -0
- package/src/status-codes.js +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defra/lis-hubs-infra-access",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Shared hub access and capability resolution for the livestock platform.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
},
|
|
10
10
|
"exports": {
|
|
11
11
|
".": "./src/module-access.js",
|
|
12
|
-
"./auth": "./src/auth.js"
|
|
12
|
+
"./auth": "./src/auth.js",
|
|
13
|
+
"./ish-client": "./src/identity-service-helper-client.js"
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
15
16
|
"src"
|
|
@@ -32,6 +33,7 @@
|
|
|
32
33
|
"author": "Defra DDTS",
|
|
33
34
|
"license": "OGL-UK-3.0",
|
|
34
35
|
"dependencies": {
|
|
36
|
+
"@defra/lis-hubs-infra-core": "^0.2.0",
|
|
35
37
|
"@defra/lis-hubs-infra-registry": "^0.1.0",
|
|
36
38
|
"jose": "6.2.8",
|
|
37
39
|
"eslint-plugin-jsdoc": "^63.0.8",
|
|
@@ -41,10 +43,12 @@
|
|
|
41
43
|
"convict-format-with-validator": "6.2.0"
|
|
42
44
|
},
|
|
43
45
|
"peerDependencies": {
|
|
44
|
-
"eslint": "^9.0.0"
|
|
46
|
+
"eslint": "^9.0.0",
|
|
47
|
+
"@hapi/wreck": "18.1.2"
|
|
45
48
|
},
|
|
46
49
|
"devDependencies": {
|
|
47
50
|
"@defra/lis-infra-eslint-config": "^0.6.0",
|
|
51
|
+
"@hapi/wreck": "18.1.2",
|
|
48
52
|
"@vitest/coverage-istanbul": "^4.1.10",
|
|
49
53
|
"audit-ci": "^7.1.0",
|
|
50
54
|
"eslint": "^9.0.0",
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import Wreck from '@hapi/wreck'
|
|
2
|
+
import { requestContext } from '@defra/lis-hubs-infra-core'
|
|
3
|
+
import { statusCodes } from './status-codes.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {object} UserDetails
|
|
7
|
+
* @property {string} id
|
|
8
|
+
* @property {string} email
|
|
9
|
+
* @property {string} firstName
|
|
10
|
+
* @property {string} lastName
|
|
11
|
+
* @property {string} displayName
|
|
12
|
+
* @property {boolean} active
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {object} CphAssignment
|
|
17
|
+
* @property {string} id
|
|
18
|
+
* @property {string} countyParishHoldingId
|
|
19
|
+
* @property {string} countyParishHoldingNumber
|
|
20
|
+
* @property {string} userId
|
|
21
|
+
* @property {string} roleId
|
|
22
|
+
* @property {string} roleName
|
|
23
|
+
* @property {string} email
|
|
24
|
+
* @property {string} displayName
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @typedef {object} CphDelegation
|
|
29
|
+
* @property {string} id
|
|
30
|
+
* @property {string} countyParishHoldingId
|
|
31
|
+
* @property {string} countyParishHoldingNumber
|
|
32
|
+
* @property {string} delegatingUserId
|
|
33
|
+
* @property {string} delegatingUserName
|
|
34
|
+
* @property {string | null} delegatedUserId
|
|
35
|
+
* @property {string | null} delegatedUserName
|
|
36
|
+
* @property {string} delegatedUserRoleId
|
|
37
|
+
* @property {string} delegatedUserRoleName
|
|
38
|
+
* @property {string} delegatedUserEmail
|
|
39
|
+
* @property {string | null} invitationExpiresAt
|
|
40
|
+
* @property {string | null} invitationAcceptedAt
|
|
41
|
+
* @property {string | null} invitationRejectedAt
|
|
42
|
+
* @property {string | null} revokedAt
|
|
43
|
+
* @property {string | null} revokedById
|
|
44
|
+
* @property {string | null} revokedByName
|
|
45
|
+
* @property {string | null} expiresAt
|
|
46
|
+
* @property {boolean} active
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @typedef {object} UserProfile
|
|
51
|
+
* @property {UserDetails} userDetails
|
|
52
|
+
* @property {CphAssignment[]} directAssignments
|
|
53
|
+
* @property {CphDelegation[]} inboundDelegations
|
|
54
|
+
* @property {CphDelegation[]} outboundDelegations
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
export class IdentityServiceHelperClient {
|
|
58
|
+
#baseUrl
|
|
59
|
+
#apiKey
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @param {string} baseUrl
|
|
63
|
+
* @param {string} apiKey
|
|
64
|
+
*/
|
|
65
|
+
constructor(baseUrl, apiKey) {
|
|
66
|
+
this.#baseUrl = baseUrl
|
|
67
|
+
this.#apiKey = apiKey
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Fetches a user's profile from identity-service-helper.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} userId
|
|
74
|
+
* @returns {Promise<UserProfile>}
|
|
75
|
+
*/
|
|
76
|
+
async fetchUserProfile(userId) {
|
|
77
|
+
let result
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
result = await Wreck.get(`users/${userId}/profile`, {
|
|
81
|
+
baseUrl: this.#baseUrl,
|
|
82
|
+
json: true,
|
|
83
|
+
headers: this.#getHeaders()
|
|
84
|
+
})
|
|
85
|
+
} catch (err) {
|
|
86
|
+
throw this.#parseError(err.output?.statusCode, err.data?.payload)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (result.res.statusCode >= statusCodes.badRequest) {
|
|
90
|
+
throw this.#parseError(result.res.statusCode, result.payload)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return result.payload
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#getHeaders() {
|
|
97
|
+
return {
|
|
98
|
+
'x-api-key': this.#apiKey,
|
|
99
|
+
'x-correlation-id': requestContext.get('correlation_id'),
|
|
100
|
+
'x-cdp-request-id': requestContext.get('correlation_id')
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
#parseError(statusCode, payload) {
|
|
105
|
+
if (payload?.error?.code) {
|
|
106
|
+
return new Error(`${payload.error.code} - ${payload.error.message}`)
|
|
107
|
+
}
|
|
108
|
+
if (payload?.detail || payload?.title) {
|
|
109
|
+
return new Error(`${payload.status} - ${payload.detail || payload.title}`)
|
|
110
|
+
}
|
|
111
|
+
if (statusCode === statusCodes.unprocessableEntity) {
|
|
112
|
+
return new Error('Validation failed')
|
|
113
|
+
}
|
|
114
|
+
return new Error(`Request failed - ${statusCode}`)
|
|
115
|
+
}
|
|
116
|
+
}
|