Rails wrapper for MyInfo API
MyInfo Documentation (Government)
Basic Setup (Public)
bundle add myinfo
Create a
config/initializers/myinfo.rb
and add the required configuration based on your environment.MyInfo.configure do |config| config.app_id = '' config.client_id = '' config.client_secret = '' config.base_url = 'test.api.myinfo.gov.sg' config.redirect_uri = 'https://localhost:3001/callback' config.public_facing = true config.private_key = File.read(Rails.root.join('private_key_location')) config.public_cert = File.read(Rails.root.join('public_cert_location')) config.sandbox = false # optional, false by default config.proxy = { address: 'proxy_address', port: 'proxy_port' } # optional, nil by default config.gateway_url = 'https://test_gateway_url' #optional, nil by default config.gateway_key = '44d953c796cccebcec9bdc826852857ab412fbe2' #optional, nil by default end
To obtain a person's MyInfo information, we need to authorise the query first:
redirect_to MyInfo::V3::AuthoriseUrl.call( purpose: 'set your purpose here', state: SecureRandom.hex # set a state to check on callback )
On
redirect_url
, obtain aMyInfo::V3::Token
. This token can only be used once.response = MyInfo::V3::Token.call( code: params[:code], state: params[:state] )
Obtain the
access_token
from theresponse
and query forMyInfo::V3::Person
:result = MyInfo::V3::Person.call(access_token: response.data) if response.success?
Basic Setup (Government)
bundle add myinfo
Create a
config/initializers/myinfo.rb
and add the required configuration based on your environment.MyInfo.configure do |config| config.app_id = '' config.client_id = '' config.client_secret = '' config.base_url = 'test.api.myinfo.gov.sg' config.redirect_uri = 'https://localhost:3001/callback' config.singpass_eservice_id = 'MYINFO-CONSENTPLATFORM' config.private_key = File.read(Rails.root.join('private_key_location')) config.public_cert = File.read(Rails.root.join('public_cert_location')) config.sandbox = false # optional, false by default config.proxy = { address: 'proxy_address', port: 'proxy_port' } # optional, nil by default config.gateway_url = 'https://test_gateway_url' #optional, nil by default config.gateway_key = '44d953c796cccebcec9bdc826852857ab412fbe2' #optional, nil by default end
To obtain a person's MyInfo information, we need to authorise the query first:
redirect_to MyInfo::V3::AuthoriseUrl.call( nric_fin: "user's NRIC", # see documentation for list of sample NRICs purpose: 'set your purpose here', state: SecureRandom.hex # set a state to check on callback )
On
redirect_url
, obtain aMyInfo::V3::Token
. This token can only be used once.response = MyInfo::V3::Token.call( code: params[:code], state: params[:state] )
Obtain the
access_token
from theresponse
and query forMyInfo::V3::Person
:result = MyInfo::V3::Person.call(access_token: response.data) if response.success?
Sample App Demo
git clone git@github.com:GovTechSG/myinfo-rails.git
cd myinfo-rails
bundle install
cd spec/dummy && rails s
- Navigate to
localhost:3001
Advanced
attributes
can be passed toAuthoriseUrl
andPerson
as an array to override the default attributes queried - check MyInfo for a list of available attributes.success?
can be called onMyInfo::V3::Response
to determine whether the query has succeeded or failed. Check MyInfo API for a list of responses and how to handle them.
Disclaimer
Provided credentials in the repository are either obtained from MyInfo Demo App or samples online, and are only for testing purposes. They should not be re-used for staging or production environments. Visit the official MyInfo tutorial for more information.
Contributing
Contributions are welcome!
- Fork the repository
- Write code and tests
- Submit a PR