mirror of
https://github.com/status-im/PyGithub.git
synced 2026-09-01 03:11:09 +00:00
Merge pull request #1021 from rigaspapas/implement-apps-oauth-endpoints
Implement OAuth for apps
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
############################ Copyrights and license ############################
|
||||
# #
|
||||
# Copyright 2019 Rigas Papathanasopoulos <rigaspapas@gmail.com> #
|
||||
# #
|
||||
# This file is part of PyGithub. #
|
||||
# http://pygithub.readthedocs.io/ #
|
||||
# #
|
||||
# PyGithub is free software: you can redistribute it and/or modify it under #
|
||||
# the terms of the GNU Lesser General Public License as published by the Free #
|
||||
# Software Foundation, either version 3 of the License, or (at your option) #
|
||||
# any later version. #
|
||||
# #
|
||||
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY #
|
||||
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
|
||||
# details. #
|
||||
# #
|
||||
# You should have received a copy of the GNU Lesser General Public License #
|
||||
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
|
||||
# #
|
||||
################################################################################
|
||||
|
||||
import github.GithubObject
|
||||
|
||||
|
||||
class AccessToken(github.GithubObject.NonCompletableGithubObject):
|
||||
"""
|
||||
This class represents access tokens.
|
||||
"""
|
||||
|
||||
def __repr__(self):
|
||||
return self.get__repr__(
|
||||
{
|
||||
"token": "{}...".format(self.token[:5]),
|
||||
"scope": self.scope,
|
||||
"type": self.type,
|
||||
}
|
||||
)
|
||||
|
||||
@property
|
||||
def token(self):
|
||||
"""
|
||||
:type: string
|
||||
"""
|
||||
return self._token.value
|
||||
|
||||
@property
|
||||
def type(self):
|
||||
"""
|
||||
:type: string
|
||||
"""
|
||||
return self._type.value
|
||||
|
||||
@property
|
||||
def scope(self):
|
||||
"""
|
||||
:type: string
|
||||
"""
|
||||
return self._scope.value
|
||||
|
||||
def _initAttributes(self):
|
||||
self._token = github.GithubObject.NotSet
|
||||
self._type = github.GithubObject.NotSet
|
||||
self._scope = github.GithubObject.NotSet
|
||||
|
||||
def _useAttributes(self, attributes):
|
||||
if "access_token" in attributes: # pragma no branch
|
||||
self._token = self._makeStringAttribute(attributes["access_token"])
|
||||
if "token_type" in attributes: # pragma no branch
|
||||
self._type = self._makeStringAttribute(attributes["token_type"])
|
||||
if "scope" in attributes: # pragma no branch
|
||||
self._scope = self._makeStringAttribute(attributes["scope"])
|
||||
@@ -0,0 +1,114 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
############################ Copyrights and license ###########################
|
||||
# #
|
||||
# Copyright 2019 Rigas Papathanasopoulos <rigaspapas@gmail.com> #
|
||||
# #
|
||||
# This file is part of PyGithub. #
|
||||
# http://pygithub.readthedocs.io/ #
|
||||
# #
|
||||
# PyGithub is free software: you can redistribute it and/or modify it under #
|
||||
# the terms of the GNU Lesser General Public License as published by the Free #
|
||||
# Software Foundation, either version 3 of the License, or (at your option) #
|
||||
# any later version. #
|
||||
# #
|
||||
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY #
|
||||
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
||||
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more#
|
||||
# details. #
|
||||
# #
|
||||
# You should have received a copy of the GNU Lesser General Public License #
|
||||
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
|
||||
# #
|
||||
###############################################################################
|
||||
|
||||
import urllib
|
||||
|
||||
import github.GithubObject
|
||||
from github.AccessToken import AccessToken
|
||||
|
||||
|
||||
class ApplicationOAuth(github.GithubObject.NonCompletableGithubObject):
|
||||
"""
|
||||
This class is used for identifying and authorizing users for Github Apps.
|
||||
https://developer.github.com/apps/building-github-apps/identifying-and-authorizing-users-for-github-apps/#1-users-are-redirected-to-request-their-github-identity
|
||||
"""
|
||||
|
||||
def __repr__(self):
|
||||
return self.get__repr__({"client_id": self._client_id.value})
|
||||
|
||||
@property
|
||||
def client_id(self):
|
||||
return self._client_id.value
|
||||
|
||||
@property
|
||||
def client_secret(self):
|
||||
return self._client_secret.value
|
||||
|
||||
def _initAttributes(self):
|
||||
self._client_id = github.GithubObject.NotSet
|
||||
self._client_secret = github.GithubObject.NotSet
|
||||
|
||||
def _useAttributes(self, attributes):
|
||||
if "client_id" in attributes: # pragma no branch
|
||||
self._client_id = self._makeStringAttribute(attributes["client_id"])
|
||||
if "client_secret" in attributes: # pragma no branch
|
||||
self._client_secret = self._makeStringAttribute(attributes["client_secret"])
|
||||
|
||||
def get_login_url(self, redirect_uri=None, state=None, login=None):
|
||||
"""
|
||||
Return the URL you need to redirect a user to in order to authorize
|
||||
your App.
|
||||
:type: string
|
||||
"""
|
||||
parameters = {"client_id": self.client_id}
|
||||
if redirect_uri is not None:
|
||||
assert isinstance(redirect_uri, str), redirect_uri
|
||||
parameters["redirect_uri"] = redirect_uri
|
||||
if state is not None:
|
||||
assert isinstance(state, str), state
|
||||
parameters["state"] = state
|
||||
if login is not None:
|
||||
assert isinstance(login, str), login
|
||||
parameters["login"] = login
|
||||
|
||||
parameters = urllib.parse.urlencode(parameters)
|
||||
|
||||
base_url = "https://github.com/login/oauth/authorize"
|
||||
return u"{}?{}".format(base_url, parameters)
|
||||
|
||||
def get_access_token(self, code, state=None):
|
||||
"""
|
||||
:calls: `POST /login/oauth/access_token <https://developer.github.com/apps/building-github-apps/identifying-and-authorizing-users-for-github-apps/>`_
|
||||
:param code: string
|
||||
:param state: string
|
||||
"""
|
||||
assert isinstance(code, str), code
|
||||
post_parameters = {
|
||||
"code": code,
|
||||
"client_id": self.client_id,
|
||||
"client_secret": self.client_secret,
|
||||
}
|
||||
|
||||
if state is not None:
|
||||
post_parameters["state"] = state
|
||||
|
||||
self._requester._Requester__authorizationHeader = None
|
||||
headers, data = self._requester.requestJsonAndCheck(
|
||||
"POST",
|
||||
"https://github.com/login/oauth/access_token",
|
||||
headers={
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "PyGithub/Python",
|
||||
},
|
||||
input=post_parameters,
|
||||
)
|
||||
|
||||
return AccessToken(
|
||||
requester=self._requester,
|
||||
# not required, this is a NonCompletableGithubObject
|
||||
headers={},
|
||||
attributes=data,
|
||||
completed=False,
|
||||
)
|
||||
@@ -21,6 +21,7 @@
|
||||
# Copyright 2018 bryanhuntesl <31992054+bryanhuntesl@users.noreply.github.com> #
|
||||
# Copyright 2018 sfdye <tsfdye@gmail.com> #
|
||||
# Copyright 2018 itsbruce <it.is.bruce@gmail.com> #
|
||||
# Copyright 2019 Rigas Papathanasopoulos <rigaspapas@gmail.com> #
|
||||
# #
|
||||
# This file is part of PyGithub. #
|
||||
# http://pygithub.readthedocs.io/ #
|
||||
@@ -1108,6 +1109,20 @@ class AuthenticatedUser(github.GithubObject.CompletableGithubObject):
|
||||
github.Repository.Repository, self._requester, "/user/subscriptions", None
|
||||
)
|
||||
|
||||
def get_installations(self):
|
||||
"""
|
||||
:calls: `GET /user/installations <http://developer.github.com/v3/apps>`_
|
||||
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Installation.Installation`
|
||||
"""
|
||||
return github.PaginatedList.PaginatedList(
|
||||
github.Installation.Installation,
|
||||
self._requester,
|
||||
"/user/installations",
|
||||
None,
|
||||
headers={"Accept": Consts.mediaTypeIntegrationPreview},
|
||||
list_item="installations",
|
||||
)
|
||||
|
||||
def has_in_following(self, following):
|
||||
"""
|
||||
:calls: `GET /user/following/:user <http://developer.github.com/v3/users/followers>`_
|
||||
|
||||
+35
-1
@@ -6,6 +6,7 @@
|
||||
# Copyright 2017 Simon <spam@esemi.ru> #
|
||||
# Copyright 2018 Wan Liuyang <tsfdye@gmail.com> #
|
||||
# Copyright 2018 sfdye <tsfdye@gmail.com> #
|
||||
# Copyright 2019 Rigas Papathanasopoulos <rigaspapas@gmail.com> #
|
||||
# #
|
||||
# This file is part of PyGithub. #
|
||||
# http://pygithub.readthedocs.io/ #
|
||||
@@ -52,7 +53,31 @@ class Installation(github.GithubObject.NonCompletableGithubObject):
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self._id
|
||||
"""
|
||||
:type: integer
|
||||
"""
|
||||
return self._id.value
|
||||
|
||||
@property
|
||||
def app_id(self):
|
||||
"""
|
||||
:type: integer
|
||||
"""
|
||||
return self._app_id.value
|
||||
|
||||
@property
|
||||
def target_id(self):
|
||||
"""
|
||||
:type: integer
|
||||
"""
|
||||
return self._target_id.value
|
||||
|
||||
@property
|
||||
def target_type(self):
|
||||
"""
|
||||
:type: string
|
||||
"""
|
||||
return self._target_type.value
|
||||
|
||||
def get_repos(self):
|
||||
"""
|
||||
@@ -72,7 +97,16 @@ class Installation(github.GithubObject.NonCompletableGithubObject):
|
||||
|
||||
def _initAttributes(self):
|
||||
self._id = github.GithubObject.NotSet
|
||||
self._app_id = github.GithubObject.NotSet
|
||||
self._target_id = github.GithubObject.NotSet
|
||||
self._target_type = github.GithubObject.NotSet
|
||||
|
||||
def _useAttributes(self, attributes):
|
||||
if "id" in attributes: # pragma no branch
|
||||
self._id = self._makeIntAttribute(attributes["id"])
|
||||
if "app_id" in attributes: # pragma no branch
|
||||
self._app_id = self._makeIntAttribute(attributes["app_id"])
|
||||
if "target_id" in attributes: # pragma no branch
|
||||
self._target_id = self._makeIntAttribute(attributes["target_id"])
|
||||
if "target_type" in attributes: # pragma no branch
|
||||
self._target_type = self._makeStringAttribute(attributes["target_type"])
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
# Copyright 2018 sfdye <tsfdye@gmail.com> #
|
||||
# Copyright 2018 itsbruce <it.is.bruce@gmail.com> #
|
||||
# Copyright 2019 Tomas Tomecek <tomas@tomecek.net> #
|
||||
# Copyright 2019 Rigas Papathanasopoulos <rigaspapas@gmail.com> #
|
||||
# #
|
||||
# This file is part of PyGithub. #
|
||||
# http://pygithub.readthedocs.io/ #
|
||||
@@ -57,6 +58,7 @@ import jwt
|
||||
import requests
|
||||
import urllib3
|
||||
|
||||
import github.ApplicationOAuth
|
||||
import github.Event
|
||||
import github.Gist
|
||||
import github.GithubObject
|
||||
@@ -778,6 +780,14 @@ class Github(object):
|
||||
self.__requester, headers={}, attributes={"id": id}, completed=True
|
||||
)
|
||||
|
||||
def get_oauth_application(self, client_id, client_secret):
|
||||
return github.ApplicationOAuth.ApplicationOAuth(
|
||||
self.__requester,
|
||||
headers={},
|
||||
attributes={"client_id": client_id, "client_secret": client_secret},
|
||||
completed=False,
|
||||
)
|
||||
|
||||
|
||||
class GithubIntegration(object):
|
||||
"""
|
||||
|
||||
@@ -538,6 +538,7 @@ class Requester:
|
||||
self.__hostname,
|
||||
"uploads.github.com",
|
||||
"status.github.com",
|
||||
"github.com",
|
||||
], o.hostname
|
||||
assert o.path.startswith((self.__prefix, "/api/"))
|
||||
assert o.port == self.__port
|
||||
|
||||
Reference in New Issue
Block a user