feat: decrypt json data with SEK

This commit is contained in:
Saqib Ansari
2020-09-25 23:29:45 +05:30
parent b6509ead64
commit 7c427aa890
2 changed files with 35 additions and 4 deletions

View File

@@ -4,6 +4,18 @@
frappe.ui.form.on('E Invoice Settings', {
refresh: function(frm) {
frm.trigger("show_fetch_token_btn");
frm.add_custom_button(__("Get GSTIN Details"),
() => {
frm.call({
doc: frm.doc,
method: 'get_gstin_details',
args: {
'gstin': '27AAACW8099E1ZX'
},
freeze: true,
callback: (res) => console.log(res)
});
});
},
show_fetch_token_btn(frm) {
@@ -16,7 +28,8 @@ frappe.ui.form.on('E Invoice Settings', {
frm.call({
doc: frm.doc,
method: 'make_authentication_request',
freeze: true
freeze: true,
callback: () => frm.refresh()
});
}
);

View File

@@ -14,7 +14,7 @@ from Crypto.Cipher import PKCS1_v1_5, AES
from Crypto.Util.Padding import pad, unpad
from frappe.utils.data import get_datetime
from frappe.model.document import Document
from frappe.integrations.utils import make_post_request
from frappe.integrations.utils import make_post_request, make_get_request
class EInvoiceSettings(Document):
def validate(self):
@@ -40,15 +40,18 @@ class EInvoiceSettings(Document):
return b64_enc_msg.decode()
def aes_decrypt(self, enc_msg, key):
encode_as_b64 = True
if not (isinstance(key, bytes) or isinstance(key, bytearray)):
key = base64.b64decode(key)
encode_as_b64 = False
cipher = AES.new(key, AES.MODE_ECB)
b64_enc_msg = base64.b64decode(enc_msg)
msg_bytes = cipher.decrypt(b64_enc_msg)
msg_bytes = unpad(msg_bytes, AES.block_size) # due to ECB/PKCS5Padding
b64_msg_bytes = base64.b64encode(msg_bytes)
return b64_msg_bytes.decode()
if encode_as_b64:
msg_bytes = base64.b64encode(msg_bytes)
return msg_bytes.decode()
def make_authentication_request(self):
endpoint = 'https://einv-apisandbox.nic.in/eivital/v1.03/auth'
@@ -80,6 +83,21 @@ class EInvoiceSettings(Document):
self.token_expiry = get_datetime(token_expiry)
self.sek = sek
self.save()
def get_gstin_details(self, gstin):
endpoint = 'https://einv-apisandbox.nic.in/eivital/v1.03/Master/gstin/{gstin}'.format(gstin=gstin)
headers = { 'content-type': 'application/json' }
headers.update(dict(client_id=self.client_id, client_secret=self.client_secret, user_name=self.username))
headers.update(dict(Gstin=self.gstin, AuthToken=self.auth_token))
res = make_get_request(endpoint, headers=headers)
self.handle_err_response(res)
enc_json = res.get('Data')
json_str = self.aes_decrypt(enc_json, self.sek)
data = json.loads(json_str)
return data
def handle_err_response(self, response):
if response.get('Status') == 0: