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.oidc;
018
019import com.revetsec.json.JsonObject;
020import com.revetsec.json.JsonString;
021import com.revetsec.json.JsonBoolean;
022import org.jspecify.annotations.NonNull;
023import javax.annotation.concurrent.Immutable;
024import java.util.Optional;
025import static java.util.Objects.requireNonNull;
026
027/**
028 * UserInfo whose subject exactly matches a verified authentication. JSON responses rely on the provider's TLS
029 * endpoint; signed responses additionally pass JOSE issuer, audience, signature and profile checks. Claims can
030 * contain personal data. The exact issuer/subject pair is the account key; email is a mutable attribute.
031 * No public constructor or parse-only factory exists.
032 * @author <a href="https://www.revetkn.com">Mark Allen</a>
033 * @since 1.0.0
034 */
035@Immutable
036public final class OidcUserInfo {
037        private final String issuer;
038        private final JsonObject claims;
039        private final boolean signed;
040        OidcUserInfo(@NonNull String issuer, @NonNull JsonObject claims, boolean signed) {
041                this.issuer = requireNonNull(issuer); this.claims = requireNonNull(claims); this.signed = signed;
042        }
043        /**
044         * Returns the exact provider issuer to which this result is bound.
045         * @return issuer
046         * @since 1.0.0
047         */
048        public @NonNull String getIssuer() { return this.issuer; }
049        /**
050         * Returns the subject already matched against the validated ID token.
051         * @return subject
052         * @since 1.0.0
053         */
054        public @NonNull String getSubject() { return ((JsonString) requireNonNull(this.claims.getMembers().get("sub"))).getValue(); }
055        /**
056         * Explicitly returns the checked claims. JSON serialization deliberately emits personal data; do not log it.
057         * Aggregated/distributed claim references remain data and are never followed by Revetsec.
058         * @return immutable claims object
059         * @since 1.0.0
060         */
061        public @NonNull JsonObject getClaims() { return this.claims; }
062        /**
063         * Returns whether the response was authenticated with the configured signing algorithm.
064         * @return whether signed
065         * @since 1.0.0
066         */
067        public @NonNull Boolean isSigned() { return this.signed; }
068        /**
069         * Redacts claims and identity.
070         * @return redacted description
071         * @since 1.0.0
072         */
073        @Override public @NonNull String toString() { return "OidcUserInfo{claims=<redacted>}"; }
074}