1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.orcid.oauth;
20
21 import java.io.IOException;
22
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.mycore.common.config.MCRConfiguration2;
26 import org.mycore.common.config.MCRConfigurationException;
27
28
29
30
31
32
33
34
35
36
37
38 public class MCRReadPublicTokenFactory {
39
40 private static final Logger LOGGER = LogManager.getLogger(MCRReadPublicTokenFactory.class);
41
42 private static final String CONFIG_PROPERTY = "MCR.ORCID.OAuth.ReadPublicToken";
43
44 private static String token = MCRConfiguration2.getString(CONFIG_PROPERTY).orElse(null);
45
46
47
48
49 public static String getToken() {
50 if ((token == null) || token.isEmpty()) {
51 requestToken();
52 }
53 return token;
54 }
55
56
57
58
59
60
61 private static void requestToken() {
62 LOGGER.info("requesting read-public access token...");
63
64 MCRTokenRequest request = MCROAuthClient.instance().getTokenRequest();
65 request.set("grant_type", "client_credentials");
66 request.set("scope", "/read-public");
67
68 try {
69 MCRTokenResponse response = request.post();
70 token = response.getAccessToken();
71 } catch (IOException ex) {
72 String msg = "Could not get read-public access token from ORCID OAuth API";
73 throw new MCRConfigurationException(msg, ex);
74 }
75
76 LOGGER.warn("You should set the access token in mycore.properties:");
77 LOGGER.warn(CONFIG_PROPERTY + "={}", token);
78 }
79 }