SaaSERP3/addons/ospp_base/tools/cloud_auth.py

55 lines
1.6 KiB
Python

import functools
import requests
from odoo import http
from odoo.tools import config
RESPONSE = {
"success": 1,
"message": "OK",
}
def verified(permission):
oauth_url = config["oauth_url"]
oauth_realm = config["oauth_realm"]
url = f"{oauth_url}/realms/{oauth_realm}/protocol/openid-connect/token"
grant_type = "urn:ietf:params:oauth:grant-type:uma-ticket"
def decorator(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
authorization = http.request.httprequest.headers.get("Authorization")
if not authorization:
response = RESPONSE.copy()
response.update(
{
"success": 0,
"message": "No Authorization Bearer Provided!"
}
)
return response
payload = {
"grant_type": grant_type,
"audience": oauth_realm,
"permission": permission
}
files = []
headers = {
'Authorization': authorization
}
res = requests.request("POST", url, headers=headers, data=payload, files=files)
if not (res.status_code == 200 and 'access_token' in res.json()):
response = RESPONSE.copy()
response.update(
{
"success": 0,
"message": res.text
}
)
return response
return func(self, *args, **kwargs)
return wrapper
return decorator