8000 Update the authorization class and its methods · pythonthings/github3.py@d2fb594 · GitHub
[go: up one dir, main page]

Skip to content

Commit d2fb594

Browse files
committed
Update the authorization class and its methods
1 parent 5b35075 commit d2fb594

File tree

8 files changed

+150
-67
lines changed

8 files changed

+150
-67
lines changed

github3/auths.py

Lines changed: 113 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,103 @@
11
# -*- coding: utf-8 -*-
2-
"""
3-
github3.auths
4-
=============
5-
6-
This module contains the Authorization object.
7-
8-
"""
2+
"""This module contains the Authorization object."""
93
from __future__ import unicode_literals
104

115
from .decorators import requires_basic_auth
126
from .models import GitHubCore
137

148

159
class Authorization(GitHubCore):
10+
"""Representation of an OAuth Authorization.
1611
17-
"""The :class:`Authorization <Authorization>` object.
12+
See also: https://developer.github.com/v3/oauth_authorizations/
1813
19-
Two authorization instances can be checked like so::
14+
This object has the following attributes:
2015
21-
a1 == a2
22-
a1 != a2
16+
.. attribute:: app
2317
24-
And is equivalent to::
18+
Details about the application the authorization was created for.
2519
26-
a1.id == a2.id
27-
a1.id != a2.id
20+
.. attribute:: created_at
2821
29-
See also: http://developer.github.com/v3/oauth/# 8000 oauth-authorizations-api
22+
A :class:`~datetime.datetime` representing when this authorization was
23+
created.
3024
31-
"""
25+
.. attribute:: fingerprint
3226
33-
def _update_attributes(self, auth):
34-
self._api = auth['url']
27+
.. versionadded:: 1.0
3528
36-
#: Details about the application (name, url)
37-
self.app = auth['app']
29+
The optional parameter that is used to allow an OAuth application to
30+
create multiple authorizations for the same user. This will help
31+
distinguish two authorizations for the same app.
3832
39-
#: Returns the Authorization token
40-
self.token = auth['token']
33+
.. attribute:: hashed_token
4134
42-
#: App name
43-
self.name = self.app['name']
35+
.. versionadded:: 1.0
4436
45-
#: URL about the note
46-
self.note_url = auth['note_url']
37+
This is the base64 of the SHA-256 digest of the token.
4738
48-
#: Note about the authorization
49-
self.note = auth['note']
39+
.. seealso::
5040
51-
#: List of scopes this applies to
52-
self.scopes = auth['scopes']
41+
`Removing Authorization Tokens`_
42+
The blog post announcing the removal of :attr:`token`.
5343
54-
#: Unique id of the authorization
55-
self.id = auth['id']
44+
.. attribute:: id
5645
57-
#: datetime object representing when the authorization was created.
58-
self.created_at = self._strptime(auth['created_at'])
46+
The unique identifier for this authorization.
47+
48+
.. attribute:: note_url
49+
50+
The URL that points to a longer description about the purpose of this
51+
autohrization.
52+
53+
.. attribute:: note
54+
55+
The short note provided when this authorization was created.
56+
57+
.. attribute:: scopes
58+
59+
The list of scopes assigned to this token.
60+
61+
.. seealso::
62+
63+
`Scopes for OAuth Applications`_
64+
GitHub's documentation around available scopes and what they
65+
mean
5966
60-
#: datetime object representing when the authorization was updated.
67+
.. attribute:: token
68+
69+
If this authorization was created, this will contain the full token.
70+
Otherwise, this attribute will be an empty string.
71+
72+
.. attribute:: token_last_eight
73+
74+
.. versionadded:: 1.0
75+
76+
The last eight characters of the token. This allows users to identify
77+
a token after the initial retrieval.
78+
79+
.. attribute:: updated_at
80+
81+
A :class:`~datetime.datetime` representing when this authorization was
82+
most recently updated.
83+
84+
.. _Scopes for OAuth Applications:
85+
https://developer.github.com/apps/building-oauth-apps/scopes-for-oauth-apps/
86+
.. _Removing Authorization Tokens:
87+
https://developer.github.com/changes/2014-12-08-removing-authorizations-token/#what-should-you-do
88+
"""
89+
90+
def _update_attributes(self, auth):
91+
self._api = auth['url']
92+
self.app = auth['app']
93+
self.created_at = self._strptime(auth['created_at'])
94+
self.fingerprint = auth['fingerprint']
95+
self.id = auth['id']
96+
self.note_url = auth['note_url']
97+
self.note = auth['note']
98+
self.scopes = auth['scopes']
99+
self.token = auth['token']
100+
self.token_last_eight = auth['token_last_eight']
61101
self.updated_at = self._strptime(auth['updated_at'])
62102

