1 /* |
1 /* |
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
27 |
27 |
28 import java.net.URL; |
28 import java.net.URL; |
29 import java.net.URI; |
29 import java.net.URI; |
30 import java.net.URISyntaxException; |
30 import java.net.URISyntaxException; |
31 import java.net.PasswordAuthentication; |
31 import java.net.PasswordAuthentication; |
|
32 import java.nio.ByteBuffer; |
|
33 import java.nio.CharBuffer; |
|
34 import java.nio.charset.Charset; |
32 import java.io.IOException; |
35 import java.io.IOException; |
33 import java.io.OutputStream; |
36 import java.io.OutputStream; |
|
37 import java.util.Arrays; |
34 import java.util.Base64; |
38 import java.util.Base64; |
35 import java.util.Objects; |
39 import java.util.Objects; |
36 import sun.net.www.HeaderParser; |
40 import sun.net.www.HeaderParser; |
|
41 import static java.nio.charset.StandardCharsets.UTF_8; |
|
42 import static java.nio.charset.StandardCharsets.ISO_8859_1; |
37 |
43 |
38 /** |
44 /** |
39 * BasicAuthentication: Encapsulate an http server authentication using |
45 * BasicAuthentication: Encapsulate an http server authentication using |
40 * the "basic" scheme. |
46 * the "basic" scheme. |
41 * |
47 * |
43 */ |
49 */ |
44 |
50 |
45 |
51 |
46 class BasicAuthentication extends AuthenticationInfo { |
52 class BasicAuthentication extends AuthenticationInfo { |
47 |
53 |
|
54 @java.io.Serial |
48 private static final long serialVersionUID = 100L; |
55 private static final long serialVersionUID = 100L; |
49 |
56 |
50 /** The authentication string for this host, port, and realm. This is |
57 /** The authentication string for this host, port, and realm. This is |
51 a simple BASE64 encoding of "login:password". */ |
58 a simple BASE64 encoding of "login:password". */ |
52 String auth; |
59 final String auth; |
53 |
60 |
54 /** |
61 /** |
55 * Create a BasicAuthentication |
62 * Create a BasicAuthentication |
56 */ |
63 */ |
57 public BasicAuthentication(boolean isProxy, String host, int port, |
64 public BasicAuthentication(boolean isProxy, String host, int port, |
58 String realm, PasswordAuthentication pw, |
65 String realm, PasswordAuthentication pw, |
59 String authenticatorKey) { |
66 boolean isUTF8, String authenticatorKey) { |
60 super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, |
67 super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, |
61 AuthScheme.BASIC, host, port, realm, |
68 AuthScheme.BASIC, host, port, realm, |
62 Objects.requireNonNull(authenticatorKey)); |
69 Objects.requireNonNull(authenticatorKey)); |
63 String plain = pw.getUserName() + ":"; |
70 this.auth = authValueFrom(pw, isUTF8); |
64 byte[] nameBytes = null; |
|
65 try { |
|
66 nameBytes = plain.getBytes("ISO-8859-1"); |
|
67 } catch (java.io.UnsupportedEncodingException uee) { |
|
68 assert false; |
|
69 } |
|
70 |
|
71 // get password bytes |
|
72 char[] passwd = pw.getPassword(); |
|
73 byte[] passwdBytes = new byte[passwd.length]; |
|
74 for (int i=0; i<passwd.length; i++) |
|
75 passwdBytes[i] = (byte)passwd[i]; |
|
76 |
|
77 // concatenate user name and password bytes and encode them |
|
78 byte[] concat = new byte[nameBytes.length + passwdBytes.length]; |
|
79 System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length); |
|
80 System.arraycopy(passwdBytes, 0, concat, nameBytes.length, |
|
81 passwdBytes.length); |
|
82 this.auth = "Basic " + Base64.getEncoder().encodeToString(concat); |
|
83 this.pw = pw; |
71 this.pw = pw; |
84 } |
72 } |
85 |
73 |
86 /** |
74 /** |
87 * Create a BasicAuthentication |
75 * Create a BasicAuthentication |
97 |
85 |
98 /** |
86 /** |
99 * Create a BasicAuthentication |
87 * Create a BasicAuthentication |
100 */ |
88 */ |
101 public BasicAuthentication(boolean isProxy, URL url, String realm, |
89 public BasicAuthentication(boolean isProxy, URL url, String realm, |
102 PasswordAuthentication pw, |
90 PasswordAuthentication pw, boolean isUTF8, |
103 String authenticatorKey) { |
91 String authenticatorKey) { |
104 super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, |
92 super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION, |
105 AuthScheme.BASIC, url, realm, |
93 AuthScheme.BASIC, url, realm, |
106 Objects.requireNonNull(authenticatorKey)); |
94 Objects.requireNonNull(authenticatorKey)); |
|
95 this.auth = authValueFrom(pw, isUTF8); |
|
96 this.pw = pw; |
|
97 } |
|
98 |
|
99 private static String authValueFrom(PasswordAuthentication pw, boolean isUTF8) { |
107 String plain = pw.getUserName() + ":"; |
100 String plain = pw.getUserName() + ":"; |
108 byte[] nameBytes = null; |
101 char[] password = pw.getPassword(); |
109 try { |
102 CharBuffer cbuf = CharBuffer.allocate(plain.length() + password.length); |
110 nameBytes = plain.getBytes("ISO-8859-1"); |
103 cbuf.put(plain).put(password).flip(); |
111 } catch (java.io.UnsupportedEncodingException uee) { |
104 Charset charset = isUTF8 ? UTF_8 : ISO_8859_1; |
112 assert false; |
105 ByteBuffer buf = charset.encode(cbuf); |
113 } |
106 ByteBuffer enc = Base64.getEncoder().encode(buf); |
114 |
107 String ret = "Basic " + new String(enc.array(), enc.position(), enc.remaining(), ISO_8859_1); |
115 // get password bytes |
108 Arrays.fill(buf.array(), (byte) 0); |
116 char[] passwd = pw.getPassword(); |
109 Arrays.fill(enc.array(), (byte) 0); |
117 byte[] passwdBytes = new byte[passwd.length]; |
110 Arrays.fill(cbuf.array(), (char) 0); |
118 for (int i=0; i<passwd.length; i++) |
111 return ret; |
119 passwdBytes[i] = (byte)passwd[i]; |
|
120 |
|
121 // concatenate user name and password bytes and encode them |
|
122 byte[] concat = new byte[nameBytes.length + passwdBytes.length]; |
|
123 System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length); |
|
124 System.arraycopy(passwdBytes, 0, concat, nameBytes.length, |
|
125 passwdBytes.length); |
|
126 this.auth = "Basic " + Base64.getEncoder().encodeToString(concat); |
|
127 this.pw = pw; |
|
128 } |
112 } |
129 |
113 |
130 /** |
114 /** |
131 * Create a BasicAuthentication |
115 * Create a BasicAuthentication |
132 */ |
116 */ |