使用自定义身份验证器时验证AppEngine端点客户端ID

时间:2016-05-24 13:26:43

标签: google-app-engine google-cloud-endpoints

之前我们的客户端应用使用了Google登录。

现在我们正在转向自定义身份验证,因为我们计划将用户的电话号码作为唯一身份(而不是Google帐户)。但是在实现自定义Authenticator之后,客户端ID没有被检查,我可以从任何地方进行API调用。

当仅在客户端使用Google登录时,客户端ID正在验证,我无法从授权的客户端之外的任何客户端进行API调用。

如何在使用自定义身份验证器时验证客户端ID?

Api端点代码

@Api(name = "apiSubscriber",
        clientIds = {
        Constants.webClientId,
        Constants.androidClientId,
        Constants.iOSClientId
        },

        authenticators = {com.google.api.server.spi.auth.EndpointsAuthenticator.class, 
        CustomAuth.class},
        audiences = {Constants.androidAudience},
     )

     public class ApiSubscriber {

            @ApiMethod
            public Subscriber getSubscriberData(User user){

                if(user!=null){
                //fetches subscriber data
                }

            }

        //... Other ApiMethods

     }

自定义身份验证器代码

public class CustomAuth implements Authenticator {


    @Override
    public User authenticate(HttpServletRequest request) {

         String phoneNumber = request.getHeader("phoneNumber");
         String token = request.getHeader("Authorization");

         if(checkToken(phoneNumber,token)){
                return new User(phoneNumber);
         }

         return null;
    }

    private boolean checkToken(String phoneNumber, String token){
        //Checks if authorization token is valid
    }


}

2 个答案:

答案 0 :(得分:1)

此时不幸的是,您似乎无法将Endpoints API限制为客户端,也不会使用Google登录。

使用Google的oAuth2身份验证时,会发生一些神奇的伏都教(不完全确定是什么),并且应用程序仅限于您指定的ClientId。

然而,当你停止使用那种身份验证方法时,我发现(令我非常失望的是),它不再起作用了。

在这里查看我的问题,您可以在其中阅读有关我的测试以及可能为您提供更多信息的其他一些内容:Authenticating your client to Cloud Endpoints without a Google Account login

答案 1 :(得分:0)

我不确定这是一个问题,但您提供的代码中存在一些错误。

 authenticators = {com.google.api.server.spi.auth.EndpointsAuthenticator.class, 
    CustomAuth.class,

而不是逗号必须是括号。另外,imho,这里只需要CustomAuth类。

audiences = {Constants.androidAudience},

逗号是多余的。

二。您不需要使用自定义身份验证器。您可以将令牌和电话号码作为连接参数或两个参数发送到您的服务方法并在那里进行检查。

相关问题