Path: blob/master/src/java.security.sasl/share/classes/com/sun/security/sasl/ExternalClient.java
41161 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.security.sasl;2627import javax.security.sasl.*;2829import static java.nio.charset.StandardCharsets.UTF_8;3031/**32* Implements the EXTERNAL SASL client mechanism.33* (<A HREF="http://www.ietf.org/rfc/rfc2222.txt">RFC 2222</A>).34* The EXTERNAL mechanism returns the optional authorization ID as35* the initial response. It processes no challenges.36*37* @author Rosanna Lee38*/39final class ExternalClient implements SaslClient {40private byte[] username;41private boolean completed = false;4243/**44* Constructs an External mechanism with optional authorization ID.45*46* @param authorizationID If non-null, used to specify authorization ID.47*/48ExternalClient(String authorizationID) {49if (authorizationID != null) {50username = authorizationID.getBytes(UTF_8);51} else {52username = new byte[0];53}54}5556/**57* Retrieves this mechanism's name for initiating the "EXTERNAL" protocol58* exchange.59*60* @return The string "EXTERNAL".61*/62public String getMechanismName() {63return "EXTERNAL";64}6566/**67* This mechanism has an initial response.68*/69public boolean hasInitialResponse() {70return true;71}7273public void dispose() throws SaslException {74}7576/**77* Processes the challenge data.78* It returns the EXTERNAL mechanism's initial response,79* which is the authorization id encoded in UTF-8.80* This is the optional information that is sent along with the SASL command.81* After this method is called, isComplete() returns true.82*83* @param challengeData Ignored.84* @return The possible empty initial response.85* @throws IllegalStateException If authentication has already been called.86*/87public byte[] evaluateChallenge(byte[] challengeData) {88if (completed) {89throw new IllegalStateException(90"EXTERNAL authentication already completed");91}92completed = true;93return username;94}9596/**97* Returns whether this mechanism is complete.98* @return true if initial response has been sent; false otherwise.99*/100public boolean isComplete() {101return completed;102}103104/**105* Unwraps the incoming buffer.106*107* @throws SaslException Not applicable to this mechanism.108*/109public byte[] unwrap(byte[] incoming, int offset, int len)110throws SaslException {111if (completed) {112throw new SaslException("EXTERNAL has no supported QOP");113} else {114throw new IllegalStateException(115"EXTERNAL authentication Not completed");116}117}118119/**120* Wraps the outgoing buffer.121*122* @throws SaslException Not applicable to this mechanism.123*/124public byte[] wrap(byte[] outgoing, int offset, int len)125throws SaslException {126if (completed) {127throw new SaslException("EXTERNAL has no supported QOP");128} else {129throw new IllegalStateException(130"EXTERNAL authentication not completed");131}132}133134/**135* Retrieves the negotiated property.136* This method can be called only after the authentication exchange has137* completed (i.e., when {@code isComplete()} returns true);138* otherwise, an {@code IllegalStateException} is thrown.139*140* @return null No property is applicable to this mechanism.141* @exception IllegalStateException if this authentication exchange142* has not completed143*/144public Object getNegotiatedProperty(String propName) {145if (completed) {146return null;147} else {148throw new IllegalStateException(149"EXTERNAL authentication not completed");150}151}152}153154155