Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/security/sasl/ExternalClient.java
38924 views
/*1* Copyright (c) 1999, 2019, 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.*;2829/**30* Implements the EXTERNAL SASL client mechanism.31* (<A HREF="http://www.ietf.org/rfc/rfc2222.txt">RFC 2222</A>).32* The EXTERNAL mechanism returns the optional authorization ID as33* the initial response. It processes no challenges.34*35* @author Rosanna Lee36*/37final class ExternalClient implements SaslClient {38private byte[] username;39private boolean completed = false;4041/**42* Constructs an External mechanism with optional authorization ID.43*44* @param authorizationID If non-null, used to specify authorization ID.45* @throws SaslException if cannot convert authorizationID into UTF-846* representation.47*/48ExternalClient(String authorizationID) throws SaslException {49if (authorizationID != null) {50try {51username = authorizationID.getBytes("UTF8");52} catch (java.io.UnsupportedEncodingException e) {53throw new SaslException("Cannot convert " + authorizationID +54" into UTF-8", e);55}56} else {57username = new byte[0];58}59}6061/**62* Retrieves this mechanism's name for initiating the "EXTERNAL" protocol63* exchange.64*65* @return The string "EXTERNAL".66*/67public String getMechanismName() {68return "EXTERNAL";69}7071/**72* This mechanism has an initial response.73*/74public boolean hasInitialResponse() {75return true;76}7778public void dispose() throws SaslException {79}8081/**82* Processes the challenge data.83* It returns the EXTERNAL mechanism's initial response,84* which is the authorization id encoded in UTF-8.85* This is the optional information that is sent along with the SASL command.86* After this method is called, isComplete() returns true.87*88* @param challengeData Ignored.89* @return The possible empty initial response.90* @throws SaslException If authentication has already been called.91*/92public byte[] evaluateChallenge(byte[] challengeData)93throws SaslException {94if (completed) {95throw new IllegalStateException(96"EXTERNAL authentication already completed");97}98completed = true;99return username;100}101102/**103* Returns whether this mechanism is complete.104* @return true if initial response has been sent; false otherwise.105*/106public boolean isComplete() {107return completed;108}109110/**111* Unwraps the incoming buffer.112*113* @throws SaslException Not applicable to this mechanism.114*/115public byte[] unwrap(byte[] incoming, int offset, int len)116throws SaslException {117if (completed) {118throw new SaslException("EXTERNAL has no supported QOP");119} else {120throw new IllegalStateException(121"EXTERNAL authentication Not completed");122}123}124125/**126* Wraps the outgoing buffer.127*128* @throws SaslException Not applicable to this mechanism.129*/130public byte[] wrap(byte[] outgoing, int offset, int len)131throws SaslException {132if (completed) {133throw new SaslException("EXTERNAL has no supported QOP");134} else {135throw new IllegalStateException(136"EXTERNAL authentication not completed");137}138}139140/**141* Retrieves the negotiated property.142* This method can be called only after the authentication exchange has143* completed (i.e., when {@code isComplete()} returns true); otherwise, a144* {@code IllegalStateException} is thrown.145*146* @return null No property is applicable to this mechanism.147* @exception IllegalStateException if this authentication exchange148* has not completed149*/150public Object getNegotiatedProperty(String propName) {151if (completed) {152return null;153} else {154throw new IllegalStateException(155"EXTERNAL authentication not completed");156}157}158}159160161