comparison src/luan/modules/url/LuanUrl.java @ 1318:35a6a195819f

in authorization rename user to username
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Feb 2019 22:17:50 -0700
parents c286c1e36b81
children 25746915a241
comparison
equal deleted inserted replaced
1317:c286c1e36b81 1318:35a6a195819f
35 private Method method = Method.GET; 35 private Method method = Method.GET;
36 private final Map<String,Object> headers = new HashMap<String,Object>(); 36 private final Map<String,Object> headers = new HashMap<String,Object>();
37 private String content = ""; 37 private String content = "";
38 private MultipartClient multipart = null; 38 private MultipartClient multipart = null;
39 private int timeout = 0; 39 private int timeout = 0;
40 private String authUser = null; 40 private String authUsername = null;
41 private String authPassword = null; 41 private String authPassword = null;
42 42
43 public LuanUrl(URL url,LuanTable options) throws LuanException { 43 public LuanUrl(URL url,LuanTable options) throws LuanException {
44 this.url = url; 44 this.url = url;
45 if( options != null ) { 45 if( options != null ) {
73 } 73 }
74 Map auth = getMap(map,"authorization"); 74 Map auth = getMap(map,"authorization");
75 if( auth != null ) { 75 if( auth != null ) {
76 if( headers!=null && headers.containsKey("authorization") ) 76 if( headers!=null && headers.containsKey("authorization") )
77 throw new LuanException( "can't define authorization with header 'authorization' defined" ); 77 throw new LuanException( "can't define authorization with header 'authorization' defined" );
78 String user = getString(auth,"user"); 78 String username = getString(auth,"username");
79 if( user==null ) user = ""; 79 if( username==null ) username = "";
80 String password = getString(auth,"password"); 80 String password = getString(auth,"password");
81 if( password==null ) password = ""; 81 if( password==null ) password = "";
82 String type = getString(auth,"type"); 82 String type = getString(auth,"type");
83 if( !auth.isEmpty() ) 83 if( !auth.isEmpty() )
84 throw new LuanException( "unrecognized authorization options: "+auth ); 84 throw new LuanException( "unrecognized authorization options: "+auth );
85 if( type != null ) { 85 if( type != null ) {
86 if( !type.toLowerCase().equals("basic") ) 86 if( !type.toLowerCase().equals("basic") )
87 throw new LuanException( "authorization type can only be 'basic' or nil" ); 87 throw new LuanException( "authorization type can only be 'basic' or nil" );
88 String val = basicAuth(user,password); 88 String val = basicAuth(username,password);
89 headers.put("authorization",val); 89 headers.put("authorization",val);
90 } else { 90 } else {
91 authUser = user; 91 authUsername = username;
92 authPassword = password; 92 authPassword = password;
93 } 93 }
94 } 94 }
95 Map params = getMap(map,"parameters"); 95 Map params = getMap(map,"parameters");
96 String enctype = getString(map,"enctype"); 96 String enctype = getString(map,"enctype");
277 return httpCon.getInputStream(); 277 return httpCon.getInputStream();
278 } catch(FileNotFoundException e) { 278 } catch(FileNotFoundException e) {
279 throw e; 279 throw e;
280 } catch(IOException e) { 280 } catch(IOException e) {
281 int responseCode = httpCon.getResponseCode(); 281 int responseCode = httpCon.getResponseCode();
282 if( responseCode == 401 && authUser != null && authorization==null ) { 282 if( responseCode == 401 && authUsername != null && authorization==null ) {
283 String authStr = httpCon.getHeaderField("www-authenticate"); 283 String authStr = httpCon.getHeaderField("www-authenticate");
284 //System.out.println("auth = "+authStr); 284 //System.out.println("auth = "+authStr);
285 try { 285 try {
286 WwwAuthenticate auth = new WwwAuthenticate(authStr); 286 WwwAuthenticate auth = new WwwAuthenticate(authStr);
287 if( auth.type.equals("Basic") ) { 287 if( auth.type.equals("Basic") ) {
288 String val = basicAuth(authUser,authPassword); 288 String val = basicAuth(authUsername,authPassword);
289 throw new AuthException(val); 289 throw new AuthException(val);
290 } else if( auth.type.equals("Digest") ) { 290 } else if( auth.type.equals("Digest") ) {
291 String realm = auth.options.get("realm"); 291 String realm = auth.options.get("realm");
292 if(realm==null) throw new RuntimeException("missing realm"); 292 if(realm==null) throw new RuntimeException("missing realm");
293 String algorithm = auth.options.get("algorithm"); 293 String algorithm = auth.options.get("algorithm");
297 if( qop!=null && !qop.equals("auth") ) 297 if( qop!=null && !qop.equals("auth") )
298 throw new LuanException("unsupported digest qop: "+qop); 298 throw new LuanException("unsupported digest qop: "+qop);
299 String nonce = auth.options.get("nonce"); 299 String nonce = auth.options.get("nonce");
300 if(nonce==null) throw new RuntimeException("missing nonce"); 300 if(nonce==null) throw new RuntimeException("missing nonce");
301 String uri = fullPath(url); 301 String uri = fullPath(url);
302 String a1 = authUser + ':' + realm + ':' + authPassword; 302 String a1 = authUsername + ':' + realm + ':' + authPassword;
303 String a2 = "" + method + ':' + uri; 303 String a2 = "" + method + ':' + uri;
304 String nc = "00000001"; 304 String nc = "00000001";
305 String cnonce = "7761faf2daa45b3b"; // who cares? 305 String cnonce = "7761faf2daa45b3b"; // who cares?
306 String response = md5(a1) + ':' + nonce; 306 String response = md5(a1) + ':' + nonce;
307 if( qop != null ) { 307 if( qop != null ) {
308 response += ':' + nc + ':' + cnonce + ':' + qop; 308 response += ':' + nc + ':' + cnonce + ':' + qop;
309 } 309 }
310 response += ':' + md5(a2); 310 response += ':' + md5(a2);
311 response = md5(response); 311 response = md5(response);
312 String val = "Digest"; 312 String val = "Digest";
313 val += " username=\"" + authUser + "\""; 313 val += " username=\"" + authUsername + "\"";
314 val += ", realm=\"" + realm + "\""; 314 val += ", realm=\"" + realm + "\"";
315 val += ", uri=\"" + uri + "\""; 315 val += ", uri=\"" + uri + "\"";
316 val += ", nonce=\"" + nonce + "\""; 316 val += ", nonce=\"" + nonce + "\"";
317 val += ", response=\"" + response + "\""; 317 val += ", response=\"" + response + "\"";
318 if( qop != null ) { 318 if( qop != null ) {
349 349
350 @Override public String to_uri_string() { 350 @Override public String to_uri_string() {
351 return url.toString(); 351 return url.toString();
352 } 352 }
353 353
354 private static String basicAuth(String user,String password) { 354 private static String basicAuth(String username,String password) {
355 String s = user + ':' + password; 355 String s = username + ':' + password;
356 return "Basic " + Base64.getEncoder().encodeToString(s.getBytes()); 356 return "Basic " + Base64.getEncoder().encodeToString(s.getBytes());
357 } 357 }
358 358
359 private final class AuthException extends Exception { 359 private final class AuthException extends Exception {
360 final String authorization; 360 final String authorization;