Skip to content

Commit d51858b

Browse files
author
Jon Wayne Parrott
committed
Merge pull request #247 from GoogleCloudPlatform/appengine-identity
Adding additional app identity samples.
2 parents 92d24a7 + 5e07d5c commit d51858b

File tree

7 files changed

+179
-1
lines changed

7 files changed

+179
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runtime: python27
2+
threadsafe: yes
3+
api_version: 1
4+
5+
handlers:
6+
- url: .*
7+
script: main.app
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2015 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+
"""
16+
Sample Google App Engine application that demonstrates using the App Engine
17+
identity API to generate an auth token.
18+
19+
For more information about App Engine, see README.md under /appengine.
20+
"""
21+
22+
# [START all]
23+
import json
24+
import logging
25+
26+
from google.appengine.api import app_identity
27+
from google.appengine.api import urlfetch
28+
import webapp2
29+
30+
31+
class MainPage(webapp2.RequestHandler):
32+
def get(self):
33+
auth_token, _ = app_identity.get_access_token(
34+
'https://www.googleapis.com/auth/cloud-platform')
35+
logging.info(
36+
'Using token {} to represent identity {}'.format(
37+
auth_token, app_identity.get_service_account_name()))
38+
39+
response = urlfetch.fetch(
40+
'https://www.googleapis.com/storage/v1/b?project={}'.format(
41+
app_identity.get_application_id()),
42+
method=urlfetch.GET,
43+
headers={
44+
'Authorization': 'Bearer {}'.format(auth_token)
45+
}
46+
)
47+
48+
if response.status_code != 200:
49+
raise Exception(
50+
'Call failed. Status code {}. Body {}'.format(
51+
response.status_code, response.content))
52+
53+
result = json.loads(response.content)
54+
self.response.headers['Content-Type'] = 'application/json'
55+
self.response.write(json.dumps(result, indent=2))
56+
57+
app = webapp2.WSGIApplication([
58+
('/', MainPage)
59+
], debug=True)
60+
61+
# [END all]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
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+
import main
16+
import mock
17+
import webtest
18+
19+
20+
def test_app(testbed):
21+
app = webtest.TestApp(main.app)
22+
23+
with mock.patch('main.urlfetch.fetch') as fetch_mock:
24+
result_mock = mock.Mock()
25+
result_mock.status_code = 200
26+
result_mock.content = '{}'
27+
fetch_mock.return_value = result_mock
28+
29+
response = app.get('/')
30+
assert response.status_int == 200
31+
assert fetch_mock.called
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
runtime: python27
2+
threadsafe: yes
3+
api_version: 1
4+
5+
handlers:
6+
- url: .*
7+
script: main.app
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2015 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+
"""
16+
Sample Google App Engine application that demonstrates usage of the app
17+
engine inbound app ID header.
18+
19+
For more information about App Engine, see README.md under /appengine.
20+
"""
21+
22+
# [START all]
23+
import webapp2
24+
25+
26+
class MainPage(webapp2.RequestHandler):
27+
allowed_app_ids = [
28+
'other-app-id',
29+
'other-app-id-2'
30+
]
31+
32+
def get(self):
33+
incoming_app_id = self.request.headers.get(
34+
'X-Appengine-Inbound-Appid', None)
35+
36+
if incoming_app_id not in self.allowed_app_ids:
37+
self.abort(403)
38+
39+
self.response.write('This is a protected page.')
40+
41+
app = webapp2.WSGIApplication([
42+
('/', MainPage)
43+
], debug=True)
44+
45+
# [END all]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
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+
import main
16+
import webtest
17+
18+
19+
def test_app(testbed):
20+
app = webtest.TestApp(main.app)
21+
22+
response = app.get('/', status=403)
23+
24+
response = app.get('/', headers={
25+
'X-Appengine-Inbound-Appid': 'other-app-id'
26+
})
27+
assert response.status_int == 200

appengine/app_identity/signing/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""
1616
Sample Google App Engine application that demonstrates usage of the app
17-
identity API.
17+
identity API to sign bytes and verify signatures.
1818
1919
For more information about App Engine, see README.md under /appengine.
2020
"""

0 commit comments

Comments
 (0)