13
13
# limitations under the License.
14
14
15
15
"""Firebase credentials module."""
16
+ import collections
16
17
import json
17
18
18
- import httplib2
19
+ import google .auth
20
+ from google .auth .transport import requests
21
+ from google .oauth2 import credentials
22
+ from google .oauth2 import service_account
19
23
20
- from oauth2client import client
21
- from oauth2client import crypt
22
24
25
+ _request = requests .Request ()
23
26
24
- _http = httplib2 .Http ()
27
+
28
+ AccessTokenInfo = collections .namedtuple (
29
+ 'AccessTokenInfo' , ['access_token' , 'expiry' ])
25
30
26
31
27
32
class Base (object ):
@@ -39,6 +44,8 @@ def get_credential(self):
39
44
class Certificate (Base ):
40
45
"""A credential initialized from a JSON certificate keyfile."""
41
46
47
+ _CREDENTIAL_TYPE = 'service_account'
48
+
42
49
def __init__ (self , file_path ):
43
50
"""Initializes a credential from a certificate file.
44
51
@@ -53,23 +60,15 @@ def __init__(self, file_path):
53
60
ValueError: If the certificate file is invalid.
54
61
"""
55
62
super (Certificate , self ).__init__ ()
56
- # TODO(hkj): Clean this up once we are able to take a dependency
57
- # TODO(hkj): on latest oauth2client.
58
63
with open (file_path ) as json_keyfile :
59
64
json_data = json .load (json_keyfile )
60
- if json_data .get ('type' ) != client . SERVICE_ACCOUNT :
61
- raise ValueError ('Invalid certificate file. File must contain a '
62
- '"type" field set to "{0 }".' .format (client . SERVICE_ACCOUNT ))
65
+ if json_data .get ('type' ) != self . _CREDENTIAL_TYPE :
66
+ raise ValueError ('Invalid certificate file: "{0}" . File must contain a '
67
+ '"type" field set to "{1 }".' .format (file_path , self . _CREDENTIAL_TYPE ))
63
68
self ._project_id = json_data .get ('project_id' )
64
- self ._service_account_email = json_data .get ('client_email' )
65
- try :
66
- self ._signer = crypt .Signer .from_string (json_data .get ('private_key' ))
67
- except Exception as error :
68
- raise ValueError ('Failed to parse the private key string or initialize an '
69
- 'RSA signer. Caused by: "{0}".' .format (error ))
70
69
try :
71
- self ._g_credential = client . GoogleCredentials . from_stream ( file_path )
72
- except client . ApplicationDefaultCredentialsError as error :
70
+ self ._g_credential = service_account . Credentials . from_service_account_info ( json_data )
71
+ except ValueError as error :
73
72
raise ValueError ('Failed to initialize a certificate credential from file "{0}". '
74
73
'Caused by: "{1}"' .format (file_path , error ))
75
74
@@ -79,25 +78,26 @@ def project_id(self):
79
78
80
79
@property
81
80
def signer (self ):
82
- return self ._signer
81
+ return self ._g_credential . signer
83
82
84
83
@property
85
84
def service_account_email (self ):
86
- return self ._service_account_email
85
+ return self ._g_credential . service_account_email
87
86
88
87
def get_access_token (self ):
89
88
"""Fetches a Google OAuth2 access token using this certificate credential.
90
89
91
90
Returns:
92
- oauth2client.client. AccessTokenInfo: An access token obtained via oauth2client .
91
+ AccessTokenInfo: An access token obtained using the credential .
93
92
"""
94
- return self ._g_credential .get_access_token (_http )
93
+ self ._g_credential .refresh (_request )
94
+ return AccessTokenInfo (self ._g_credential .token , self ._g_credential .expiry )
95
95
96
96
def get_credential (self ):
97
97
"""Returns the underlying Google credential.
98
98
99
99
Returns:
100
- oauth2client.client.GoogleCredentials: An oauth2client credential instance."""
100
+ google.auth.credentials.Credentials: A Google Auth credential instance."""
101
101
return self ._g_credential
102
102
103
103
@@ -108,31 +108,38 @@ def __init__(self):
108
108
"""Initializes the Application Default credentials for the current environment.
109
109
110
110
Raises:
111
- oauth2client.client.ApplicationDefaultCredentialsError : If Application Default
111
+ google.auth.exceptions.DefaultCredentialsError : If Application Default
112
112
credentials cannot be initialized in the current environment.
113
113
"""
114
114
super (ApplicationDefault , self ).__init__ ()
115
- self ._g_credential = client . GoogleCredentials . get_application_default ()
115
+ self ._g_credential , self . _project_id = google . auth . default ()
116
116
117
117
def get_access_token (self ):
118
118
"""Fetches a Google OAuth2 access token using this application default credential.
119
119
120
120
Returns:
121
- oauth2client.client. AccessTokenInfo: An access token obtained via oauth2client .
121
+ AccessTokenInfo: An access token obtained using the credential .
122
122
"""
123
- return self ._g_credential .get_access_token (_http )
123
+ self ._g_credential .refresh (_request )
124
+ return AccessTokenInfo (self ._g_credential .token , self ._g_credential .expiry )
124
125
125
126
def get_credential (self ):
126
127
"""Returns the underlying Google credential.
127
128
128
129
Returns:
129
- oauth2client.client.GoogleCredentials: An oauth2client credential instance."""
130
+ google.auth.credentials.Credentials: A Google Auth credential instance."""
130
131
return self ._g_credential
131
132
133
+ @property
134
+ def project_id (self ):
135
+ return self ._project_id
136
+
132
137
133
138
class RefreshToken (Base ):
134
139
"""A credential initialized from an existing refresh token."""
135
140
141
+ _CREDENTIAL_TYPE = 'authorized_user'
142
+
136
143
def __init__ (self , file_path ):
137
144
"""Initializes a refresh token credential from the specified JSON file.
138
145
@@ -146,41 +153,45 @@ def __init__(self, file_path):
146
153
super (RefreshToken , self ).__init__ ()
147
154
with open (file_path ) as json_keyfile :
148
155
json_data = json .load (json_keyfile )
149
- if json_data .get ('type' ) != client .AUTHORIZED_USER :
150
- raise ValueError ('Invalid refresh token file. File must contain a '
151
- '"type" field set to "{0}".' .format (client .AUTHORIZED_USER ))
152
- self ._client_id = json_data .get ('client_id' )
153
- self ._client_secret = json_data .get ('client_secret' )
154
- self ._refresh_token = json_data .get ('refresh_token' )
156
+ if json_data .get ('type' ) != self ._CREDENTIAL_TYPE :
157
+ raise ValueError ('Invalid refresh token file: "{0}". File must contain a '
158
+ '"type" field set to "{1}".' .format (file_path , self ._CREDENTIAL_TYPE ))
155
159
try :
156
- self ._g_credential = client .GoogleCredentials .from_stream (file_path )
157
- except client .ApplicationDefaultCredentialsError as error :
160
+ client_id = json_data ['client_id' ]
161
+ client_secret = json_data ['client_secret' ]
162
+ refresh_token = json_data ['refresh_token' ]
163
+ except KeyError as error :
158
164
raise ValueError ('Failed to initialize a refresh token credential from file "{0}". '
159
- 'Caused by: "{1}".' .format (file_path , error ))
165
+ 'Caused by: "{1}"' .format (file_path , error ))
166
+ self ._g_credential = credentials .Credentials (
167
+ token = None , refresh_token = refresh_token ,
168
+ token_uri = 'https://accounts.google.com/o/oauth2/token' ,
169
+ client_id = client_id , client_secret = client_secret )
160
170
161
171
@property
162
172
def client_id (self ):
163
- return self ._client_id
173
+ return self ._g_credential . client_id
164
174
165
175
@property
166
176
def client_secret (self ):
167
- return self ._client_secret
177
+ return self ._g_credential . client_secret
168
178
169
179
@property
170
180
def refresh_token (self ):
171
- return self ._refresh_token
181
+ return self ._g_credential . refresh_token
172
182
173
183
def get_access_token (self ):
174
184
"""Fetches a Google OAuth2 access token using this refresh token credential.
175
185
176
186
Returns:
177
- oauth2client.client. AccessTokenInfo: An access token obtained via oauth2client .
187
+ AccessTokenInfo: An access token obtained using the credential .
178
188
"""
179
- return self ._g_credential .get_access_token (_http )
189
+ self ._g_credential .refresh (_request )
190
+ return AccessTokenInfo (self ._g_credential .token , self ._g_credential .expiry )
180
191
181
192
def get_credential (self ):
182
193
"""Returns the underlying Google credential.
183
194
184
195
Returns:
185
- oauth2client.client.GoogleCredentials: An oauth2client credential instance."""
196
+ google.auth.credentials.Credentials: A Google Auth credential instance."""
186
197
return self ._g_credential
0 commit comments