| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from google.appengine.api import oauth |
| 6 from google.appengine.api import users |
| 5 from google.appengine.api.app_identity import app_identity | 7 from google.appengine.api.app_identity import app_identity |
| 6 | 8 |
| 7 | 9 |
| 8 _EMAIL_SCOPE = 'https://www.googleapis.com/auth/userinfo.email' | 10 _EMAIL_SCOPE = 'https://www.googleapis.com/auth/userinfo.email' |
| 9 | 11 |
| 10 | 12 |
| 11 def GetAuthToken(scope=_EMAIL_SCOPE): # pragma: no cover | 13 def GetAuthToken(scope=_EMAIL_SCOPE): # pragma: no cover |
| 12 """Gets auth token for requests to hosts that need authorizations.""" | 14 """Gets auth token for requests to hosts that need authorizations.""" |
| 13 auth_token, _ = app_identity.get_access_token(scope) | 15 auth_token, _ = app_identity.get_access_token(scope) |
| 14 return auth_token | 16 return auth_token |
| 17 |
| 18 |
| 19 def GetUserEmail(scope=_EMAIL_SCOPE): # pragma: no cover |
| 20 """Returns the email of the logged-in user or None if not logged-in.""" |
| 21 user = users.get_current_user() # Cookie-based authentication. |
| 22 if not user: |
| 23 try: |
| 24 user = oauth.get_current_user(scope) # Oauth-based authentication. |
| 25 except oauth.OAuthRequestError: |
| 26 pass # Not logged-in or invalid oauth token. |
| 27 return user.email() if user else None |
| 28 |
| 29 |
| 30 def IsCurrentUserAdmin(scope=_EMAIL_SCOPE): # pragma: no cover |
| 31 """Returns True if the logged-in user is an admin.""" |
| 32 is_admin = users.is_current_user_admin() |
| 33 try: |
| 34 is_admin = is_admin or oauth.is_current_user_admin(scope) |
| 35 except oauth.OAuthRequestError: |
| 36 pass # Not logged-in or invalid oauth token. |
| 37 return is_admin |
| OLD | NEW |