MCPcopy Index your code
hub / github.com/python/cpython / get_authorization

Method get_authorization

Lib/urllib/request.py:1118–1178  ·  view source on GitHub ↗
(self, req, chal)

Source from the content-addressed store, hash-verified

1116 return dig[:16]
1117
1118 def get_authorization(self, req, chal):
1119 try:
1120 realm = chal['realm']
1121 nonce = chal['nonce']
1122 qop = chal.get('qop')
1123 algorithm = chal.get('algorithm', 'MD5')
1124 # mod_digest doesn't send an opaque, even though it isn't
1125 # supposed to be optional
1126 opaque = chal.get('opaque', None)
1127 except KeyError:
1128 return None
1129
1130 H, KD = self.get_algorithm_impls(algorithm)
1131 if H is None:
1132 return None
1133
1134 user, pw = self.passwd.find_user_password(realm, req.full_url)
1135 if user is None:
1136 return None
1137
1138 # XXX not implemented yet
1139 if req.data is not None:
1140 entdig = self.get_entity_digest(req.data, chal)
1141 else:
1142 entdig = None
1143
1144 A1 = "%s:%s:%s" % (user, realm, pw)
1145 A2 = "%s:%s" % (req.get_method(),
1146 # XXX selector: what about proxies and full urls
1147 req.selector)
1148 # NOTE: As per RFC 2617, when server sends "auth,auth-int", the client could use either `auth`
1149 # or `auth-int` to the response back. we use `auth` to send the response back.
1150 if qop is None:
1151 respdig = KD(H(A1), "%s:%s" % (nonce, H(A2)))
1152 elif 'auth' in qop.split(','):
1153 if nonce == self.last_nonce:
1154 self.nonce_count += 1
1155 else:
1156 self.nonce_count = 1
1157 self.last_nonce = nonce
1158 ncvalue = '%08x' % self.nonce_count
1159 cnonce = self.get_cnonce(nonce)
1160 noncebit = "%s:%s:%s:%s:%s" % (nonce, ncvalue, cnonce, 'auth', H(A2))
1161 respdig = KD(H(A1), noncebit)
1162 else:
1163 # XXX handle auth-int.
1164 raise URLError("qop '%s' is not supported." % qop)
1165
1166 # XXX should the partial digests be encoded too?
1167
1168 base = 'username="%s", realm="%s", nonce="%s", uri="%s", ' \
1169 'response="%s"' % (user, realm, nonce, req.selector,
1170 respdig)
1171 if opaque:
1172 base += ', opaque="%s"' % opaque
1173 if entdig:
1174 base += ', digest="%s"' % entdig
1175 base += ', algorithm="%s"' % algorithm

Callers 1

Calls 9

get_algorithm_implsMethod · 0.95
get_entity_digestMethod · 0.95
get_cnonceMethod · 0.95
URLErrorClass · 0.90
HClass · 0.50
getMethod · 0.45
find_user_passwordMethod · 0.45
get_methodMethod · 0.45
splitMethod · 0.45

Tested by

no test coverage detected