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 */
029@SuppressWarnings("PMD.DataClass")
030public class OidcError extends Error {
031
032    /**
033     * The error kind.
034     */
035    @SuppressWarnings("PMD.LongVariable")
036    public enum Kind {
037        INVALID_ISSUER, INVALID_AUDIENCE, ID_TOKEN_EXPIRED,
038        PREFERRED_USERNAME_MISSING, ACCESS_DENIED, INTERNAL_SERVER_ERROR
039    }
040
041    private final Kind kind;
042
043    /**
044     * Instantiates a new oidc authentication failure.
045     *
046     * @param event the event
047     * @param kind the kind
048     */
049    public OidcError(Error event, Kind kind) {
050        super(event);
051        this.kind = kind;
052    }
053
054    /**
055     * Instantiates a new oidc authentication failure.
056     *
057     * @param event the event
058     * @param kind the kind
059     * @param message the message
060     * @param throwable the throwable
061     */
062    public OidcError(Event<?> event, Kind kind, String message,
063            Throwable throwable) {
064        super(event, message, throwable);
065        this.kind = kind;
066    }
067
068    /**
069     * Instantiates a new oidc authentication failure.
070     *
071     * @param event the event
072     * @param kind the kind
073     * @param message the message
074     */
075    public OidcError(Event<?> event, Kind kind, String message) {
076        super(event, message);
077        this.kind = kind;
078    }
079
080    /**
081     * Instantiates a new oidc authentication failure.
082     *
083     * @param event the event
084     * @param kind the kind
085     * @param throwable the throwable
086     */
087    public OidcError(Event<?> event, Kind kind, Throwable throwable) {
088        super(event, throwable);
089        this.kind = kind;
090    }
091
092    /**
093     * Gets the kind.
094     *
095     * @return the kind
096     */
097    public Kind kind() {
098        return kind;
099    }
100
101    /*
102     * (non-Javadoc)
103     * 
104     * @see java.lang.Object#toString()
105     */
106    @Override
107    public String toString() {
108        StringBuilder builder = new StringBuilder(50);
109        builder.append(Components.objectName(this)).append(" [")
110            .append(kind()).append(" (\"").append(message()).append("\")");
111        if (channels().length > 0) {
112            builder.append(", channels=").append(Channel.toString(channels()));
113        }
114        if (event() != null) {
115            builder.append(", caused by: ").append(event().toString());
116        }
117        builder.append(']');
118        return builder.toString();
119    }
120}