1 /*
2 (C) Copyright IBM Corp. 2006, 2010
3
4 THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5 ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6 CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7
8 You can obtain a current copy of the Eclipse Public License from
9 http://www.opensource.org/licenses/eclipse-1.0.php
10
11 @author : Endre Bak, ebak@de.ibm.com
12 *
13 * Change History
14 * Flag Date Prog Description
15 *-------------------------------------------------------------------------------------------------
16 * 2006-04-25 ebak Initial commit
17 * 2006-04-27 wolfalex Full implementation
18 * 2003590 2008-06-30 blaschke-oss Change licensing from CPL to EPL
19 * 2524131 2009-01-21 raman_arora Upgrade client to JDK 1.5 (Phase 1)
20 * 2899859 2009-11-18 blaschke-oss javax.wbem.client missing JSR48 credential/principal APIs
21 * 2959264 2010-02-25 blaschke-oss Sync up javax.client.* javadoc with JSR48 1.0.0
22 */
23 package org.metricshub.wbem.javax.wbem.client;
24
25 /*-
26 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
27 * WBEM Java Client
28 * ჻჻჻჻჻჻
29 * Copyright 2023 - 2025 MetricsHub
30 * ჻჻჻჻჻჻
31 * Licensed under the Apache License, Version 2.0 (the "License");
32 * you may not use this file except in compliance with the License.
33 * You may obtain a copy of the License at
34 *
35 * http://www.apache.org/licenses/LICENSE-2.0
36 *
37 * Unless required by applicable law or agreed to in writing, software
38 * distributed under the License is distributed on an "AS IS" BASIS,
39 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40 * See the License for the specific language governing permissions and
41 * limitations under the License.
42 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
43 */
44
45 import java.security.Principal;
46
47 //Sync'd against JSR48 1.0.0 javadoc (build 1.5.0_10) on Wed Jan 20 02:20:59 EST 2010
48 /**
49 * <code>UserPrincipal</code> implements a Principal identity for a client user
50 * identity that authenticates with a username and password. In other words, it
51 * represents the user's login identity on the remote system.
52 * <code>UserPrincipal</code> includes the username and optionally the host
53 * information for which the username is used to authenticate.
54 */
55 public class UserPrincipal implements Principal {
56 private String iUserName;
57
58 private String iHostName;
59
60 /**
61 * This constructor accepts the user name.
62 *
63 * @param pUserName
64 * The user login name.
65 * @throws IllegalArgumentException
66 * If the <codepUuserName</code> is <code>null</code>.
67 */
68 public UserPrincipal(String pUserName) throws IllegalArgumentException {
69 if (pUserName == null) throw new IllegalArgumentException("userName is null!");
70 this.iUserName = pUserName;
71 }
72
73 /**
74 * This constructor accepts the user name and host name.
75 *
76 * @param pUserName
77 * The user login name.
78 * @param pHostName
79 * The host name for this principal.
80 * @throws IllegalArgumentException
81 * If the <code>pUserName</code> is <code>null</code>.
82 */
83 public UserPrincipal(String pUserName, String pHostName) throws IllegalArgumentException {
84 if (pUserName == null) throw new IllegalArgumentException("userName is null!");
85 this.iUserName = pUserName;
86 this.iHostName = pHostName;
87 }
88
89 /**
90 * The <code>equals</code> method checks if the specified object is the same
91 * principal as this object. The principals are equal if the specified
92 * object is an instance of <code>UserPrincipal</code> and the user name and
93 * authentication host name are the same.
94 *
95 * @param pOtherPrincipal
96 * <code>Principal</code> instance to compare for equality.
97 * @return <code>true</code> if the object are equal; <code>false</code>
98 * otherwise.
99 */
100 @Override
101 public boolean equals(Object pOtherPrincipal) {
102 if (!(pOtherPrincipal instanceof UserPrincipal)) return false;
103 UserPrincipal that = (UserPrincipal) pOtherPrincipal;
104 return (
105 (this.iUserName == null ? that.iUserName == null : this.iUserName.equalsIgnoreCase(that.iUserName)) &&
106 (this.iHostName == null ? that.iHostName == null : this.iHostName.equalsIgnoreCase(that.iHostName))
107 );
108 }
109
110 /**
111 * Return the host name associated with this principal.
112 *
113 * @return The host name.
114 */
115 public String getHostName() {
116 return this.iHostName;
117 }
118
119 /**
120 * Return the name of this principal identity; that is, return the login
121 * name.
122 *
123 * @return The name of this principal identity.
124 * @see java.security.Principal#getName()
125 */
126 public String getName() {
127 return this.iUserName;
128 }
129
130 /**
131 * Return the principal's login user name.
132 *
133 * @return The user login name.
134 */
135 public String getUserName() {
136 return this.iUserName;
137 }
138
139 /**
140 * The <code>hashCode</code> method returns an integer hash code to
141 * represent this principal. It can be used to test for non-equality, or as
142 * an index key in a hash table.
143 *
144 * @return An integer hash code representing the principal.
145 */
146 @Override
147 public int hashCode() {
148 return this.iUserName != null ? this.iUserName.hashCode() : 0;
149 }
150
151 /**
152 * The <code>toString</code> method returns a string representation of the
153 * principal suitable for displaying in messages. It should not be used for
154 * making authorization checks, however.
155 *
156 * @return A printable string form of the principal identity.
157 */
158 @Override
159 public String toString() {
160 return this.iUserName;
161 }
162 }