Skip to content

Commit 15b7063

Browse files
author
Jon Wayne Parrott
authored
Adding firebase auth to endpoints sample, fixes #396 (#400)
1 parent a74a20f commit 15b7063

File tree

4 files changed

+126
-5
lines changed

4 files changed

+126
-5
lines changed

appengine/flexible/endpoints/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import logging
2424

2525
from flask import Flask, jsonify, request, send_from_directory
26+
from flask_cors import cross_origin
2627
from six.moves import http_client
2728
import yaml
2829

@@ -76,6 +77,13 @@ def auth_info_google_id_token():
7677
return auth_info()
7778

7879

80+
@app.route('/auth/info/firebase', methods=['GET'])
81+
@cross_origin(send_wildcard=True)
82+
def auth_info_firebase():
83+
"""Auth info with Firebase auth."""
84+
return auth_info()
85+
86+
7987
@app.errorhandler(http_client.INTERNAL_SERVER_ERROR)
8088
def unexpected_error(e):
8189
"""Handle exceptions by returning swagger-compliant json."""
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright 2016 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 base64
16+
import json
17+
import os
18+
19+
import main
20+
import pytest
21+
22+
23+
@pytest.fixture
24+
def client(monkeypatch):
25+
monkeypatch.chdir(os.path.dirname(main.__file__))
26+
main.app.testing = True
27+
client = main.app.test_client()
28+
return client
29+
30+
31+
def test_index(client):
32+
r = client.get('/')
33+
assert r.status_code == 200
34+
35+
36+
def test_api_docs(client):
37+
r = client.get('/api-docs')
38+
assert r.status_code == 200
39+
40+
41+
def test_echo(client):
42+
r = client.post(
43+
'/echo',
44+
data='{"message": "Hello"}',
45+
headers={
46+
'Content-Type': 'application/json'
47+
})
48+
49+
assert r.status_code == 200
50+
data = json.loads(r.data.decode('utf-8'))
51+
assert data['message'] == 'Hello'
52+
53+
54+
def test_auth_info(client):
55+
endpoints = [
56+
'/auth/info/googlejwt',
57+
'/auth/info/googleidtoken',
58+
'/auth/info/firebase']
59+
60+
encoded_info = base64.b64encode(json.dumps({
61+
'id': '123'
62+
}).encode('utf-8'))
63+
64+
for endpoint in endpoints:
65+
r = client.get(
66+
endpoint,
67+
headers={
68+
'Content-Type': 'application/json'
69+
})
70+
71+
assert r.status_code == 200
72+
data = json.loads(r.data.decode('utf-8'))
73+
assert data['id'] == 'anonymous'
74+
75+
r = client.get(
76+
endpoint,
77+
headers={
78+
'Content-Type': 'application/json',
79+
'X-Endpoint-API-UserInfo': encoded_info
80+
})
81+
82+
assert r.status_code == 200
83+
data = json.loads(r.data.decode('utf-8'))
84+
assert data['id'] == '123'
85+
86+
87+
def test_cors(client):
88+
r = client.options(
89+
'/auth/info/firebase', headers={'Origin': 'example.com'})
90+
assert r.status_code == 200
91+
assert r.headers['Access-Control-Allow-Origin'] == '*'

appengine/flexible/endpoints/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Flask==0.11.1
2+
flask-cors==2.1.2
23
gunicorn==19.6.0
34
gcloud==0.17.0
45
six==1.10.0

appengine/flexible/endpoints/swagger.yaml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ paths:
3030
required: true
3131
schema:
3232
$ref: "#/definitions/echoMessage"
33+
security:
34+
- api_key: []
3335
"/auth/info/googlejwt":
3436
get:
3537
description: "Returns the requests' authentication information."
@@ -38,7 +40,7 @@ paths:
3840
- "application/json"
3941
responses:
4042
200:
41-
description: "Authenication info."
43+
description: "Authentication info."
4244
schema:
4345
$ref: "#/definitions/authInfoResponse"
4446
x-security:
@@ -55,7 +57,7 @@ paths:
5557
- "application/json"
5658
responses:
5759
200:
58-
description: "Authenication info."
60+
description: "Authentication info."
5961
schema:
6062
$ref: "#/definitions/authInfoResponse"
6163
x-security:
@@ -64,6 +66,21 @@ paths:
6466
# Your OAuth2 client's Client ID must be added here. You can add
6567
# multiple client IDs to accept tokens from multiple clients.
6668
- "YOUR-CLIENT-ID"
69+
"/auth/info/firebase":
70+
get:
71+
description: "Returns the requests' authentication information."
72+
operationId: "authInfoFirebase"
73+
produces:
74+
- "application/json"
75+
responses:
76+
200:
77+
description: "Authentication info."
78+
schema:
79+
$ref: "#/definitions/authInfoResponse"
80+
x-security:
81+
- firebase:
82+
audiences:
83+
- "YOUR-PROJECT-ID"
6784
definitions:
6885
echoMessage:
6986
properties:
@@ -75,9 +92,6 @@ definitions:
7592
type: "string"
7693
email:
7794
type: "string"
78-
# This section requires all requests to any path to require an API key.
79-
security:
80-
- api_key: []
8195
securityDefinitions:
8296
# This section configures basic authentication with an API key.
8397
api_key:
@@ -104,3 +118,10 @@ securityDefinitions:
104118
type: "oauth2"
105119
x-issuer: "accounts.google.com"
106120
x-jwks_uri: "https://www.googleapis.com/oauth2/v1/certs"
121+
# This section configures authentication using Firebase Auth.
122+
firebase:
123+
authorizationUrl: ""
124+
flow: "implicit"
125+
type: "oauth2"
126+
x-issuer: "https://securetoken.google.com/YOUR-PROJECT-ID"
127+
x-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/[email protected]"

0 commit comments

Comments
 (0)