本文整理汇总了Python中astakosclient.AstakosClient.validate_token方法的典型用法代码示例。如果您正苦于以下问题:Python AstakosClient.validate_token方法的具体用法?Python AstakosClient.validate_token怎么用?Python AstakosClient.validate_token使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astakosclient.AstakosClient
的用法示例。
在下文中一共展示了AstakosClient.validate_token方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wrapper
# 需要导入模块: from astakosclient import AstakosClient [as 别名]
# 或者: from astakosclient.AstakosClient import validate_token [as 别名]
def wrapper(request, *args, **kwargs):
if request.method not in ['GET', 'HEAD']:
return HttpResponseNotAllowed(['GET', 'HEAD'])
try:
access_token = request.GET.get('access_token')
requested_resource = request.path.split(VIEW_PREFIX, 2)[-1]
requested_resource = smart_str(requested_resource,
encoding="utf-8")
astakos = AstakosClient(SERVICE_TOKEN, ASTAKOS_AUTH_URL,
retry=2, use_pool=True,
logger=logger)
if access_token is not None:
# authenticate using the short-term access token
try:
request.user = astakos.validate_token(
access_token, requested_resource)
except AstakosClientException:
return HttpResponseRedirect(request.path)
request.user_uniq = request.user["access"]["user"]["id"]
_func = api_method(token_required=False,
user_required=False)(func)
response = _func(request, *args, **kwargs)
if response.status_code == 404:
raise Http404
elif response.status_code == 403:
raise PermissionDenied
return response
client_id, client_secret = OAUTH2_CLIENT_CREDENTIALS
# TODO: check if client credentials are not set
authorization_code = request.GET.get('code')
redirect_uri = unquote(request.build_absolute_uri(
request.get_full_path()))
if authorization_code is None:
# request authorization code
params = {'response_type': 'code',
'client_id': client_id,
'redirect_uri': redirect_uri,
'state': '', # TODO include state for security
'scope': requested_resource}
return HttpResponseRedirect('%s?%s' %
(join_urls(astakos.oauth2_url,
'auth'),
urlencode(params)))
else:
# request short-term access token
parts = list(urlsplit(redirect_uri))
params = dict(parse_qsl(parts[3], keep_blank_values=True))
if 'code' in params: # always True
del params['code']
if 'state' in params:
del params['state']
parts[3] = urlencode(params)
redirect_uri = urlunsplit(parts)
data = astakos.get_token('authorization_code',
*OAUTH2_CLIENT_CREDENTIALS,
redirect_uri=redirect_uri,
scope=requested_resource,
code=authorization_code)
params['access_token'] = data.get('access_token', '')
parts[3] = urlencode(params)
redirect_uri = urlunsplit(parts)
return HttpResponseRedirect(redirect_uri)
except AstakosClientException as err:
logger.exception(err)
raise PermissionDenied
示例2: wrapper
# 需要导入模块: from astakosclient import AstakosClient [as 别名]
# 或者: from astakosclient.AstakosClient import validate_token [as 别名]
def wrapper(request, *args, **kwargs):
if request.method not in ["GET", "HEAD"]:
return HttpResponseNotAllowed(["GET", "HEAD"])
try:
access_token = request.GET.get("access_token")
requested_resource = text.uenc(request.path.split(VIEW_PREFIX, 2)[-1])
astakos = AstakosClient(SERVICE_TOKEN, ASTAKOS_AUTH_URL, retry=2, use_pool=True, logger=logger)
if access_token is not None:
# authenticate using the short-term access token
try:
request.user = astakos.validate_token(access_token, requested_resource)
except AstakosClientException:
return HttpResponseRedirect(request.path)
request.user_uniq = request.user["access"]["user"]["id"]
_func = api_method(token_required=False, user_required=False)(func)
response = _func(request, *args, **kwargs)
if response.status_code == 404:
raise Http404
elif response.status_code == 403:
raise PermissionDenied
return response
client_id, client_secret = OAUTH2_CLIENT_CREDENTIALS
# TODO: check if client credentials are not set
authorization_code = request.GET.get("code")
redirect_uri = unquote(request.build_absolute_uri(request.get_full_path()))
if authorization_code is None:
# request authorization code
params = {
"response_type": "code",
"client_id": client_id,
"redirect_uri": redirect_uri,
"state": "", # TODO include state for security
"scope": requested_resource,
}
return HttpResponseRedirect("%s?%s" % (join_urls(astakos.oauth2_url, "auth"), urlencode(params)))
else:
# request short-term access token
parts = list(urlsplit(redirect_uri))
params = dict(parse_qsl(parts[3], keep_blank_values=True))
if "code" in params: # always True
del params["code"]
if "state" in params:
del params["state"]
parts[3] = urlencode(params)
redirect_uri = urlunsplit(parts)
data = astakos.get_token(
"authorization_code",
*OAUTH2_CLIENT_CREDENTIALS,
redirect_uri=redirect_uri,
scope=requested_resource,
code=authorization_code
)
params["access_token"] = data.get("access_token", "")
parts[3] = urlencode(params)
redirect_uri = urlunsplit(parts)
return HttpResponseRedirect(redirect_uri)
except AstakosClientException, err:
logger.exception(err)
raise PermissionDenied