Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
48e87857
Commit
48e87857
authored
Apr 18, 2018
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 修改users otp secret key
parent
b90d3306
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
3 deletions
+37
-3
fields.py
apps/common/fields.py
+28
-2
user.py
apps/users/models/user.py
+9
-1
No files found.
apps/common/fields.py
View file @
48e87857
...
...
@@ -2,11 +2,15 @@
#
import
json
from
django.db
import
models
from
django
import
forms
from
django.utils
import
six
from
django.core.exceptions
import
ValidationError
from
django.utils.translation
import
ugettext
as
_
from
rest_framework
import
serializers
from
.utils
import
get_signer
signer
=
get_signer
()
class
DictField
(
forms
.
Field
):
...
...
@@ -46,4 +50,27 @@ class StringIDField(serializers.Field):
class
StringManyToManyField
(
serializers
.
RelatedField
):
def
to_representation
(
self
,
value
):
return
value
.
__str__
()
\ No newline at end of file
return
value
.
__str__
()
class
EncryptMixin
:
def
from_db_value
(
self
,
value
,
expression
,
connection
,
context
):
if
value
is
not
None
:
return
signer
.
unsign
(
value
)
return
super
()
.
from_db_value
(
self
,
value
,
expression
,
connection
,
context
)
def
get_prep_value
(
self
,
value
):
if
value
is
None
:
return
value
return
signer
.
sign
(
value
)
.
decode
(
'utf-8'
)
class
EncryptTextField
(
EncryptMixin
,
models
.
TextField
):
description
=
_
(
"Encrypt field using Secret Key"
)
class
EncryptCharField
(
EncryptMixin
,
models
.
CharField
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
kwargs
[
'max_length'
]
=
2048
super
()
.
__init__
(
*
args
,
**
kwargs
)
apps/users/models/user.py
View file @
48e87857
...
...
@@ -45,7 +45,7 @@ class User(AbstractUser):
wechat
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
verbose_name
=
_
(
'Wechat'
))
phone
=
models
.
CharField
(
max_length
=
20
,
blank
=
True
,
null
=
True
,
verbose_name
=
_
(
'Phone'
))
otp_level
=
models
.
SmallIntegerField
(
default
=
0
,
choices
=
OTP_LEVEL_CHOICES
,
verbose_name
=
_
(
'Enable OTP'
))
otp_secret_key
=
models
.
CharField
(
max_length
=
16
,
blank
=
True
,
null
=
True
)
_otp_secret_key
=
models
.
CharField
(
max_length
=
128
,
blank
=
True
,
null
=
True
)
# Todo: Auto generate key, let user download
_private_key
=
models
.
CharField
(
max_length
=
5000
,
blank
=
True
,
verbose_name
=
_
(
'Private key'
))
_public_key
=
models
.
CharField
(
max_length
=
5000
,
blank
=
True
,
verbose_name
=
_
(
'Public key'
))
...
...
@@ -70,6 +70,14 @@ class User(AbstractUser):
def
password_raw
(
self
,
password_raw_
):
self
.
set_password
(
password_raw_
)
@property
def
otp_secret_key
(
self
):
return
signer
.
unsign
(
self
.
_otp_secret_key
)
@otp_secret_key.setter
def
otp_secret_key
(
self
,
item
):
self
.
_otp_secret_key
=
signer
.
sign
(
item
)
.
decode
(
'utf-8'
)
def
get_absolute_url
(
self
):
return
reverse
(
'users:user-detail'
,
args
=
(
self
.
id
,))
...
...
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