001/* 002 * Copyright 2026 Revetware LLC. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.revetsec.scim; 018 019import com.google.errorprone.annotations.CheckReturnValue; 020import com.revetsec.json.*; 021import org.jspecify.annotations.NonNull; 022import javax.annotation.concurrent.Immutable; 023import java.util.ArrayList; 024import java.util.List; 025import java.util.Optional; 026import static java.util.Objects.requireNonNull; 027 028/** Intentional typed provisioning view; diagnostics are redacted. 029 * @since 1.0.0 030 */ 031@Immutable public final class ScimUser { 032 private final @NonNull ScimResource resource; 033 private ScimUser(@NonNull ScimResource resource) { this.resource = resource; } 034 /** Creates a typed view of a complete standard User schema. 035 * Equivalent rebuilt schemas are accepted; a foreign or altered schema is a programming error. 036 * @param resource the resource 037 * @return the view 038 * @since 1.0.0 039 */ 040 @CheckReturnValue public static @NonNull ScimUser fromResource(@NonNull ScimResource resource) { 041 requireNonNull(resource); 042 if (!resource.getResourceType().hasStandardUserSchema()) 043 throw new IllegalArgumentException("The resource does not have the required core schema."); 044 return new ScimUser(resource); 045 } 046 /** Returns the underlying resource. 047 * @return the resource 048 * @since 1.0.0 049 */ 050 public @NonNull ScimResource getResource() { return resource; } 051 /** Returns userName. 052 * @return the provisioning value 053 * @since 1.0.0 054 */ 055 public @NonNull String getUserName() { return resource.getDocument().find("userName").filter(JsonString.class::isInstance).map(JsonString.class::cast).map(JsonString::getValue).orElseThrow(); } 056 /** Returns displayName. 057 * @return the provisioning value 058 * @since 1.0.0 059 */ 060 public @NonNull Optional<@NonNull String> getDisplayName() { return resource.getDocument().find("displayName").filter(JsonString.class::isInstance).map(JsonString.class::cast).map(JsonString::getValue); } 061 /** Returns active. 062 * @return the provisioning value 063 * @since 1.0.0 064 */ 065 public @NonNull Optional<@NonNull Boolean> isActive() { return resource.getDocument().find("active").filter(JsonBoolean.class::isInstance).map(JsonBoolean.class::cast).map(JsonBoolean::getValue); } 066 /** Returns emails. 067 * @return the provisioning value 068 * @since 1.0.0 069 */ 070 public @NonNull List<@NonNull JsonObject> getEmails() { return objects("emails"); } 071 private @NonNull List<@NonNull JsonObject> objects(@NonNull String name) { 072 JsonValue raw = resource.getDocument().getMembers().get(name); 073 if (!(raw instanceof JsonArray array)) return List.of(); 074 List<JsonObject> objects = new ArrayList<>(); 075 for (JsonValue value : array.getElements()) if (value instanceof JsonObject object) objects.add(object); 076 return List.copyOf(objects); 077 } 078 /** Returns a fixed redacted description. 079 * @return the description 080 * @since 1.0.0 081 */ 082 @Override public @NonNull String toString() { return "ScimUser{resource=<redacted>}"; } 083}