Stangoesagain
Stangoesagain

Reputation: 75

How to grant access to google appengine page in Python?

I have a simple app that serves a page, I want this page to be accessible only to a couple of predetermined users already signed into Google. In fact, I want access only through importHTML function in Google Spreadsheets.

How to implement this with minimum fuss? Here's the app:

import webapp2

class MainPage(webapp2.RequestHandler):
  def get(self):
    self.response.out.write('/Statement.htm')

app = webapp2.WSGIApplication([('/Statement.htm', MainPage)],
                              debug=True)

Here's the app.yaml

application: *********
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  static_files: Statement.htm
  upload: Statement.htm

I've seen this in the tutorial:

from google.appengine.api import users

class MainPage(webapp2.RequestHandler):

        user = users.get_current_user()

But who is this "get_current_user"? I don't want any logins, I just want it to be some kind of Google ID that I can check with if/else and serve out error page if it doesn't match one or two allowed names.

Upvotes: 0

Views: 321

Answers (3)

Stangoesagain
Stangoesagain

Reputation: 75

Since importHTML function does not seem to provide for any kind of authorization I simply changed url handler to a really long string of random numbers (actually passphrase encoded as SHA1).

To the jinja based solution above I added a handler in app.yaml like so

- url: /a...................6/.*
  script: spreadsheet.app

Where spreadsheet.app points to a spreadsheet.py which is the same as statement.py except for a different url string in webapp2 call and a new class name.

Now people accessing through browser need to be authorized as admin while the long link exists only inside a cell of a google spreadsheet document and so is not left in browser history or anywhere.

I hope it would also not be indexed by Google and not appear in search results.

Maybe not the perfect solution but a passable workaround.

Upvotes: 0

doru
doru

Reputation: 9110

You should give them admin permission from your google app engine dashboard - under Administration -> Permissions - and add to your app.yaml file:

EDIT:

This is a way to do it using jinja2 template system.

app.yaml file:

application: statement
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /statement
  script: statement.app
  login: admin

libraries:
- name: jinja2
  version: latest

statement.py file:

import webapp2
import jinja2
import os

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class Statement(webapp2.RequestHandler):
    def get(self):
        template = jinja_environment.get_template('Statement.htm')
        self.response.out.write(template.render())



app = webapp2.WSGIApplication([('/statement', Statement)],
                              debug=True)

Statement.htm file:

You must have been granted admin permission.

When you go to http://127.0.0.1:8080/statement you must login as admin to get there. All three files are in the same directory. If your python main file has other name you should use that name and the extension .app in the app.yaml file:

script: your-python-file-name.app

Upvotes: 1

T. Steinrücken
T. Steinrücken

Reputation: 469

Put "login: admin" into your app.yaml and add these user as viewer to the application. See https://developers.google.com/appengine/docs/python/config/appconfig#Requiring_Login_or_Administrator_Status.

Upvotes: 0

Related Questions