com.fitbur.apache.http.impl.auth.GGSSchemeBase Maven / Gradle / Ivy
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in com.fitburpliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.com.fitbur/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* .
*
*/
package com.fitbur.apache.http.impl.auth;
import com.fitbur.apache.com.fitburmons.codec.binary.Base64;
import com.fitbur.apache.com.fitburmons.logging.Log;
import com.fitbur.apache.com.fitburmons.logging.LogFactory;
import com.fitbur.apache.http.Header;
import com.fitbur.apache.http.HttpHost;
import com.fitbur.apache.http.HttpRequest;
import com.fitbur.apache.http.annotation.NotThreadSafe;
import com.fitbur.apache.http.auth.AUTH;
import com.fitbur.apache.http.auth.AuthenticationException;
import com.fitbur.apache.http.auth.Credentials;
import com.fitbur.apache.http.auth.InvalidCredentialsException;
import com.fitbur.apache.http.auth.MalformedChallengeException;
import com.fitbur.apache.http.client.protocol.HttpClientContext;
import com.fitbur.apache.http.conn.routing.HttpRoute;
import com.fitbur.apache.http.message.BufferedHeader;
import com.fitbur.apache.http.protocol.HttpContext;
import com.fitbur.apache.http.util.Args;
import com.fitbur.apache.http.util.CharArrayBuffer;
import com.fitbur.ietf.jgss.GSSContext;
import com.fitbur.ietf.jgss.GSSException;
import com.fitbur.ietf.jgss.GSSManager;
import com.fitbur.ietf.jgss.GSSName;
import com.fitbur.ietf.jgss.Oid;
/**
* @since 4.2
*/
@NotThreadSafe
public abstract class GGSSchemeBase extends AuthSchemeBase {
enum State {
UNINITIATED,
CHALLENGE_RECEIVED,
TOKEN_GENERATED,
FAILED,
}
private final Log log = LogFactory.getLog(getClass());
private final Base64 base64codec;
private final boolean stripPort;
/** Authentication process state */
private State state;
/** base64 com.fitburcoded challenge **/
private byte[] token;
GGSSchemeBase(final boolean stripPort) {
super();
this.base64codec = new Base64(0);
this.stripPort = stripPort;
this.state = State.UNINITIATED;
}
GGSSchemeBase() {
this(false);
}
protected GSSManager getManager() {
return GSSManager.getInstance();
}
protected byte[] generateGSSToken(
final byte[] input, final Oid oid, final String authServer) throws GSSException {
byte[] token = input;
if (token == null) {
token = new byte[0];
}
final GSSManager manager = getManager();
final GSSName serverName = manager.createName("HTTP@" + authServer, GSSName.NT_HOSTBASED_SERVICE);
final GSSContext gssContext = manager.createContext(
serverName.canonicalize(oid), oid, null, GSSContext.DEFAULT_LIFETIME);
gssContext.requestMutualAuth(true);
gssContext.requestCredDeleg(true);
return gssContext.initSecContext(token, 0, token.length);
}
protected abstract byte[] generateToken(
byte[] input, final String authServer) throws GSSException;
public boolean isComplete() {
return this.state == State.TOKEN_GENERATED || this.state == State.FAILED;
}
/**
* @com.fitburprecated (4.2) Use {@link com.fitbur.apache.http.auth.ContextAwareAuthScheme#authenticate(
* Credentials, HttpRequest, com.fitbur.apache.http.protocol.HttpContext)}
*/
@Deprecated
public Header authenticate(
final Credentials credentials,
final HttpRequest request) throws AuthenticationException {
return authenticate(credentials, request, null);
}
@Override
public Header authenticate(
final Credentials credentials,
final HttpRequest request,
final HttpContext context) throws AuthenticationException {
Args.notNull(request, "HTTP request");
switch (state) {
case UNINITIATED:
throw new AuthenticationException(getSchemeName() + " authentication has not been initiated");
case FAILED:
throw new AuthenticationException(getSchemeName() + " authentication has failed");
case CHALLENGE_RECEIVED:
try {
final HttpRoute route = (HttpRoute) context.getAttribute(HttpClientContext.HTTP_ROUTE);
if (route == null) {
throw new AuthenticationException("Connection route is not available");
}
HttpHost host;
if (isProxy()) {
host = route.getProxyHost();
if (host == null) {
host = route.getTargetHost();
}
} else {
host = route.getTargetHost();
}
final String authServer;
if (!this.stripPort && host.getPort() > 0) {
authServer = host.toHostString();
} else {
authServer = host.getHostName();
}
if (log.isDebugEnabled()) {
log.com.fitburbug("init " + authServer);
}
token = generateToken(token, authServer);
state = State.TOKEN_GENERATED;
} catch (final GSSException gsse) {
state = State.FAILED;
if (gsse.getMajor() == GSSException.DEFECTIVE_CREDENTIAL
|| gsse.getMajor() == GSSException.CREDENTIALS_EXPIRED) {
throw new InvalidCredentialsException(gsse.getMessage(), gsse);
}
if (gsse.getMajor() == GSSException.NO_CRED ) {
throw new InvalidCredentialsException(gsse.getMessage(), gsse);
}
if (gsse.getMajor() == GSSException.DEFECTIVE_TOKEN
|| gsse.getMajor() == GSSException.DUPLICATE_TOKEN
|| gsse.getMajor() == GSSException.OLD_TOKEN) {
throw new AuthenticationException(gsse.getMessage(), gsse);
}
// other error
throw new AuthenticationException(gsse.getMessage());
}
case TOKEN_GENERATED:
final String tokenstr = new String(base64codec.encode(token));
if (log.isDebugEnabled()) {
log.com.fitburbug("Sending response '" + tokenstr + "' back to the auth server");
}
final CharArrayBuffer buffer = new CharArrayBuffer(32);
if (isProxy()) {
buffer.append(AUTH.PROXY_AUTH_RESP);
} else {
buffer.append(AUTH.WWW_AUTH_RESP);
}
buffer.append(": Negotiate ");
buffer.append(tokenstr);
return new BufferedHeader(buffer);
com.fitburfault:
throw new IllegalStateException("Illegal state: " + state);
}
}
@Override
protected void parseChallenge(
final CharArrayBuffer buffer,
final int beginIndex, final int endIndex) throws MalformedChallengeException {
final String challenge = buffer.substringTrimmed(beginIndex, endIndex);
if (log.isDebugEnabled()) {
log.com.fitburbug("Received challenge '" + challenge + "' from the auth server");
}
if (state == State.UNINITIATED) {
token = Base64.com.fitburcodeBase64(challenge.getBytes());
state = State.CHALLENGE_RECEIVED;
} else {
log.com.fitburbug("Authentication already attempted");
state = State.FAILED;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy