001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2022 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.webconlet.oidclogin;
020
021import org.jgrapes.core.Channel;
022import org.jgrapes.core.Components;
023import org.jgrapes.core.Event;
024import org.jgrapes.core.events.Error;
025
026/**
027 * The Class OidcError.
028 */
029public class OidcError extends Error {
030
031    /**
032     * The error kind.
033     */
034    @SuppressWarnings("PMD.LongVariable")
035    public enum Kind {
036        INVALID_ISSUER, INVALID_AUDIENCE, ID_TOKEN_EXPIRED,
037        PREFERRED_USERNAME_MISSING, ACCESS_DENIED, INTERNAL_SERVER_ERROR
038    }
039
040    private final Kind kind;
041
042    /**
043     * Instantiates a new oidc authentication failure.
044     *
045     * @param event the event
046     * @param kind the kind
047     */
048    public OidcError(Error event, Kind kind) {
049        super(event);
050        this.kind = kind;
051    }
052
053    /**
054     * Instantiates a new oidc authentication failure.
055     *
056     * @param event the event
057     * @param kind the kind
058     * @param message the message
059     * @param throwable the throwable
060     */
061    public OidcError(Event<?> event, Kind kind, String message,
062            Throwable throwable) {
063        super(event, message, throwable);
064        this.kind = kind;
065    }
066
067    /**
068     * Instantiates a new oidc authentication failure.
069     *
070     * @param event the event
071     * @param kind the kind
072     * @param message the message
073     */
074    public OidcError(Event<?> event, Kind kind, String message) {
075        super(event, message);
076        this.kind = kind;
077    }
078
079    /**
080     * Instantiates a new oidc authentication failure.
081     *
082     * @param event the event
083     * @param kind the kind
084     * @param throwable the throwable
085     */
086    public OidcError(Event<?> event, Kind kind, Throwable throwable) {
087        super(event, throwable);
088        this.kind = kind;
089    }
090
091    /**
092     * Gets the kind.
093     *
094     * @return the kind
095     */
096    public Kind kind() {
097        return kind;
098    }
099
100    /*
101     * (non-Javadoc)
102     * 
103     * @see java.lang.Object#toString()
104     */
105    @Override
106    public String toString() {
107        StringBuilder builder = new StringBuilder(50);
108        builder.append(Components.objectName(this)).append(" [")
109            .append(kind()).append(" (\"").append(message()).append("\")");
110        if (channels().length > 0) {
111            builder.append(", channels=").append(Channel.toString(channels()));
112        }
113        if (event() != null) {
114            builder.append(", caused by: ").append(event().toString());
115        }
116        builder.append(']');
117        return builder.toString();
118    }
119}