63103
def _repr(self):
@@ -79,23 +119,32 @@ def _update(self, scopes_data, note, note_url):
79119

80120
@requires_basic_auth
81121
def add_scopes(self, scopes, note=None, note_url=None):
82-
"""Adds the scopes to this authorization.
122+
"""Add the scopes to this authorization.
83123
84124
.. versionadded:: 1.0
85125
86-
:param list scopes: Adds these scopes to the ones present on this
87-
authorization
88-
:param str note: (optional), Note about the authorization
89-
:param str note_url: (optional), URL to link to when the user views
90-
the authorization
91-
:returns: True if successful, False otherwise
92-
:rtype: bool
126+
:param list scopes:
127+
Adds these scopes to the ones present on this authorization
128+
:param str note:
129+
(optional), Note about the authorization
130+
:param str note_url:
131+
(optional), URL to link to when the user views the authorization
132+
:returns:
133+
True if successful, False otherwise
134+
:rtype:
135+
bool
93136
"""
94137
return self._update({'add_scopes': scopes}, note, note_url)
95138

96139
@requires_basic_auth
97140
def delete(self):
98-
"""Delete this authorization."""
141+
"""Delete this authorization.
142+
143+
:returns:
144+
True if successful, False otherwise
145+
:rtype:
146+
bool
147+
"""
99148
return self._boolean(self._delete(self._api), 204, 404)
100149

101150
@requires_basic_auth
@@ -104,13 +153,16 @@ def remove_scopes(self, scopes, note=None, note_url=None):
104153
105154
.. versionadded:: 1.0
106155
107-
:param list scopes: Remove these scopes from the ones present on this
108-
authorization
109-
:param str note: (optional), Note about the authorization
110-
:param str note_url: (optional), URL to link to when the user views
111-
the authorization
112-
:returns: True if successful, False otherwise
113-
:rtype: bool
156+
:param list scopes:
157+
Remove these scopes from the ones present on this authorization
158+
:param str note:
159+
(optional), Note about the authorization
160+
:param str note_url:
161+
(optional), URL to link to when the user views the authorization
162+
:returns:
163+
True if successful, False otherwise
164+
:rtype:
165+
bool
114166
"""
115167
return self._update({'rm_scopes': scopes}, note, note_url)
116168

