Skip to content

Commit 17552f5

Browse files
Merge pull request #4675 from communitybridge/unicron-add-option-for-manual-redirect
Allow get_redirect_url param
2 parents 4b1d697 + fd56031 commit 17552f5

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

cla-backend/cla/controllers/repository_service.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88
import cla
9-
from falcon import HTTP_404
9+
from falcon import HTTP_202, HTTP_404
1010

1111
def received_activity(provider, data):
1212
"""
@@ -34,7 +34,7 @@ def sign_request(provider, installation_id, github_repository_id, change_request
3434
service = cla.utils.get_repository_service(provider)
3535
return service.sign_request(installation_id, github_repository_id, change_request_id, request)
3636

37-
def user_from_session(request, response=None):
37+
def user_from_session(get_redirect_url, request, response=None):
3838
"""
3939
Return user from OAuth2 session
4040
"""
@@ -45,9 +45,12 @@ def user_from_session(request, response=None):
4545
# os.environ["CLA_API_BASE"] = os.getenv("CLA_API_BASE_CLI", os.environ["CLA_API_BASE"])
4646
# LG: to test using MockGitHub class
4747
# from cla.models.github_models import MockGitHub
48-
# user = MockGitHub(os.environ["GITHUB_OAUTH_TOKEN"]).user_from_session(request)
49-
user = cla.utils.get_repository_service('github').user_from_session(request)
48+
# user = MockGitHub(os.environ["GITHUB_OAUTH_TOKEN"]).user_from_session(request, get_redirect_url)
49+
user = cla.utils.get_repository_service('github').user_from_session(request, get_redirect_url)
5050
if user is None:
5151
response.status = HTTP_404
5252
return {"errors": "Cannot find user from session"}
53+
if isinstance(user, dict):
54+
response.status = HTTP_202
55+
return user
5356
return user.to_dict()

cla-backend/cla/models/github_models.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def received_activity(self, data):
9898
else:
9999
cla.log.debug("github_models.received_activity - Ignoring unsupported action: {}".format(data["action"]))
100100

101-
def user_from_session(self, request):
101+
def user_from_session(self, request, get_redirect_url):
102102
fn = "github_models.user_from_session"
103103
cla.log.debug(f"{fn} - loading session from request: {request}...")
104104
session = self._get_request_session(request)
@@ -118,9 +118,13 @@ def user_from_session(self, request):
118118
cla.log.debug(f"{fn} - obtained GitHub OAuth2 state from authorization - storing CSRF token in the session...")
119119
session["github_oauth2_state"] = csrf_token
120120
cla.log.debug(f"{fn} - GitHub OAuth2 request with CSRF token {csrf_token} - sending user to {authorization_url}")
121-
cla.log.debug(f"{fn} - redirecting by returning 302 and redirect URL")
122121
# We must redirect to GitHub OAuth app for authentication, it will return you to /v2/github/installation which will handle returning user data
123-
raise falcon.HTTPFound(authorization_url)
122+
if get_redirect_url:
123+
cla.log.debug(f"{fn} - sending redirect_url via 202 HTTP status JSON payload")
124+
return { "redirect_url": authorization_url }
125+
else:
126+
cla.log.debug(f"{fn} - redirecting by returning 302 and redirect URL")
127+
raise falcon.HTTPFound(authorization_url)
124128

125129
def sign_request(self, installation_id, github_repository_id, change_request_id, request):
126130
"""

cla-backend/cla/routes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,7 @@ def user_from_session(request, response):
18091809
"""
18101810
GET: /user-from-session
18111811
Example: https://api.dev.lfcla.com/v2/user-from-session
1812+
Example: https://api.dev.lfcla.com/v2/user-from-session?get_redirect_url=1
18121813
Returns user object from OAuth2 session
18131814
Example user returned:
18141815
{
@@ -1833,10 +1834,13 @@ def user_from_session(request, response):
18331834
"version": "v1"
18341835
}
18351836
Will 302 redirect to /v2/github/installation if there is no session and that callback will return user data then
1837+
WIll return 202 redirect to the same in reponse's JSON 'redirect_url' property if get_redirect_url=1 (param)
18361838
Will return 200 and user data if there is an active GitHub session
18371839
Can return 404 on OAuth2 errors
18381840
"""
1839-
return cla.controllers.repository_service.user_from_session(request, response)
1841+
raw_redirect = request.params.get('get_redirect_url', 'false').lower()
1842+
get_redirect_url = raw_redirect in ('1', 'true', 'yes')
1843+
return cla.controllers.repository_service.user_from_session(get_redirect_url, request, response)
18401844

18411845

18421846
@hug.post("/events", versions=1)

utils/get_user_from_session_py.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
#!/bin/bash
22
# API_URL=https://[xyz].ngrok-free.app (defaults to localhost:5000)
33
# API_URL=https://api.lfcla.dev.platform.linuxfoundation.org
4-
# DEBUG='' ./utils/get_user_from_session_py.sh
4+
# DEBUG='' [GET_REDIRECT_URL=1] ./utils/get_user_from_session_py.sh
55
# Flow with custom GitHub app: see 'LG:' in cla/controllers/repository_service.py, then:
66
# Start server via: CLA_API_BASE_CLI='http://147.75.85.27:5000' GH_OAUTH_CLIENT_ID_CLI="$(cat ../lg-github-oauth-app.client-id.secret)" GH_OAUTH_SECRET_CLI="$(cat ../lg-github-oauth-app.client-secret.secret)" yarn serve:ext
77
# In the browser: open page: http://147.75.85.27:5000/v2/user-from-session
88

9+
if [ -z "${GET_REDIRECT_URL}" ]
10+
then
11+
export GET_REDIRECT_URL="0"
12+
fi
13+
914
if [ -z "$API_URL" ]
1015
then
1116
export API_URL="http://localhost:5000"
1217
fi
1318

14-
export API="${API_URL}/v2/user-from-session"
19+
export API="${API_URL}/v2/user-from-session?get_redirect_url=${GET_REDIRECT_URL}"
1520

1621
if [ ! -z "$DEBUG" ]
1722
then

0 commit comments

Comments
 (0)