Skip to content

Commit

Permalink
Add gitlab access (#43)
Browse files Browse the repository at this point in the history
* Add Gitlab SSO
  • Loading branch information
Cereal84 authored Jul 25, 2023
1 parent 24a16eb commit bcd0cc6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
37 changes: 37 additions & 0 deletions examples/gitlab.py
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)
28 changes: 28 additions & 0 deletions fastapi_sso/sso/gitlab.py
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"],
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fastapi-sso"
version = "0.6.4"
version = "0.6.5"
description = "FastAPI plugin to enable SSO to most common providers (such as Facebook login, Google login and login via Microsoft Office 365 Account)"
authors = ["Tomas Votava <info@tomasvotava.eu>"]
readme = "README.md"
Expand Down

0 comments on commit bcd0cc6

Please sign in to comment.