mirror of
https://github.com/frappe/erpnext.git
synced 2026-03-25 14:11:45 +01:00
feat: rsa encryption with public key
This commit is contained in:
@@ -2,7 +2,19 @@
|
||||
// For license information, please see license.txt
|
||||
|
||||
frappe.ui.form.on('E Invoice Settings', {
|
||||
// refresh: function(frm) {
|
||||
refresh: function(frm) {
|
||||
frm.trigger("show_fetch_token_btn");
|
||||
},
|
||||
|
||||
// }
|
||||
show_fetch_token_btn(frm) {
|
||||
frm.add_custom_button(__("Fetch Token"),
|
||||
() => {
|
||||
frm.call({
|
||||
doc: frm.doc,
|
||||
method: 'make_authentication_request',
|
||||
freeze: true
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -12,7 +12,8 @@
|
||||
"column_break_3",
|
||||
"gstin",
|
||||
"username",
|
||||
"password"
|
||||
"password",
|
||||
"auto_refresh_token"
|
||||
],
|
||||
"fields": [
|
||||
{
|
||||
@@ -61,12 +62,19 @@
|
||||
"fieldtype": "Long Text",
|
||||
"hidden": 1,
|
||||
"read_only": 1
|
||||
},
|
||||
{
|
||||
"default": "0",
|
||||
"description": "Token will be automatically refreshed 10 mins before expiry",
|
||||
"fieldname": "auto_refresh_token",
|
||||
"fieldtype": "Check",
|
||||
"label": "Auto Refresh Token"
|
||||
}
|
||||
],
|
||||
"index_web_pages_for_search": 1,
|
||||
"issingle": 1,
|
||||
"links": [],
|
||||
"modified": "2020-09-25 21:24:00.404460",
|
||||
"modified": "2020-09-25 21:32:36.553678",
|
||||
"modified_by": "Administrator",
|
||||
"module": "ERPNext Integrations",
|
||||
"name": "E Invoice Settings",
|
||||
|
||||
@@ -3,9 +3,15 @@
|
||||
# For license information, please see license.txt
|
||||
|
||||
from __future__ import unicode_literals
|
||||
import os
|
||||
import json
|
||||
import base64
|
||||
import frappe
|
||||
from frappe.utils import cstr
|
||||
from Crypto.PublicKey import RSA
|
||||
from Crypto.Cipher import PKCS1_v1_5
|
||||
from frappe.model.document import Document
|
||||
from frappe.integrations.utils import make_post_request
|
||||
|
||||
class EInvoiceSettings(Document):
|
||||
def validate(self):
|
||||
@@ -19,3 +25,32 @@ class EInvoiceSettings(Document):
|
||||
key_file = frappe.get_doc('File', dict(attached_to_name=self.doctype, attached_to_field='public_key_file'))
|
||||
with open(key_file.get_full_path(), 'rb') as f:
|
||||
return cstr(f.read())
|
||||
|
||||
def rsa_encrypt(self, msg, key):
|
||||
if not (isinstance(msg, bytes) or isinstance(msg, bytearray)):
|
||||
msg = str.encode(msg)
|
||||
|
||||
rsa_pub_key = RSA.import_key(key)
|
||||
cipher = PKCS1_v1_5.new(rsa_pub_key)
|
||||
enc_msg = cipher.encrypt(msg)
|
||||
b64_enc_msg = base64.b64encode(enc_msg)
|
||||
return b64_enc_msg.decode()
|
||||
|
||||
def make_authentication_request(self):
|
||||
endpoint = 'https://einv-apisandbox.nic.in/eivital/v1.03/auth'
|
||||
headers = { 'content-type': 'application/json' }
|
||||
headers.update(dict(client_id=self.client_id, client_secret=self.client_secret))
|
||||
payload = dict(UserName=self.username, ForceRefreshAccessToken=bool(self.auto_refresh_token))
|
||||
|
||||
appkey = bytearray(os.urandom(32))
|
||||
enc_appkey = self.rsa_encrypt(appkey, self.public_key)
|
||||
|
||||
password = self.get_password(fieldname='password')
|
||||
enc_password = self.rsa_encrypt(password, self.public_key)
|
||||
|
||||
payload.update(dict(Password=enc_password, AppKey=enc_appkey))
|
||||
|
||||
res = make_post_request(endpoint, headers=headers, data=json.dumps({ 'data': payload }))
|
||||
|
||||
print(res)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user