001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.client.mcp.auth;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021
022/**
023 * Signals that an MCP call was rejected with a SEP-2350 {@code 401}/{@code 403 insufficient_scope} step-up challenge.
024 *
025 * <p>
026 * A caller-supplied {@link McpStepUpAuthorizer.ScopedCall} throws this (carrying the parsed
027 * {@link WwwAuthenticateChallenge}) so {@link McpStepUpAuthorizer} can compute the scope union, re-authorize, and retry.
028 * It is deliberately <b>not</b> an {@link McpAuthException}: the transient step-up signal must be distinguishable from
029 * the permanent authorization failure the authorizer ultimately throws when retries are exhausted.
030 *
031 * @since 10.0.0
032 */
033public class McpInsufficientScopeException extends RuntimeException {
034
035   private static final long serialVersionUID = 1L;
036
037   // Non-transient: WwwAuthenticateChallenge is Serializable (a scheme string + immutable string map), so the
038   // challenge survives serialization and the challenge() "Never null" guarantee holds after a round-trip.
039   private final WwwAuthenticateChallenge challenge;
040
041   /**
042    * Constructor.
043    *
044    * @param challenge The parsed {@code WWW-Authenticate} challenge from the server.  Must not be <jk>null</jk>.
045    */
046   public McpInsufficientScopeException(WwwAuthenticateChallenge challenge) {
047      super("MCP call rejected with an insufficient_scope step-up challenge: "
048         + reqnn("challenge", challenge).scopes());
049      this.challenge = challenge;
050   }
051
052   /**
053    * Returns the parsed challenge carrying the required scopes for the current operation.
054    *
055    * @return The challenge.  Never <jk>null</jk>.
056    */
057   public WwwAuthenticateChallenge challenge() {
058      return challenge;
059   }
060}