001/* 002 * Copyright 2017-2024 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2017-2024 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-2024 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.NotNull; 045import com.unboundid.util.StaticUtils; 046import com.unboundid.util.ThreadSafety; 047import com.unboundid.util.ThreadSafetyLevel; 048 049import static com.unboundid.ldap.listener.ListenerMessages.*; 050 051 052 053/** 054 * This class provides an implementation of a password encoder output formatter 055 * that will format the encoded password using the base64 mechanism described in 056 * <A HREF="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</A>. 057 */ 058@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 059public final class Base64PasswordEncoderOutputFormatter 060 extends PasswordEncoderOutputFormatter 061{ 062 /** 063 * The singleton instance of this base64 password encoder output formatter. 064 */ 065 @NotNull private static final Base64PasswordEncoderOutputFormatter INSTANCE = 066 new Base64PasswordEncoderOutputFormatter(); 067 068 069 070 /** 071 * Creates an instance of this base64 password encoder output formatter. 072 */ 073 private Base64PasswordEncoderOutputFormatter() 074 { 075 // No implementation is required. 076 } 077 078 079 080 /** 081 * Retrieves the singleton instance of this base64 password encoder output 082 * formatter. 083 * 084 * @return The singleton instance of this base64 password encoder output 085 * formatter. 086 */ 087 @NotNull() 088 public static Base64PasswordEncoderOutputFormatter getInstance() 089 { 090 return INSTANCE; 091 } 092 093 094 095 /** 096 * {@inheritDoc} 097 */ 098 @Override() 099 @NotNull() 100 public byte[] format(@NotNull final byte[] unformattedData) 101 throws LDAPException 102 { 103 return StaticUtils.getBytes(Base64.encode(unformattedData)); 104 } 105 106 107 108 /** 109 * {@inheritDoc} 110 */ 111 @Override() 112 @NotNull() 113 public byte[] unFormat(@NotNull final byte[] formattedData) 114 throws LDAPException 115 { 116 try 117 { 118 return Base64.decode(StaticUtils.toUTF8String(formattedData)); 119 } 120 catch (final Exception e) 121 { 122 Debug.debugException(e); 123 throw new LDAPException(ResultCode.PARAM_ERROR, 124 ERR_BASE64_PW_FORMATTER_CANNOT_DECODE.get(), e); 125 } 126 } 127 128 129 130 /** 131 * {@inheritDoc} 132 */ 133 @Override() 134 public void toString(@NotNull final StringBuilder buffer) 135 { 136 buffer.append("Base64PasswordEncoderOutputFormatter()"); 137 } 138}