13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.rom googleapiclient import discovery
15
15
16
- """Use a service account to access an IAP protected resource"""
16
+ """Use a service account and key file to access an IAP protected resource"""
17
17
18
18
19
19
def get_private_key (json_file_name ):
@@ -25,7 +25,6 @@ def get_private_key(json_file_name):
25
25
Returns:
26
26
The private key from the file
27
27
"""
28
-
29
28
import json
30
29
31
30
with open (json_file_name , 'r' ) as f :
@@ -41,7 +40,6 @@ def build_claim(client_id, service_account):
41
40
client_id: the OAuth client ID. Available from API/Credentials console
42
41
service_account: the service account email
43
42
44
-
45
43
Returns:
46
44
The claim
47
45
"""
@@ -65,14 +63,14 @@ def create_assertion(claim, private_key):
65
63
"""Creates an assertion - a signed claim of authorization
66
64
67
65
Args:
68
- claim: the claim to send to the OAuth2 service (from build_claim)
66
+ claim: the claim to send to the OAuth2 service
69
67
private_key: the service account's private key (in PEM format)
70
68
71
69
Returns:
72
70
The assertion
73
71
"""
74
72
import jwt
75
-
73
+
76
74
assertion = jwt .encode (
77
75
claim ,
78
76
private_key ,
@@ -86,13 +84,12 @@ def get_id_token(claim, private_key):
86
84
"""Gets an OpenID Connect token for the given private key
87
85
88
86
Args:
89
- claim: the claim to send to the OAuth2 service (from build_claim)
87
+ claim: the claim to send to the OAuth2 service
90
88
private_key: the service account's private key (in PEM format)
91
89
92
90
Returns:
93
91
An OpenID connect token to authenticate requests from the service acct
94
92
"""
95
-
96
93
import json
97
94
import requests
98
95
@@ -109,8 +106,6 @@ def get_id_token(claim, private_key):
109
106
}
110
107
)
111
108
112
- print (response .text )
113
-
114
109
id_token = response .json ()['id_token' ]
115
110
return id_token
116
111
@@ -131,7 +126,6 @@ def request(client_id, service_account, private_key, method, url, **kwargs):
131
126
Returns:
132
127
The requests Response object from the request
133
128
"""
134
-
135
129
import requests
136
130
137
131
# Add Authorization header using service account and client information
@@ -147,24 +141,21 @@ def request(client_id, service_account, private_key, method, url, **kwargs):
147
141
148
142
149
143
def main ():
150
- """Make a GET request to the IAP-protected URL using service account creds
144
+ """Make a GET request to the IAP-protected URL using service account key
151
145
"""
152
146
153
147
import argparse
154
148
155
149
parser = argparse .ArgumentParser (
156
150
description = 'Call IAP protected resource with service account'
157
151
)
158
-
159
152
parser .add_argument ('client_id' , help = "The protected site's client ID" )
160
153
parser .add_argument ('service_account' , help = "The service account's email" )
161
154
parser .add_argument ('key_file' , help = "The service account's key file" )
162
155
parser .add_argument ('url' , help = "URL to access" )
163
-
164
156
args = parser .parse_args ()
165
157
166
158
private_key = get_private_key (args .key_file )
167
-
168
159
response = request (
169
160
args .client_id , args .service_account , private_key , 'GET' , args .url
170
161
)
0 commit comments