Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
django-sshkey
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b46a13cb
authored
Jun 13, 2013
by
Scott Duckworth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use UserKey.clean() instead of pre_save signal to set and verify fingerprint
parent
04e475dd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
7 additions
and
6 deletions
+7
-6
sshkey/models.py
+6
-5
sshkey/util.py
+1
-1
No files found.
sshkey/models.py
View file @
b46a13cb
from
django.db
import
models
from
django.dispatch
import
receiver
from
django.db.models.signals
import
pre_save
from
django.contrib.auth.models
import
User
from
django.core.exceptions
import
ValidationError
from
sshkey.util
import
sshkey_fingerprint
class
UserKey
(
models
.
Model
):
...
...
@@ -18,6 +17,8 @@ class UserKey(models.Model):
def
__unicode__
(
self
):
return
unicode
(
self
.
user
)
+
u': '
+
self
.
name
@receiver
(
pre_save
,
sender
=
UserKey
,
dispatch_uid
=
__name__
+
'.set_fingerprint'
)
def
set_fingerprint
(
sender
,
instance
,
**
kwargs
):
instance
.
fingerprint
=
sshkey_fingerprint
(
instance
.
key
)
def
clean
(
self
):
try
:
self
.
fingerprint
=
sshkey_fingerprint
(
self
.
key
)
except
Exception
,
e
:
raise
ValidationError
(
'Not a valid SSH key: '
+
str
(
e
))
sshkey/util.py
View file @
b46a13cb
...
...
@@ -7,7 +7,7 @@ sshkey_re = re.compile(r'\s*(?:(?P<options>.*?)\s+)?(?P<type>ssh-\w+)\s+(?P<key>
def
sshkey_fingerprint
(
key_line
):
match
=
sshkey_re
.
match
(
key_line
)
if
not
match
:
r
eturn
None
r
aise
Exception
(
'Key is not in OpenSSH authorized_keys format'
)
key
=
base64
.
b64decode
(
match
.
group
(
'key'
))
fp_plain
=
hashlib
.
md5
(
key
)
.
hexdigest
()
return
':'
.
join
(
a
+
b
for
a
,
b
in
zip
(
fp_plain
[::
2
],
fp_plain
[
1
::
2
]))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment