Skip to content
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 22 commits into from
Jan 7, 2025
Merged

Conversation

PathumN99
Copy link
Collaborator

@PathumN99 PathumN99 commented Dec 11, 2024

#7980

  • Moved the opencrvs/opencrvs-core Gateway OIDPUserInfo logic into the server folder of opencrvs/mosip
  • Created an esignet-mock server
  • Created a monorepo to start all servers for dev purposes

Screenshot 2025-01-06 at 13 08 41

TODO:

  • pass state correctly to authorize

TODO in next pull request:

  • fix location matching

@PathumN99 PathumN99 requested a review from euanmillar December 11, 2024 16:47
@euanmillar
Copy link
Contributor

@PathumN99 you have done some great work so far. I just pushed a couple of commits. I changed the PORT of the esignet-mock to be one which wont clash with OpenCRVS client. Also we need to keep the fetch token request as E-Signet expects with the application/x-www-form-urlencoded content type.


/*

TODO: Understand from Tahmid about how to handle this:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tahmidrahman-dsi can you advise how we can send this state and callback URL to E-Signet using the REDIRECT button please?

Copy link
Collaborator

@tahmidrahman-dsi tahmidrahman-dsi Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@euanmillar Current implementation saves no state. But can be saved if the requirement needs. Before developing, it would be great if I have a bit more understanding from my side.

client_id=$openIdProviderClientId
response_type='code'
scope='openid profile'
acr_values='mosip:idp:acr:static-code'
claims=$openIdProviderClaims

As per my understanding, the above parameters are appended as search query params to the redirect url (esignetAuthUrl). And when the redirection is completed, eSignet forwards user to the opencrvs form url with param authorized=true which causes calling the callback url esignetUserinfoUrl. But you need those params for calling the esignetUserinfoUrl. Isn't it?

If my understanding is correct, does putting those params inside redirectCallbackFetch field's options field solve the issue? Please let me know your thoughts.

cc: @naftis

Copy link
Contributor

@naftis naftis Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct to this step:

As per my understanding, the above parameters are appended as search query params to the redirect url (esignetAuthUrl).

After that though, the above URL redirects back to OpenCRVS with

https://register.mosip.opencrvs.dev/drafts/1234-1234-1234-1234/informant?state=same-that-you-supplied&nonce=same-that-you-supplied&code=authorization_code&error_description=in_case_of_error_this_is_sent&error=error_code

The onmount-fetch should be able to be configured so that for example a ?code=.. exists. To the ?state=.. we can set anything that E-Signet sends us back as-is, so we could put something like "?state=trigger-onmount" there to be explicit. So yes, we can use these parameters instead of authorized=true. This code is then used to fetch the details from @opencrvs/mosip/server, which then again fetches the userinfo from E-Signet.

https://docs.esignet.io/integration/relying-party
The full flow described above is documented here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tahmidrahman-dsi @naftis my question was, how does the REDIRECT button know the draftId in order to format the state URL query param so that this works with mock-authorizer HTML which appears to need it like this: const params = new URLSearchParams(window.location.search); const opencrvsDraft = params.get("opencrvsDraft"); const opencrvsFormPath = "{{CLIENT_URL}}" + "/drafts/" + opencrvsDraft + "/events/birth/child/group/child-view-group"; const destinationURL = new URL(opencrvsFormPath); destinationURL.searchParams.set("authorized", true); window.location.replace(destinationURL);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -13,6 +13,10 @@
"envalid": "^8.0.0",
"fastify": "^5.0.0",
"fastify-type-provider-zod": "^4.0.2",
"jose": "^5.9.6",
"jsonwebtoken": "^9.0.2",
"node-fetch": "2",
Copy link
Contributor

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 one

Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor

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

yarn.lock Outdated Show resolved Hide resolved
@euanmillar euanmillar requested a review from naftis January 6, 2025 12:49
@euanmillar euanmillar added 💅 Waiting For Review Further information is requested and removed 🚧 work in progress labels Jan 6, 2025
node_modules
.secrets
.turbo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 nice, please introduce Turbo to me!

packages/.DS_Store Outdated Show resolved Hide resolved
PORT: port({ default: 20260 }),
HOST: str({ default: "0.0.0.0", devDefault: "localhost" }),
CLIENT_URL: url({ devDefault: "http://localhost:3000" }),
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
});
});

hmmm have I might have forgot to install Prettier to this repo. I can action on that

Comment on lines +181 to +184
const searchLocationFromFHIR = (name: string) =>
fetchLocationFromFHIR<fhir2.Bundle>(
`/locations?${new URLSearchParams({ name, type: "ADMIN_STRUCTURE" })}`
);
Copy link
Contributor

@naftis naftis Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add an issue to refactor this to use a GraphQL endpoint, or something else than directly querying Hearth: but for first version to just get everything working it's fine.

Locations... 🤯 🤔 🤔

};

const pickUserInfo = async (userInfo: OIDPUserInfo) => {
// TODO: refactor using shared IDs and leaf level search using Barry's work on Uganda
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -8,6 +8,8 @@ import { mosipHandler, mosipNidSchema } from "./webhooks/mosip";
import { opencrvsHandler, opencrvsRecordSchema } from "./webhooks/opencrvs";
import { env } from "./constants";
import * as openapi from "./openapi-documentation";
import { getOIDPUserInfo, OIDPUserInfoSchema } from "./esignet-api";
import formbody from "@fastify/formbody";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is formbody needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got an error for a fetch call which uses content type application/x-www-form-urlencoded. This is a plugin for Fastify that adds a content type parser for the content type application/x-www-form-urlencoded which resolved the issue. Wonder whether there is a workaround for this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, but where do we make a fetch call using application/x-www-form-urlencoded?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in the fetchToken function here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably makes sense to just change that to be application/json. Not sure why it's www-form in the first place :o

Copy link
Contributor

@naftis naftis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks like it's going to a good direction and like to see recent libraries being tried out like Turbo :D

@euanmillar euanmillar removed the 💅 Waiting For Review Further information is requested label Jan 7, 2025
@euanmillar euanmillar marked this pull request as ready for review January 7, 2025 13:19
@euanmillar euanmillar merged commit 3c8a01f into main Jan 7, 2025
1 check passed
@euanmillar euanmillar deleted the ocrvs-7980 branch January 7, 2025 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants