|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2026 Contributors to the openHAB project |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Eclipse Public License 2.0 which is available at |
| 9 | + * http://www.eclipse.org/legal/epl-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: EPL-2.0 |
| 12 | + */ |
| 13 | +package org.openhab.core.auth.client.oauth2; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Set; |
| 19 | + |
| 20 | +import com.google.gson.Gson; |
| 21 | +import com.google.gson.JsonElement; |
| 22 | +import com.google.gson.JsonObject; |
| 23 | +import com.google.gson.JsonPrimitive; |
| 24 | +import com.google.gson.TypeAdapter; |
| 25 | +import com.google.gson.TypeAdapterFactory; |
| 26 | +import com.google.gson.reflect.TypeToken; |
| 27 | +import com.google.gson.stream.JsonReader; |
| 28 | +import com.google.gson.stream.JsonWriter; |
| 29 | + |
| 30 | +public final class AccessTokenResponseExtraFieldsAdapterFactory implements TypeAdapterFactory { |
| 31 | + |
| 32 | + private static final Set<String> KNOWN_FIELDS = Set.of("access_token", "token_type", "expires_in", "refresh_token", |
| 33 | + "scope", "state"); |
| 34 | + |
| 35 | + @Override |
| 36 | + public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { |
| 37 | + if (!AccessTokenResponse.class.isAssignableFrom(type.getRawType())) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); |
| 42 | + final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class); |
| 43 | + |
| 44 | + return new TypeAdapter<>() { |
| 45 | + |
| 46 | + @Override |
| 47 | + public void write(JsonWriter out, T value) throws IOException { |
| 48 | + delegate.write(out, value); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public T read(JsonReader in) throws IOException { |
| 53 | + JsonElement tree = elementAdapter.read(in); |
| 54 | + if (tree != null) { |
| 55 | + JsonObject obj = tree.getAsJsonObject(); |
| 56 | + |
| 57 | + T parsed = delegate.fromJsonTree(tree); |
| 58 | + AccessTokenResponse response = (AccessTokenResponse) parsed; |
| 59 | + |
| 60 | + Map<String, String> extras = new HashMap<>(); |
| 61 | + for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { |
| 62 | + String key = entry.getKey(); |
| 63 | + if (KNOWN_FIELDS.contains(key)) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + extras.put(key, toStringValue(gson, entry.getValue())); |
| 67 | + } |
| 68 | + |
| 69 | + if (response != null) { |
| 70 | + response.setExtraFields(extras); |
| 71 | + } |
| 72 | + return parsed; |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + private static String toStringValue(Gson gson, JsonElement el) { |
| 80 | + if (el == null || el.isJsonNull()) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + if (el.isJsonPrimitive()) { |
| 84 | + JsonPrimitive p = el.getAsJsonPrimitive(); |
| 85 | + return p.getAsString(); |
| 86 | + } |
| 87 | + |
| 88 | + return gson.toJson(el); |
| 89 | + } |
| 90 | +} |
0 commit comments