2018-05-15 17:28:43 +00:00
|
|
|
from oauth.base import OAuthService, OAuthEndpoint
|
2017-01-20 20:21:08 +00:00
|
|
|
from util import slash_join
|
|
|
|
|
|
|
|
class GitLabOAuthService(OAuthService):
|
|
|
|
def __init__(self, config, key_name):
|
|
|
|
super(GitLabOAuthService, self).__init__(config, key_name)
|
|
|
|
|
|
|
|
def service_id(self):
|
|
|
|
return 'gitlab'
|
|
|
|
|
|
|
|
def service_name(self):
|
|
|
|
return 'GitLab'
|
|
|
|
|
|
|
|
def _endpoint(self):
|
|
|
|
return self.config.get('GITLAB_ENDPOINT', 'https://gitlab.com')
|
|
|
|
|
|
|
|
def user_endpoint(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def api_endpoint(self):
|
|
|
|
return self._endpoint()
|
|
|
|
|
|
|
|
def get_public_url(self, suffix):
|
|
|
|
return slash_join(self._endpoint(), suffix)
|
|
|
|
|
|
|
|
def authorize_endpoint(self):
|
2018-05-15 17:28:43 +00:00
|
|
|
return OAuthEndpoint(slash_join(self._endpoint(), '/oauth/authorize'))
|
2017-01-20 20:21:08 +00:00
|
|
|
|
|
|
|
def token_endpoint(self):
|
2018-05-15 17:28:43 +00:00
|
|
|
return OAuthEndpoint(slash_join(self._endpoint(), '/oauth/token'))
|
2017-01-20 20:21:08 +00:00
|
|
|
|
|
|
|
def validate_client_id_and_secret(self, http_client, app_config):
|
2017-01-24 20:20:19 +00:00
|
|
|
# We validate the client ID and secret by hitting the OAuth token exchange endpoint with
|
|
|
|
# the real client ID and secret, but a fake auth code to exchange. Gitlab's implementation will
|
|
|
|
# return `invalid_client` as the `error` if the client ID or secret is invalid; otherwise, it
|
|
|
|
# will return another error.
|
2018-05-15 17:28:43 +00:00
|
|
|
url = self.token_endpoint().to_url()
|
2017-01-20 20:21:08 +00:00
|
|
|
redirect_uri = self.get_redirect_uri(app_config, redirect_suffix='trigger')
|
|
|
|
data = {
|
|
|
|
'code': 'fakecode',
|
|
|
|
'client_id': self.client_id(),
|
|
|
|
'client_secret': self.client_secret(),
|
|
|
|
'grant_type': 'authorization_code',
|
|
|
|
'redirect_uri': redirect_uri
|
|
|
|
}
|
|
|
|
|
|
|
|
# We validate by checking the error code we receive from this call.
|
|
|
|
result = http_client.post(url, data=data, timeout=5)
|
|
|
|
value = result.json()
|
|
|
|
if not value:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return value.get('error', '') != 'invalid_client'
|
|
|
|
|
|
|
|
def get_public_config(self):
|
|
|
|
return {
|
|
|
|
'CLIENT_ID': self.client_id(),
|
2018-05-15 17:28:43 +00:00
|
|
|
'AUTHORIZE_ENDPOINT': self.authorize_endpoint().to_url_prefix(),
|
2017-01-20 20:21:08 +00:00
|
|
|
'GITLAB_ENDPOINT': self._endpoint(),
|
|
|
|
}
|