blob: 077a95f73fb6615bceda97abc83256a11a47c833 [file] [log] [blame]
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -07001# Copyright 2016 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""OAuth 2.0 Credentials.
16
17This module provides credentials based on OAuth 2.0 access and refresh tokens.
18These credentials usually access resources on behalf of a user (resource
19owner).
20
21Specifically, this is intended to use access tokens acquired using the
22`Authorization Code grant`_ and can refresh those tokens using a
23optional `refresh token`_.
24
25Obtaining the initial access and refresh token is outside of the scope of this
26module. Consult `rfc6749 section 4.1`_ for complete details on the
27Authorization Code grant flow.
28
29.. _Authorization Code grant: https://tools.ietf.org/html/rfc6749#section-1.3.1
30.. _refresh token: https://tools.ietf.org/html/rfc6749#section-6
31.. _rfc6749 section 4.1: https://tools.ietf.org/html/rfc6749#section-4.1
32"""
33
34from google.auth import _helpers
35from google.auth import credentials
36from google.oauth2 import _client
37
38
39class Credentials(credentials.Scoped, credentials.Credentials):
40 """Credentials using OAuth 2.0 access and refresh tokens."""
41
42 def __init__(self, token, refresh_token=None, token_uri=None,
43 client_id=None, client_secret=None, scopes=None):
44 """
45 Args:
46 token (Optional(str)): The OAuth 2.0 access token. Can be None
47 if refresh information is provided.
48 refresh_token (str): The OAuth 2.0 refresh token. If specified,
49 credentials can be refreshed.
50 token_uri (str): The OAuth 2.0 authorization server's token
51 endpoint URI. Must be specified for refresh, can be left as
52 None if the token can not be refreshed.
53 client_id (str): The OAuth 2.0 client ID. Must be specified for
54 refresh, can be left as None if the token can not be refreshed.
55 client_secret(str): The OAuth 2.0 client secret. Must be specified
56 for refresh, can be left as None if the token can not be
57 refreshed.
58 scopes (Sequence[str]): The scopes that were originally used
59 to obtain authorization. This is a purely informative parameter
60 that can be used by :meth:`has_scopes`. OAuth 2.0 credentials
61 can not request additional scopes after authorization.
62 """
63 super(Credentials, self).__init__()
64 self.token = token
65 self._refresh_token = refresh_token
66 self._scopes = scopes
67 self._token_uri = token_uri
68 self._client_id = client_id
69 self._client_secret = client_secret
70
71 @property
Jon Wayne Parrott2d0549a2017-03-01 09:27:16 -080072 def refresh_token(self):
73 """Optional[str]: The OAuth 2.0 refresh token."""
74 return self._refresh_token
75
76 @property
77 def token_uri(self):
78 """Optional[str]: The OAuth 2.0 authorization server's token endpoint
79 URI."""
80 return self._token_uri
81
82 @property
83 def client_id(self):
84 """Optional[str]: The OAuth 2.0 client ID."""
85 return self._client_id
86
87 @property
88 def client_secret(self):
89 """Optional[str]: The OAuth 2.0 client secret."""
90 return self._client_secret
91
92 @property
Jon Wayne Parrott10ec7e92016-10-17 10:46:38 -070093 def requires_scopes(self):
94 """False: OAuth 2.0 credentials have their scopes set when
95 the initial token is requested and can not be changed."""
96 return False
97
98 def with_scopes(self, scopes):
99 """Unavailable, OAuth 2.0 credentials can not be re-scoped.
100
101 OAuth 2.0 credentials have their scopes set when the initial token is
102 requested and can not be changed.
103 """
104 raise NotImplementedError(
105 'OAuth 2.0 Credentials can not modify their scopes.')
106
107 @_helpers.copy_docstring(credentials.Credentials)
108 def refresh(self, request):
109 access_token, refresh_token, expiry, _ = _client.refresh_grant(
110 request, self._token_uri, self._refresh_token, self._client_id,
111 self._client_secret)
112
113 self.token = access_token
114 self.expiry = expiry
115 self._refresh_token = refresh_token