103928aee4356845252ac6b662d5c72c29903813eJake Slack//
203928aee4356845252ac6b662d5c72c29903813eJake Slack//  ========================================================================
303928aee4356845252ac6b662d5c72c29903813eJake Slack//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
403928aee4356845252ac6b662d5c72c29903813eJake Slack//  ------------------------------------------------------------------------
503928aee4356845252ac6b662d5c72c29903813eJake Slack//  All rights reserved. This program and the accompanying materials
603928aee4356845252ac6b662d5c72c29903813eJake Slack//  are made available under the terms of the Eclipse Public License v1.0
703928aee4356845252ac6b662d5c72c29903813eJake Slack//  and Apache License v2.0 which accompanies this distribution.
803928aee4356845252ac6b662d5c72c29903813eJake Slack//
903928aee4356845252ac6b662d5c72c29903813eJake Slack//      The Eclipse Public License is available at
1003928aee4356845252ac6b662d5c72c29903813eJake Slack//      http://www.eclipse.org/legal/epl-v10.html
1103928aee4356845252ac6b662d5c72c29903813eJake Slack//
1203928aee4356845252ac6b662d5c72c29903813eJake Slack//      The Apache License v2.0 is available at
1303928aee4356845252ac6b662d5c72c29903813eJake Slack//      http://www.opensource.org/licenses/apache2.0.php
1403928aee4356845252ac6b662d5c72c29903813eJake Slack//
1503928aee4356845252ac6b662d5c72c29903813eJake Slack//  You may elect to redistribute this code under either of these licenses.
1603928aee4356845252ac6b662d5c72c29903813eJake Slack//  ========================================================================
1703928aee4356845252ac6b662d5c72c29903813eJake Slack//
1803928aee4356845252ac6b662d5c72c29903813eJake Slack
1903928aee4356845252ac6b662d5c72c29903813eJake Slackpackage org.eclipse.jetty.client.security;
2003928aee4356845252ac6b662d5c72c29903813eJake Slack
2103928aee4356845252ac6b662d5c72c29903813eJake Slack
2203928aee4356845252ac6b662d5c72c29903813eJake Slackimport java.io.IOException;
2303928aee4356845252ac6b662d5c72c29903813eJake Slack
2403928aee4356845252ac6b662d5c72c29903813eJake Slackimport org.eclipse.jetty.client.HttpExchange;
2503928aee4356845252ac6b662d5c72c29903813eJake Slackimport org.eclipse.jetty.http.HttpHeaders;
2603928aee4356845252ac6b662d5c72c29903813eJake Slackimport org.eclipse.jetty.io.Buffer;
2703928aee4356845252ac6b662d5c72c29903813eJake Slackimport org.eclipse.jetty.io.ByteArrayBuffer;
2803928aee4356845252ac6b662d5c72c29903813eJake Slackimport org.eclipse.jetty.util.B64Code;
2903928aee4356845252ac6b662d5c72c29903813eJake Slackimport org.eclipse.jetty.util.StringUtil;
3003928aee4356845252ac6b662d5c72c29903813eJake Slack
3103928aee4356845252ac6b662d5c72c29903813eJake Slack/**
3203928aee4356845252ac6b662d5c72c29903813eJake Slack * Sets authentication headers for BASIC authentication challenges
3303928aee4356845252ac6b662d5c72c29903813eJake Slack *
3403928aee4356845252ac6b662d5c72c29903813eJake Slack *
3503928aee4356845252ac6b662d5c72c29903813eJake Slack */
3603928aee4356845252ac6b662d5c72c29903813eJake Slackpublic class BasicAuthentication implements Authentication
3703928aee4356845252ac6b662d5c72c29903813eJake Slack{
3803928aee4356845252ac6b662d5c72c29903813eJake Slack    private Buffer _authorization;
3903928aee4356845252ac6b662d5c72c29903813eJake Slack
4003928aee4356845252ac6b662d5c72c29903813eJake Slack    public BasicAuthentication(Realm realm) throws IOException
4103928aee4356845252ac6b662d5c72c29903813eJake Slack    {
4203928aee4356845252ac6b662d5c72c29903813eJake Slack        String authenticationString = "Basic " + B64Code.encode( realm.getPrincipal() + ":" + realm.getCredentials(), StringUtil.__ISO_8859_1);
4303928aee4356845252ac6b662d5c72c29903813eJake Slack        _authorization= new ByteArrayBuffer(authenticationString);
4403928aee4356845252ac6b662d5c72c29903813eJake Slack    }
4503928aee4356845252ac6b662d5c72c29903813eJake Slack
4603928aee4356845252ac6b662d5c72c29903813eJake Slack    /**
4703928aee4356845252ac6b662d5c72c29903813eJake Slack     * BASIC authentication is of the form
4803928aee4356845252ac6b662d5c72c29903813eJake Slack     *
4903928aee4356845252ac6b662d5c72c29903813eJake Slack     * encoded credentials are of the form: username:password
5003928aee4356845252ac6b662d5c72c29903813eJake Slack     *
5103928aee4356845252ac6b662d5c72c29903813eJake Slack     *
5203928aee4356845252ac6b662d5c72c29903813eJake Slack     */
5303928aee4356845252ac6b662d5c72c29903813eJake Slack    public void setCredentials( HttpExchange exchange ) throws IOException
5403928aee4356845252ac6b662d5c72c29903813eJake Slack    {
5503928aee4356845252ac6b662d5c72c29903813eJake Slack        exchange.setRequestHeader( HttpHeaders.AUTHORIZATION_BUFFER, _authorization);
5603928aee4356845252ac6b662d5c72c29903813eJake Slack    }
5703928aee4356845252ac6b662d5c72c29903813eJake Slack}
58