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 */
016package com.revetsec.oauth;
017
018import static java.util.Objects.requireNonNull;
019import com.revetsec.jose.*;
020import com.revetsec.json.*;
021import java.time.*;
022import java.util.*;
023import javax.annotation.concurrent.Immutable;
024import org.jspecify.annotations.NonNull;
025import org.jspecify.annotations.Nullable;
026
027/**
028 * Access-token proof released only after all configured profile checks. Claims access is sensitive.
029 * No received credential is retained; diagnostic text contains no identity, claim or scope data.
030 * @author <a href="https://www.revetkn.com">Mark Allen</a>
031 * @since 1.0.0
032 */
033@Immutable
034public final class VerifiedAccessToken {
035
036        @NonNull
037        private final String issuer;
038
039        @Nullable
040        private final String subject;
041
042        @Nullable
043        private final String clientId;
044
045        @NonNull
046        private final Set<@NonNull String> scopes;
047
048        @NonNull
049        private final List<@NonNull String> audiences;
050
051        @Nullable
052        private final Instant expiresAt;
053
054        @NonNull
055        private final JsonObject claims;
056
057        VerifiedAccessToken(@NonNull String issuer, @Nullable String subject, @Nullable String clientId, @NonNull Set<@NonNull String> scopes, @NonNull List<@NonNull String> audiences, @Nullable Instant expiresAt, @NonNull JsonObject claims) {
058                this.issuer = requireNonNull(issuer);
059                this.subject = subject;
060                this.clientId = clientId;
061                this.scopes = Set.copyOf(scopes);
062                this.audiences = List.copyOf(audiences);
063                this.expiresAt = expiresAt;
064                this.claims = requireNonNull(claims);
065        }
066
067        /**
068         * Returns the configured authoritative issuer; introspection may omit the issuer member.
069         * @return issuer
070         * @since 1.0.0
071         */
072        @NonNull
073        public String getIssuer() {
074                return this.issuer;
075        }
076
077        /**
078         * Returns the checked subject, when supplied.
079         * @return subject
080         * @since 1.0.0
081         */
082        @NonNull
083        public Optional<@NonNull String> getSubject() {
084                return Optional.ofNullable(this.subject);
085        }
086
087        /**
088         * Returns the checked client ID, when supplied.
089         * @return clientId
090         * @since 1.0.0
091         */
092        @NonNull
093        public Optional<@NonNull String> getClientId() {
094                return Optional.ofNullable(this.clientId);
095        }
096
097        /**
098         * Returns the checked scopes; the application decides permission.
099         * @return scopes
100         * @since 1.0.0
101         */
102        @NonNull
103        public Set<@NonNull String> getScopes() {
104                return this.scopes;
105        }
106
107        /**
108         * Returns the checked audiences.
109         * @return audiences
110         * @since 1.0.0
111         */
112        @NonNull
113        public List<@NonNull String> getAudiences() {
114                return this.audiences;
115        }
116
117        /**
118         * Returns the checked expiry, when supplied.
119         * @return expiresAt
120         * @since 1.0.0
121         */
122        @NonNull
123        public Optional<@NonNull Instant> getExpiresAt() {
124                return Optional.ofNullable(this.expiresAt);
125        }
126
127        /**
128         * Returns the checked claims; this explicit access may reveal sensitive data.
129         * @return claims
130         * @since 1.0.0
131         */
132        @NonNull
133        public JsonObject getClaims() {
134                return this.claims;
135        }
136
137        /**
138         * Returns a redacted description.
139         * @return description
140         * @since 1.0.0
141         */
142        @Override
143        @NonNull
144        public String toString() {
145                return "VerifiedAccessToken{data=<redacted>}";
146        }
147}