如何检查Google Cloud Endpoints中的服务到服务身份验证? [英] How to check service-to-service authentication in Google Cloud Endpoints?

查看:220
本文介绍了如何检查Google Cloud Endpoints中的服务到服务身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个应用程序中将一个庞大的Google App Engine应用程序(使用Python和标准环境)拆分为多个服务。默认服务是使用其他服务中的端点框架实现的调用API。



除了我不了解如何正确检查默认服务的身份验证(以及使它在本地开发服务器和生产环境中都能正常工作)。



调用我使用的服务 google-api-python-client 和默认应用程序凭证。

  from googleapiclient.discovery import build $ b $ from oauth2client.client import GoogleCredentials 
service = build(
name,version,
credentials = GoogleCredentials.get_application_default(),
discoveryServiceUrl = discovery_url)
service.client_token()。execute()

我的服务API代码如下所示

  @ endpoints.api(
name ='test',
version ='v1',

class TestApi(remote。服务):

@ endpoints.method(
message_types.VoidMessage,
TestResponse,
path ='test',
http_method ='GET',
name ='test')
def get_test(self,请求):
#user = endpoints.get_current_user()
#如果不是用户:
#raise endpoints.UnauthorizedException
Test TestResponse(test ='test')

在生产中 endpoints.get_current_user()似乎返回正确应用程序用户,但我不知道如何正确验证它是同一个应用程序。在本地开发环境中 endpoints.get_current_user()返回 None

解决方案

你做错了。您正在定义用户,但未使用它。



以下示例问候通过个性化消息和注销链接登录应用的用户。如果用户未登录,该应用会提供指向Google帐户登录页面的链接。



如果您使用Google的 .appengine.api导入用户模块:

  def get(self):
user = users.get_current_user()
如果用户:
nickname = user.nickname()
logout_url = users.create_logout_url('/')
greeting ='欢迎,{} (< a href ={}>注销< / a>)'。format(nickname,logout_url)
else:
login_url = users.create_login_url('/')$ b $格式(login_url)

self.response.write('< html>< body>'< {}< / body>< / html>'格式(问候))

一个用户你仍然需要检查它是否为空。加用户存储不同的值。所以你只需要打电话给他们并定义他们。

如果你有页面需要用户登录才能访问,你可以强制执行此操作在您的 app.yaml 文件中。



默认情况下,您的应用将使用Google帐户进行身份验证。要选择其他选项,例如Google Apps域,请转到设置页面,然后点击编辑。在Google身份验证下拉菜单中,选择所需的身份验证类型,然后点击保存






然而,您也可以使用 Tipfy 框架。


I'm trying to split a monolith Google App Engine application (using Python & standard environment) into several services within one application. Default service is calling API implemented using the Endpoints framework in another service.

Everything works nicely except that I don't understand how to correctly check authentication of the default service (and make it work both in local development server and in production).

To call the service I'm using google-api-python-client and default application credentials.

from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials
service = build(
    name, version,
    credentials=GoogleCredentials.get_application_default(),
    discoveryServiceUrl=discovery_url)
service.client_token().execute()

My service API code looks like the following

@endpoints.api(
    name='test',
    version='v1',
)
class TestApi(remote.Service):

    @endpoints.method(
        message_types.VoidMessage,
        TestResponse,
        path='test',
        http_method='GET',
        name='test')
    def get_test(self, request):
        # user = endpoints.get_current_user()
        # if not user:
        #     raise endpoints.UnauthorizedException
        return TestResponse(test='test')

In production endpoints.get_current_user() seems to return a correct application user, but I don't know how to correctly validate that it's the same application. In local development environment endpoints.get_current_user() returns None.

解决方案

You're doing it wrong. You're defining user, but not using it.

The following example greets a user who has signed in to the app with a personalized message and a link to sign out. If the user is not signed in, the app offers a link to the sign-in page for Google Accounts.

If you use the from google.appengine.api import users module:

def get(self):
    user = users.get_current_user()
    if user:
        nickname = user.nickname()
        logout_url = users.create_logout_url('/')
        greeting = 'Welcome, {}! (<a href="{}">sign out</a>)'.format(nickname, logout_url)
    else:
        login_url = users.create_login_url('/')
        greeting = '<a href="{}">Sign in</a>'.format(login_url)

    self.response.write('<html><body>{}</body></html>'.format(greeting))

When creating a user you still need to check if it's empty or not. Plus user stores different values. So you just need to make a call to them and define them.

If you have pages that require the user to be signed in in order to access, you can enforce this in your app.yaml file.

By default, your app will use Google Accounts for authentication. To choose another option, such as Google Apps domain, go to the settings page for your project in the Google Cloud Platform Console and click Edit. In the Google authentication dropdown menu, select the desired authentication type, and then click Save.


You could however also use the Tipfy framework.

这篇关于如何检查Google Cloud Endpoints中的服务到服务身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