-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Gitlab SSO
- Loading branch information
Showing
3 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Github Login Example | ||
""" | ||
|
||
import os | ||
import uvicorn | ||
from fastapi import FastAPI, Request | ||
from fastapi_sso.sso.gitlab import GitlabSSO | ||
|
||
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" | ||
CLIENT_ID = os.environ["CLIENT_ID"] | ||
CLIENT_SECRET = os.environ["CLIENT_SECRET"] | ||
|
||
app = FastAPI() | ||
|
||
sso = GitlabSSO( | ||
client_id=CLIENT_ID, | ||
client_secret=CLIENT_SECRET, | ||
redirect_uri="http://localhost:5000/auth/callback", | ||
allow_insecure_http=True, | ||
) | ||
|
||
|
||
@app.get("/auth/login") | ||
async def auth_init(): | ||
"""Initialize auth and redirect""" | ||
return await sso.get_login_redirect() | ||
|
||
|
||
@app.get("/auth/callback") | ||
async def auth_callback(request: Request): | ||
"""Verify login""" | ||
user = await sso.verify_and_process(request) | ||
return user | ||
|
||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app="examples.gitlab:app", host="127.0.0.1", port=5000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Gitlab SSO Oauth Helper class""" | ||
|
||
from fastapi_sso.sso.base import DiscoveryDocument, OpenID, SSOBase | ||
|
||
|
||
class GitlabSSO(SSOBase): | ||
"""Class providing login via Gitlab SSO""" | ||
|
||
provider = "gitlab" | ||
scope = ["read_user", "openid", "profile"] | ||
additional_headers = {"accept": "application/json"} | ||
|
||
async def get_discovery_document(self) -> DiscoveryDocument: | ||
return { | ||
"authorization_endpoint": "https://gitlab.com/oauth/authorize", | ||
"token_endpoint": "https://gitlab.com/oauth/token", | ||
"userinfo_endpoint": "https://gitlab.com/api/v4/user", | ||
} | ||
|
||
@classmethod | ||
async def openid_from_response(cls, response: dict) -> OpenID: | ||
return OpenID( | ||
email=response["email"], | ||
provider=cls.provider, | ||
id=response["id"], | ||
display_name=response["username"], | ||
picture=response["avatar_url"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters