mirror of https://github.com/doccano/doccano.git
6 changed files with 263 additions and 0 deletions
Split View
Diff Options
-
6app/app/settings.py
-
44app/server/social_auth.py
-
76app/server/tests/cassettes/TestGithubSocialAuth.test_fetch_permissions_is_admin.yaml
-
77app/server/tests/cassettes/TestGithubSocialAuth.test_fetch_permissions_not_admin.yaml
-
57app/server/tests/test_social_auth.py
-
3requirements.txt
@ -0,0 +1,44 @@ |
|||
import requests |
|||
from django.conf import settings |
|||
from social_core.backends.github import GithubOAuth2 |
|||
|
|||
|
|||
# noinspection PyUnusedLocal |
|||
def fetch_github_permissions(strategy, details, user=None, is_new=False, *args, **kwargs): |
|||
org_name = getattr(settings, 'GITHUB_ADMIN_ORG_NAME', '') |
|||
team_name = getattr(settings, 'GITHUB_ADMIN_TEAM_NAME', '') |
|||
if not user or not isinstance(kwargs['backend'], GithubOAuth2) or not org_name or not team_name: |
|||
return |
|||
|
|||
response = requests.post( |
|||
url='https://api.github.com/graphql', |
|||
headers={ |
|||
'Authorization': 'Bearer {}'.format(kwargs['response']['access_token']), |
|||
}, |
|||
json={ |
|||
'query': ''' |
|||
query($userName: String!, $orgName: String!, $teamName: String!) { |
|||
organization(login: $orgName) { |
|||
teams(query: $teamName, userLogins: [$userName], first: 1) { |
|||
nodes { |
|||
name |
|||
} |
|||
} |
|||
} |
|||
} |
|||
''', |
|||
'variables': { |
|||
'userName': details['username'], |
|||
'orgName': org_name, |
|||
'teamName': team_name, |
|||
} |
|||
} |
|||
) |
|||
response.raise_for_status() |
|||
response = response.json() |
|||
|
|||
is_superuser = {'name': team_name} in response['data']['organization']['teams']['nodes'] |
|||
|
|||
if user.is_superuser != is_superuser: |
|||
user.is_superuser = is_superuser |
|||
user.save() |
@ -0,0 +1,76 @@ |
|||
interactions: |
|||
- request: |
|||
body: '{"query": "\n query($userName: String!, $orgName: String!, |
|||
$teamName: String!) {\n organization(login: $orgName) {\n teams(query: |
|||
$teamName, userLogins: [$userName], first: 1) {\n nodes |
|||
{\n name\n }\n }\n }\n }\n ", |
|||
"variables": {"userName": "c-w", "orgName": "CatalystCode", "teamName": "doccano-dev"}}' |
|||
headers: |
|||
Accept: |
|||
- '*/*' |
|||
Accept-Encoding: |
|||
- gzip, deflate |
|||
Connection: |
|||
- keep-alive |
|||
Content-Length: |
|||
- '513' |
|||
Content-Type: |
|||
- application/json |
|||
User-Agent: |
|||
- python-requests/2.21.0 |
|||
method: POST |
|||
uri: https://api.github.com/graphql |
|||
response: |
|||
body: |
|||
string: '{"data":{"organization":{"teams":{"nodes":[{"name":"doccano-dev"}]}}}}' |
|||
headers: |
|||
Access-Control-Allow-Origin: |
|||
- '*' |
|||
Access-Control-Expose-Headers: |
|||
- ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, |
|||
X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, |
|||
X-GitHub-Media-Type |
|||
Cache-Control: |
|||
- no-cache |
|||
Content-Security-Policy: |
|||
- default-src 'none' |
|||
Content-Type: |
|||
- application/json; charset=utf-8 |
|||
Date: |
|||
- Mon, 20 May 2019 17:38:20 GMT |
|||
Referrer-Policy: |
|||
- origin-when-cross-origin, strict-origin-when-cross-origin |
|||
Server: |
|||
- GitHub.com |
|||
Status: |
|||
- 200 OK |
|||
Strict-Transport-Security: |
|||
- max-age=31536000; includeSubdomains; preload |
|||
Transfer-Encoding: |
|||
- chunked |
|||
X-Accepted-OAuth-Scopes: |
|||
- repo |
|||
X-Content-Type-Options: |
|||
- nosniff |
|||
X-Frame-Options: |
|||
- deny |
|||
X-GitHub-Media-Type: |
|||
- github.v4; format=json |
|||
X-GitHub-Request-Id: |
|||
- E979:03BD:225D930:49FB694:5CE2E60C |
|||
X-OAuth-Scopes: |
|||
- read:org |
|||
X-RateLimit-Limit: |
|||
- '5000' |
|||
X-RateLimit-Remaining: |
|||
- '4955' |
|||
X-RateLimit-Reset: |
|||
- '1558377500' |
|||
X-XSS-Protection: |
|||
- 1; mode=block |
|||
content-length: |
|||
- '70' |
|||
status: |
|||
code: 200 |
|||
message: OK |
|||
version: 1 |
@ -0,0 +1,77 @@ |
|||
interactions: |
|||
- request: |
|||
body: '{"query": "\n query($userName: String!, $orgName: String!, |
|||
$teamName: String!) {\n organization(login: $orgName) {\n teams(query: |
|||
$teamName, userLogins: [$userName], first: 1) {\n nodes |
|||
{\n name\n }\n }\n }\n }\n ", |
|||
"variables": {"userName": "hirosan", "orgName": "CatalystCode", "teamName": |
|||
"doccano-dev"}}' |
|||
headers: |
|||
Accept: |
|||
- '*/*' |
|||
Accept-Encoding: |
|||
- gzip, deflate |
|||
Connection: |
|||
- keep-alive |
|||
Content-Length: |
|||
- '517' |
|||
Content-Type: |
|||
- application/json |
|||
User-Agent: |
|||
- python-requests/2.21.0 |
|||
method: POST |
|||
uri: https://api.github.com/graphql |
|||
response: |
|||
body: |
|||
string: '{"data":{"organization":{"teams":{"nodes":[]}}}}' |
|||
headers: |
|||
Access-Control-Allow-Origin: |
|||
- '*' |
|||
Access-Control-Expose-Headers: |
|||
- ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, |
|||
X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, |
|||
X-GitHub-Media-Type |
|||
Cache-Control: |
|||
- no-cache |
|||
Content-Security-Policy: |
|||
- default-src 'none' |
|||
Content-Type: |
|||
- application/json; charset=utf-8 |
|||
Date: |
|||
- Mon, 20 May 2019 17:38:20 GMT |
|||
Referrer-Policy: |
|||
- origin-when-cross-origin, strict-origin-when-cross-origin |
|||
Server: |
|||
- GitHub.com |
|||
Status: |
|||
- 200 OK |
|||
Strict-Transport-Security: |
|||
- max-age=31536000; includeSubdomains; preload |
|||
Transfer-Encoding: |
|||
- chunked |
|||
X-Accepted-OAuth-Scopes: |
|||
- repo |
|||
X-Content-Type-Options: |
|||
- nosniff |
|||
X-Frame-Options: |
|||
- deny |
|||
X-GitHub-Media-Type: |
|||
- github.v4; format=json |
|||
X-GitHub-Request-Id: |
|||
- E97B:0EFE:220AB47:4963FE2:5CE2E60C |
|||
X-OAuth-Scopes: |
|||
- read:org |
|||
X-RateLimit-Limit: |
|||
- '5000' |
|||
X-RateLimit-Remaining: |
|||
- '4954' |
|||
X-RateLimit-Reset: |
|||
- '1558377500' |
|||
X-XSS-Protection: |
|||
- 1; mode=block |
|||
content-length: |
|||
- '48' |
|||
status: |
|||
code: 200 |
|||
message: OK |
|||
version: 1 |
@ -0,0 +1,57 @@ |
|||
from django.contrib.auth import get_user_model |
|||
from django.test import TestCase, override_settings |
|||
from social_core.backends.github import GithubOAuth2 |
|||
from vcr_unittest import VCRMixin |
|||
|
|||
from .. import social_auth |
|||
|
|||
User = get_user_model() |
|||
|
|||
|
|||
class VCRTestCase(VCRMixin, TestCase): |
|||
@property |
|||
def access_token(self): |
|||
raise NotImplementedError() |
|||
|
|||
def _get_vcr(self, **kwargs): |
|||
kwargs['decode_compressed_response'] = True |
|||
kwargs['record_mode'] = 'none' if self.access_token == 'censored' else 'all' |
|||
return super()._get_vcr(**kwargs) |
|||
|
|||
def _get_vcr_kwargs(self, **kwargs): |
|||
kwargs['filter_headers'] = ['Authorization'] |
|||
return super()._get_vcr_kwargs(**kwargs) |
|||
|
|||
|
|||
@override_settings(GITHUB_ADMIN_ORG_NAME='CatalystCode') |
|||
@override_settings(GITHUB_ADMIN_TEAM_NAME='doccano-dev') |
|||
class TestGithubSocialAuth(VCRTestCase): |
|||
strategy = None |
|||
backend = GithubOAuth2(strategy=strategy) |
|||
access_token = 'censored' |
|||
|
|||
def test_fetch_permissions_is_admin(self): |
|||
user = User() |
|||
|
|||
social_auth.fetch_github_permissions( |
|||
strategy=self.strategy, |
|||
details={'username': 'c-w'}, |
|||
user=user, |
|||
backend=self.backend, |
|||
response={'access_token': self.access_token}, |
|||
) |
|||
|
|||
self.assertTrue(user.is_superuser) |
|||
|
|||
def test_fetch_permissions_not_admin(self): |
|||
user = User() |
|||
|
|||
social_auth.fetch_github_permissions( |
|||
strategy=self.strategy, |
|||
details={'username': 'hirosan'}, |
|||
user=user, |
|||
backend=self.backend, |
|||
response={'access_token': self.access_token}, |
|||
) |
|||
|
|||
self.assertFalse(user.is_superuser) |
Write
Preview
Loading…
Cancel
Save