Skip to content

Commit 420bee1

Browse files
Merge pull request #4706 from communitybridge/unicron-v2-user-from-token
Add '/v2/user-from-token' API
2 parents edef5d2 + 7c8bcd8 commit 420bee1

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

cla-backend/cla/routes.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,37 @@ def user_from_session(request, response):
18431843
get_redirect_url = raw_redirect in ('1', 'true', 'yes')
18441844
return cla.controllers.repository_service.user_from_session(get_redirect_url, request, response)
18451845

1846+
@hug.get("/user-from-token", versions=2)
1847+
def user_from_token(auth_user: check_auth, request, response):
1848+
"""
1849+
GET: /user-from-token
1850+
Example: https://api.dev.lfcla.com/v2/user-from-token
1851+
Returns user object from Bearer token
1852+
Example user returned:
1853+
{
1854+
"date_created": "2025-02-11T08:16:01.000000+0000",
1855+
"date_modified": "2025-02-11T08:16:01.000000+0000",
1856+
"lf_email": "[email protected]",
1857+
"lf_sub": null,
1858+
"lf_username": "lgryglicki",
1859+
"note": null,
1860+
"user_company_id": "0ca30016-6457-466c-bc41-a09560c1f9bf",
1861+
"user_emails": null,
1862+
"user_external_id": "0014100000Te0yqAAB",
1863+
"user_github_id": null,
1864+
"user_github_username": null,
1865+
"user_gitlab_id": null,
1866+
"user_gitlab_username": null,
1867+
"user_id": "6e1fd921-e850-11ef-b5df-92cef1e60fc3",
1868+
"user_ldap_id": null,
1869+
"user_name": "Lukasz Gryglicki",
1870+
"version": "v1"
1871+
}
1872+
Will return 200 and user data if token is valid
1873+
Can return 404 on token errors
1874+
"""
1875+
return cla.controllers.user.get_or_create_user(auth_user).to_dict()
1876+
18461877

18471878
@hug.post("/events", versions=1)
18481879
def create_event(

utils/get_user_from_token_py.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
# API_URL=https://[xyz].ngrok-free.app (defaults to localhost:5000)
3+
# API_URL=https://api.lfcla.dev.platform.linuxfoundation.org
4+
# DEBUG='' ./utils/get_user_from_token_py.sh
5+
# For testing locally two options:
6+
# 1) cla/routes.py: 'LG:': return request and cla.auth.fake_authenticate_user(request.headers) - test with fake data
7+
# Or to get a real user data:
8+
# 2a) on local (non remote) computer: ~/get_oauth_token.sh (or ~/get_oauth_token_prod.sh) (will open browser, authenticate to LF, and return token data)
9+
# 2b) edit 'cla/auth.py': uncomment: 'LG: for local environment override', then run server via: clear && AUTH0_USERNAME_CLAIM_CLI='http://lfx.dev/claims/username' yarn serve:ext
10+
# 2c) then TOKEN='value from the get_oauth_token.sh script' DEBUG='' ./utils/get_user_from_token_py.sh
11+
12+
if [ -z "$TOKEN" ]
13+
then
14+
# source ./auth0_token.secret
15+
TOKEN="$(cat ./auth0.token.secret)"
16+
fi
17+
18+
if [ -z "$TOKEN" ]
19+
then
20+
echo "$0: TOKEN not specified and unable to obtain one"
21+
exit 1
22+
fi
23+
24+
if [ -z "$XACL" ]
25+
then
26+
XACL="$(cat ./x-acl.secret)"
27+
fi
28+
29+
if [ -z "$XACL" ]
30+
then
31+
echo "$0: XACL not specified and unable to obtain one"
32+
exit 2
33+
fi
34+
35+
if [ -z "$API_URL" ]
36+
then
37+
export API_URL="http://localhost:5000"
38+
fi
39+
40+
API="${API_URL}/v2/user-from-token"
41+
42+
if [ ! -z "$DEBUG" ]
43+
then
44+
echo "curl -s -XGET -H \"X-ACL: ${XACL}\" -H \"Authorization: Bearer ${TOKEN}\" -H \"Content-Type: application/json\" \"${API}\""
45+
curl -s -XGET -H "X-ACL: ${XACL}" -H "Authorization: Bearer ${TOKEN}" -H "Content-Type: application/json" "${API}"
46+
else
47+
curl -s -XGET -H "X-ACL: ${XACL}" -H "Authorization: Bearer ${TOKEN}" -H "Content-Type: application/json" "${API}" | jq -r '.'
48+
fi

0 commit comments

Comments
 (0)