-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Re-implementing E-Signet flow to mosip repository #23
Merged
Merged
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
693c91f
feat: initial commit of re-implementing E-Signet flow to mosip reposi…
2aa2ea2
feat: set up the get-oidp-user-info endpoint and replicated GraphQL q…
PathumN99 4b67f2f
feat: E-Signet mock server initial commit
PathumN99 695007b
refactor: moved e-signet related methods to esignet-api.ts
PathumN99 aed6020
refactor: refactored get-oidp-user-info endpoint
PathumN99 4f4ddb7
feat: Form configuration changes
PathumN99 f9a5720
Some minor amends
euanmillar fe72e50
aligned fetch token to e-signet requirements and changed port
euanmillar 8e9c98b
Add search params to authorise and comments for todos
euanmillar ef95c8f
Add note around search params
euanmillar fd610fd
feat: oidc/userinfo endpoint in esignet-mock server
PathumN99 ad5cb20
/authorize endpoint in esignet-mock server
PathumN99 7e0e4b1
Added @fastify/formbody plugin to accept x-www-form-urlencoded conten…
PathumN99 823cdc5
/esignet/get-oidp-user-info endpoint minor changes
PathumN99 993c8b2
Minor changes in fetch location from FHIR URL
PathumN99 720bbb1
generateSignedJwt issue fixed in get-oidp-user-info API
PathumN99 1175621
Refactor JWT and set up monorepo
euanmillar 1825d50
Remove completed todos
euanmillar 54ae37e
Fix conflicts
euanmillar 9f0f475
remove .DS_Store and update gitignore
euanmillar 9f68810
rename webhooks in a future PR
euanmillar 8781067
Bump version number
euanmillar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
|
||
import * as jwt from "jsonwebtoken"; | ||
import { env } from "./constants"; | ||
import fetch from 'node-fetch' | ||
import { logger } from "./logger"; | ||
|
||
type OIDPUserAddress = { | ||
formatted: string; | ||
street_address: string; | ||
locality: string; | ||
region: string; | ||
postal_code: string; | ||
city: string; | ||
country: string; | ||
}; | ||
|
||
type OIDPUserInfo = { | ||
sub: string; | ||
name?: string; | ||
given_name?: string; | ||
family_name?: string; | ||
middle_name?: string; | ||
nickname?: string; | ||
preferred_username?: string; | ||
profile?: string; | ||
picture?: string; | ||
website?: string; | ||
email?: string; | ||
email_verified?: boolean; | ||
gender?: "female" | "male"; | ||
birthdate?: string; | ||
zoneinfo?: string; | ||
locale?: string; | ||
phone_number?: string; | ||
phone_number_verified?: boolean; | ||
address?: Partial<OIDPUserAddress>; | ||
updated_at?: number; | ||
}; | ||
|
||
const OIDP_USERINFO_ENDPOINT = | ||
env.NATIONAL_ID_OIDP_REST_URL && new URL('oidc/userinfo', env.NATIONAL_ID_OIDP_REST_URL).toString() | ||
|
||
const decodeUserInfoResponse = (response: string) => { | ||
return jwt.decode(response) as OIDPUserInfo; | ||
}; | ||
|
||
export const fetchFromHearth = <T = any>( | ||
euanmillar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
suffix: string, | ||
method = "GET", | ||
body: string | undefined = undefined | ||
): Promise<T> => { | ||
return fetch(`${env.HEARTH_URL}${suffix}`, { | ||
method, | ||
headers: { | ||
"Content-Type": "application/fhir+json", | ||
}, | ||
body, | ||
}) | ||
.then((response) => { | ||
return response.json(); | ||
}) | ||
.catch((error) => { | ||
return Promise.reject( | ||
new Error(`FHIR with Hearth request failed: ${error.message}`) | ||
); | ||
}); | ||
}; | ||
|
||
const searchLocationFromHearth = (name: string) => | ||
euanmillar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fetchFromHearth<fhir2.Bundle>( | ||
`/Location?${new URLSearchParams({ name, type: "ADMIN_STRUCTURE" })}` | ||
); | ||
|
||
const findAdminStructureLocationWithName = async (name: string) => { | ||
const fhirBundleLocations = await searchLocationFromHearth(name); | ||
|
||
if ((fhirBundleLocations.entry?.length ?? 0) > 1) { | ||
throw new Error( | ||
"Multiple admin structure locations found with the same name" | ||
); | ||
} | ||
|
||
if ((fhirBundleLocations.entry?.length ?? 0) === 0) { | ||
logger.warn("No admin structure location found with the name: " + name); | ||
return null; | ||
} | ||
|
||
return fhirBundleLocations.entry?.[0].resource?.id; | ||
}; | ||
|
||
const pickUserInfo = async (userInfo: OIDPUserInfo) => { | ||
const stateFhirId = | ||
userInfo.address?.country && | ||
(await findAdminStructureLocationWithName(userInfo.address.country)); | ||
|
||
return { | ||
oidpUserInfo: userInfo, | ||
stateFhirId, | ||
districtFhirId: | ||
userInfo.address?.region && | ||
(await findAdminStructureLocationWithName(userInfo.address.region)), | ||
locationLevel3FhirId: | ||
userInfo.address?.locality && | ||
(await findAdminStructureLocationWithName(userInfo.address.locality)), | ||
}; | ||
}; | ||
|
||
export const fetchUserInfo = async (accessToken: string) => { | ||
const request = await fetch(OIDP_USERINFO_ENDPOINT!, { | ||
headers: { | ||
Authorization: "Bearer " + accessToken, | ||
}, | ||
}); | ||
|
||
const response = await request.text(); | ||
const decodedResponse = decodeUserInfoResponse(response); | ||
|
||
logger.info(`OIDP user info response: ${JSON.stringify(decodedResponse)}`); | ||
|
||
return pickUserInfo(decodedResponse); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
import pino from "pino" | ||
export const logger = pino() | ||
|
||
const level = process.env.NODE_ENV === 'test' ? 'silent' : process.env.LOG_LEVEL | ||
if (level) { | ||
logger.level = level | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we need
node-fetch
with a recent Node version? The country-config package might need it (as @types/node-fetch) as it's exported into the country-configuration but otherwise we could be fine with the default oneThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that we can test the server as standalone before it is merged into opencrvs-countryconfig-mosio, I think we should leave it in and remove later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made it a devDependency. Is that OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When running locally standalone, we can use the Node's native fetch? And when the country-config uses the Docker image of
packages/server
, the contained image also can just use the native fetch? I think the country-config itself isn't running this code as is, if I understand this correctly