1
1
"""Firebase credentials module."""
2
2
import json
3
- import sys
4
3
5
4
import httplib2
6
5
@@ -41,24 +40,28 @@ def __init__(self, file_path):
41
40
42
41
Raises:
43
42
IOError: If the specified file doesn't exist or cannot be read.
44
- ValueError: If an error occurs while parsing the file content .
43
+ ValueError: If the certificate file is invalid .
45
44
"""
46
45
super (Certificate , self ).__init__ ()
47
46
# TODO(hkj): Clean this up once we are able to take a dependency
48
47
# TODO(hkj): on latest oauth2client.
49
48
with open (file_path ) as json_keyfile :
50
49
json_data = json .load (json_keyfile )
50
+ if json_data .get ('type' ) != client .SERVICE_ACCOUNT :
51
+ raise ValueError ('Invalid certificate file. File must contain a '
52
+ '"type" field set to "{0}".' .format (client .SERVICE_ACCOUNT ))
51
53
self ._project_id = json_data .get ('project_id' )
54
+ self ._service_account_email = json_data .get ('client_email' )
52
55
try :
53
- self ._signer = crypt .Signer .from_string (
54
- json_data .get ('private_key' ))
56
+ self ._signer = crypt .Signer .from_string (json_data .get ('private_key' ))
55
57
except Exception as error :
56
- err_type , err_value , err_traceback = sys .exc_info ()
57
- err_message = 'Failed to parse the private key string: {0}' .format (
58
- error )
59
- raise ValueError , (err_message , err_type , err_value ), err_traceback
60
- self ._service_account_email = json_data .get ('client_email' )
61
- self ._g_credential = client .GoogleCredentials .from_stream (file_path )
58
+ raise ValueError ('Failed to parse the private key string or initialize an '
59
+ 'RSA signer. Caused by: "{0}".' .format (error ))
60
+ try :
61
+ self ._g_credential = client .GoogleCredentials .from_stream (file_path )
62
+ except client .ApplicationDefaultCredentialsError as error :
63
+ raise ValueError ('Failed to initialize a certificate credential from file "{0}". '
64
+ 'Caused by: "{1}"' .format (file_path , error ))
62
65
63
66
@property
64
67
def project_id (self ):
@@ -77,3 +80,70 @@ def get_access_token(self):
77
80
78
81
def get_credential (self ):
79
82
return self ._g_credential
83
+
84
+
85
+ class ApplicationDefault (Base ):
86
+ """A Google Application Default credential."""
87
+
88
+ def __init__ (self ):
89
+ """Initializes the Application Default credentials for the current environment.
90
+
91
+ Raises:
92
+ oauth2client.client.ApplicationDefaultCredentialsError: If Application Default
93
+ credentials cannot be initialized in the current environment.
94
+ """
95
+ super (ApplicationDefault , self ).__init__ ()
96
+ self ._g_credential = client .GoogleCredentials .get_application_default ()
97
+
98
+ def get_access_token (self ):
99
+ return self ._g_credential .get_access_token (_http )
100
+
101
+ def get_credential (self ):
102
+ return self ._g_credential
103
+
104
+
105
+ class RefreshToken (Base ):
106
+ """A credential initialized from an existing refresh token."""
107
+
108
+ def __init__ (self , file_path ):
109
+ """Initializes a refresh token credential from the specified JSON file.
110
+
111
+ Args:
112
+ file_path: File path to a refresh token JSON file.
113
+
114
+ Raises:
115
+ IOError: If the specified file doesn't exist or cannot be read.
116
+ ValueError: If the refresh token file is invalid.
117
+ """
118
+ super (RefreshToken , self ).__init__ ()
119
+ with open (file_path ) as json_keyfile :
120
+ json_data = json .load (json_keyfile )
121
+ if json_data .get ('type' ) != client .AUTHORIZED_USER :
122
+ raise ValueError ('Invalid refresh token file. File must contain a '
123
+ '"type" field set to "{0}".' .format (client .AUTHORIZED_USER ))
124
+ self ._client_id = json_data .get ('client_id' )
125
+ self ._client_secret = json_data .get ('client_secret' )
126
+ self ._refresh_token = json_data .get ('refresh_token' )
127
+ try :
128
+ self ._g_credential = client .GoogleCredentials .from_stream (file_path )
129
+ except client .ApplicationDefaultCredentialsError as error :
130
+ raise ValueError ('Failed to initialize a refresh token credential from file "{0}". '
131
+ 'Caused by: "{1}".' .format (file_path , error ))
132
+
133
+ @property
134
+ def client_id (self ):
135
+ return self ._client_id
136
+
137
+ @property
138
+ def client_secret (self ):
139
+ return self ._client_secret
140
+
141
+ @property
142
+ def refresh_token (self ):
143
+ return self ._refresh_token
144
+
145
+ def get_access_token (self ):
146
+ return self ._g_credential .get_access_token (_http )
147
+
148
+ def get_credential (self ):
149
+ return self ._g_credential
0 commit comments