MyInfo Connector aims to simplify consumer's integration effort with MyInfo by providing an easy to use .NET library to integrate into your application.
.NET Framework 4.8
Add the following nuget packages to your application.
jose-jwt
Newtonsoft.Json
Add the namespace as below to access the MyInfoConnector into your code:
namespace sg.gov.ndi;
Config can be supplied in a number of ways, app.config, NameValueCollection or by passing a Func<string,string>
.
You are required to create a config file with the following key values for this library. Sample config files can be found in this repository under the Sample Configuration folder.
Required config values | Description |
---|---|
PrivateKeyFilename | Path of the .p12 file that holds the private key. Absolute or relative path to assembly |
PrivateKeyPassword | Password of your private key p12 file. |
PublicCertificateFilename | Path to the MyInfo public certificate. Absolute or relative path to assembly |
ClientId | Unique ID provided upon approval of your application to use MyInfo. For our sample application, it is STG2-MYINFO-SELF-TEST |
ClientSecret | Secret key provided upon approval of your application to use MyInfo. For our sample application, it is 44d953c796cccebcec9bdc826852857ab412fbe2 |
AttributeCsv | Comma separated list of attributes requested. Possible attributes are listed in the Person object definition in the API specifications. |
Environment | The environment your application is configured. This can be SANDBOX, TEST or PROD. |
AuthoriseUrl | Specify the AUTHORISE API URL for MyInfo. The API is available in three environments: SANDBOX: https://sandbox.api.myinfo.gov.sg/com/v3/authorise TEST: https://test.api.myinfo.gov.sg/com/v3/authorise PROD: https://api.myinfo.gov.sg/com/v3/authorise |
TokenUrl | Specify the TOKEN API URL for MyInfo. The API is available in three environments: SANDBOX: https://sandbox.api.myinfo.gov.sg/com/v3/token TEST: https://test.api.myinfo.gov.sg/com/v3/token PROD: https://api.myinfo.gov.sg/com/v3/token |
PersonUrl | Specify the PERSON API URL for MyInfo. The API is available in three environments: SANDBOX: https://sandbox.api.myinfo.gov.sg/com/v3/person TEST: https://test.api.myinfo.gov.sg/com/v3/person PROD: https://api.myinfo.gov.sg/com/v3/person |
Purpose | The text passed to the OAuth service to inform the user about the purpose to get his/her data. This will be shown to the user when requesting for his/her consent. |
Func<string, string> getConfig = key =>
{
// given a key, return the value. This pattern allows external methods of encryption/decryption of secrets
};
var connector = MyInfoConnector.Create(getConfig);
The authorise URL includes the redirect URL so that the service can redirect the user to it after granting consent.
var authoriseUrl = connector.GetAuthoriseUrl(redirectUrl);
// Invoke the authorise endpoint with authoriseUrl. Receive the callback and get the `authcode`
Retrieve person's data by passing the authorisation code and state from the Authorise API call:
connector.GetPersonJson(authCode, state);
txnNo is an optional parameter that can be passed through the overloaded method, if required.
connector.GetPersonJson(authCode, txnNo, state);
Under the hood, MyInfoConnector make use of MyInfoSecurityHelper and you may use the class as util methods to meet your application needs.
This method takes in the API call method (GET, POST, etc.), API URL, and all the required parameters into a treemap, sort them and form the base string.
MyInfoSecurityHelper.GenerateBaseString(method, url, baseParams);
This method takes in the base string and the private key to sign and generate the signature.
MyInfoSecurityHelper.generateSignature(baseString, privateKey);
This method takes in all the required parameters into a treemap and assemble the header.
MyInfoSecurityHelper.GenerateAuthorizationHeader(authHeaderParams);
It also provide an overloaded method that takes in the bearer token, if required.
MyInfoSecurityHelper.generateAuthorizationHeader(authHeaderParams, bearer);
This method takes in the decrypted payload and the public key to verify the token.
MyInfoSecurityHelper.verifyToken(decryptedToken, pubKey);
You may contact [email protected] for any other technical issues, and we will respond to you within 5 working days.