fix: gitlab authentication (#4826)

This commit is contained in:
Nikhil 2024-06-14 18:29:12 +05:30 committed by GitHub
parent 5bbb796e5e
commit 84236f506b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -16,49 +16,37 @@ from plane.authentication.adapter.error import (
class GitLabOAuthProvider(OauthAdapter): class GitLabOAuthProvider(OauthAdapter):
(GITLAB_HOST,) = get_configuration_value(
[
{
"key": "GITLAB_HOST",
"default": os.environ.get("GITLAB_HOST", "https://gitlab.com"),
},
]
)
if not GITLAB_HOST:
raise AuthenticationException(
error_code=AUTHENTICATION_ERROR_CODES["GITLAB_NOT_CONFIGURED"],
error_message="GITLAB_NOT_CONFIGURED",
)
host = GITLAB_HOST
token_url = (
f"{host}/oauth/token"
)
userinfo_url = (
f"{host}/api/v4/user"
)
provider = "gitlab" provider = "gitlab"
scope = "read_user" scope = "read_user"
def __init__(self, request, code=None, state=None, callback=None): def __init__(self, request, code=None, state=None, callback=None):
GITLAB_CLIENT_ID, GITLAB_CLIENT_SECRET = get_configuration_value( GITLAB_CLIENT_ID, GITLAB_CLIENT_SECRET, GITLAB_HOST = (
[ get_configuration_value(
{ [
"key": "GITLAB_CLIENT_ID", {
"default": os.environ.get("GITLAB_CLIENT_ID"), "key": "GITLAB_CLIENT_ID",
}, "default": os.environ.get("GITLAB_CLIENT_ID"),
{ },
"key": "GITLAB_CLIENT_SECRET", {
"default": os.environ.get("GITLAB_CLIENT_SECRET"), "key": "GITLAB_CLIENT_SECRET",
}, "default": os.environ.get("GITLAB_CLIENT_SECRET"),
] },
{
"key": "GITLAB_HOST",
"default": os.environ.get(
"GITLAB_HOST", "https://gitlab.com"
),
},
]
)
) )
if not (GITLAB_CLIENT_ID and GITLAB_CLIENT_SECRET): self.host = GITLAB_HOST
self.token_url = f"{self.host}/oauth/token"
self.userinfo_url = f"{self.host}/api/v4/user"
if not (GITLAB_CLIENT_ID and GITLAB_CLIENT_SECRET and GITLAB_HOST):
raise AuthenticationException( raise AuthenticationException(
error_code=AUTHENTICATION_ERROR_CODES["GITLAB_NOT_CONFIGURED"], error_code=AUTHENTICATION_ERROR_CODES["GITLAB_NOT_CONFIGURED"],
error_message="GITLAB_NOT_CONFIGURED", error_message="GITLAB_NOT_CONFIGURED",
@ -75,9 +63,7 @@ class GitLabOAuthProvider(OauthAdapter):
"scope": self.scope, "scope": self.scope,
"state": state, "state": state,
} }
auth_url = ( auth_url = f"{self.host}/oauth/authorize?{urlencode(url_params)}"
f"{self.host}/oauth/authorize?{urlencode(url_params)}"
)
super().__init__( super().__init__(
request, request,
self.provider, self.provider,
@ -98,7 +84,7 @@ class GitLabOAuthProvider(OauthAdapter):
"client_secret": self.client_secret, "client_secret": self.client_secret,
"code": self.code, "code": self.code,
"redirect_uri": self.redirect_uri, "redirect_uri": self.redirect_uri,
"grant_type": "authorization_code" "grant_type": "authorization_code",
} }
token_response = self.get_user_token( token_response = self.get_user_token(
data=data, headers={"Accept": "application/json"} data=data, headers={"Accept": "application/json"}
@ -109,7 +95,8 @@ class GitLabOAuthProvider(OauthAdapter):
"refresh_token": token_response.get("refresh_token", None), "refresh_token": token_response.get("refresh_token", None),
"access_token_expired_at": ( "access_token_expired_at": (
datetime.fromtimestamp( datetime.fromtimestamp(
token_response.get("created_at") + token_response.get("expires_in"), token_response.get("created_at")
+ token_response.get("expires_in"),
tz=pytz.utc, tz=pytz.utc,
) )
if token_response.get("expires_in") if token_response.get("expires_in")