leeleelee3264 / myinfo-connector-python

python connector for Singapore government mydata, myinfo.

Home Page:https://leelee-1.gitbook.io/myinfo-connector-python-api-doc/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

myinfo-connector-python


Start Date 2022-07-05
End Date 2022-07-23

This is repository for Python/Django Connector for Singapore government mydata service, Singpass myinfo. MyInfo Connector aims to simplify consumer's integration effort with MyInfo by providing an easy to use Python library to integrate into your application.

Myinfo provide Java, nodejs connector. I wanted to use myinfo API in Python, so I made a myinfo connector with python.

Documents

Check Quick Start: link
Check API Doc: link



Quick Start

myinfo-connector-python is built with Django to make easy-to-go application.

Step 1: Clone the repository in your local

git clone git@github.com:leeleelee3264/myinfo-connector-python.git

Step 2: Install Pre-requisite

Install Python

You can exclude python 3 is already installed in your local.

brew install python@3.8 pipenv

Set Python Path in ~/.zshrc

export PATH="/opt/homebrew/opt/python@3.8/bin:$PATH"

Refresh ~/.zshrc

source ~/.zshrc

Step 3: Install python packages

PIPENV_VENV_IN_PROJECT=1 
cd ~/myinfo-connector-python
pipenv install 

Step 4: Start server

pipenv run ./connector/manage.py runserver 0:3001


Make your request

The REST API to make request is described below.

Step 1: Get myinfo redirect login url

Request

GET /users/me/external/myinfo-redirect-login

curl -i -H 'Accept: application/json' http://localhost:3001/user/me/external/myinfo-redirect-login

Response

{
    "message": "OK",
    "data": {
        "url": "https://test.api.myinfo.gov.sg/com/v3/authorise?client_id=STG2-MYINFO-SELF-TEST&attributes=name,dob,birthcountry,nationality,uinfin,sex,regadd&state=eb03c000-00a3-4708-ab30-926306bfc4a8&redirect_uri=http://localhost:3001/callback&purpose=python-myinfo-connector",
        "state": "eb03c000-00a3-4708-ab30-926306bfc4a8"
    }
}

Step 2: Browse myinfo redirect login

curl https://test.api.myinfo.gov.sg/com/v3/authorise?client_id=STG2-MYINFO-SELF-TEST&attributes=name,dob,birthcountry,nationality,uinfin,sex,regadd&state=eb03c000-00a3-4708-ab30-926306bfc4a8&redirect_uri=http://localhost:3001/callback&purpose=python-myinfo-connector

Step 3: Do login and check agree terms

Myinfo Login Page

Myinfo Login Page

Myinfo Terms Agreement Page

Myinfo Terms Agreement Page


(Automated) Step 4: Callback API get called by Myinfo

After login Myinfo and agree terms, Myinfo service automatically call myinfo-connector-python's callback API to pass auth code. The authcode given by myinfo's authorise API

callback url example

http://localhost:3001/callback?code=8932a98da8720a10e356bc76475d76c4c628aa7f&state=e2ad339a-337f-45ec-98fa-1672160cf463

callback response HTML example

Response Page for callback api


(Automated) Final Step: Get Person data from Myinfo

After callback, callback page automatically calls our api for person data. The API is final step of myinfo-connector-python.

Request

GET /users/me/external/myinfo

curl -i -H 'Accept: application/json' http://localhost:3001/user/me/external/myinfo

Response

{
    "message": "OK",
    "sodata": {
        "regadd": {
            "country": {
                "code": "SG",
                "desc": "SINGAPORE"
            },
            "unit": {
                "value": "10"
            },
            "street": {
                "value": "ANCHORVALE DRIVE"
            },
            "lastupdated": "2022-07-14",
            "block": {
                "value": "319"
            },
            "source": "1",
            "postal": {
                "value": "542319"
            },
            "classification": "C",
            "floor": {
                "value": "38"
            },
            "type": "SG",
            "building": {
                "value": ""
            }
        },
        "dob": "1988-10-06",
        "sex": "M",
        "name": "ANDY LAU",
        "birthcountry": "SG",
        "nationality": "SG",
        "uinfin": "S6005048A"
    }
}


Project TODO List

  • 1st Implement
  • Apply Pipenv for package management
  • Apply python lint (flake8, pylint, mypy)
  • Documentation
  • 2nd Refactoring (Move DTO to domain layer)
  • 3th Redactoring (Entire)

About

python connector for Singapore government mydata, myinfo.

https://leelee-1.gitbook.io/myinfo-connector-python-api-doc/


Languages

Language:Python 99.3%Language:HTML 0.7%