@@ -120,11 +172,15 @@ def replace_scopes(self, scopes, note=None, note_url=None):
120172
121173
.. versionadded:: 1.0
122174
123-
:param list scopes: Use these scopes instead of the previous list
124-
:param str note: (optional), Note about the authorization
125-
:param str note_url: (optional), URL to link to when the user views
126-
the authorization
127-
:returns: True if successful, False otherwise
128-
:rtype: bool
175+
:param list scopes:
176+
Use these scopes instead of the previous list
177+
:param str note:
178+
(optional), Note about the authorization
179+
:param str note_url:
180+
(optional), URL to link to when the user views the authorization
181+
:returns:
182+
True if successful, False otherwise
183+
:rtype:
184+
bool
129185
"""
130186
return self._update({'scopes': scopes}, note, note_url)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"http_interactions": [{"request": {"body": {"string": "", "encoding": "utf-8"}, "headers": {"Accept-Encoding": "gzip, deflate", "Accept": "application/vnd.github.v3.full+json", "User-Agent": "github3.py/1.0.0b1", "Accept-Charset": "utf-8", "Content-Type": "application/json", "Authorization": "Basic <BASIC_AUTH>"}, "method": "GET", "uri": "https://api.github.com/authorizations/10716101"}, "response": {"body": {"string": "", "base64_string": "H4sIAAAAAAAAA4WPzWrDMBCEXyXsqQUnliz/6pZjbj3k1FLMWl5iUVsS9hpKQ969MrSUQmj3OjPfzlzB9qClqGQphUxgnUfQMDCHRacpBnu4WB7W7mD8lOLKg5/tB7L1bkm/U5AAhgD6Cg4nivEzLbz7Zd49HJ9Oj9H4w494escpjLSxo2RGS47brQ+IOwe3BNi/kYu6MqaQJFXXmKLLZFEj9iozJseubiqq0NSqzKmJWOf5fqcvrf2r0kzI1LfI8WcmZL4X9T4rzyLTRaOVet4Whf5fz2J8oAX0y+vtE4dC6a5xAQAA", "encoding": "utf-8"}, "headers": {"vary": "Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding", "x-served-by": "3061975e1f37121b3751604ad153c687", "x-xss-protection": "1; mode=block", "x-content-type-options": "nosniff", "etag": "\"af5e035a265070984e2b70fe601d470d\"", "access-control-allow-credentials": "true", "status": "200 OK", "x-ratelimit-remaining": "4960", "x-github-media-type": "github.v3; param=full; format=json", "access-control-expose-headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval", "transfer-encoding": "chunked", "x-github-request-id": "48A0C4D3:4A6F:E6B62CD:53FBF998", "cache-control": "private, max-age=60, s-maxage=60", "last-modified": "Tue, 26 Aug 2014 02:59:33 GMT", "date": "Tue, 26 Aug 2014 03:06:00 GMT", "access-control-allow-origin": "*", "content-security-policy": "default-src 'none'", "content-encoding": "gzip", "strict-transport-security": "max-age=31536000; includeSubdomains", "server": "GitHub.com", "x-ratelimit-limit": "5000", "x-frame-options": "deny", "content-type": "application/json; charset=utf-8", "x-ratelimit-reset": "1409025614"}, "status": {"message": "OK", "code": 200}, "url": "https://api.github.com/authorizations/10716101"}, "recorded_at": "2014-08-26T03:06:00"}, {"request": {"body": {"string": "{\"add_scopes\": [\"user\"]}", "encoding": "utf-8"}, "headers": {"Content-Length": "24", "Accept-Encoding": "gzip, deflate", "Accept": "application/vnd.github.v3.full+json", "User-Agent": "github3.py/1.0.0b1", "Accept-Charset": "utf-8", "Content-Type": "application/json", "Authorization": "Basic <BASIC_AUTH>"}, "method": "POST", "uri": "https://api.github.com/authorizations/10716101"}, "response": {"body": {"string": "", "base64_string": "H4sIAAAAAAAAA32PTWvDMAyG/0rRaYO0seN8+rZjbz30tDGC4ojFLIlNrMBY6X+fAxtjEKbr++jRqxvYHrQUlSylkAmsywgaBmYfdJqit6c3y8PanYybUlx5cIv9RLZuDunPFiSA3oO+wYwTxfUrBT78gQ8PT5fzYwR//VFPHzj5kTZ3jMxoaeZ26wNiZ+CeALt3mmOujCkkSdU1pugyWdSIvcqMybGrm4oqNLUqc2qidna83+k7a/+rtBAy9S1yvJkJmR9FfczKq8h00WilnrePfL/PKC1KLcTGBOM8BdAvsAZa4PX+BQxYish3AQAA", "encoding": "utf-8"}, "headers": {"vary": "Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding", "x-served-by": "88d924ed861736d2749ce1a55766cb53", "x-xss-protection": "1; mode=block", "x-content-type-options": "nosniff", "etag": "\"def9f0eba11df7c937fd185809fab2f1\"", "access-control-allow-credentials": "true", "status": "200 OK", "x-ratelimit-remaining": "4959", "x-github-media-type": "github.v3; param=full; format=json", "access-control-expose-headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval", "transfer-encoding": "chunked", "x-github-request-id": "48A0C4D3:4A6F:E6B62E3:53FBF998", "cache-control": "private, max-age=60, s-maxage=60", "date": "Tue, 26 Aug 2014 03:06:00 GMT", "access-control-allow-origin": "*", "content-security-policy": "default-src 'none'", "content-encoding": "gzip", "strict-transport-security": "max-age=31536000; includeSubdomains", "server": "GitHub.com", "x-ratelimit-limit": "5000", "x-frame-options": "deny", "content-type": "application/json; charset=utf-8", "x-ratelimit-reset": "1409025614"}, "status": {"message": "OK", "code": 200}, "url": "https://api.github.com/authorizations/10716101"}, "recorded_at": "2014-08-26T03:06:01"}], "recorded_with": "betamax/0.4.0"}
1+
{"http_interactions": [{"request": {"body": {"encoding": "utf-8", "string": "{\"note\": \"testing github3.py\", \"note_url\": \"\", \"client_id\": \"\", \"client_secret\": \"\", \"scopes\": [\"gist\"]}"}, "headers": {"User-Agent": "github3.py/1.0.0a4", "Accept-Encoding": "gzip, deflate", "Accept": "application/vnd.github.v3.full+json", "Connection": "keep-alive", "Accept-Charset": "utf-8", "Content-Type": "application/json", "Content-Length": "104", "Authorization": "Basic <BASIC_AUTH>"}, "method": "POST", "uri": "https://api.github.com/authorizations"}, "response": {"body": {"encoding": "utf-8", "string": "{\"id\":168042708,\"url\":\"https://api.github.com/authorizations/168042708\",\"app\":{\"name\":\"testing github3.py\",\"url\":\"https://developer.github.com/v3/oauth_authorizations/\",\"client_id\":\"00000000000000000000\"},\"token\":\"e710ce692a74b47e0dcf971cf22383d9763e77e5\",\"hashed_token\":\"34ddb39bb2a0df0c24059b3649b19c2542f7b84677ebea715dbe9966171821ba\",\"token_last_eight\":\"763e77e5\",\"note\":\"testing github3.py\",\"note_url\":\"\",\"created_at\":\"2018-02-24T13:59:08Z\",\"updated_at\":\"2018-02-24T13:59:08Z\",\"scopes\":[\"gist\"],\"fingerprint\":null}"}, "headers": {"Server": "GitHub.com", "Date": "Sat, 24 Feb 2018 13:59:08 GMT", "Content-Type": "application/json; charset=utf-8", "Content-Length": "519", "Status": "201 Created", "X-RateLimit-Limit": "5000", "X-RateLimit-Remaining": "4998", "X-RateLimit-Reset": "1519483301", "Cache-Control": "private, max-age=60, s-maxage=60", "Vary": "Accept, Authorization, Cookie, X-GitHub-OTP", "ETag": "\"53afa5813f3b57d0cc6e5e3d28664ef6\"", "Location": "https://api.github.com/authorizations/168042708", "X-GitHub-Media-Type": "github.v3; param=full; format=json", "Access-Control-Expose-Headers": "ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval", "Access-Control-Allow-Origin": "*", "Content-Security-Policy": "default-src 'none'", "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", "X-Content-Type-Options": "nosniff", "X-Frame-Options": "deny", "X-XSS-Protection": "1; mode=block", "X-Runtime-rack": "0.136552", "X-GitHub-Request-Id": "BB66:328F:2F607B:9153C5:5A916FAC"}, "status": {"code": 201, "message": "Created"}, "url": "https://api.github.com/authorizations"}, "recorded_at": "2018-02-24T13:59:08"}, {"request": {"body": {"encoding": "utf-8", "string": ""}, "headers": {"User-Agent": "github3.py/1.0.0a4", "Accept-Encoding": "gzip, deflate", "Accept": "application/vnd.github. 179B v3.full+json", "Connection": "keep-alive", "Accept-Charset": "utf-8", "Content-Type": "application/json", "Authorization": "Basic <BASIC_AUTH>"}, "method": "GET", "uri": "https://api.github.com/authorizations/168042708"}, "response": {"body": {"encoding": "utf-8", "base64_string": "H4sIAAAAAAAAA4VQ207EIBD9FTPP3RboFb7DJ41poMy2xC4lZbqJbvbfpW40ajTyyLnNORdwFhRvOlaJlnUZbOsMCiaiEFVR6ODy0dG0mXxYToXeaFpW96rJLT4WnzLIQIcA6gJenzDpCSM5P97dtGUeXhLlu7XFM85LwPVrwLkslj2k/5GU1MPs0FO/nwvslwfXDGh5Rp/wRJ90nND2Hz9lZa0ppTFCM3tkg6hYLU3ZVNJwOYi6EsfWdFXTtmhQt7y2BqVsGt7yTnCjk+O7VT/rSD26caKU0zYlJkWdUL/Qn8V3rL+134usqCmdpncHwXh3YOIgqnteqloq1j3sUwX7LycOab0I6hFGFwmeMjimyXENq/PJ2m/zfH0D5tfGR98BAAA=", "string": ""}, "headers": {"Server": "GitHub.com", "Date": "Sat, 24 Feb 2018 13:59:08 GMT", "Content-Type": "application/json; charset=utf-8", "Transfer-Encoding": "chunked", "Status": "200 OK", "X-RateLimit-Limit": "5000", "X-RateLimit-Remaining": "4997", "X-RateLimit-Reset": "1519483301", "Cache-Control": "private, max-age=60, s-maxage=60", "Vary": "Accept, Authorization, Cookie, X-GitHub-OTP", "ETag": "W/\"cf7dbbd60d744f43163f591c5d03afd7\"", "Last-Modified": "Sat, 24 Feb 2018 13:59:08 GMT", "X-GitHub-Media-Type": "github.v3; param=full; format=json", "Access-Control-Expose-Headers": "ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval", "Access-Control-Allow-Origin": "*", "Content-Security-Policy": "default-src 'none'", "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", "X-Content-Type-Options": "nosniff", "X-Frame-Options": "deny", "X-XSS-Protection": "1; mode=block", "X-Runtime-rack": "0.045674", "Content-Encoding": "gzip", "X-GitHub-Request-Id": "BB66:328F:2F6086:9153E3:5A916FAC"}, "status": {"code": 200, "message": "OK"}, "url": "https://api.github.com/authorizations/168042708"}, "recorded_at": "2018-02-24T13:59:08"}, {"request": {"body": {"encoding": "utf-8", "string": "{\"add_scopes\": [\"user\"]}"}, "headers": {"User-Agent": "github3.py/1.0.0a4", "Accept-Encoding": "gzip, deflate", "Accept": "application/vnd.github.v3.full+json", "Connection": "keep-alive", "Accept-Charset": "utf-8", "Content-Type": "application/json", "Content-Length": "24", "Authorization": "Basic <BASIC_AUTH>"}, "method": "POST", "uri": "https://api.github.com/authorizations/168042708"}, "response": {"body": {"encoding": "utf-8", "base64_string": "H4sIAAAAAAAAA4VQ2U7EIBT9FXOfOy1LN/gOnzSmgXKnJXYoKXQSncy/C040ajTyyNnuORewBiRte1KzjvQF7NsCEuYYfZBVpbwtJxvnXZfjeqrUHud1s68q2tWF6lMGBSjvQV7AqRMmfcQQrZvublpe+pdE+W5t8IzL6nH7GnDm1ZpDhh9JST0uFl0c8rlAfnlwLSCuz+gSnuizCjOa4eOH18ZoLrRmipgjGVlNGqF5WwtNxciamh073ddt16FG1dHGaBSibWlHe0a1So7vVsOiQhzQTnNMOV3LMSmahLo1/lk8Y8OtfS6yoYrpNJUdGKH9gbADq+8pl42QpH/IU3nzLyeMab0A8hEmG2IWBdzgqYBjWh43v1mXEty+LNc3omXqsOYBAAA=", "string": ""}, "headers": {"Server": "GitHub.com", "Date": "Sat, 24 Feb 2018 13:59:08 GMT", "Content-Type": "application/json; charset=utf-8", "Transfer-Encoding": "chunked", "Status": "200 OK", "X-RateLimit-Limit": "5000", "X-RateLimit-Remaining": "4996", "X-RateLimit-Reset": "1519483301", "Cache-Control": "private, max-age=60, s-maxage=60", "Vary": "Accept, Authorization, Cookie, X-GitHub-OTP", "ETag": "W/\"68a5d20f096d0dae11a525aa86a55fb7\"", "X-GitHub-Media-Type": "github.v3; param=full; format=json", "Access-Control-Expose-Headers": "ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval", "Access-Control-Allow-Origin": "*", "Content-Security-Policy": "default-src 'none'", "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", "X-Content-Type-Options": "nosniff", "X-Frame-Options": "deny", "X-XSS-Protection": "1; mode=block", "X-Runtime-rack": "0.156098", "Content-Encoding": "gzip", "X-GitHub-Request-Id": "BB66:328F:2F608A:9153F5:5A916FAC"}, "status": {"code": 200, "message": "OK"}, "url": "https://api.github.com/authorizations/168042708"}, "recorded_at": "2018-02-24T13:59:08"}, {"request": {"body": {"encoding": "utf-8", "string": ""}, "headers": {"User-Agent": "github3.py/1.0.0a4", "Accept-Encoding": "gzip, deflate", 3C84 "Accept": "application/vnd.github.v3.full+json", "Connection": "keep-alive", "Accept-Charset": "utf-8", "Content-Type": "application/json", "Content-Length": "0", "Authorization": "Basic <BASIC_AUTH>"}, "method": "DELETE", "uri": "https://api.github.com/authorizations/168042708"}, "response": {"body": {"encoding": null, "string": ""}, "headers": {"Server": "GitHub.com", "Date": "Sat, 24 Feb 2018 13:59:08 GMT", "Content-Type": "application/octet-stream", "Status": "204 No Content", "X-RateLimit-Limit": "5000", "X-RateLimit-Remaining": "4995", "X-RateLimit-Reset": "1519483301", "X-GitHub-Media-Type": "github.v3; param=full; format=json", "Access-Control-Expose-Headers": "ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval", "Access-Control-Allow-Origin": "*", "Content-Security-Policy": "default-src 'none'", "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", "X-Content-Type-Options": "nosniff", "X-Frame-Options": "deny", "X-XSS-Protection": "1; mode=block", "X-Runtime-rack": "0.097571", "X-GitHub-Request-Id": "BB66:328F:2F6092:915412:5A916FAC"}, "status": {"code": 204, "message": "No Content"}, "url": "https://api.github.com/authorizations/168042708"}, "recorded_at": "2018-02-24T13:59:08"}], "recorded_with": "betamax/0.8.0"}

0 commit comments

Comments
 (0)
0