Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
indicators-parser-saas
backend
Commits
c2be9270
Verified
Commit
c2be9270
authored
2 years ago
by
Власов Иван Юрьевич
Browse files
Options
Download
Patches
Plain Diff
Added GitHub OAuth endpoints
parent
0b80ea53
master
No related merge requests found
Pipeline
#98292
passed with stage
in 54 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
endpoints/__init__.py
+1
-0
endpoints/__init__.py
endpoints/oauth.py
+24
-0
endpoints/oauth.py
endpoints/routes.py
+7
-3
endpoints/routes.py
tests/test_github_oauth.py
+37
-0
tests/test_github_oauth.py
with
69 additions
and
3 deletions
+69
-3
endpoints/__init__.py
+
1
−
0
View file @
c2be9270
from
.oauth
import
github_oauth_callback
,
github_oauth_redirect
from
.service
import
ping
This diff is collapsed.
Click to expand it.
endpoints/oauth.py
0 → 100644
+
24
−
0
View file @
c2be9270
from
starlette.requests
import
Request
from
starlette.responses
import
RedirectResponse
from
settings
import
GITHUB_CLIENT_ID
from
integrations.github
import
GithubAPI
from
responses.errors
import
NoGithubCode
async
def
github_oauth_callback
(
request
:
Request
):
""" GitHub OAuth callback request """
if
"code"
not
in
request
.
query_params
:
raise
NoGithubCode
github_code
=
request
.
query_params
[
"code"
]
access_token
=
await
GithubAPI
.
process_code
(
github_code
)
github_data
=
await
GithubAPI
.
get_user
(
access_token
)
return
RedirectResponse
(
"/"
,
301
,
headers
=
{
"Set-Cookie"
:
f
"token=[user=
{
github_data
[
'user_id'
]
}
&login=
{
github_data
[
'username'
]
}
]"
# TODO: save token to cookies
})
async
def
github_oauth_redirect
(
request
:
Request
):
""" Redirect to GitHub OAuth login window """
return
RedirectResponse
(
f
"https://github.com/login/oauth/authorize?client_id=
{
GITHUB_CLIENT_ID
}
"
,
301
)
This diff is collapsed.
Click to expand it.
endpoints/routes.py
+
7
−
3
View file @
c2be9270
from
starlette.routing
import
Route
from
endpoints
import
ping
from
endpoints
import
*
unauthenticated_routes
=
[
Route
(
"/ping"
,
ping
,
methods
=
[
"GET"
])
Route
(
"/ping"
,
ping
,
methods
=
[
"GET"
]),
Route
(
"/login/github"
,
github_oauth_redirect
,
methods
=
[
"GET"
]),
Route
(
"/oauth/github"
,
github_oauth_callback
,
methods
=
[
"GET"
])
]
api_available_routes
=
[]
...
...
@@ -10,4 +14,4 @@ user_only_routes = []
admin_routes
=
[]
routes
=
unauthenticated_routes
+
api_available_routes
+
user_only_routes
routes
=
unauthenticated_routes
+
api_available_routes
+
user_only_routes
+
admin_routes
This diff is collapsed.
Click to expand it.
tests/test_github_oauth.py
0 → 100644
+
37
−
0
View file @
c2be9270
from
json
import
dumps
from
urllib.parse
import
urlparse
from
responses.errors
import
NoGithubCode
from
responses.responses
import
BadResponse
from
tests.fixtures.aioresponse
import
mocked
from
tests.fixtures.client
import
sti
def
test_github_oauth
(
sti
,
mocked
):
mocked
.
post
(
"https://github.com/login/oauth/access_token"
,
status
=
200
,
body
=
dumps
({
"access_token"
:
"ghAAAA"
,
"scope"
:
"repo,gist"
,
"token_type"
:
"bearer"
}))
mocked
.
get
(
"https://api.github.com/user"
,
status
=
200
,
body
=
dumps
({
"login"
:
"krol"
,
"id"
:
54
}))
resp
=
sti
.
get
(
"/oauth/github?code=12345"
,
follow_redirects
=
False
)
assert
resp
.
status_code
==
301
assert
resp
.
headers
.
get
(
"Set-Cookie"
)
==
"token=[user=54&login=krol]"
def
test_github_oauth_no_code
(
sti
):
resp
=
sti
.
get
(
"/oauth/github"
)
assert
resp
.
status_code
==
422
assert
resp
.
json
()[
"data"
][
"error_code"
]
==
NoGithubCode
().
code
def
test_github_oauth_redirect
(
sti
):
resp
=
sti
.
get
(
"/login/github"
,
follow_redirects
=
False
)
assert
resp
.
status_code
==
301
redirect_url
=
urlparse
(
resp
.
headers
[
"Location"
])
assert
redirect_url
.
path
==
"/login/oauth/authorize"
assert
redirect_url
.
hostname
==
"github.com"
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets