1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.oauth;
|
---|
3 |
|
---|
4 | import jakarta.json.JsonObject;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * A generic OAuth 2.0 exception
|
---|
8 | * @since 18650
|
---|
9 | */
|
---|
10 | public final class OAuth20Exception extends OAuthException {
|
---|
11 | private static final long serialVersionUID = -203910656089454886L;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Invalid request types
|
---|
15 | */
|
---|
16 | public enum Type {
|
---|
17 | invalid_request,
|
---|
18 | invalid_client,
|
---|
19 | invalid_grant,
|
---|
20 | unauthorized_client,
|
---|
21 | unsupported_grant_type,
|
---|
22 | invalid_scope,
|
---|
23 | unknown
|
---|
24 | }
|
---|
25 |
|
---|
26 | private final Type type;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Create a new exception with a specified cause
|
---|
30 | * @param cause The cause leading to this exception
|
---|
31 | */
|
---|
32 | public OAuth20Exception(Exception cause) {
|
---|
33 | super(cause);
|
---|
34 | this.type = Type.unknown;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Create a new exception with a given message
|
---|
39 | * @param message The message to use
|
---|
40 | */
|
---|
41 | public OAuth20Exception(String message) {
|
---|
42 | super(message);
|
---|
43 | this.type = Type.unknown;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Create an exception from a server message
|
---|
48 | * @param serverMessage The server message. Should conform to
|
---|
49 | * <a href="https://datatracker.ietf.org/doc/html/rfc6749#section-4.2.2">RFC 6747 Section 4.2.2</a>, but in JSON
|
---|
50 | * format.
|
---|
51 | */
|
---|
52 | OAuth20Exception(JsonObject serverMessage) {
|
---|
53 | super(serverMessage != null
|
---|
54 | ? serverMessage.getString("error_description", serverMessage.getString("error", "Unknown error"))
|
---|
55 | : "Unknown error");
|
---|
56 | if (serverMessage != null && serverMessage.containsKey("error")) {
|
---|
57 | switch (serverMessage.getString("error")) {
|
---|
58 | case "invalid_request":
|
---|
59 | case "invalid_client":
|
---|
60 | case "invalid_grant":
|
---|
61 | case "unauthorized_client":
|
---|
62 | case "unsupported_grant_type":
|
---|
63 | case "invalid_scope":
|
---|
64 | this.type = Type.valueOf(serverMessage.getString("error"));
|
---|
65 | break;
|
---|
66 | default:
|
---|
67 | this.type = Type.unknown;
|
---|
68 | }
|
---|
69 | } else {
|
---|
70 | this.type = Type.unknown;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | OAuthVersion[] getOAuthVersions() {
|
---|
76 | return new OAuthVersion[] {OAuthVersion.OAuth20};
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public String getMessage() {
|
---|
81 | String message = super.getMessage();
|
---|
82 | if (message == null) {
|
---|
83 | return "OAuth error " + this.type;
|
---|
84 | }
|
---|
85 | return "OAuth error " + this.type + ": " + message;
|
---|
86 | }
|
---|
87 | }
|
---|