Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling SSL on Flask + Google App Engine

I'm trying to implement some Google APIs in my Flask/Google App Engine web app.

I'm currently stuck on the authentication part as oauth2 redirect URI requires the connection to be secure and I cannot do that with Flask since it's the GAE environment that starts my flask app and I cannot do as suggested from the Flask documentation here

Do you know how to enforce HTTPS on a Flask application ran by Google App Engine?

like image 618
venturieffect Avatar asked Apr 03 '17 15:04

venturieffect


2 Answers

In addition to Justin's answer which works well for app engine apps on the flexible runtime, it might be 'simpler' to just use the secure option or attribute on your url handler in your app.yaml file since you're running in standard.

For example:

- url: .*
  script: main.app  # the flask app handling requests
  secure: always

This will force all requests to use https on the appspot domain.

Note that if you decide to add a custom domain later, you'll need to provide valid https certificates yourself to handle https properly

like image 141
Jeffrey Godwyll Avatar answered Sep 18 '22 16:09

Jeffrey Godwyll


You need to use a library that forces redirects based on the x-forwarded-proto header. Take a look at this:

https://github.com/GoogleCloudPlatform/flask-talisman

like image 28
Justin Beckwith Avatar answered Sep 18 '22 16:09

Justin Beckwith