List of usage examples for org.apache.http.auth AuthScheme authenticate
@Deprecated
Header authenticate(Credentials credentials, HttpRequest request) throws AuthenticationException;
From source file:edu.syr.bytecast.githubcampfire.CampfirePost.java
public CampfirePostReply post(String message) { //see: http://stackoverflow.com/questions/2603691/android-httpclient-and-https try {//from w w w. jav a 2 s . c o m String xml_message = "<message><type>TextMessage</type><body>" + message + "</body></message>"; SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); BasicSchemeFactory factory = new BasicSchemeFactory(); HttpParams params = new BasicHttpParams(); params.setParameter("realm", "https://trifort.campfirenow.com/"); SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry); DefaultHttpClient client = new DefaultHttpClient(mgr, params); String url = "https://trifort.campfirenow.com/room/" + m_room + "/speak.xml"; HttpPost post = new HttpPost(url); post.setHeader("Content-type", "application/xml"); AuthScheme scheme = factory.newInstance(params); Header header = scheme.authenticate(new UsernamePasswordCredentials(m_apiKey), post); HttpEntity entity = new StringEntity(xml_message); post.setEntity(entity); post.setHeader(header); HttpResponse response = client.execute(post); return new CampfirePostReply(response); } catch (Exception ex) {//post.setEntity; ex.printStackTrace(); return null; } }
From source file:com.soundcloud.playerapi.OAuth2HttpRequestInterceptor.java
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { if (request == null) throw new IllegalArgumentException("HTTP request may not be null"); if (context == null) throw new IllegalArgumentException("HTTP context may not be null"); if (!request.getRequestLine().getMethod().equalsIgnoreCase("CONNECT")) { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState != null) { AuthScheme authScheme = authState.getAuthScheme(); if (authScheme != null && !authScheme.isConnectionBased()) { try { request.setHeader(authScheme.authenticate(null, request)); } catch (AuthenticationException ignored) { // ignored }/* ww w.java 2 s .c o m*/ } } } }
From source file:securitydigest.TestDigestScheme.java
public void testDigestAuthenticationWithDefaultCreds() throws Exception { String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\""; Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge); HttpRequest request = new BasicHttpRequest("Simple", "/"); Credentials cred = new UsernamePasswordCredentials("username", "password"); AuthScheme authscheme = new DigestScheme(); authscheme.processChallenge(authChallenge); Header authResponse = authscheme.authenticate(cred, request); Map<String, String> table = parseAuthResponse(authResponse); assertEquals("username", table.get("username")); assertEquals("realm1", table.get("realm")); assertEquals("/", table.get("uri")); assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce")); assertEquals("e95a7ddf37c2eab009568b1ed134f89a", table.get("response")); }
From source file:securitydigest.TestDigestScheme.java
public void testDigestAuthentication() throws Exception { String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\""; Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge); HttpRequest request = new BasicHttpRequest("Simple", "/"); Credentials cred = new UsernamePasswordCredentials("username", "password"); AuthScheme authscheme = new DigestScheme(); authscheme.processChallenge(authChallenge); Header authResponse = authscheme.authenticate(cred, request); Map<String, String> table = parseAuthResponse(authResponse); assertEquals("username", table.get("username")); assertEquals("realm1", table.get("realm")); assertEquals("/", table.get("uri")); assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce")); assertEquals("e95a7ddf37c2eab009568b1ed134f89a", table.get("response")); }
From source file:securitydigest.TestDigestScheme.java
public void testDigestAuthenticationWithQueryStringInDigestURI() throws Exception { String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\""; Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge); HttpRequest request = new BasicHttpRequest("Simple", "/?param=value"); Credentials cred = new UsernamePasswordCredentials("username", "password"); AuthScheme authscheme = new DigestScheme(); authscheme.processChallenge(authChallenge); Header authResponse = authscheme.authenticate(cred, request); Map<String, String> table = parseAuthResponse(authResponse); assertEquals("username", table.get("username")); assertEquals("realm1", table.get("realm")); assertEquals("/?param=value", table.get("uri")); assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce")); assertEquals("a847f58f5fef0bc087bcb9c3eb30e042", table.get("response")); }
From source file:securitydigest.TestDigestScheme.java
public void testDigestAuthenticationWithSHA() throws Exception { String challenge = "Digest realm=\"realm1\", " + "nonce=\"f2a3f18799759d4f1a1c068b92b573cb\", " + "algorithm=SHA"; Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge); HttpRequest request = new BasicHttpRequest("Simple", "/"); Credentials cred = new UsernamePasswordCredentials("username", "password"); AuthScheme authscheme = new DigestScheme(); authscheme.processChallenge(authChallenge); Header authResponse = authscheme.authenticate(cred, request); Map<String, String> table = parseAuthResponse(authResponse); assertEquals("username", table.get("username")); assertEquals("realm1", table.get("realm")); assertEquals("/", table.get("uri")); assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce")); assertEquals("8769e82e4e28ecc040b969562b9050580c6d186d", table.get("response")); }
From source file:securitydigest.TestDigestScheme.java
public void testDigestAuthenticationWithMultipleRealms() throws Exception { String challenge1 = "Digest realm=\"realm1\", nonce=\"abcde\""; String challenge2 = "Digest realm=\"realm2\", nonce=\"123546\""; Credentials cred = new UsernamePasswordCredentials("username", "password"); Credentials cred2 = new UsernamePasswordCredentials("uname2", "password2"); Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge1); HttpRequest request = new BasicHttpRequest("Simple", "/"); AuthScheme authscheme = new DigestScheme(); authscheme.processChallenge(authChallenge); Header authResponse = authscheme.authenticate(cred, request); Map<String, String> table = parseAuthResponse(authResponse); assertEquals("username", table.get("username")); assertEquals("realm1", table.get("realm")); assertEquals("/", table.get("uri")); assertEquals("abcde", table.get("nonce")); assertEquals("786f500303eac1478f3c2865e676ed68", table.get("response")); authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge2); AuthScheme authscheme2 = new DigestScheme(); authscheme2.processChallenge(authChallenge); authResponse = authscheme2.authenticate(cred2, request); table = parseAuthResponse(authResponse); assertEquals("uname2", table.get("username")); assertEquals("realm2", table.get("realm")); assertEquals("/", table.get("uri")); assertEquals("123546", table.get("nonce")); assertEquals("0283edd9ef06a38b378b3b74661391e9", table.get("response")); }
From source file:securitydigest.TestDigestScheme.java
/** * Test digest authentication using the MD5-sess algorithm. *///from w w w . j a v a 2 s. com public void testDigestAuthenticationMD5SessNoQop() throws Exception { // Example using Digest auth with MD5-sess String realm = "realm"; String username = "username"; String password = "password"; String nonce = "e273f1776275974f1a120d8b92c5b3cb"; String challenge = "Digest realm=\"" + realm + "\", " + "nonce=\"" + nonce + "\", " + "opaque=\"SomeString\", " + "stale=false, " + "algorithm=MD5-sess"; Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge); Credentials cred = new UsernamePasswordCredentials(username, password); HttpRequest request = new BasicHttpRequest("Simple", "/"); AuthScheme authscheme = new DigestScheme(); authscheme.processChallenge(authChallenge); Header authResponse = authscheme.authenticate(cred, request); Map<String, String> table = parseAuthResponse(authResponse); assertEquals(username, table.get("username")); assertEquals(realm, table.get("realm")); assertEquals("MD5-sess", table.get("algorithm")); assertEquals("/", table.get("uri")); assertEquals(nonce, table.get("nonce")); assertTrue(null == table.get("nc")); assertEquals("SomeString", table.get("opaque")); assertTrue(null == table.get("qop")); //@TODO: add better check assertTrue(null != table.get("response")); }
From source file:securitydigest.TestDigestScheme.java
/** * Test digest authentication using the MD5-sess algorithm. *///from w ww. j a v a 2 s. c om public void testDigestAuthenticationMD5Sess() throws Exception { // Example using Digest auth with MD5-sess String realm = "realm"; String username = "username"; String password = "password"; String nonce = "e273f1776275974f1a120d8b92c5b3cb"; String challenge = "Digest realm=\"" + realm + "\", " + "nonce=\"" + nonce + "\", " + "opaque=\"SomeString\", " + "stale=false, " + "algorithm=MD5-sess, " + "qop=\"auth,auth-int\""; // we pass both but expect auth to be used Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge); Credentials cred = new UsernamePasswordCredentials(username, password); HttpRequest request = new BasicHttpRequest("Simple", "/"); AuthScheme authscheme = new DigestScheme(); authscheme.processChallenge(authChallenge); Header authResponse = authscheme.authenticate(cred, request); String response = authResponse.getValue(); assertTrue(response.indexOf("nc=00000001") > 0); // test for quotes assertTrue(response.indexOf("qop=auth") > 0); // test for quotes Map<String, String> table = parseAuthResponse(authResponse); assertEquals(username, table.get("username")); assertEquals(realm, table.get("realm")); assertEquals("MD5-sess", table.get("algorithm")); assertEquals("/", table.get("uri")); assertEquals(nonce, table.get("nonce")); assertEquals(1, Integer.parseInt(table.get("nc"), 16)); assertTrue(null != table.get("cnonce")); assertEquals("SomeString", table.get("opaque")); assertEquals("auth", table.get("qop")); //@TODO: add better check assertTrue(null != table.get("response")); }
From source file:org.callimachusproject.client.HttpAuthenticator.java
@SuppressWarnings("deprecation") private Header doAuth(final AuthScheme authScheme, final Credentials creds, final HttpRequest request, final HttpContext context) throws AuthenticationException { if (authScheme instanceof ContextAwareAuthScheme) { return ((ContextAwareAuthScheme) authScheme).authenticate(creds, request, context); } else {/*from w w w .ja v a 2 s. c o m*/ return authScheme.authenticate(creds, request); } }