Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
douban-api-proxy
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
captainwong
douban-api-proxy
Commits
4b76b840
Commit
4b76b840
authored
Oct 13, 2018
by
Jim Ma
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add OAuth link&unlink in user profile
parent
1abbcfa3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
21 deletions
+90
-21
user_edit.html
cps/templates/user_edit.html
+15
-0
web.py
cps/web.py
+75
-21
No files found.
cps/templates/user_edit.html
View file @
4b76b840
...
@@ -52,6 +52,21 @@
...
@@ -52,6 +52,21 @@
{% endfor %}
{% endfor %}
</select>
</select>
</div>
</div>
{% if registered_oauth.keys()| length > 0 %}
<div
class=
"form-group"
>
<label
for=
"oauth_settings"
>
{{_('OAuth Settings')}}
</label>
<br/>
{% for oauth, name in registered_oauth.iteritems() %}
<label
for=
"config_{{ oauth }}_oauth"
style=
"margin-left: 15px"
>
{{ name }}
</label>
{% if oauth not in oauth_status %}
<a
href=
"/{{ oauth }}"
target=
"_blank"
style=
"margin-left: 5px"
>
Link
</a>
{% else %}
<a
href=
"/unlink/{{ oauth }}"
style=
"margin-left: 5px"
>
Unlink
</a>
{% endif %}
<br/>
{% endfor %}
</div>
{% endif %}
<div
class=
"col-sm-6"
>
<div
class=
"col-sm-6"
>
<div
class=
"form-group"
>
<div
class=
"form-group"
>
<input
type=
"checkbox"
name=
"show_random"
id=
"show_random"
{%
if
content
.
show_random_books
()
%}
checked
{%
endif
%}
>
<input
type=
"checkbox"
name=
"show_random"
id=
"show_random"
{%
if
content
.
show_random_books
()
%}
checked
{%
endif
%}
>
...
...
cps/web.py
View file @
4b76b840
...
@@ -119,7 +119,7 @@ EXTENSIONS_CONVERT = {'pdf', 'epub', 'mobi', 'azw3', 'docx', 'rtf', 'fb2', 'lit'
...
@@ -119,7 +119,7 @@ EXTENSIONS_CONVERT = {'pdf', 'epub', 'mobi', 'azw3', 'docx', 'rtf', 'fb2', 'lit'
# EXTENSIONS_READER = set(['txt', 'pdf', 'epub', 'zip', 'cbz', 'tar', 'cbt'] + (['rar','cbr'] if rar_support else []))
# EXTENSIONS_READER = set(['txt', 'pdf', 'epub', 'zip', 'cbz', 'tar', 'cbt'] + (['rar','cbr'] if rar_support else []))
oauth_check
=
[]
oauth_check
=
{}
'''class ReverseProxied(object):
'''class ReverseProxied(object):
"""Wrap the application in this middleware and configure the
"""Wrap the application in this middleware and configure the
...
@@ -2751,6 +2751,7 @@ def profile():
...
@@ -2751,6 +2751,7 @@ def profile():
downloads
=
list
()
downloads
=
list
()
languages
=
speaking_language
()
languages
=
speaking_language
()
translations
=
babel
.
list_translations
()
+
[
LC
(
'en'
)]
translations
=
babel
.
list_translations
()
+
[
LC
(
'en'
)]
oauth_status
=
get_oauth_status
()
for
book
in
content
.
downloads
:
for
book
in
content
.
downloads
:
downloadBook
=
db
.
session
.
query
(
db
.
Books
)
.
filter
(
db
.
Books
.
id
==
book
.
book_id
)
.
first
()
downloadBook
=
db
.
session
.
query
(
db
.
Books
)
.
filter
(
db
.
Books
.
id
==
book
.
book_id
)
.
first
()
if
downloadBook
:
if
downloadBook
:
...
@@ -2812,11 +2813,11 @@ def profile():
...
@@ -2812,11 +2813,11 @@ def profile():
ub
.
session
.
rollback
()
ub
.
session
.
rollback
()
flash
(
_
(
u"Found an existing account for this e-mail address."
),
category
=
"error"
)
flash
(
_
(
u"Found an existing account for this e-mail address."
),
category
=
"error"
)
return
render_title_template
(
"user_edit.html"
,
content
=
content
,
downloads
=
downloads
,
return
render_title_template
(
"user_edit.html"
,
content
=
content
,
downloads
=
downloads
,
title
=
_
(
u"
%(name)
s's profile"
,
name
=
current_user
.
nickname
))
title
=
_
(
u"
%(name)
s's profile"
,
name
=
current_user
.
nickname
,
registered_oauth
=
oauth_check
,
oauth_status
=
oauth_status
))
flash
(
_
(
u"Profile updated"
),
category
=
"success"
)
flash
(
_
(
u"Profile updated"
),
category
=
"success"
)
return
render_title_template
(
"user_edit.html"
,
translations
=
translations
,
profile
=
1
,
languages
=
languages
,
return
render_title_template
(
"user_edit.html"
,
translations
=
translations
,
profile
=
1
,
languages
=
languages
,
content
=
content
,
downloads
=
downloads
,
title
=
_
(
u"
%(name)
s's profile"
,
content
=
content
,
downloads
=
downloads
,
title
=
_
(
u"
%(name)
s's profile"
,
name
=
current_user
.
nickname
),
page
=
"me"
)
name
=
current_user
.
nickname
),
page
=
"me"
,
registered_oauth
=
oauth_check
,
oauth_status
=
oauth_status
)
@
app
.
route
(
"/admin/view"
)
@
app
.
route
(
"/admin/view"
)
...
@@ -3945,22 +3946,22 @@ def convert_bookformat(book_id):
...
@@ -3945,22 +3946,22 @@ def convert_bookformat(book_id):
return
redirect
(
request
.
environ
[
"HTTP_REFERER"
])
return
redirect
(
request
.
environ
[
"HTTP_REFERER"
])
def
register_oauth_blueprint
(
blueprint
):
def
register_oauth_blueprint
(
blueprint
,
show_name
):
if
blueprint
.
name
!=
""
:
if
blueprint
.
name
!=
""
:
oauth_check
.
append
(
blueprint
.
name
)
oauth_check
[
blueprint
.
name
]
=
show_name
def
register_user_with_oauth
(
user
=
None
):
def
register_user_with_oauth
(
user
=
None
):
all_oauth
=
[]
all_oauth
=
{}
for
oauth
in
oauth_check
:
for
oauth
in
oauth_check
.
keys
()
:
if
oauth
+
'_oauth_user_id'
in
session
and
session
[
oauth
+
'_oauth_user_id'
]
!=
''
:
if
oauth
+
'_oauth_user_id'
in
session
and
session
[
oauth
+
'_oauth_user_id'
]
!=
''
:
all_oauth
.
append
(
oauth
)
all_oauth
[
oauth
]
=
oauth_check
[
oauth
]
if
len
(
all_oauth
)
==
0
:
if
len
(
all_oauth
.
keys
()
)
==
0
:
return
return
if
user
is
None
:
if
user
is
None
:
flash
(
_
(
u"Register with
%
s"
%
", "
.
join
(
all_oauth
)),
category
=
"success"
)
flash
(
_
(
u"Register with
%
s"
%
", "
.
join
(
list
(
all_oauth
.
values
())
)),
category
=
"success"
)
else
:
else
:
for
oauth
in
all_oauth
:
for
oauth
in
all_oauth
.
keys
()
:
# Find this OAuth token in the database, or create it
# Find this OAuth token in the database, or create it
query
=
ub
.
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
query
=
ub
.
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
provider
=
oauth
,
provider
=
oauth
,
...
@@ -3980,7 +3981,7 @@ def register_user_with_oauth(user=None):
...
@@ -3980,7 +3981,7 @@ def register_user_with_oauth(user=None):
def
logout_oauth_user
():
def
logout_oauth_user
():
for
oauth
in
oauth_check
:
for
oauth
in
oauth_check
.
keys
()
:
if
oauth
+
'_oauth_user_id'
in
session
:
if
oauth
+
'_oauth_user_id'
in
session
:
session
.
pop
(
oauth
+
'_oauth_user_id'
)
session
.
pop
(
oauth
+
'_oauth_user_id'
)
...
@@ -4006,20 +4007,22 @@ app.register_blueprint(github_blueprint, url_prefix='/login')
...
@@ -4006,20 +4007,22 @@ app.register_blueprint(github_blueprint, url_prefix='/login')
github_blueprint
.
backend
=
OAuthBackend
(
ub
.
OAuth
,
ub
.
session
,
user
=
current_user
,
user_required
=
True
)
github_blueprint
.
backend
=
OAuthBackend
(
ub
.
OAuth
,
ub
.
session
,
user
=
current_user
,
user_required
=
True
)
google_blueprint
.
backend
=
OAuthBackend
(
ub
.
OAuth
,
ub
.
session
,
user
=
current_user
,
user_required
=
True
)
google_blueprint
.
backend
=
OAuthBackend
(
ub
.
OAuth
,
ub
.
session
,
user
=
current_user
,
user_required
=
True
)
register_oauth_blueprint
(
github_blueprint
)
register_oauth_blueprint
(
google_blueprint
)
if
config
.
config_use_github_oauth
:
register_oauth_blueprint
(
github_blueprint
,
'GitHub'
)
if
config
.
config_use_google_oauth
:
register_oauth_blueprint
(
google_blueprint
,
'Google'
)
@
oauth_authorized
.
connect_via
(
github_blueprint
)
@
oauth_authorized
.
connect_via
(
github_blueprint
)
def
github_logged_in
(
blueprint
,
token
):
def
github_logged_in
(
blueprint
,
token
):
if
not
token
:
if
not
token
:
flash
(
"Failed to log in with GitHub."
,
category
=
"error"
)
flash
(
_
(
"Failed to log in with GitHub."
)
,
category
=
"error"
)
return
False
return
False
resp
=
blueprint
.
session
.
get
(
"/user"
)
resp
=
blueprint
.
session
.
get
(
"/user"
)
if
not
resp
.
ok
:
if
not
resp
.
ok
:
msg
=
"Failed to fetch user info from GitHub."
flash
(
_
(
"Failed to fetch user info from GitHub."
),
category
=
"error"
)
flash
(
msg
,
category
=
"error"
)
return
False
return
False
github_info
=
resp
.
json
()
github_info
=
resp
.
json
()
...
@@ -4030,13 +4033,12 @@ def github_logged_in(blueprint, token):
...
@@ -4030,13 +4033,12 @@ def github_logged_in(blueprint, token):
@
oauth_authorized
.
connect_via
(
google_blueprint
)
@
oauth_authorized
.
connect_via
(
google_blueprint
)
def
google_logged_in
(
blueprint
,
token
):
def
google_logged_in
(
blueprint
,
token
):
if
not
token
:
if
not
token
:
flash
(
"Failed to log in with Google."
,
category
=
"error"
)
flash
(
_
(
"Failed to log in with Google."
)
,
category
=
"error"
)
return
False
return
False
resp
=
blueprint
.
session
.
get
(
"/oauth2/v2/userinfo"
)
resp
=
blueprint
.
session
.
get
(
"/oauth2/v2/userinfo"
)
if
not
resp
.
ok
:
if
not
resp
.
ok
:
msg
=
"Failed to fetch user info from Google."
flash
(
_
(
"Failed to fetch user info from Google."
),
category
=
"error"
)
flash
(
msg
,
category
=
"error"
)
return
False
return
False
google_info
=
resp
.
json
()
google_info
=
resp
.
json
()
...
@@ -4088,7 +4090,7 @@ def bind_oauth_or_register(provider, provider_user_id, redirect_url):
...
@@ -4088,7 +4090,7 @@ def bind_oauth_or_register(provider, provider_user_id, redirect_url):
return
redirect
(
url_for
(
'index'
))
return
redirect
(
url_for
(
'index'
))
else
:
else
:
# bind to current user
# bind to current user
if
current_user
and
not
current_user
.
is_anonymous
:
if
current_user
and
current_user
.
is_authenticated
:
oauth
.
user
=
current_user
oauth
.
user
=
current_user
try
:
try
:
ub
.
session
.
add
(
oauth
)
ub
.
session
.
add
(
oauth
)
...
@@ -4101,6 +4103,46 @@ def bind_oauth_or_register(provider, provider_user_id, redirect_url):
...
@@ -4101,6 +4103,46 @@ def bind_oauth_or_register(provider, provider_user_id, redirect_url):
return
redirect
(
url_for
(
redirect_url
))
return
redirect
(
url_for
(
redirect_url
))
def
get_oauth_status
():
status
=
[]
query
=
ub
.
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
user_id
=
current_user
.
id
,
)
try
:
oauths
=
query
.
all
()
for
oauth
in
oauths
:
status
.
append
(
oauth
.
provider
)
return
status
except
NoResultFound
:
return
None
def
unlink_oauth
(
provider
):
if
request
.
host_url
+
'me'
!=
request
.
referrer
:
pass
query
=
ub
.
session
.
query
(
ub
.
OAuth
)
.
filter_by
(
provider
=
provider
,
user_id
=
current_user
.
id
,
)
try
:
oauth
=
query
.
one
()
if
current_user
and
current_user
.
is_authenticated
:
oauth
.
user
=
current_user
try
:
ub
.
session
.
delete
(
oauth
)
ub
.
session
.
commit
()
logout_oauth_user
()
flash
(
_
(
"Unlink to
%(oauth)
s success."
,
oauth
=
oauth_check
[
provider
]),
category
=
"success"
)
except
Exception
as
e
:
app
.
logger
.
exception
(
e
)
ub
.
session
.
rollback
()
flash
(
_
(
"Unlink to
%(oauth)
s failed."
,
oauth
=
oauth_check
[
provider
]),
category
=
"error"
)
except
NoResultFound
:
app
.
logger
.
warning
(
"oauth
%
s for user
%
d not fount"
%
(
provider
,
current_user
.
id
))
flash
(
_
(
"Not linked to
%(oauth)
s."
,
oauth
=
oauth_check
[
provider
]),
category
=
"error"
)
return
redirect
(
url_for
(
'profile'
))
# notify on OAuth provider error
# notify on OAuth provider error
@
oauth_error
.
connect_via
(
github_blueprint
)
@
oauth_error
.
connect_via
(
github_blueprint
)
def
github_error
(
blueprint
,
error
,
error_description
=
None
,
error_uri
=
None
):
def
github_error
(
blueprint
,
error
,
error_description
=
None
,
error_uri
=
None
):
...
@@ -4129,6 +4171,12 @@ def github_login():
...
@@ -4129,6 +4171,12 @@ def github_login():
return
redirect
(
url_for
(
'login'
))
return
redirect
(
url_for
(
'login'
))
@
app
.
route
(
'/unlink/github'
,
methods
=
[
"GET"
])
@
login_required
def
github_login_unlink
():
return
unlink_oauth
(
github_blueprint
.
name
)
@
app
.
route
(
'/google'
)
@
app
.
route
(
'/google'
)
@
google_oauth_required
@
google_oauth_required
def
google_login
():
def
google_login
():
...
@@ -4154,3 +4202,9 @@ def google_error(blueprint, error, error_description=None, error_uri=None):
...
@@ -4154,3 +4202,9 @@ def google_error(blueprint, error, error_description=None, error_uri=None):
uri
=
error_uri
,
uri
=
error_uri
,
)
)
flash
(
msg
,
category
=
"error"
)
flash
(
msg
,
category
=
"error"
)
@
app
.
route
(
'/unlink/google'
,
methods
=
[
"GET"
])
@
login_required
def
google_login_unlink
():
return
unlink_oauth
(
google_blueprint
.
name
)
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