本文整理汇总了Python中werkzeug.http.parse_authorization_header函数的典型用法代码示例。如果您正苦于以下问题:Python parse_authorization_header函数的具体用法?Python parse_authorization_header怎么用?Python parse_authorization_header使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_authorization_header函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_authorization_header
def test_authorization_header(self):
a = http.parse_authorization_header("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
assert a.type == "basic"
assert a.username == u"Aladdin"
assert a.password == u"open sesame"
a = http.parse_authorization_header(
"Basic 0YDRg9GB0YHQutC40IE60JHRg9C60LLRiw=="
)
assert a.type == "basic"
assert a.username == u"русскиЁ"
assert a.password == u"Буквы"
a = http.parse_authorization_header("Basic 5pmu6YCa6K+dOuS4reaWhw==")
assert a.type == "basic"
assert a.username == u"普通话"
assert a.password == u"中文"
a = http.parse_authorization_header(
'''Digest username="Mufasa",
realm="[email protected]",
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
uri="/dir/index.html",
qop=auth,
nc=00000001,
cnonce="0a4f113b",
response="6629fae49393a05397450978507c4ef1",
opaque="5ccc069c403ebaf9f0171e9517f40e41"'''
)
assert a.type == "digest"
assert a.username == "Mufasa"
assert a.realm == "[email protected]"
assert a.nonce == "dcd98b7102dd2f0e8b11d0f600bfb0c093"
assert a.uri == "/dir/index.html"
assert a.qop == "auth"
assert a.nc == "00000001"
assert a.cnonce == "0a4f113b"
assert a.response == "6629fae49393a05397450978507c4ef1"
assert a.opaque == "5ccc069c403ebaf9f0171e9517f40e41"
a = http.parse_authorization_header(
'''Digest username="Mufasa",
realm="[email protected]",
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
uri="/dir/index.html",
response="e257afa1414a3340d93d30955171dd0e",
opaque="5ccc069c403ebaf9f0171e9517f40e41"'''
)
assert a.type == "digest"
assert a.username == "Mufasa"
assert a.realm == "[email protected]"
assert a.nonce == "dcd98b7102dd2f0e8b11d0f600bfb0c093"
assert a.uri == "/dir/index.html"
assert a.response == "e257afa1414a3340d93d30955171dd0e"
assert a.opaque == "5ccc069c403ebaf9f0171e9517f40e41"
assert http.parse_authorization_header("") is None
assert http.parse_authorization_header(None) is None
assert http.parse_authorization_header("foo") is None
示例2: actual_token
def actual_token(cls) -> str:
"""Gets the **unhashed** token. Use `hash_token` to hash it."""
x = request.headers.environ['HTTP_AUTHORIZATION']
header = parse_authorization_header(x)
if header is None:
raise StandardError('The Authorization header is not well written: ' + x, 400)
return header['username']
示例3: digest_auth
def digest_auth(qop=None, user='user', passwd='passwd', algorithm='MD5', stale_after='never'):
"""Prompts the user for authorization using HTTP Digest auth"""
require_cookie_handling = (request.args.get('require-cookie', '').lower() in
('1', 't', 'true'))
if algorithm not in ('MD5', 'SHA-256', 'SHA-512'):
algorithm = 'MD5'
if qop not in ('auth', 'auth-int'):
qop = None
authorization = request.headers.get('Authorization')
credentials = None
if authorization:
credentials = parse_authorization_header(authorization)
if (not authorization or
not credentials or
(require_cookie_handling and 'Cookie' not in request.headers)):
response = digest_challenge_response(app, qop, algorithm)
response.set_cookie('stale_after', value=stale_after)
response.set_cookie('fake', value='fake_value')
return response
if (require_cookie_handling and
request.cookies.get('fake') != 'fake_value'):
response = jsonify({'errors': ['missing cookie set on challenge']})
response.set_cookie('fake', value='fake_value')
response.status_code = 403
return response
current_nonce = credentials.get('nonce')
stale_after_value = None
if 'stale_after' in request.cookies:
stale_after_value = request.cookies.get('stale_after')
if ('last_nonce' in request.cookies and
current_nonce == request.cookies.get('last_nonce') or
stale_after_value == '0'):
response = digest_challenge_response(app, qop, algorithm, True)
response.set_cookie('stale_after', value=stale_after)
response.set_cookie('last_nonce', value=current_nonce)
response.set_cookie('fake', value='fake_value')
return response
if not check_digest_auth(user, passwd):
response = digest_challenge_response(app, qop, algorithm, False)
response.set_cookie('stale_after', value=stale_after)
response.set_cookie('last_nonce', value=current_nonce)
response.set_cookie('fake', value='fake_value')
return response
response = jsonify(authenticated=True, user=user)
response.set_cookie('fake', value='fake_value')
if stale_after_value :
response.set_cookie('stale_after', value=next_stale_after_value(stale_after_value))
return response
示例4: check_digest_auth
def check_digest_auth(user, passwd):
"""Check user authentication using HTTP Digest auth"""
if request.headers.get("Authorization"):
credentails = parse_authorization_header(request.headers.get("Authorization"))
if not credentails:
return
response_hash = response(credentails, passwd, dict(uri=request.path, body=request.data, method=request.method))
if credentails["response"] == response_hash:
return True
return False
示例5: check_digest_auth
def check_digest_auth(user, passwd):
"""Check user authentication using HTTP Digest auth"""
if request.headers.get('Authorization'):
credentials = parse_authorization_header(request.headers.get('Authorization'))
if not credentials:
return
request_uri = request.script_root + request.path
if request.query_string:
request_uri += '?' + request.query_string
response_hash = response(credentials, passwd, dict(uri=request_uri,
body=request.data,
method=request.method))
if credentials.get('response') == response_hash:
return True
return False
示例6: basic_auth_mock
def basic_auth_mock(self, request, **values):
example = {
"name": "Wow! You got in a restricted area with security level 1 (Basic auth)",
"status": 200,
"requested_at": str(datetime.datetime.now()),
"body": json.dumps(request.form)
}
if 'Authorization' in request.headers:
auth = parse_authorization_header(request.headers['Authorization'])
try:
if auth['username'] == 'dev' and auth['password'] == 'example':
return Response(json.dumps(example))
except KeyError:
pass
raise Unauthorized
示例7: digest_auth
def digest_auth(qop=None, user='user', passwd='passwd', algorithm='MD5', stale_after='never'):
"""Prompts the user for authorization using HTTP Digest auth"""
if algorithm not in ('MD5', 'SHA-256'):
algorithm = 'MD5'
if qop not in ('auth', 'auth-int'):
qop = None
if 'Authorization' not in request.headers or \
'Cookie' not in request.headers:
response = digest_challenge_response(app, qop, algorithm)
response.set_cookie('stale_after', value=stale_after)
return response
credentails = parse_authorization_header(request.headers.get('Authorization'))
if not credentails :
response = digest_challenge_response(app, qop, algorithm)
response.set_cookie('stale_after', value=stale_after)
return response
current_nonce = credentails.get('nonce')
stale_after_value = None
if 'stale_after' in request.cookies :
stale_after_value = request.cookies.get('stale_after')
if 'last_nonce' in request.cookies and current_nonce == request.cookies.get('last_nonce') or \
stale_after_value == '0' :
response = digest_challenge_response(app, qop, algorithm, True)
response.set_cookie('stale_after', value=stale_after)
response.set_cookie('last_nonce', value=current_nonce)
return response
if not check_digest_auth(user, passwd) :
response = digest_challenge_response(app, qop, algorithm, False)
response.set_cookie('stale_after', value=stale_after)
response.set_cookie('last_nonce', value=current_nonce)
return response
response = jsonify(authenticated=True, user=user)
if stale_after_value :
response.set_cookie('stale_after', value=next_stale_after_value(stale_after_value))
return response
示例8: test_authorization_header
def test_authorization_header(self):
a = http.parse_authorization_header('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==')
assert a.type == 'basic'
assert a.username == 'Aladdin'
assert a.password == 'open sesame'
a = http.parse_authorization_header('''Digest username="Mufasa",
realm="[email protected]",
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
uri="/dir/index.html",
qop=auth,
nc=00000001,
cnonce="0a4f113b",
response="6629fae49393a05397450978507c4ef1",
opaque="5ccc069c403ebaf9f0171e9517f40e41"''')
assert a.type == 'digest'
assert a.username == 'Mufasa'
assert a.realm == '[email protected]'
assert a.nonce == 'dcd98b7102dd2f0e8b11d0f600bfb0c093'
assert a.uri == '/dir/index.html'
assert 'auth' in a.qop
assert a.nc == '00000001'
assert a.cnonce == '0a4f113b'
assert a.response == '6629fae49393a05397450978507c4ef1'
assert a.opaque == '5ccc069c403ebaf9f0171e9517f40e41'
a = http.parse_authorization_header('''Digest username="Mufasa",
realm="[email protected]",
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
uri="/dir/index.html",
response="e257afa1414a3340d93d30955171dd0e",
opaque="5ccc069c403ebaf9f0171e9517f40e41"''')
assert a.type == 'digest'
assert a.username == 'Mufasa'
assert a.realm == '[email protected]'
assert a.nonce == 'dcd98b7102dd2f0e8b11d0f600bfb0c093'
assert a.uri == '/dir/index.html'
assert a.response == 'e257afa1414a3340d93d30955171dd0e'
assert a.opaque == '5ccc069c403ebaf9f0171e9517f40e41'
assert http.parse_authorization_header('') is None
assert http.parse_authorization_header(None) is None
assert http.parse_authorization_header('foo') is None
示例9: basicAuthSession
def basicAuthSession(request):
result = parse_authorization_header(request.headers.get('authorization'))
return result.password
示例10: authorization
def authorization(self):
"""The `Authorization` object in parsed form."""
header = self.environ.get('HTTP_AUTHORIZATION')
return parse_authorization_header(header)
示例11: digest_auth
def digest_auth(qop=None, user='user', passwd='passwd', algorithm='MD5', stale_after='never'):
""""Prompts the user for authorization using Digest Auth + Algorithm.
allow settings the stale_after argument.
---
tags:
- Auth
parameters:
- in: path
name: qop
type: string
description: auth or auth-int
- in: path
name: user
type: string
- in: path
name: passwd
type: string
- in: path
name: algorithm
type: string
description: MD5, SHA-256, SHA-512
default: MD5
- in: path
name: stale_after
type: string
default: never
produces:
- application/json
responses:
200:
description: Sucessful authentication.
401:
description: Unsuccessful authentication.
"""
require_cookie_handling = (request.args.get('require-cookie', '').lower() in
('1', 't', 'true'))
if algorithm not in ('MD5', 'SHA-256', 'SHA-512'):
algorithm = 'MD5'
if qop not in ('auth', 'auth-int'):
qop = None
authorization = request.headers.get('Authorization')
credentials = None
if authorization:
credentials = parse_authorization_header(authorization)
if (not authorization or
not credentials or credentials.type.lower() != 'digest' or
(require_cookie_handling and 'Cookie' not in request.headers)):
response = digest_challenge_response(app, qop, algorithm)
response.set_cookie('stale_after', value=stale_after)
response.set_cookie('fake', value='fake_value')
return response
if (require_cookie_handling and
request.cookies.get('fake') != 'fake_value'):
response = jsonify({'errors': ['missing cookie set on challenge']})
response.set_cookie('fake', value='fake_value')
response.status_code = 403
return response
current_nonce = credentials.get('nonce')
stale_after_value = None
if 'stale_after' in request.cookies:
stale_after_value = request.cookies.get('stale_after')
if ('last_nonce' in request.cookies and
current_nonce == request.cookies.get('last_nonce') or
stale_after_value == '0'):
response = digest_challenge_response(app, qop, algorithm, True)
response.set_cookie('stale_after', value=stale_after)
response.set_cookie('last_nonce', value=current_nonce)
response.set_cookie('fake', value='fake_value')
return response
if not check_digest_auth(user, passwd):
response = digest_challenge_response(app, qop, algorithm, False)
response.set_cookie('stale_after', value=stale_after)
response.set_cookie('last_nonce', value=current_nonce)
response.set_cookie('fake', value='fake_value')
return response
response = jsonify(authenticated=True, user=user)
response.set_cookie('fake', value='fake_value')
if stale_after_value :
response.set_cookie('stale_after', value=next_stale_after_value(stale_after_value))
return response
示例12: authorization
def authorization(self):
header = self.environ.get('HTTP_AUTHORIZATION')
return parse_authorization_header(header)
示例13: digest_auth
def digest_auth(
qop=None, user="user", passwd="passwd", algorithm="MD5", stale_after="never"
):
"""Prompts the user for authorization using Digest Auth + Algorithm.
allow settings the stale_after argument.
---
tags:
- Auth
parameters:
- in: path
name: qop
type: string
description: auth or auth-int
- in: path
name: user
type: string
- in: path
name: passwd
type: string
- in: path
name: algorithm
type: string
description: MD5, SHA-256, SHA-512
default: MD5
- in: path
name: stale_after
type: string
default: never
produces:
- application/json
responses:
200:
description: Sucessful authentication.
401:
description: Unsuccessful authentication.
"""
require_cookie_handling = request.args.get("require-cookie", "").lower() in (
"1",
"t",
"true",
)
if algorithm not in ("MD5", "SHA-256", "SHA-512"):
algorithm = "MD5"
if qop not in ("auth", "auth-int"):
qop = None
authorization = request.headers.get("Authorization")
credentials = None
if authorization:
credentials = parse_authorization_header(authorization)
if (
not authorization
or not credentials
or credentials.type.lower() != "digest"
or (require_cookie_handling and "Cookie" not in request.headers)
):
response = digest_challenge_response(app, qop, algorithm)
response.set_cookie("stale_after", value=stale_after)
response.set_cookie("fake", value="fake_value")
return response
if require_cookie_handling and request.cookies.get("fake") != "fake_value":
response = jsonify({"errors": ["missing cookie set on challenge"]})
response.set_cookie("fake", value="fake_value")
response.status_code = 403
return response
current_nonce = credentials.get("nonce")
stale_after_value = None
if "stale_after" in request.cookies:
stale_after_value = request.cookies.get("stale_after")
if (
"last_nonce" in request.cookies
and current_nonce == request.cookies.get("last_nonce")
or stale_after_value == "0"
):
response = digest_challenge_response(app, qop, algorithm, True)
response.set_cookie("stale_after", value=stale_after)
response.set_cookie("last_nonce", value=current_nonce)
response.set_cookie("fake", value="fake_value")
return response
if not check_digest_auth(user, passwd):
response = digest_challenge_response(app, qop, algorithm, False)
response.set_cookie("stale_after", value=stale_after)
response.set_cookie("last_nonce", value=current_nonce)
response.set_cookie("fake", value="fake_value")
return response
response = jsonify(authenticated=True, user=user)
response.set_cookie("fake", value="fake_value")
if stale_after_value:
response.set_cookie(
"stale_after", value=next_stale_after_value(stale_after_value)
)
#.........这里部分代码省略.........