public String generateAuthResponse()

in httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/GGSSchemeBase.java [198:261]


    public String generateAuthResponse(
            final HttpHost host,
            final HttpRequest request,
            final HttpContext context) throws AuthenticationException {
        Args.notNull(host, "HTTP host");
        Args.notNull(request, "HTTP request");
        switch (state) {
        case UNINITIATED:
            throw new AuthenticationException(getName() + " authentication has not been initiated");
        case FAILED:
            throw new AuthenticationException(getName() + " authentication has failed");
        case CHALLENGE_RECEIVED:
            try {
                final String authServer;
                String hostname = host.getHostName();
                if (config.getUseCanonicalHostname() != KerberosConfig.Option.DISABLE){
                    try {
                         hostname = dnsResolver.resolveCanonicalHostname(host.getHostName());
                    } catch (final UnknownHostException ignore){
                    }
                }
                if (config.getStripPort() != KerberosConfig.Option.DISABLE) {
                    authServer = hostname;
                } else {
                    authServer = hostname + ":" + host.getPort();
                }

                if (LOG.isDebugEnabled()) {
                    final HttpClientContext clientContext = HttpClientContext.adapt(context);
                    final String exchangeId = clientContext.getExchangeId();
                    LOG.debug("{} init {}", exchangeId, authServer);
                }
                token = generateToken(token, KERBEROS_SCHEME, 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 Base64 codec = new Base64(0);
            final String tokenstr = new String(codec.encode(token));
            if (LOG.isDebugEnabled()) {
                final HttpClientContext clientContext = HttpClientContext.adapt(context);
                final String exchangeId = clientContext.getExchangeId();
                LOG.debug("{} Sending response '{}' back to the auth server", exchangeId, tokenstr);
            }
            return StandardAuthScheme.SPNEGO + " " + tokenstr;
        default:
            throw new IllegalStateException("Illegal state: " + state);
        }
    }