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.petstore.auth;
018
019import java.security.*;
020import java.util.*;
021import java.util.concurrent.*;
022
023import org.apache.juneau.rest.server.auth.*;
024
025/**
026 * Stub {@link TokenValidator} for the petstore demo — recognises an in-memory map of opaque bearer tokens.
027 *
028 * <p>
029 * <b>NOT FOR PRODUCTION USE.</b>  This is a sample/demo validator that maps fixed token strings to fixed user
030 * principals.  Real deployments should plug in {@code JwtTokenValidator} from {@code juneau-rest-server-auth-jwt}
031 * (signed JWTs against a JWKS), an opaque-token store backed by a database, or RFC 7662 introspection.
032 *
033 * <p>
034 * Default token table:
035 * <ul>
036 *    <li>{@code petstore-user} → principal {@code "alice"}
037 *    <li>{@code petstore-admin} → principal {@code "admin"}
038 * </ul>
039 *
040 * <p>
041 * Other token strings throw {@link AuthenticationException} carrying a {@code WWW-Authenticate: Bearer realm="petstore"}
042 * challenge — the {@code AuthFilterChain} in turn returns {@code 401}.
043 */
044public class StubBearerTokenValidator implements TokenValidator {
045
046   /** Default in-memory token table. */
047   public static final Map<String,String> DEFAULT_TOKENS = Map.of(
048      "petstore-user", "alice",
049      "petstore-admin", "admin"
050   );
051
052   private final Map<String,String> tokenToUser;
053
054   /** Constructor using the {@link #DEFAULT_TOKENS} table. */
055   public StubBearerTokenValidator() {
056      this(DEFAULT_TOKENS);
057   }
058
059   /**
060    * Constructor.
061    *
062    * @param tokenToUser Map of token string to principal name.  Copied defensively.
063    */
064   public StubBearerTokenValidator(Map<String,String> tokenToUser) {
065      this.tokenToUser = new ConcurrentHashMap<>(Objects.requireNonNull(tokenToUser, "tokenToUser"));
066   }
067
068   @Override
069   public Principal validate(String token) throws AuthenticationException {
070      var name = tokenToUser.get(token);
071      if (name == null)
072         throw new AuthenticationException("Unknown bearer token").wwwAuthenticate("Bearer realm=\"petstore\"");
073      return () -> name;
074   }
075}