AbstractOAuth2Authenticator implements IAuthenticator { @Override public final Response delegate(final Authenticator authenticator, final URI callback) { validate(authenticator); if (callback == null) { throw new ServerErrorException(); } String state = HttpUtil.parseQueryParams(callback) .getFirst("state"); if (Strings.isNullOrEmpty(state)) { throw new ServerErrorException(); } URI redirect = buildRedirect(callback); String clientId = getClientId(authenticator); UriBuilder b = UriBuilder .fromUri(getAuthEndpoint()) .queryParam("client_id", clientId) .queryParam("redirect_uri", redirect) .queryParam("response_type", "code") .queryParam("state", state) .queryParam("scope", getScopes()); return Response.status(Status.FOUND) .location(b.build()) .build(); }  final Client getClient(); final void setClient(final Client client); final Session getSession(); final void setSession(final Session session); @Override final Response delegate(final Authenticator authenticator,
                                   final URI callback); @Override final void validate(final Authenticator authenticator); @Override final UserIdentity authenticate(final Authenticator authenticator,
                                           final MultivaluedMap<String, String>
                                                   parameters,
                                           final URI callback); static final GenericType<HashMap<String, String>> MAP_TYPE; static final String CLIENT_ID_KEY; static final String CLIENT_SECRET_KEY; }
@Test public void testDelegate() { Response r = authenticator.delegate(config, validCallback); assertEquals(302, r.getStatus()); String location = r.getHeaderString(HttpHeaders.LOCATION); URI redirect = URI.create(location); assertEquals("example.com", redirect.getHost()); assertEquals("/authorize", redirect.getPath()); assertEquals("http", redirect.getScheme()); MultivaluedMap<String, String> params = HttpUtil.parseQueryParams(redirect); assertEquals("id", params.getFirst("client_id")); assertEquals("test scope", params.getFirst("scope")); assertEquals("code", params.getFirst("response_type")); assertEquals("http: params.getFirst("redirect_uri")); assertEquals("state", params.getFirst("state")); }
@Test(expected = MisconfiguredAuthenticatorException.class) public void testDelegateInvalidConfiguration() { Authenticator testConfig = new Authenticator(); authenticator.delegate(testConfig, validCallback); }
@Test(expected = ServerErrorException.class) public void testDelegateNoCallback() { authenticator.delegate(config, null); }
@Test(expected = ServerErrorException.class) public void testDelegateNoCallbackState() { URI callback = URI.create("http: authenticator.delegate(config, callback); }