001/*
002 * Copyright 2012-2018 the original author or authors.
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 org.springframework.boot.actuate.autoconfigure.cloudfoundry;
018
019import org.springframework.http.HttpStatus;
020
021/**
022 * Authorization exceptions thrown to limit access to the endpoints.
023 *
024 * @author Madhura Bhave
025 * @since 2.0.0
026 */
027public class CloudFoundryAuthorizationException extends RuntimeException {
028
029        private final Reason reason;
030
031        public CloudFoundryAuthorizationException(Reason reason, String message) {
032                this(reason, message, null);
033        }
034
035        public CloudFoundryAuthorizationException(Reason reason, String message,
036                        Throwable cause) {
037                super(message, cause);
038                this.reason = reason;
039        }
040
041        /**
042         * Return the status code that should be returned to the client.
043         * @return the HTTP status code
044         */
045        public HttpStatus getStatusCode() {
046                return getReason().getStatus();
047        }
048
049        /**
050         * Return the reason why the authorization exception was thrown.
051         * @return the reason
052         */
053        public Reason getReason() {
054                return this.reason;
055        }
056
057        /**
058         * Reasons why the exception can be thrown.
059         */
060        public enum Reason {
061
062                ACCESS_DENIED(HttpStatus.FORBIDDEN),
063
064                INVALID_AUDIENCE(HttpStatus.UNAUTHORIZED),
065
066                INVALID_ISSUER(HttpStatus.UNAUTHORIZED),
067
068                INVALID_KEY_ID(HttpStatus.UNAUTHORIZED),
069
070                INVALID_SIGNATURE(HttpStatus.UNAUTHORIZED),
071
072                INVALID_TOKEN(HttpStatus.UNAUTHORIZED),
073
074                MISSING_AUTHORIZATION(HttpStatus.UNAUTHORIZED),
075
076                TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED),
077
078                UNSUPPORTED_TOKEN_SIGNING_ALGORITHM(HttpStatus.UNAUTHORIZED),
079
080                SERVICE_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE);
081
082                private final HttpStatus status;
083
084                Reason(HttpStatus status) {
085                        this.status = status;
086                }
087
088                public HttpStatus getStatus() {
089                        return this.status;
090                }
091
092        }
093
094}