Skip to content

Commit cb3177d

Browse files
authored
Merge pull request #23 from auth0-samples/update-endpoints
Update endpoints, return JSON resposne
2 parents 5d3184f + 12f1c63 commit cb3177d

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

00-Starter-Seed/server.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from six.moves.urllib.request import urlopen
88

99
from dotenv import load_dotenv, find_dotenv
10-
from flask import Flask, request, jsonify, _app_ctx_stack
10+
from flask import Flask, request, jsonify, _request_ctx_stack
1111
from flask_cors import cross_origin
1212
from jose import jwt
1313

@@ -131,42 +131,48 @@ def decorated(*args, **kwargs):
131131
"Unable to parse authentication"
132132
" token."}, 400)
133133

134-
_app_ctx_stack.top.current_user = payload
134+
_request_ctx_stack.top.current_user = payload
135135
return f(*args, **kwargs)
136136
raise AuthError({"code": "invalid_header",
137137
"description": "Unable to find appropriate key"}, 400)
138138
return decorated
139139

140140

141141
# Controllers API
142-
@APP.route("/ping")
142+
@APP.route("/api/public")
143143
@cross_origin(headers=["Content-Type", "Authorization"])
144-
def ping():
144+
def public():
145145
"""No access token required to access this route
146146
"""
147-
return "All good. You don't need to be authenticated to call this"
147+
response = "All good. You don't need to be authenticated to call this"
148+
return jsonify(message=response)
148149

149150

150-
@APP.route("/secured/ping")
151+
@APP.route("/api/private")
151152
@cross_origin(headers=["Content-Type", "Authorization"])
152153
@cross_origin(headers=["Access-Control-Allow-Origin", "*"])
153154
@requires_auth
154-
def secured_ping():
155+
def private():
155156
"""A valid access token is required to access this route
156157
"""
157-
return "All good. You only get this message if you're authenticated"
158+
response = "All good. You only get this message if you're authenticated"
159+
return jsonify(message=response)
158160

159161

160-
@APP.route("/secured/private/ping")
162+
@APP.route("/api/private-scoped")
161163
@cross_origin(headers=["Content-Type", "Authorization"])
162164
@cross_origin(headers=["Access-Control-Allow-Origin", "*"])
163165
@requires_auth
164-
def secured_private_ping():
166+
def private_scoped():
165167
"""A valid access token and an appropriate scope are required to access this route
166168
"""
167-
if requires_scope("read:agenda"):
168-
return "All good. You're authenticated and the access token has the appropriate scope"
169-
return "You don't have access to this resource"
169+
if requires_scope("read:messages"):
170+
response = "All good. You're authenticated and the access token has the appropriate scope"
171+
return jsonify(message=response)
172+
raise AuthError({
173+
"code": "Anauthorized",
174+
"desciption": "You don't have access to this resource"
175+
}, 403)
170176

171177

172178
if __name__ == "__main__":

0 commit comments

Comments
 (0)