How to transfer user information from Authenticator for App Engine to an endpoint?

I refer to @MinWan's wonderful answer to this Google Cloud Endpoints post and user authentication , where he describes how to add custom headers to a request against the App Engine Endpoints cloud.

It is clear that we can add a custom header and write an authenticator for each service (for example, Google, Twitter, Facebook), against which we want to confirm where each authenticator reads a specific header and authenticates against this service. If the token is valid, the service usually returns a response with an email address or user ID, as well as additional information [A] from which we create com.google.api.server.spi.auth.common.User, which is later passed to the final method points like com.google.appengine.api.users.User.

First question: why do we have two different user objects, for example. users with different namespaces? These are apparently not sub / superclasses, so they are probably clearly hidden behind the scenes.

The second question: the problem associated with the explicit nature of the user, and that there is no special field where I could add additional information [A] returned by the service, is that additional information is lost. Such additional information may be useful for mapping the user of an external oauth2 service to a local user or to oauth2 users returned by other services.

Any input? What is the proposed way to handle multiple authentication services?

+12
authentication google-app-engine google-oauth facebook-authentication google-cloud-endpoints
Feb 11 '15 at 3:00
source share
2 answers

Just tested, and you can definitely subclass User so that it contains any private fields you want. Just use class inheritance polymorphism to return an object of this type from the Authenticator method without changing the default user type in the method signature.

import javax.servlet.http.HttpServletRequest; import com.google.api.server.spi.auth.common.User; import com.google.api.server.spi.config.Authenticator; public class BazUser extends User { private String secret; // extra piece of data held by this User public BazUser(String email) { super(email); this.secret = "notasecret"; } public BazUser (String email, String secret) { super (email); this.secret = secret; } } public class BazAuthenticator implements Authenticator { public User authenticate(HttpServletRequest req) { return new BazUser ("userid@baz.com", "secret"); } } 
+6
Nov 11 '15 at 18:02
source share

Functionally, everything works with:

 import com.google.api.server.spi.auth.common.User; 

even with gradle:

 compile 'com.google.endpoints:endpoints-framework:2.0.0-beta.11' 

The IDE warning can be cleared by including @SuppressWarnings("ResourceParameter") as follows:

 /** * Adds a new PmpUser. * * @param pmpUser pmpUser object */ @SuppressWarnings("ResourceParameter") @ApiMethod( name = "pmpUser.post", path = "pmpUser", httpMethod = ApiMethod.HttpMethod.POST) ... 
+1
Mar 25 '17 at 21:40
source share



All Articles