001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Thomas Roger
018 *
019 */
020
021package org.nuxeo.ecm.platform.oauth2;
022
023/**
024 * @since 9.2
025 */
026public class OAuth2Error {
027
028    public static final String INVALID_REQUEST = "invalid_request";
029
030    public static final String UNAUTHORIZED_CLIENT = "unauthorized_client";
031
032    public static final String ACCESS_DENIED = "access_denied";
033
034    public static final String UNSUPPORTED_RESPONSE_TYPE = "unsupported_response_type";
035
036    public static final String INVALID_SCOPE = "invalid_scope";
037
038    public static final String SERVER_ERROR = "server_error";
039
040    public static final String TEMPORARILY_UNAVAILABLE = "temporarily_unavailable";
041
042    public static final String INVALID_CLIENT = "invalid_client";
043
044    public static final String INVALID_GRANT = "invalid_grant";
045
046    public static final String UNSUPPORTED_GRANT_TYPE = "unsupported_grant_type";
047
048    protected final String id;
049
050    protected final String description;
051
052    protected OAuth2Error(String id, String description) {
053        this.id = id;
054        this.description = description;
055    }
056
057    public String getId() {
058        return id;
059    }
060
061    public String getDescription() {
062        return description;
063    }
064
065    public static OAuth2Error invalidRequest(String description) {
066        return new OAuth2Error(INVALID_REQUEST, description);
067    }
068
069    public static OAuth2Error invalidRequest() {
070        return invalidRequest(null);
071    }
072
073    public static OAuth2Error unauthorizedClient(String description) {
074        return new OAuth2Error(UNAUTHORIZED_CLIENT, description);
075    }
076
077    public static OAuth2Error unauthorizedClient() {
078        return unauthorizedClient(null);
079    }
080
081    public static OAuth2Error accessDenied(String description) {
082        return new OAuth2Error(ACCESS_DENIED, description);
083    }
084
085    public static OAuth2Error accessDenied() {
086        return accessDenied(null);
087    }
088
089    public static OAuth2Error unsupportedResponseType(String description) {
090        return new OAuth2Error(UNSUPPORTED_RESPONSE_TYPE, description);
091    }
092
093    public static OAuth2Error unsupportedResponseType() {
094        return unsupportedResponseType(null);
095    }
096
097    public static OAuth2Error invalidScope(String description) {
098        return new OAuth2Error(INVALID_SCOPE, description);
099    }
100
101    public static OAuth2Error invalidScope() {
102        return invalidScope(null);
103    }
104
105    public static OAuth2Error serverError(String description) {
106        return new OAuth2Error(SERVER_ERROR, description);
107    }
108
109    public static OAuth2Error serverError() {
110        return serverError(null);
111    }
112
113    public static OAuth2Error temporarilyUnavailable(String description) {
114        return new OAuth2Error(TEMPORARILY_UNAVAILABLE, description);
115    }
116
117    public static OAuth2Error temporarilyUnavailable() {
118        return temporarilyUnavailable(null);
119    }
120
121    public static OAuth2Error invalidClient(String description) {
122        return new OAuth2Error(INVALID_CLIENT, description);
123    }
124
125    public static OAuth2Error invalidClient() {
126        return invalidClient(null);
127    }
128
129    public static OAuth2Error invalidGrant(String description) {
130        return new OAuth2Error(INVALID_GRANT, description);
131    }
132
133    public static OAuth2Error invalidGrant() {
134        return invalidGrant(null);
135    }
136
137    public static OAuth2Error unsupportedGrantType(String description) {
138        return new OAuth2Error(UNSUPPORTED_GRANT_TYPE, description);
139    }
140
141    public static OAuth2Error unsupportedGrantType() {
142        return unsupportedGrantType(null);
143    }
144
145    @Override
146    public String toString() {
147        return String.format("%s(id=%s, description=%s)", getClass().getSimpleName(), id, description);
148    }
149}