feat: save token and sek from auth request

This commit is contained in:
Saqib Ansari
2020-09-25 21:59:38 +05:30
committed by Deepesh Garg
parent 6f035d2169
commit 9546e3ea32
3 changed files with 54 additions and 12 deletions

View File

@@ -7,14 +7,19 @@ frappe.ui.form.on('E Invoice Settings', {
}, },
show_fetch_token_btn(frm) { show_fetch_token_btn(frm) {
frm.add_custom_button(__("Fetch Token"), const { token_expiry } = frm.doc;
() => { const now = frappe.datetime.now_datetime();
frm.call({ const expiry_in_mins = moment(token_expiry).diff(now, "minute");
doc: frm.doc, if (expiry_in_mins <= 1) {
method: 'make_authentication_request', frm.add_custom_button(__("Fetch Token"),
freeze: true () => {
}); frm.call({
} doc: frm.doc,
); method: 'make_authentication_request',
freeze: true
});
}
);
}
} }
}); });

View File

@@ -13,7 +13,10 @@
"gstin", "gstin",
"username", "username",
"password", "password",
"auto_refresh_token" "auto_refresh_token",
"auth_token",
"token_expiry",
"sek"
], ],
"fields": [ "fields": [
{ {
@@ -69,12 +72,30 @@
"fieldname": "auto_refresh_token", "fieldname": "auto_refresh_token",
"fieldtype": "Check", "fieldtype": "Check",
"label": "Auto Refresh Token" "label": "Auto Refresh Token"
},
{
"fieldname": "auth_token",
"fieldtype": "Data",
"hidden": 1,
"read_only": 1
},
{
"fieldname": "token_expiry",
"fieldtype": "Datetime",
"hidden": 1,
"read_only": 1
},
{
"fieldname": "sek",
"fieldtype": "Data",
"hidden": 1,
"read_only": 1
} }
], ],
"index_web_pages_for_search": 1, "index_web_pages_for_search": 1,
"issingle": 1, "issingle": 1,
"links": [], "links": [],
"modified": "2020-09-25 21:32:36.553678", "modified": "2020-09-25 21:57:58.187647",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "ERPNext Integrations", "module": "ERPNext Integrations",
"name": "E Invoice Settings", "name": "E Invoice Settings",

View File

@@ -10,6 +10,7 @@ import frappe
from frappe.utils import cstr from frappe.utils import cstr
from Crypto.PublicKey import RSA from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 from Crypto.Cipher import PKCS1_v1_5
from frappe.utils.data import get_datetime
from frappe.model.document import Document from frappe.model.document import Document
from frappe.integrations.utils import make_post_request from frappe.integrations.utils import make_post_request
@@ -35,6 +36,9 @@ class EInvoiceSettings(Document):
enc_msg = cipher.encrypt(msg) enc_msg = cipher.encrypt(msg)
b64_enc_msg = base64.b64encode(enc_msg) b64_enc_msg = base64.b64encode(enc_msg)
return b64_enc_msg.decode() return b64_enc_msg.decode()
def decrypt_sek(self, enc_sek, key):
return enc_sek
def make_authentication_request(self): def make_authentication_request(self):
endpoint = 'https://einv-apisandbox.nic.in/eivital/v1.03/auth' endpoint = 'https://einv-apisandbox.nic.in/eivital/v1.03/auth'
@@ -52,5 +56,17 @@ class EInvoiceSettings(Document):
res = make_post_request(endpoint, headers=headers, data=json.dumps({ 'data': payload })) res = make_post_request(endpoint, headers=headers, data=json.dumps({ 'data': payload }))
print(res) self.extract_token_and_sek(res, appkey)
def extract_token_and_sek(self, response, appkey):
data = response.get('Data')
auth_token = data.get('AuthToken')
token_expiry = data.get('TokenExpiry')
enc_sek = data.get('Sek')
sek = self.decrypt_sek(enc_sek, appkey)
self.auth_token = auth_token
self.token_expiry = get_datetime(token_expiry)
self.sek = sek
self.save()