001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.assertions.jwt;
019
020
021import com.nimbusds.jwt.proc.DefaultJWTClaimsVerifier;
022import com.nimbusds.oauth2.sdk.id.Audience;
023import com.nimbusds.oauth2.sdk.id.Identifier;
024import com.nimbusds.oauth2.sdk.util.CollectionUtils;
025import net.jcip.annotations.Immutable;
026
027import java.util.Arrays;
028import java.util.HashSet;
029import java.util.Set;
030
031
032/**
033 * JSON Web Token (JWT) bearer assertion details (claims set) verifier for
034 * OAuth 2.0 client authentication and authorisation grants. Intended for
035 * initial validation of JWT assertions:
036 *
037 * <ul>
038 *     <li>Audience check
039 *     <li>Expiration time check
040 *     <li>Not-before time check (is set)
041 *     <li>Subject and issuer presence check
042 * </ul>
043 *
044 * <p>Related specifications:
045 *
046 * <ul>
047 *     <li>JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication and
048 *         Authorization Grants (RFC 7523).
049 * </ul>
050 */
051@Immutable
052public class JWTAssertionDetailsVerifier extends DefaultJWTClaimsVerifier {
053
054
055        /**
056         * The expected audience.
057         */
058        private final Set<Audience> expectedAudience;
059
060
061        /**
062         * Creates a new JWT bearer assertion details (claims set) verifier.
063         *
064         * @param expectedAudience The expected audience (aud) claim values.
065         *                         Must not be empty or {@code null}. Should
066         *                         typically contain the token endpoint URI and
067         *                         for OpenID provider it may also include the
068         *                         issuer URI.
069         */
070        public JWTAssertionDetailsVerifier(final Set<Audience> expectedAudience) {
071
072                super(
073                        new HashSet<>(Identifier.toStringList(expectedAudience)),
074                        null,
075                        new HashSet<>(Arrays.asList("aud", "exp", "sub", "iss")),
076                        null);
077
078                if (CollectionUtils.isEmpty(expectedAudience)) {
079                        throw new IllegalArgumentException("The expected audience set must not be null or empty");
080                }
081
082                this.expectedAudience = expectedAudience;
083        }
084
085
086        /**
087         * Returns the expected audience values.
088         *
089         * @return The expected audience (aud) claim values.
090         */
091        public Set<Audience> getExpectedAudience() {
092
093                return expectedAudience;
094        }
095}