@Override public Identity identity() throws IOException { Identity identity = Identity.ANONYMOUS; final boolean authed = this.resource.uriInfo() .getQueryParameters().containsKey(Google.STATE) && this.resource.uriInfo().getQueryParameters() .getFirst(Google.STATE).equals(Google.FLAG); if (authed) { final List<String> code = this.resource.uriInfo() // @checkstyle MultipleStringLiterals (1 line) .getQueryParameters().get("code"); if (code == null || code.isEmpty()) { throw new WebApplicationException( new IllegalArgumentException( "HTTP query parameter 'code' is mandatory" ), HttpURLConnection.HTTP_BAD_REQUEST ); } identity = this.fetch(this.token(code.get(0))); } return identity; }
/** * Get user name from Google, by the code provided. * @param token Google access token * @return The user found in Google * @throws IOException If fails */ private Identity fetch(final String token) throws IOException { final URI uri = UriBuilder .fromPath("https://www.googleapis.com/oauth2/v1/userinfo") .queryParam("alt", "json") .queryParam("access_token", "{token}") .build(token); return this.parse( new JdkRequest(uri).fetch() .as(JsonResponse.class).json() .readObject() ); }
/** * Authentication inset. * @return The inset */ @Inset.Runtime public final AuthInset auth() { return new AuthInset(this, Manifests.read("Stateful-SecurityKey")) .with(new Auth(this, this.base())) // @checkstyle LineLength (3 lines) .with(new Facebook(this, Manifests.read("Stateful-FbId"), Manifests.read("Stateful-FbSecret"))) .with(new Google(this, Manifests.read("Stateful-GoogleId"), Manifests.read("Stateful-GoogleSecret"))) .with(new Github(this, Manifests.read("Stateful-GithubId"), Manifests.read("Stateful-GithubSecret"))) .with(BaseRs.TESTER); }