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
a62a2178
Commit
a62a2178
authored
Oct 14, 2016
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add user backend
parent
f038423c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
4 deletions
+69
-4
utils.py
apps/common/utils.py
+22
-4
backends.py
apps/users/backends.py
+47
-0
No files found.
apps/common/utils.py
View file @
a62a2178
...
...
@@ -7,15 +7,19 @@ from itertools import chain
import
string
import
logging
from
itsdangerous
import
TimedJSONWebSignatureSerializer
from
itsdangerous
import
Signer
,
TimedJSONWebSignatureSerializer
,
JSONWebSignatureSerializer
,
TimestampSigner
,
\
BadSignature
,
SignatureExpired
from
django.shortcuts
import
reverse
as
dj_reverse
from
django.conf
import
settings
from
django.core
import
signing
from
django.utils
import
timezone
SECRET_KEY
=
settings
.
SECRET_KEY
SIGNER
=
TimestampSigner
(
SECRET_KEY
)
def
reverse
(
viewname
,
urlconf
=
None
,
args
=
None
,
kwargs
=
None
,
current_app
=
None
,
external
=
False
):
url
=
dj_reverse
(
viewname
,
urlconf
=
urlconf
,
args
=
args
,
kwargs
=
kwargs
,
current_app
=
current_app
)
def
reverse
(
view_name
,
urlconf
=
None
,
args
=
None
,
kwargs
=
None
,
current_app
=
None
,
external
=
False
):
url
=
dj_reverse
(
view_name
,
urlconf
=
urlconf
,
args
=
args
,
kwargs
=
kwargs
,
current_app
=
current_app
)
if
external
:
url
=
settings
.
SITE_URL
.
strip
(
'/'
)
+
url
...
...
@@ -44,13 +48,27 @@ def decrypt(*args, **kwargs):
return
''
def
sign
(
value
):
return
SIGNER
.
sign
(
value
)
def
unsign
(
value
,
max_age
=
3600
):
try
:
return
SIGNER
.
unsign
(
value
,
max_age
=
max_age
)
except
(
BadSignature
,
SignatureExpired
):
return
None
def
date_expired_default
():
try
:
years
=
int
(
settings
.
CONFIG
.
DEFAULT_EXPIRED_YEARS
)
except
TypeError
:
years
=
70
return
timezone
.
now
()
+
timezone
.
timedelta
(
days
=
365
*
years
)
return
timezone
.
now
()
+
timezone
.
timedelta
(
days
=
365
*
years
)
def
sign
(
value
):
return
SIGNER
.
sign
(
value
)
def
combine_seq
(
s1
,
s2
,
callback
=
None
):
...
...
apps/users/backends.py
0 → 100644
View file @
a62a2178
# -*- coding: utf-8 -*-
#
from
rest_framework
import
authentication
,
exceptions
from
django.utils.translation
import
ugettext
as
_
from
common.utils
import
unsign
from
.models
import
User
class
APPSignAuthentication
(
authentication
.
BaseAuthentication
):
keyword
=
'Sign'
model
=
User
def
authenticate
(
self
,
request
):
auth
=
authentication
.
get_authorization_header
(
request
)
.
split
()
if
not
auth
or
auth
[
0
]
.
lower
()
!=
self
.
keyword
.
lower
()
.
encode
():
return
None
if
len
(
auth
)
==
1
:
msg
=
_
(
'Invalid sign header. No credentials provided.'
)
raise
exceptions
.
AuthenticationFailed
(
msg
)
elif
len
(
auth
)
>
2
:
msg
=
_
(
'Invalid sign header. Sign string should not contain spaces.'
)
raise
exceptions
.
AuthenticationFailed
(
msg
)
try
:
sign
=
auth
[
1
]
.
decode
()
except
UnicodeError
:
msg
=
_
(
'Invalid token header. Sign string should not contain invalid characters.'
)
raise
exceptions
.
AuthenticationFailed
(
msg
)
return
self
.
authenticate_credentials
(
sign
)
def
authenticate_credentials
(
self
,
key
):
try
:
token
=
self
.
model
.
objects
.
select_related
(
'user'
)
.
get
(
key
=
key
)
except
self
.
model
.
DoesNotExist
:
raise
exceptions
.
AuthenticationFailed
(
_
(
'Invalid token.'
))
if
not
token
.
user
.
is_active
:
raise
exceptions
.
AuthenticationFailed
(
_
(
'User inactive or deleted.'
))
if
__name__
==
'__main__'
:
pass
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