Project

General

Profile

1
package eu.dnetlib.openaire.usermanagement;
2

    
3
import com.google.gson.*;
4

    
5
import java.lang.reflect.Type;
6

    
7
public class JwksDeserializer implements JsonDeserializer<Jwks> {
8

    
9
    @Override
10
    public Jwks deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
11
            throws JsonParseException {
12

    
13
        JsonObject jsonObject = jsonElement.getAsJsonObject();
14
        if (jsonObject == null) throw new JsonParseException("Jwks not valid.");
15
        JsonArray jsonArray = jsonObject.getAsJsonArray("keys");
16

    
17
        if (jsonArray == null ) throw new JsonParseException("Jwks not valid.");
18

    
19
        Jwks jwks = new Jwks();
20
        Key[] keys = new Key[jsonArray.size()];
21

    
22
        Key key = null;
23
        for (int i = 0; i < jsonArray.size(); i++) {
24
            key = new Key();
25
            JsonElement je = jsonArray.get(i);
26

    
27
            if (je == null) throw new JsonParseException("Jwks not valid.");
28
            if (je.getAsJsonObject().get("kty")==null) throw new JsonParseException("Jwks not valid.");
29
            key.setKty(je.getAsJsonObject().get("kty").getAsString());
30

    
31
            if (je.getAsJsonObject().get("e")==null) throw new JsonParseException("Jwks not valid.");
32
            key.setE(je.getAsJsonObject().get("e").getAsString());
33

    
34
            if (je.getAsJsonObject().get("kid")==null) throw new JsonParseException("Jwks not valid.");
35
            key.setKid(je.getAsJsonObject().get("kid").getAsString());
36

    
37
            if (je.getAsJsonObject().get("alg")==null) throw new JsonParseException("Jwks not valid.");
38
            key.setAlg(je.getAsJsonObject().get("alg").getAsString());
39

    
40
            if (je.getAsJsonObject().get("n")==null) throw new JsonParseException("Jwks not valid.");
41
            key.setN(je.getAsJsonObject().get("n").getAsString());
42
            keys[i] = key;
43
        }
44

    
45
        jwks.setKeys(keys);
46
        return jwks;
47
    }
48
}
49
/*
50
    public static void main(String[] args) {
51
        Gson gson = new GsonBuilder().registerTypeAdapter(Jwks.class, new JwksDeserializer()).create();
52

    
53
        String jwksJson = "{\n" +
54
                "  \"keys\": [\n" +
55
                "    {\n" +
56
                "      \"kty\": \"RSA\",\n" +
57
                "      \"e\": \"AQAB\",\n" +
58
                "      \"kid\": \"05794a3c-a6f5-430c-9822-da4e53597ba5\",\n" +
59
                "      \"alg\": \"RS256\",\n" +
60
                "      \"n\": \"hm_OUny05OJEwbGBqPjE7wWvnwTMgqUHJFis_S9nM7hTivXQ_LX9f89RaVcPpXboox81Y8rrfuVwV0nc-FGr_E0FFpI-IwJ_sUUEDwf-5Qxor3LNc_S_5BiPOfFHY7c-R-ablRIAvVTXqwIjcyLVQnaHLjb9XQPf9lBt9sCZ2jN-9HOLztMO3BZWZYIFqvNr8ySKHfVPdlk0Wx3N45KPY0kgxk5RPYW0HLRakSlhIJtqYCJOr2IiDUEMAj9Z9BoWjeUKiAX3E3ZRo-DO1TWcc7feq-0Pei2IBw3lvNpgcBBv1_BlrsZYzQqkKOcDbLAppuhR3inUNhc3G67OuWt8ow\"\n" +
61
                "    }\n" +
62
                "  ]\n" +
63
                "}";
64
        Jwks jwks = gson.fromJson(jwksJson, Jwks.class);
65
        for(Key key:jwks.getKeys()) {
66
            //System.out.println(key.getE());
67
        }
68
    }
69
}
70
*/
(4-4/18)