Skip to content

Commit

Permalink
Add HA1 generation function to HTTPDigestAuth class
Browse files Browse the repository at this point in the history
  • Loading branch information
ps committed Jan 26, 2015
1 parent a490a52 commit 4f4aed3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions flask_httpauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ def __init__(self, use_ha1_pw = False):
def get_nonce(self):
return md5(str(self.random.random()).encode('utf-8')).hexdigest()

def generate_ha1(self, username, password):
a1 = username + ":" + self.realm + ":" + password
a1 = a1.encode('utf-8')
return md5(a1).hexdigest()

def authenticate_header(self):
session["auth_nonce"] = self.get_nonce()
session["auth_opaque"] = self.get_nonce()
Expand Down
6 changes: 5 additions & 1 deletion test_httpauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def setUp(self):

@digest_auth_ha1_pw.get_password
def get_digest_password(username):

if username == 'susan':
return get_ha1(username, 'hello', digest_auth_ha1_pw.realm)
elif username == 'john':
Expand Down Expand Up @@ -371,6 +370,11 @@ def test_digest_auth_login_invalid2(self):
r'nonce="[0-9a-f]+",opaque="[0-9a-f]+"$',
response.headers['WWW-Authenticate']))

def test_digest_generate_ha1(self):
ha1 = self.digest_auth.generate_ha1('pawel', 'test')
ha1_expected = get_ha1('pawel', 'test', self.digest_auth.realm)
self.assertEqual(ha1, ha1_expected)


def suite():
return unittest.makeSuite(HTTPAuthTestCase)
Expand Down

0 comments on commit 4f4aed3

Please sign in to comment.