001/* ***** BEGIN LICENSE BLOCK ***** 002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 003 * 004 * The contents of this file are subject to the Mozilla Public License Version 005 * 1.1 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * http://www.mozilla.org/MPL/ 008 * 009 * Software distributed under the License is distributed on an "AS IS" basis, 010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 011 * for the specific language governing rights and limitations under the 012 * License. 013 * 014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in 015 * Java(TM), hosted at https://github.com/gunterze/dcm4che. 016 * 017 * The Initial Developer of the Original Code is 018 * Agfa Healthcare. 019 * Portions created by the Initial Developer are Copyright (C) 2011 020 * the Initial Developer. All Rights Reserved. 021 * 022 * Contributor(s): 023 * See @authors listed below 024 * 025 * Alternatively, the contents of this file may be used under the terms of 026 * either the GNU General Public License Version 2 or later (the "GPL"), or 027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 028 * in which case the provisions of the GPL or the LGPL are applicable instead 029 * of those above. If you wish to allow use of your version of this file only 030 * under the terms of either the GPL or the LGPL, and not to allow others to 031 * use your version of this file under the terms of the MPL, indicate your 032 * decision by deleting the provisions above and replace them with the notice 033 * and other provisions required by the GPL or the LGPL. If you do not delete 034 * the provisions above, a recipient may use your version of this file under 035 * the terms of any one of the MPL, the GPL or the LGPL. 036 * 037 * ***** END LICENSE BLOCK ***** */ 038 039package org.dcm4che3.net.pdu; 040 041import java.io.UnsupportedEncodingException; 042 043import org.dcm4che3.util.StringUtils; 044 045/** 046 * @author Gunter Zeilinger <gunterze@gmail.com> 047 * 048 */ 049public class UserIdentityRQ { 050 051 public static final int USERNAME = 1; 052 public static final int USERNAME_PASSCODE = 2; 053 public static final int KERBEROS = 3; 054 public static final int SAML = 4; 055 056 private static final String[] TYPES = { 057 "0", 058 "1 - Username", 059 "2 - Username and passcode", 060 "3 - Kerberos Service ticket", 061 "4 - SAML Assertion" 062 }; 063 064 private final int type; 065 private final boolean rspReq; 066 private final byte[] primaryField; 067 private final byte[] secondaryField; 068 069 public UserIdentityRQ(int type, boolean rspReq, byte[] primaryField, 070 byte[] secondaryField) { 071 this.type = type; 072 this.rspReq = rspReq; 073 this.primaryField = primaryField.clone(); 074 this.secondaryField = secondaryField != null 075 ? secondaryField.clone() 076 : new byte[0]; 077 } 078 079 public UserIdentityRQ(int type, boolean rspReq, byte[] primaryField) { 080 this(type, rspReq, primaryField, null); 081 } 082 083 public UserIdentityRQ(String username, char[] passcode) { 084 this(USERNAME_PASSCODE, true, toBytes(username), 085 toBytes(new String(passcode))); 086 } 087 088 public UserIdentityRQ(String username, boolean rspReq) { 089 this(USERNAME, rspReq, toBytes(username)); 090 } 091 092 private static String typeAsString(int type) { 093 try { 094 return TYPES[type]; 095 } catch (IndexOutOfBoundsException e) { 096 return Integer.toString(type); 097 } 098 } 099 100 public final int getType() { 101 return type; 102 } 103 104 public final boolean isPositiveResponseRequested() { 105 return rspReq; 106 } 107 108 public final byte[] getPrimaryField() { 109 return primaryField.clone(); 110 } 111 112 public final byte[] getSecondaryField() { 113 return secondaryField.clone(); 114 } 115 116 public final String getUsername() { 117 return toString(primaryField); 118 } 119 120 public final char[] getPasscode() { 121 return toString(secondaryField).toCharArray(); 122 } 123 124 private static byte[] toBytes(String s) { 125 try { 126 return s.getBytes("UTF-8"); 127 } catch (UnsupportedEncodingException e) { 128 throw new Error(e); 129 } 130 } 131 132 private static String toString(byte[] b) { 133 try { 134 return new String(b, "UTF-8"); 135 } catch (UnsupportedEncodingException e) { 136 throw new Error(e); 137 } 138 } 139 140 public int length() { 141 return 6 + primaryField.length + secondaryField.length; 142 } 143 144 @Override 145 public String toString() { 146 return promptTo(new StringBuilder()).toString(); 147 } 148 149 StringBuilder promptTo(StringBuilder sb) { 150 sb.append(" UserIdentity[") 151 .append(StringUtils.LINE_SEPARATOR) 152 .append(" type: ") 153 .append(typeAsString(type)) 154 .append(StringUtils.LINE_SEPARATOR); 155 if (type == USERNAME 156 || type == USERNAME_PASSCODE) 157 sb.append(" username: ") 158 .append(getUsername()); 159 else 160 sb.append(" primaryField: byte[") 161 .append(primaryField.length) 162 .append(']'); 163 if (type == USERNAME_PASSCODE) { 164 sb.append(StringUtils.LINE_SEPARATOR) 165 .append(" passcode: "); 166 for (int i = secondaryField.length; --i >= 0;) 167 sb.append('*'); 168 } else if (secondaryField.length > 0) { 169 sb.append(StringUtils.LINE_SEPARATOR) 170 .append(" secondaryField: byte[") 171 .append(secondaryField.length) 172 .append(']'); 173 } 174 return sb.append(StringUtils.LINE_SEPARATOR) 175 .append(" ]"); 176 } 177 178}