34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
|
# MIT License. See license.txt
|
|
|
|
from __future__ import unicode_literals
|
|
import hmac
|
|
import urllib
|
|
|
|
import frappe
|
|
import frappe.utils
|
|
|
|
def get_url(cmd, params, nonce, secret=None):
|
|
signature = get_signature(params, nonce, secret)
|
|
params['signature'] = signature
|
|
return frappe.utils.get_url("".join(['api/method/', cmd, '?', urllib.urlencode(params)]))
|
|
|
|
def get_signature(params, nonce, secret=None):
|
|
params = "".join((frappe.utils.cstr(p) for p in params.values()))
|
|
if not secret:
|
|
secret = frappe.local.conf.get("secret") or "secret"
|
|
|
|
signature = hmac.new(str(nonce))
|
|
signature.update(secret)
|
|
signature.update(params)
|
|
return signature.hexdigest()
|
|
|
|
def verify_using_bean(bean, signature, cmd):
|
|
controller = bean.get_controller()
|
|
params = controller.get_signature_params()
|
|
return signature == get_signature(params, controller.get_nonce())
|
|
|
|
def get_url_using_bean(bean, cmd):
|
|
controller = bean.get_controller()
|
|
params = controller.get_signature_params()
|
|
return get_url(cmd, params, controller.get_nonce())
|