Skip to content

Instantly share code, notes, and snippets.

@dariocravero
Created April 13, 2019 12:06
Show Gist options
  • Select an option

  • Save dariocravero/75e14f672a8e2b9d32c408cea23c8b81 to your computer and use it in GitHub Desktop.

Select an option

Save dariocravero/75e14f672a8e2b9d32c408cea23c8b81 to your computer and use it in GitHub Desktop.

Revisions

  1. Darío Javier Cravero created this gist Apr 13, 2019.
    101 changes: 101 additions & 0 deletions ub-hackathon-connect.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    let fetch = require('node-fetch')

    // let CLIENT_ID = 'tbBFGo6qj7A90zWKLDOoIpCIsLmSzdbsebMkTaIfCCI='
    // let CLIENT_SECRET = '0us3SYDAA9otXvRrGmEvtuqHT4StQyqmsSFFK_76YfM='
    let CLIENT_ID = 'SsqVtOMuDkMSMkcWhsCcl0V3mP-6UdmeF8L7Chbup5s='
    let CLIENT_SECRET = 'U95aq9M_iqtDOM2cZKvNWPK4s4iUs6K7TLVljB5XV1M='
    let REDIRECT_URI = 'https://ub-test-hackathon-2019.ulsterbank.com/redirect'
    // customer number
    // let TEST_USER_NAME = '1492884928|2' // '0014056040072' // '1492884928'
    let TEST_USER_NAME ='001405640072' // '1495667979|1'
    let YOUR_TEAM_DOMAIN = 'test.com' // 'ub-test-hackathon-2019.ulsterbank.com'

    process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
    module.exports = async function getTokenAccounts(url = 'https://ob.ulster.useinfinite.io/open-banking/v3.1/aisp') {
    let resToken = await fetch(
    `https://ob.ulster.useinfinite.io/token?grant_type=client_credentials&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&scope=accounts`,
    {
    method: 'post',
    headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    },
    }
    )
    let token = await resToken.json()
    console.log(token)

    let resConsent = await fetch(`${url}/account-access-consents`, {
    method: 'post',
    headers: {
    Authorization: `${token.token_type} ${token.access_token}`,

    'Content-Type': 'application/json',
    'x-fapi-financial-id': 'TBD',
    },
    body: JSON.stringify({
    Data: {
    Permissions: [
    'ReadAccountsDetail',
    'ReadBalances',
    'ReadTransactionsCredits',
    'ReadTransactionsDebits',
    'ReadTransactionsDetail',
    ],
    },
    Risk: {},
    }),
    })

    let consent = await resConsent.json()
    console.log(consent)

    let resAuthorize = await fetch(
    [
    `https://api.ulster.useinfinite.io/authorize`,
    `?client_id=${CLIENT_ID}`,
    `&response_type=code id_token`,
    `&scope=openid accounts`,
    `&redirect_uri=${REDIRECT_URI}`,
    `&request=${consent.Data.ConsentId}`,
    `&authorization_mode=AUTO_POSTMAN`,
    `&authorization_result=APPROVED`,
    `&authorization_username=${TEST_USER_NAME}@${YOUR_TEAM_DOMAIN}`,
    `&authorization_accounts=*`,
    ].join('')
    )

    if (resAuthorize.status !== 200) {
    console.error(await resAuthorize.text())
    }

    let authorize = await resAuthorize.json()
    console.log(authorize)

    let code = authorize.redirectUri.split('#code=')[1].split('&')[0]
    console.log(code)

    let resExchange = await fetch(
    `https://ob.ulster.useinfinite.io/token?client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&redirect_uri=${REDIRECT_URI}&grant_type=authorization_code&code=${code}`,
    {
    method: 'post',
    headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    },
    }
    )



    let exchange = await resExchange.json()


    let data =await fetch('https://ob.ulster.useinfinite.io/open-banking/v3.1/aisp/accounts', {
    headers: {
    Authorization: `${exchange.token_type} ${exchange.access_token}`,
    'x-fapi-financial-id': 'TBD',
    },
    })

    console.log('DATA----', await data.json())
    return exchange
    }