001/* 002 * Copyright 2017-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2017-2020 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2017-2020 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.ldap.listener; 037 038 039 040import com.unboundid.ldap.sdk.LDAPException; 041import com.unboundid.ldap.sdk.ResultCode; 042import com.unboundid.util.Base64; 043import com.unboundid.util.Debug; 044import com.unboundid.util.StaticUtils; 045import com.unboundid.util.ThreadSafety; 046import com.unboundid.util.ThreadSafetyLevel; 047 048import static com.unboundid.ldap.listener.ListenerMessages.*; 049 050 051 052/** 053 * This class provides an implementation of a password encoder output formatter 054 * that will format the encoded password using the base64 mechanism described in 055 * <A HREF="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</A>. 056 */ 057@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 058public final class Base64PasswordEncoderOutputFormatter 059 extends PasswordEncoderOutputFormatter 060{ 061 /** 062 * The singleton instance of this base64 password encoder output formatter. 063 */ 064 private static final Base64PasswordEncoderOutputFormatter INSTANCE = 065 new Base64PasswordEncoderOutputFormatter(); 066 067 068 069 /** 070 * Creates an instance of this base64 password encoder output formatter. 071 */ 072 private Base64PasswordEncoderOutputFormatter() 073 { 074 // No implementation is required. 075 } 076 077 078 079 /** 080 * Retrieves the singleton instance of this base64 password encoder output 081 * formatter. 082 * 083 * @return The singleton instance of this base64 password encoder output 084 * formatter. 085 */ 086 public static Base64PasswordEncoderOutputFormatter getInstance() 087 { 088 return INSTANCE; 089 } 090 091 092 093 /** 094 * {@inheritDoc} 095 */ 096 @Override() 097 public byte[] format(final byte[] unformattedData) 098 throws LDAPException 099 { 100 return StaticUtils.getBytes(Base64.encode(unformattedData)); 101 } 102 103 104 105 /** 106 * {@inheritDoc} 107 */ 108 @Override() 109 public byte[] unFormat(final byte[] formattedData) 110 throws LDAPException 111 { 112 try 113 { 114 return Base64.decode(StaticUtils.toUTF8String(formattedData)); 115 } 116 catch (final Exception e) 117 { 118 Debug.debugException(e); 119 throw new LDAPException(ResultCode.PARAM_ERROR, 120 ERR_BASE64_PW_FORMATTER_CANNOT_DECODE.get(), e); 121 } 122 } 123 124 125 126 /** 127 * {@inheritDoc} 128 */ 129 @Override() 130 public void toString(final StringBuilder buffer) 131 { 132 buffer.append("Base64PasswordEncoderOutputFormatter()"); 133 } 134}