src/share/classes/sun/net/www/protocol/http/BasicAuthentication.java

Print this page




 114         System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length);
 115         System.arraycopy(passwdBytes, 0, concat, nameBytes.length,
 116                          passwdBytes.length);
 117         this.auth = "Basic " + (new sun.misc.BASE64Encoder()).encode(concat);
 118         this.pw = pw;
 119     }
 120 
 121     /**
 122      * Create a BasicAuthentication
 123      */
 124     public BasicAuthentication(boolean isProxy, URL url, String realm,
 125                                    String auth) {
 126         super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION,
 127               AuthScheme.BASIC, url, realm);
 128         this.auth = "Basic " + auth;
 129     }
 130 
 131     /**
 132      * @return true if this authentication supports preemptive authorization
 133      */
 134     boolean supportsPreemptiveAuthorization() {

 135         return true;
 136     }
 137 
 138     /**
 139      * @return the name of the HTTP header this authentication wants set
 140      */
 141     String getHeaderName() {
 142         if (type == SERVER_AUTHENTICATION) {
 143             return "Authorization";
 144         } else {
 145             return "Proxy-authorization";
 146         }
 147     }
 148 
 149     /**
 150      * Set header(s) on the given connection. This will only be called for
 151      * definitive (i.e. non-preemptive) authorization.
 152      * @param conn The connection to apply the header(s) to
 153      * @param p A source of header values for this connection, if needed.
 154      * @param raw The raw header values for this connection, if needed.
 155      * @return true if all goes well, false if no headers were set.
 156      */
 157     boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) {

 158         conn.setAuthenticationProperty(getHeaderName(), getHeaderValue(null,null));
 159         return true;
 160     }
 161 
 162     /**
 163      * @return the value of the HTTP header this authentication wants set
 164      */
 165     String getHeaderValue(URL url, String method) {

 166         /* For Basic the authorization string does not depend on the request URL
 167          * or the request method
 168          */
 169         return auth;
 170     }
 171 
 172     /**
 173      * For Basic Authentication, the security parameters can never be stale.
 174      * In other words there is no possibility to reuse the credentials.
 175      * They are always either valid or invalid.
 176      */
 177     boolean isAuthorizationStale (String header) {

 178         return false;
 179     }
 180 
 181     /**
 182      * @return the common root path between npath and path.
 183      * This is used to detect when we have an authentication for two
 184      * paths and the root of th authentication space is the common root.
 185      */
 186 
 187     static String getRootPath(String npath, String opath) {
 188         int index = 0;
 189         int toindex;
 190 
 191         /* Must normalize so we don't get confused by ../ and ./ segments */
 192         try {
 193             npath = new URI (npath).normalize().getPath();
 194             opath = new URI (opath).normalize().getPath();
 195         } catch (URISyntaxException e) {
 196             /* ignore error and use the old value */
 197         }


 114         System.arraycopy(nameBytes, 0, concat, 0, nameBytes.length);
 115         System.arraycopy(passwdBytes, 0, concat, nameBytes.length,
 116                          passwdBytes.length);
 117         this.auth = "Basic " + (new sun.misc.BASE64Encoder()).encode(concat);
 118         this.pw = pw;
 119     }
 120 
 121     /**
 122      * Create a BasicAuthentication
 123      */
 124     public BasicAuthentication(boolean isProxy, URL url, String realm,
 125                                    String auth) {
 126         super(isProxy ? PROXY_AUTHENTICATION : SERVER_AUTHENTICATION,
 127               AuthScheme.BASIC, url, realm);
 128         this.auth = "Basic " + auth;
 129     }
 130 
 131     /**
 132      * @return true if this authentication supports preemptive authorization
 133      */
 134     @Override
 135     public boolean supportsPreemptiveAuthorization() {
 136         return true;
 137     }
 138 
 139     /**











 140      * Set header(s) on the given connection. This will only be called for
 141      * definitive (i.e. non-preemptive) authorization.
 142      * @param conn The connection to apply the header(s) to
 143      * @param p A source of header values for this connection, if needed.
 144      * @param raw The raw header values for this connection, if needed.
 145      * @return true if all goes well, false if no headers were set.
 146      */
 147     @Override
 148     public boolean setHeaders(HttpURLConnection conn, HeaderParser p, String raw) {
 149         conn.setAuthenticationProperty(getHeaderName(), getHeaderValue(null,null));
 150         return true;
 151     }
 152 
 153     /**
 154      * @return the value of the HTTP header this authentication wants set
 155      */
 156     @Override
 157     public String getHeaderValue(URL url, String method) {
 158         /* For Basic the authorization string does not depend on the request URL
 159          * or the request method
 160          */
 161         return auth;
 162     }
 163 
 164     /**
 165      * For Basic Authentication, the security parameters can never be stale.
 166      * In other words there is no possibility to reuse the credentials.
 167      * They are always either valid or invalid.
 168      */
 169     @Override
 170     public boolean isAuthorizationStale (String header) {
 171         return false;
 172     }
 173 
 174     /**
 175      * @return the common root path between npath and path.
 176      * This is used to detect when we have an authentication for two
 177      * paths and the root of th authentication space is the common root.
 178      */
 179 
 180     static String getRootPath(String npath, String opath) {
 181         int index = 0;
 182         int toindex;
 183 
 184         /* Must normalize so we don't get confused by ../ and ./ segments */
 185         try {
 186             npath = new URI (npath).normalize().getPath();
 187             opath = new URI (opath).normalize().getPath();
 188         } catch (URISyntaxException e) {
 189             /* ignore error and use the old value */
 190         }