|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * The contents of this file are subject to the terms of either the GNU |
| 7 | + * General Public License Version 2 only ("GPL") or the Common Development |
| 8 | + * and Distribution License("CDDL") (collectively, the "License"). You |
| 9 | + * may not use this file except in compliance with the License. You can |
| 10 | + * obtain a copy of the License at |
| 11 | + * http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 | + * or packager/legal/LICENSE.txt. See the License for the specific |
| 13 | + * language governing permissions and limitations under the License. |
| 14 | + * |
| 15 | + * When distributing the software, include this License Header Notice in each |
| 16 | + * file and include the License file at packager/legal/LICENSE.txt. |
| 17 | + * |
| 18 | + * GPL Classpath Exception: |
| 19 | + * Oracle designates this particular file as subject to the "Classpath" |
| 20 | + * exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 | + * file that accompanied this code. |
| 22 | + * |
| 23 | + * Modifications: |
| 24 | + * If applicable, add the following below the License Header, with the fields |
| 25 | + * enclosed by brackets [] replaced by your own identifying information: |
| 26 | + * "Portions Copyright [year] [name of copyright owner]" |
| 27 | + * |
| 28 | + * Contributor(s): |
| 29 | + * If you wish your version of this file to be governed by only the CDDL or |
| 30 | + * only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 | + * elects to include this software in this distribution under the [CDDL or GPL |
| 32 | + * Version 2] license." If you don't indicate a single choice of license, a |
| 33 | + * recipient has the option to distribute your version of this file under |
| 34 | + * either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 | + * its licensees as provided above. However, if you add GPL Version 2 code |
| 36 | + * and therefore, elected the GPL Version 2 license, then the option applies |
| 37 | + * only if the new code is made subject to such option by the copyright |
| 38 | + * holder. |
| 39 | + */ |
| 40 | +package javax.security.identitystore; |
| 41 | + |
| 42 | +import static java.util.Collections.unmodifiableList; |
| 43 | +import static javax.security.identitystore.CredentialValidationResult.Status.INVALID; |
| 44 | +import static javax.security.identitystore.CredentialValidationResult.Status.NOT_VALIDATED; |
| 45 | +import static javax.security.identitystore.CredentialValidationResult.Status.VALID; |
| 46 | + |
| 47 | +import java.util.ArrayList; |
| 48 | +import java.util.List; |
| 49 | + |
| 50 | +/** |
| 51 | + * <code>CredentialValidationResult</code> is the result from an attempt to |
| 52 | + * validate an instance of |
| 53 | + * {@link javax.security.identitystore.credential.Credential}. |
| 54 | + * |
| 55 | + * @see javax.security.identitystore.IdentityStore#validate |
| 56 | + */ |
| 57 | +public class CredentialValidationResult { |
| 58 | + |
| 59 | + public static final CredentialValidationResult INVALID_RESULT = new CredentialValidationResult(INVALID, null, null, null); |
| 60 | + public static final CredentialValidationResult NOT_VALIDATED_RESULT = new CredentialValidationResult(NOT_VALIDATED, null, null, null); |
| 61 | + |
| 62 | + private final String callerName; |
| 63 | + private final Status status; |
| 64 | + private final List<String> roles; |
| 65 | + private final List<String> groups; |
| 66 | + |
| 67 | + public enum Status { |
| 68 | + /** |
| 69 | + * Indicates that the credential could not be validated, for example, if |
| 70 | + * no suitable |
| 71 | + * {@link javax.security.identitystore.credential.CredentialValidator} |
| 72 | + * could be found. |
| 73 | + */ |
| 74 | + NOT_VALIDATED, |
| 75 | + |
| 76 | + /** |
| 77 | + * Indicates that the credential is not valid after a validation |
| 78 | + * attempt. |
| 79 | + */ |
| 80 | + INVALID, |
| 81 | + |
| 82 | + /** |
| 83 | + * Indicates that the credential is valid after a validation attempt. |
| 84 | + */ |
| 85 | + VALID |
| 86 | + }; |
| 87 | + |
| 88 | + public CredentialValidationResult(Status status, String callerName, List<String> groups) { |
| 89 | + this(status, callerName, groups, null); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Constructor |
| 94 | + * |
| 95 | + * @param status |
| 96 | + * Validation status |
| 97 | + * @param callerName |
| 98 | + * Validated caller |
| 99 | + * @param groups |
| 100 | + * Groups associated with the caller from the identity store |
| 101 | + * @param roles |
| 102 | + * Roles associated with the caller from the identity store |
| 103 | + */ |
| 104 | + public CredentialValidationResult(Status status, String callerName, List<String> groups, List<String> roles) { |
| 105 | + |
| 106 | + if (null == status) |
| 107 | + throw new NullPointerException("status"); |
| 108 | + |
| 109 | + this.status = status; |
| 110 | + this.callerName = callerName; |
| 111 | + |
| 112 | + if (VALID == status) { |
| 113 | + if (null != groups) |
| 114 | + groups = unmodifiableList(new ArrayList<>(groups)); |
| 115 | + this.groups = groups; |
| 116 | + |
| 117 | + if (null != roles) |
| 118 | + roles = unmodifiableList(new ArrayList<>(roles)); |
| 119 | + this.roles = roles; |
| 120 | + } else { |
| 121 | + this.groups = null; |
| 122 | + this.roles = null; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Determines the validation status. |
| 128 | + * |
| 129 | + * @return The validation status |
| 130 | + */ |
| 131 | + public Status getStatus() { |
| 132 | + return status; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Determines the Caller used to validate the credential. |
| 137 | + * |
| 138 | + * @return The caller name, <code>null</code> if {@link #getStatus} does not |
| 139 | + * return {@link Status#VALID VALID}. |
| 140 | + */ |
| 141 | + public String getCallerName() { |
| 142 | + return callerName; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Determines the list of groups that the specified Caller is in, based on |
| 147 | + * the associated persistence store.. |
| 148 | + * |
| 149 | + * @return The list of groups that the specified Caller is in, empty if |
| 150 | + * none. <code>null</code> if {@link #getStatus} does not return |
| 151 | + * {@link Status#VALID VALID} or if the identity store does not |
| 152 | + * support groups. |
| 153 | + */ |
| 154 | + public List<String> getCallerGroups() { |
| 155 | + return groups; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Determines the list of roles that the specified caller is in, based on |
| 160 | + * the associated persistence store. The returned role list would include |
| 161 | + * roles directly assigned to the Caller, and roles assigned to groups which |
| 162 | + * contain the Caller. |
| 163 | + * |
| 164 | + * @return The list of roles that the specified caller is in, empty if none. |
| 165 | + * <code>null</code> if {@link #getStatus} does not return |
| 166 | + * {@link Status#VALID VALID} or if the identity store does not |
| 167 | + * support roles. |
| 168 | + */ |
| 169 | + public List<String> getCallerRoles() { |
| 170 | + return roles; |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments