Code Monkey home page Code Monkey logo

myinfo-rails's Introduction

Rails wrapper for MyInfo API

tests

MyInfo Documentation (Public)

MyInfo Documentation (Government)

Basic Setup (Public)

  1. bundle add myinfo
  2. 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
  1. 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
    )
  1. On redirect_url, obtain a MyInfo::V3::Token. This token can only be used once.
    response = MyInfo::V3::Token.call(
      code: params[:code],
      state: params[:state]
    )
  1. Obtain the access_token from the response and query for MyInfo::V3::Person:
result = MyInfo::V3::Person.call(access_token: response.data) if response.success?

Basic Setup (Government)

  1. bundle add myinfo
  2. 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
  1. 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
    )
  1. On redirect_url, obtain a MyInfo::V3::Token. This token can only be used once.
    response = MyInfo::V3::Token.call(
      code: params[:code],
      state: params[:state]
    )
  1. Obtain the access_token from the response and query for MyInfo::V3::Person:
result = MyInfo::V3::Person.call(access_token: response.data) if response.success?

Sample App Demo

  1. git clone [email protected]:GovTechSG/myinfo-rails.git
  2. cd myinfo-rails
  3. bundle install
  4. cd spec/dummy && rails s
  5. Navigate to localhost:3001

Advanced

  • attributes can be passed to AuthoriseUrl and Person as an array to override the default attributes queried - check MyInfo for a list of available attributes.

  • success? can be called on MyInfo::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!

  1. Fork the repository
  2. Write code and tests
  3. Submit a PR

myinfo-rails's People

Contributors

causztic avatar dependabot[bot] avatar kmye avatar sampradeep avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

myinfo-rails's Issues

MyInfo V4

Implement a connector for MyInfo V4 that has the following major changes:

  1. PKCE
  2. Client Assertion
  3. JWKS
  4. DPoP

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.