Skip to content

Commit

Permalink
Added Auth & Verify functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ukrocks007 committed Jun 28, 2022
1 parent 65e99da commit 7f6df67
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 183 deletions.
132 changes: 96 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

This library can be used to integrate [ONDC](http://ondc.org/) in JavaScript based applications.

## Introduction
Package is developed in TypeScript and will work with Node.Js & other JavaScript based stacks.

## Introduction 👋

ONDC stands for Open Network for Digital Commernce.

ONDC is an Open Network which makes it easy for any buyer and seller to engage in a transactions.

![LOGO](https://ondc.org/image/logo.png)

### How to install?
### How to install? 👀

`npm i ondc-node --save`

## 1. Using to call APIs
## ☝️ Using to call APIs

### Initialization

Expand Down Expand Up @@ -47,7 +53,7 @@ const response = await instance.search({
})
```

## 2. Using as Express Middleware
## ✌️ Using as Express Middleware

```JS
const ONDC = require('ondc-node');
Expand All @@ -59,35 +65,89 @@ app.use(express.json());
app.use(ONDC.Middleware({"on_search": onSearchHandler, "init": initHandler}));
```

## Functions Covered

- search
- select
- init
- confirm
- status
- track
- cancel
- update
- rating
- support
- on_search
- on_select
- on_init
- on_confirm
- on_track
- on_cancel
- on_update
- on_status
- on_rating
- on_support
- get_cancellation_reasons
- cancellation_reasons
- get_return_reasons
- return_reasons
- get_rating_categories
- rating_categories
- get_feedback_categories
- feedback_categories
- get_feedback_form
- feedback_form
## 🤩 Effortless Authorization 🔐 with ONDC

You can use functions ```ondc.createAuthorizationHeader``` function to ```Authorization``` header.

```JS
const ondc = require('ondc-node');
// Private key & Public keys are must for Auth to work
const instance = new ondc.ONDC({
host: "http://localhost:5000",
bapId: "bap.com",
bapUri: "https://bap.com/beckn",
bppId: "bpp.com",
bppUri: "https://bpp.com/beckn",
country: "IND",
city: "std:080",
ttl: "P1M",
publicKey: "<Public Key>",
privateKey: "<Private Key>",
uniqueKey: "dev.test.ondc-node.com",
subscriberId: "<Gateway Address>",
});

let body = {
"item": {
"descriptor": {
"name": "ABC Aata"
}
},
"fulfillment": {
"end": {
"location": {
"gps": "12.4535445,77.9283792"
}
}
}
};
// apiKey is sent in Authorization Header
ondc.apiKey = await ondc.createAuthorizationHeader(body);
const response = await instance.search(body)
```

Next function is ```ondc.verifyHeader``` which is used to verify the ```Authorization``` header signature with senders Public Keys.

You can fetch the public key from Gateway using ```/lookup``` call.

```JS
const publicKey = "<Public key of Sender>";

// verifyHeader return true/false
let output = await ondc.verifyHeader(req.header.Authorization, {
signing_public_key: publicKey
}, req.body);

console.log(`output: ${output}`);
```

### Reference 📚

- [Auth Header - signing and verification](https://docs.google.com/document/d/1-xECuAHxzpfF8FEZw9iN3vT7D3i6yDDB1u2dEApAjPA/edit?usp=sharing)
- [Signing Beckn APIs in HTTP](https://docs.google.com/document/d/1Iw_x-6mtfoMh0KJwL4sqQYM0kD17MLxiMCUOZDBerBo/edit?usp=sharing)


## Functions Covered 🚀

| Async Function | Callback Function | Other Function |
|:----------------:|:-------------------------:|:-------------------------:|
| search | on_search | get_cancellation_reasons |
| select | on_select | cancellation_reasons |
| init | on_init | get_return_reasons |
| confirm | on_confirm | return_reasons |
| status | on_status | get_rating_categories |
| track | on_track | rating_categories |
| cancel | on_cancel | get_feedback_categories |
| update | on_update | feedback_categories |
| rating | on_rating | get_feedback_form |
| support | on_support | feedback_form |



## Content

- [Getting Started with ONDC](https://life-of-utkarsh.medium.com/getting-started-with-ondc-21b67de6353e)

- [Integrate ONDC with just a few lines of code](https://life-of-utkarsh.medium.com/integrate-ondc-with-just-a-few-lines-of-code-6bf25b622294)

- [ONDC Search Trends Analysis with Node Js & Beckn Protocol Server](https://life-of-utkarsh.medium.com/ondc-search-trends-analysis-with-node-js-beckn-protocol-server-54a7085712aa)
73 changes: 19 additions & 54 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,29 @@ const ondc = new ONDC.default.ONDC({
bppUri: "https://bpp.com/beckn",
country: "IND",
city: "std:080",
ttl: "P1M"
ttl: "P1M",
publicKey: "",
privateKey: "",
uniqueKey: "dev.test.ondc-node.com",
subscriberId: "<Gateway Address>",
});

const init = async () => {
console.log(await ondc.init({
"provider": {
"id": "453678",
"locations": [
{
"id": "el"
}
]
},
"items": [
{
"id": "G-0007",
"quantity": {
"count": 1
}
},
{
"id": "G-0007-1",
"quantity": {
"count": 2
}
console.log(await ondc.search({
"item": {
"descriptor": {
"name": "ABC Aata"
}
],
"billing": {
"name": "Nirmal",
"phone": "9876543210",
"address": {
"door": "Landmark",
"building": "Test Building",
"street": "Test Address",
"city": "Bengaluru",
"state": "Karnataka",
"country": "IND",
"area_code": "560078"
},
"email": "[email protected]"
},
"fulfillment": {
"type": "HOME-DELIVERY",
"tracking": true,
"end": {
"location": {
"gps": "12.9063433,77.5856825",
"address": {
"door": "Landmark",
"building": "Test Building",
"street": "Test Address",
"city": "Bengaluru",
"state": "Karnataka",
"country": "IND",
"area_code": "560078"
}
},
"contact": {
"phone": "9876543210",
"email": "[email protected]"
"gps": "12.4535445,77.9283792"
}
}
}
}));
console.log(await ondc.search({
let body = {
"item": {
"descriptor": {
"name": "ABC Aata"
Expand All @@ -85,7 +43,14 @@ const init = async () => {
}
}
}
}));
};
ondc.apiKey = await ondc.createAuthorizationHeader(body)
console.log(`Header: ${ondc.apiKey}`);

let output = await ondc.verifyHeader(ondc.apiKey, {
signing_public_key: ""
}, body)
console.log(`output: ${output}`);
};

init();
Loading

0 comments on commit 7f6df67

Please sign in to comment.