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
a1d8d854
authored
Oct 08, 2014
by
Paul Kilgo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
minimum work necessary to make unit tests pass
parent
eb0d50b5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
23 deletions
+55
-23
django_sshkey/forms.py
+4
-2
django_sshkey/models.py
+50
-20
django_sshkey/views.py
+1
-1
No files found.
django_sshkey/forms.py
View file @
a1d8d854
...
@@ -27,11 +27,13 @@
...
@@ -27,11 +27,13 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from
django
import
forms
from
django
import
forms
from
django_sshkey.models
import
User
Key
from
django_sshkey.models
import
Key
class
UserKeyForm
(
forms
.
ModelForm
):
class
UserKeyForm
(
forms
.
ModelForm
):
name
=
forms
.
CharField
(
max_length
=
50
,
required
=
False
)
class
Meta
:
class
Meta
:
model
=
User
Key
model
=
Key
fields
=
[
'name'
,
'key'
]
fields
=
[
'name'
,
'key'
]
widgets
=
{
widgets
=
{
'name'
:
forms
.
TextInput
(
attrs
=
{
'name'
:
forms
.
TextInput
(
attrs
=
{
...
...
django_sshkey/models.py
View file @
a1d8d854
...
@@ -39,9 +39,7 @@ except ImportError:
...
@@ -39,9 +39,7 @@ except ImportError:
from
django_sshkey.util
import
PublicKeyParseError
,
pubkey_parse
from
django_sshkey.util
import
PublicKeyParseError
,
pubkey_parse
from
django_sshkey
import
settings
from
django_sshkey
import
settings
class
UserKey
(
models
.
Model
):
class
Key
(
models
.
Model
):
user
=
models
.
ForeignKey
(
User
,
db_index
=
True
)
name
=
models
.
CharField
(
max_length
=
50
,
blank
=
True
)
key
=
models
.
TextField
(
max_length
=
2000
)
key
=
models
.
TextField
(
max_length
=
2000
)
fingerprint
=
models
.
CharField
(
max_length
=
47
,
blank
=
True
,
db_index
=
True
)
fingerprint
=
models
.
CharField
(
max_length
=
47
,
blank
=
True
,
db_index
=
True
)
created
=
models
.
DateTimeField
(
auto_now_add
=
True
,
null
=
True
)
created
=
models
.
DateTimeField
(
auto_now_add
=
True
,
null
=
True
)
...
@@ -49,10 +47,7 @@ class UserKey(models.Model):
...
@@ -49,10 +47,7 @@ class UserKey(models.Model):
last_used
=
models
.
DateTimeField
(
null
=
True
)
last_used
=
models
.
DateTimeField
(
null
=
True
)
class
Meta
:
class
Meta
:
db_table
=
'sshkey_userkey'
db_table
=
'sshkey_key'
unique_together
=
[
(
'user'
,
'name'
),
]
def
__unicode__
(
self
):
def
__unicode__
(
self
):
return
unicode
(
self
.
user
)
+
u': '
+
self
.
name
return
unicode
(
self
.
user
)
+
u': '
+
self
.
name
...
@@ -73,27 +68,16 @@ class UserKey(models.Model):
...
@@ -73,27 +68,16 @@ class UserKey(models.Model):
raise
ValidationError
(
str
(
e
))
raise
ValidationError
(
str
(
e
))
self
.
key
=
pubkey
.
format_openssh
()
self
.
key
=
pubkey
.
format_openssh
()
self
.
fingerprint
=
pubkey
.
fingerprint
()
self
.
fingerprint
=
pubkey
.
fingerprint
()
if
not
self
.
name
:
if
not
pubkey
.
comment
:
raise
ValidationError
(
'Name or key comment required'
)
self
.
name
=
pubkey
.
comment
def
validate_unique
(
self
,
exclude
=
None
):
def
validate_unique
(
self
,
exclude
=
None
):
if
self
.
pk
is
None
:
if
self
.
pk
is
None
:
objects
=
type
(
self
)
.
objects
objects
=
type
(
self
)
.
objects
else
:
else
:
objects
=
type
(
self
)
.
objects
.
exclude
(
pk
=
self
.
pk
)
objects
=
type
(
self
)
.
objects
.
exclude
(
pk
=
self
.
pk
)
if
exclude
is
None
or
'name'
not
in
exclude
:
if
objects
.
filter
(
user
=
self
.
user
,
name
=
self
.
name
)
.
count
():
message
=
'You already have a key with that name'
raise
ValidationError
({
'name'
:
[
message
]})
if
exclude
is
None
or
'key'
not
in
exclude
:
if
exclude
is
None
or
'key'
not
in
exclude
:
try
:
try
:
other
=
objects
.
get
(
fingerprint
=
self
.
fingerprint
,
key
=
self
.
key
)
other
=
objects
.
get
(
fingerprint
=
self
.
fingerprint
,
key
=
self
.
key
)
if
self
.
user
==
other
.
user
:
message
=
'That key is already on file.'
message
=
'You already have that key on file (
%
s)'
%
other
.
name
else
:
message
=
'Somebody else already has that key on file'
raise
ValidationError
({
'key'
:
[
message
]})
raise
ValidationError
({
'key'
:
[
message
]})
except
type
(
self
)
.
DoesNotExist
:
except
type
(
self
)
.
DoesNotExist
:
pass
pass
...
@@ -110,12 +94,58 @@ class UserKey(models.Model):
...
@@ -110,12 +94,58 @@ class UserKey(models.Model):
def
save
(
self
,
*
args
,
**
kwargs
):
def
save
(
self
,
*
args
,
**
kwargs
):
if
kwargs
.
pop
(
'update_last_modified'
,
True
):
if
kwargs
.
pop
(
'update_last_modified'
,
True
):
self
.
last_modified
=
now
()
self
.
last_modified
=
now
()
super
(
User
Key
,
self
)
.
save
(
*
args
,
**
kwargs
)
super
(
Key
,
self
)
.
save
(
*
args
,
**
kwargs
)
def
touch
(
self
):
def
touch
(
self
):
self
.
last_used
=
now
()
self
.
last_used
=
now
()
self
.
save
(
update_last_modified
=
False
)
self
.
save
(
update_last_modified
=
False
)
class
ApplicationKey
(
models
.
Model
):
basekey
=
models
.
ForeignKey
(
Key
)
class
Meta
:
abstract
=
True
@property
def
key
(
self
):
return
self
.
basekey
.
key
class
NamedKey
(
ApplicationKey
):
name
=
models
.
CharField
(
max_length
=
50
,
blank
=
True
)
class
Meta
:
abstract
=
True
def
clean
(
self
):
if
not
self
.
name
:
pubkey
=
pubkey_parse
(
self
.
key
)
if
not
pubkey
.
comment
:
raise
ValidationError
(
'Name or key comment required'
)
self
.
name
=
pubkey
.
comment
class
UserKey
(
NamedKey
):
user
=
models
.
ForeignKey
(
User
,
db_index
=
True
)
class
Meta
:
db_table
=
'sshkey_userkey'
unique_together
=
[
(
'user'
,
'name'
),
]
def
validate_unique
(
self
,
exclude
=
None
):
cls
=
type
(
self
)
if
not
self
.
pk
is
None
:
qs
=
cls
.
objects
.
exclude
(
pk
=
pk
)
else
:
qs
=
cls
.
objects
.
all
()
if
exclude
is
None
or
'name'
not
in
exclude
:
if
qs
.
filter
(
user
=
self
.
user
,
name
=
self
.
name
)
.
count
():
message
=
'You already have a key with that name'
raise
ValidationError
({
'name'
:
[
message
]})
if
exclude
is
None
or
'basekey'
not
in
exclude
:
if
qs
.
filter
(
basekey
=
self
.
basekey
):
message
=
'Cannot associate key with two users.'
raise
ValidationError
({
'basekey'
:
[
message
]})
@receiver
(
pre_save
,
sender
=
UserKey
)
@receiver
(
pre_save
,
sender
=
UserKey
)
def
send_email_add_key
(
sender
,
instance
,
**
kwargs
):
def
send_email_add_key
(
sender
,
instance
,
**
kwargs
):
if
not
settings
.
SSHKEY_EMAIL_ADD_KEY
or
instance
.
pk
:
if
not
settings
.
SSHKEY_EMAIL_ADD_KEY
or
instance
.
pk
:
...
...
django_sshkey/views.py
View file @
a1d8d854
...
@@ -50,7 +50,7 @@ def lookup(request):
...
@@ -50,7 +50,7 @@ def lookup(request):
return
HttpResponse
(
str
(
key
.
last_used
),
content_type
=
'text/plain'
)
return
HttpResponse
(
str
(
key
.
last_used
),
content_type
=
'text/plain'
)
try
:
try
:
fingerprint
=
request
.
GET
[
'fingerprint'
]
fingerprint
=
request
.
GET
[
'fingerprint'
]
keys
=
UserKey
.
objects
.
filter
(
fingerprint
=
fingerprint
)
keys
=
UserKey
.
objects
.
filter
(
basekey__
fingerprint
=
fingerprint
)
except
KeyError
:
except
KeyError
:
try
:
try
:
username
=
request
.
GET
[
'username'
]
username
=
request
.
GET
[
'username'
]
...
...
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