/** * Sets the authentication response sent by a client to an origin server * given a scheme, identifier and secret. * * @param scheme * The challenge scheme. * @param identifier * The user identifier, such as a login name or an access key. * @param secret * The user secret, such as a password or a secret key. */ public void setChallengeResponse(ChallengeScheme scheme, final String identifier, String secret) { setChallengeResponse(new ChallengeResponse(scheme, identifier, secret)); }
/** * Sets the proxy authentication response sent by a client to an origin * server given a scheme, identifier and secret. * * @param scheme * The challenge scheme. * @param identifier * The user identifier, such as a login name or an access key. * @param secret * The user secret, such as a password or a secret key. */ public void setProxyChallengeResponse(ChallengeScheme scheme, final String identifier, String secret) { setProxyChallengeResponse(new ChallengeResponse(scheme, identifier, secret)); }
protected void setupAuth(Request request) { if (authType == AUTH_TYPE_BASIC) { ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; ChallengeResponse authentication = new ChallengeResponse(scheme, userName, password); request.setChallengeResponse(authentication); } else if (authType == AUTH_TYPE_SECRET) { Series<Parameter> additionnalHeaders = new Form(); Map<String, String> securityHeaders = PortalSSOAuthenticationProvider.getHeaders(secretToken, userName); for (String hn : securityHeaders.keySet()) { additionnalHeaders.add(hn, securityHeaders.get(hn)); } request.getAttributes().put("org.restlet.http.headers", additionnalHeaders); } }
public RestClientHelper( String baseUrl, String username, String password ) { ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; ChallengeResponse authentication = new ChallengeResponse( scheme, username, password ); this.challenge = authentication; this.baseUrl = baseUrl; this.restContext = new Context(); this.restClient = new Client( restContext, Protocol.HTTP ); xstream = XStreamConfigurator.configureXStream( new XStream( new LookAheadXppDriver() ) ); MIndexerXStreamConfigurator.configureXStream( xstream ); }
private Request makeRequest(Reference uri, MediaType accepts) { Request request = new Request(); logger.debug("Making new request for " + uri + " -- " + uri.getTargetRef()); request.setResourceRef(uri.getTargetRef()); if (accepts != null) { request.getClientInfo().getAcceptedMediaTypes().add( new Preference<MediaType>(accepts)); } if (baseURI.isParent(uri) && username != null) { logger.debug("Authenticating as " + username); ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, username, password); request.setChallengeResponse(challengeResponse); } else { logger.warn("Not supplying credentials for out-of-site URI " + uri); } return request; }
public static ChallengeResponse getTestChallengeResponse(final List<ChallengeRequest> list, final ChallengeScheme httpDigest, final Response response, final String userName, final char[] password) { ChallengeRequest c1 = null; for(final ChallengeRequest challengeRequest : list) { if(ChallengeScheme.HTTP_DIGEST.equals(challengeRequest.getScheme())) { c1 = challengeRequest; break; } } // 2- Create the Challenge response used by the client to authenticate its requests. final ChallengeResponse challengeResponse = new ChallengeResponse(c1, response, userName, password); return challengeResponse; }
protected ClientResource createClient(final String path) { String assembledPath = assembleEndpoint(path); log.debug("Endpoint URL: " + assembledPath); final ClientResource service = new ClientResource(new Context(getClass().getName()), assembledPath); service.setNext(client); ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; if (context.getEmailAddress() != null) { ChallengeResponse authentication = new ChallengeResponse(scheme, context.getEmailAddress(), context.getCredentials()); service.setChallengeResponse(authentication); } else { service.setChallengeResponse(null); } return service; }
public static void main(String[] args) { // Prepare the request ClientResource resource = new ClientResource("http://localhost:8182/"); // Add the client authentication to the call ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; ChallengeResponse authentication = new ChallengeResponse(scheme, "scott", "tiger"); resource.setChallengeResponse(authentication); try { // Send the HTTP GET request resource.get(); // Output the response entity on the JVM console resource.getResponseEntity().write(System.out); } catch (Exception e) { if (Status.CLIENT_ERROR_UNAUTHORIZED.equals(resource.getStatus())) { // Unauthorized access System.out .println("Access unauthorized by the server, check your credentials"); } else { // Unexpected status System.out.println("An unexpected status was returned: " + resource.getStatus()); } } } }
public static void main(String[] args) throws Exception { // Prepare the request ClientResource resource = new ClientResource("http://localhost:8111/"); // Add the client authentication to the call ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; ChallengeResponse authentication = new ChallengeResponse(scheme, "scott", "tiger"); resource.setChallengeResponse(authentication); // Send the HTTP GET request resource.get(); if (resource.getStatus().isSuccess()) { // Output the response entity on the JVM console resource.getResponseEntity().write(System.out); } else if (resource.getStatus() .equals(Status.CLIENT_ERROR_UNAUTHORIZED)) { // Unauthorized access System.out .println("Access authorized by the server, check your credentials"); } else { // Unexpected status System.out.println("An unexpected status was returned: " + resource.getStatus()); } }
@Override public List<Car> list() { Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("truststorePath", System.getProperty("javax.net.ssl.trustStore")); ClientResource clientResource = new ClientResource("https://localhost:8043/api/cars/cars"); clientResource.setNext(client); ChallengeResponse challenge = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER); challenge.setRawValue(Request.getCurrent().getAttributes().getOrDefault("token", "").toString()); clientResource.setChallengeResponse(challenge); CarServiceInterface carServiceInterface = clientResource.wrap(CarServiceInterface.class); Car[] allCars = carServiceInterface.getAllCars(); try { client.stop(); } catch (Exception e) { throw new RuntimeException(e); } return asList(allCars); } }
public static void main(String[] args) throws ResourceException, IOException { // Prepare the request ClientResource cr = new ClientResource("http://localhost:8182/"); ChallengeRequest c1 = null; // first try: unauthenticated request try { cr.get(); } catch (ResourceException re) { if (Status.CLIENT_ERROR_UNAUTHORIZED.equals(cr.getStatus())) { c1 = getDigestChallengeRequest(cr); } } // second try: authenticated request if (c1 != null) { ChallengeResponse c2 = new ChallengeResponse(c1, cr.getResponse(), "scott", "tiger".toCharArray()); cr.setChallengeResponse(c2); cr.get().write(System.out); } }
@Override protected int beforeHandle(Request request, Response response) { Cookie cookie = request.getCookies().getFirst("Credentials"); if (cookie != null) { // Extract the challenge response from the cookie String[] credentials = cookie.getValue().split("="); if (credentials.length == 2) { String identifier = credentials[0]; String secret = credentials[1]; request.setChallengeResponse(new ChallengeResponse( ChallengeScheme.HTTP_COOKIE, identifier, secret)); } } else if (Method.POST.equals(request.getMethod()) && request.getResourceRef().getQueryAsForm().getFirst("login") != null) { // Intercepting a login form Form credentials = new Form(request.getEntity()); String identifier = credentials.getFirstValue("identifier"); String secret = credentials.getFirstValue("secret"); request.setChallengeResponse(new ChallengeResponse( ChallengeScheme.HTTP_COOKIE, identifier, secret)); // Continue call processing to return the target representation if // authentication is successful or a new login page request.setMethod(Method.GET); } return super.beforeHandle(request, response); }
@Override protected int beforeHandle(Request request, Response response) { Cookie cookie = request.getCookies().getFirst("Credentials"); if (cookie != null) { // Extract the challenge response from the cookie String[] credentials = cookie.getValue().split("="); if (credentials.length == 2) { String identifier = credentials[0]; String secret = credentials[1]; request.setChallengeResponse(new ChallengeResponse( ChallengeScheme.HTTP_COOKIE, identifier, secret)); } } else if (Method.POST.equals(request.getMethod()) && request.getResourceRef().getQueryAsForm().getFirst("login") != null) { // Intercepting a login form Form credentials = new Form(request.getEntity()); String identifier = credentials.getFirstValue("identifier"); String secret = credentials.getFirstValue("secret"); request.setChallengeResponse(new ChallengeResponse( ChallengeScheme.HTTP_COOKIE, identifier, secret)); // Continue call processing to return the target representation if // authentication is successful or a new login page request.setMethod(Method.GET); } return super.beforeHandle(request, response); }
public static void main(String[] args) throws Exception { // Prepare the request Request request = new Request(Method.GET, "http://s3.amazonaws.com/quotes/nelson"); request.setChallengeResponse(new ChallengeResponse( ChallengeScheme.HTTP_AWS_S3, "44CF9590006BF252F707", "OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV")); // Add some extra headers Series<Header> extraHeaders = new Series<Header>(Header.class); extraHeaders.add("X-Amz-Meta-Author", "foo@bar.com"); extraHeaders.add("X-Amz-Magic", "abracadabra"); // For the test we hard coded a special date header. Normally you don't // need this as the // HTTP client connector will automatically provide an accurate Date // header and use it // for authentication. // extraHeaders.add("X-Amz-Date", "Thu, 17 Nov 2005 18:49:58 GMT"); request.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, extraHeaders); // Handle it using an HTTP client connector Client client = new Client(Protocol.HTTP); Response response = client.handle(request); // Write the response entity on the console Representation output = response.getEntity(); output.write(System.out); }
/** * Processes the login request. * * @param request * The current request. * @param response * The current response. */ protected void login(final Request request, final Response response) { // Login detected final Form form = new Form(request.getEntity()); final Parameter identifier = form.getFirst(this.getIdentifierFormName()); final Parameter secret = form.getFirst(this.getSecretFormName()); // Set credentials final ChallengeResponse cr = new ChallengeResponse(this.getScheme(), identifier != null ? identifier.getValue() : null, secret != null ? secret.getValue() : null); request.setChallengeResponse(cr); this.log.info("calling attemptRedirect after login"); // Attempt to redirect this.attemptRedirect(request, response, form); }
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Client sdcClient = new Client(new Context(), Protocol.valueOf("SDC")); Series<Parameter> parameters = sdcClient.getContext().getParameters(); parameters.add("keystorePath", "sdc.keystore"); parameters.add("keystorePassword", "password"); parameters.add("enabledCipherSuites", "TLS_RSA_WITH_AES_128_CBC_SHA"); parameters.add("sslProtocol", "TLSv1"); sdcClient.start(); System.out .println("Press a key when the SDC agent is started and has established a tunnel..."); System.in.read(); Request request = new Request(Method.GET, "http://restlet.org"); request.setProtocol(Protocol.valueOf("SDC")); request.setProxyChallengeResponse(new ChallengeResponse(ChallengeScheme .valueOf("SDC"), "myUser@example.com", "myPassword")); Response response = sdcClient.handle(request); response.getEntity().write(System.out); } }
public static void main(String[] args) throws Exception { // Create and configure HTTPS client Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("truststorePath", "src/org/restlet/example/book/restlet/ch05/clientTrust.jks"); parameters.add("truststorePassword", "password"); parameters.add("truststoreType", "JKS"); // Create and configure client resource ClientResource clientResource = new ClientResource( "https://localhost:8183/accounts/chunkylover53/mails/123"); clientResource.setNext(client); // Preemptively configure the authentication credentials ChallengeResponse authentication = new ChallengeResponse( ChallengeScheme.HTTP_BASIC, "chunkylover53", "pwd"); clientResource.setChallengeResponse(authentication); // Communicate with remote resource MailResource mailClient = clientResource.wrap(MailResource.class); mailClient.store(mailClient.retrieve()); // Store HTTPS client client.stop(); }
public static void main(String[] args) throws Exception { // Create and configure HTTPS client Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("truststorePath", "src/org/restlet/example/book/restlet/ch05/clientTrust.jks"); parameters.add("truststorePassword", "password"); parameters.add("truststoreType", "JKS"); // Create and configure client resource ClientResource clientResource = new ClientResource( "https://localhost:8183/accounts/chunkylover53/mails/123"); clientResource.setNext(client); // Preemptively configure the authentication credentials ChallengeResponse authentication = new ChallengeResponse( ChallengeScheme.HTTP_BASIC, "chunkylover53", "pwd"); clientResource.setChallengeResponse(authentication); // Communicate with remote resource MailResource mailClient = clientResource.wrap(MailResource.class); mailClient.store(mailClient.retrieve()); // Store HTTPS client client.stop(); }
public static void main(String[] args) throws Exception { // Create and configure HTTPS client Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("truststorePath", "src/org/restlet/example/book/restlet/ch05/clientTrust.jks"); parameters.add("truststorePassword", "password"); parameters.add("truststoreType", "JKS"); // Create and configure client resource ClientResource clientResource = new ClientResource( "https://localhost:8183/accounts/chunkylover53/mails/123"); clientResource.setNext(client); // Preemptively configure the authentication credentials ChallengeResponse authentication = new ChallengeResponse( ChallengeScheme.HTTP_BASIC, "chunkylover53", "pwd"); clientResource.setChallengeResponse(authentication); // Communicate with remote resource MailResource mailClient = clientResource.wrap(MailResource.class); mailClient.store(mailClient.retrieve()); // Store HTTPS client client.stop(); }
public static void main(String[] args) throws Exception { // Create and configure HTTPS client Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("keystorePath", "src/org/restlet/example/book/restlet/ch05/clientKey.jks"); parameters.add("keystorePassword", "password"); parameters.add("keystoreType", "JKS"); parameters.add("keyPassword", "password"); parameters.add("truststorePath", "src/org/restlet/example/book/restlet/ch05/clientTrust.jks"); parameters.add("truststorePassword", "password"); parameters.add("truststoreType", "JKS"); // Create and configure client resource ClientResource clientResource = new ClientResource( "https://localhost:8183/accounts/chunkylover53/mails/123"); clientResource.setNext(client); // Preemptively configure the authentication credentials ChallengeResponse authentication = new ChallengeResponse( ChallengeScheme.HTTP_BASIC, "chunkylover53", "pwd"); clientResource.setChallengeResponse(authentication); // Communicate with remote resource MailResource mailClient = clientResource.wrap(MailResource.class); mailClient.store(mailClient.retrieve()); // Store HTTPS client client.stop(); }