/** * Check is a AuthScope is "subsumed" by another AuthScope. * Alias for equivalence */ static boolean subsumes(AuthScope as, AuthScope has) { return equivalent(as, has); }
/** * Retrieve a credentials from the cache. * * @param principal a key for retrieving a credentials object. * @param scope a key for retrieving a credentials object. * @return the matching credentials object, else null */ static synchronized protected Credentials checkCache(String principal, AuthScope scope) { Credentials creds = null; for(Triple p : HTTPCachingProvider.cache) { if(HTTPAuthScope.identical(p.scope, scope)) { creds = p.creds; break; } } return creds; }
private void writeObject(ObjectOutputStream oos) throws IOException { oos.writeObject(principal); HTTPAuthScope.serializeScope(scope, oos); if(provider instanceof Serializable) oos.writeObject(provider); else oos.writeObject(provider.getClass()); }
/** * Create an AuthScope from a URL; pull out any principal * * @param surl to convert * @param principalp to store principal from url * @returns an AuthScope instance */ static public AuthScope urlToScope(String authscheme, String surl, String[] principalp) throws HTTPException { URI uri = HTTPAuthScope.decompose(surl); AuthScope scope = new AuthScope(uri.getHost(), uri.getPort(), ANY_REALM, authscheme); if(principalp != null) principalp[0] = uri.getUserInfo(); return scope; }
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { try { this.principal = (String) ois.readObject(); this.scope = HTTPAuthScope.deserializeScope(ois); Object o = ois.readObject(); if(o instanceof Class) this.provider = (CredentialsProvider) ((Class) o).newInstance(); else this.provider = (CredentialsProvider) o; } catch (IOException ioe) { throw ioe; } catch (Exception e) { throw new IOException(e); } }
/** * Handle authentication. * We do not know, necessarily, * which scheme(s) will be * encountered, so most testing * occurs in HTTPAuthProvider * * @return an authprovider encapsulting the request */ synchronized protected AuthScope setAuthentication() throws HTTPException { String surl = session.getURL(); // Creat a authscope from the url AuthScope scope; String[] principalp = new String[1]; if(surl == null) scope = HTTPAuthScope.ANY; else scope = HTTPAuthScope.urlToScope(HTTPAuthPolicy.BASIC, surl, principalp); // Provide a credentials (provider) to enact the process // We use the a caching instance so we can intercept getCredentials // requests to check the cache. HTTPCachingProvider hap = new HTTPCachingProvider(this.session.getAuthStore(), scope, principalp[0]); // New in httpclient 4.2; will need to change in 4.3 this.session.setAuthentication(hap); return scope; }
public void setCredentialsProvider(String surl) throws HTTPException { // Try to extract user info URI uri = HTTPAuthScope.decompose(surl); String userinfo = uri.getUserInfo(); if(userinfo != null) { int index = userinfo.indexOf(':'); String user = userinfo.substring(index); String pwd = userinfo.substring(index + 1, userinfo.length()); if(user != null && pwd != null) { // Create a non-interactive user+pwd handler CredentialsProvider bp = new HTTPBasicProvider(user, pwd); setCredentialsProvider(HTTPAuthPolicy.BASIC, bp); } } }
/** * Clear some entries matching the argument */ static synchronized public void // public only to allow testing invalidate(AuthScope scope) { if(TESTING) { if(testlist == null) testlist = new ArrayList<Triple>(); } // walk backward because we are removing entries for(int i = HTTPCachingProvider.cache.size() - 1;i >= 0;i--) { Triple p = HTTPCachingProvider.cache.get(i); if(HTTPAuthScope.equivalent(scope, p.scope)) { if(TESTING) { System.err.println("invalidating: " + p); if(testlist == null) testlist = new ArrayList<Triple>(); testlist.add(p); } HTTPCachingProvider.cache.remove(i); } } }
/** * Equivalence algorithm: * if any field is ANY_XXX, then they are equivalent. * Scheme, port, host must all be identical else return false * If this.path is prefix of other.path * or other.path is prefix of this.path * or they are string equals, then return true * else return false. */ static boolean equivalent(Entry e1, Entry e2) { if(e1 == null ^ e2 == null) return false; if(e1 == e2) return true; AuthScope a1 = e1.scope; AuthScope a2 = e2.scope; if(!HTTPAuthScope.identical(a1, a2)) return false; if(e1.principal == ANY_PRINCIPAL || e2.principal == ANY_PRINCIPAL) return true; return (e1.principal.equals(e2.principal)); }
/** * Search for all equivalent rows, then sort on the path. * * @param scope * @return list of matching entries */ synchronized public List<Entry> search(String principal, AuthScope scope) { List<Entry> matches; if(isdefault || DEFAULT == null) matches = new ArrayList<Entry>(); else matches = DEFAULT.search(principal, scope); if(scope == null || rows.size() == 0) return matches; for(Entry e : getAllRows()) { if(principal != ANY_PRINCIPAL && e.principal.equals(principal)) continue; if(HTTPAuthScope.equivalent(scope, e.scope)) matches.add(e); } Collections.sort(matches); return matches; }