View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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   * Returns the read-public access token to read public data from ORCID.org.
30   * A token is needed for access to the public API, see
31   * https://members.orcid.org/api/tutorial/read-orcid-records#readpub
32   *
33   * The token can be configured via MCR.ORCID.OAuth.ReadPublicToken.
34   * In case that is not set, the token is directly requested from the OAuth2 API and logged.
35   *
36   * @author Frank L\u00FCtzenkirchen *
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       * Returns the read-public access token
48       */
49      public static String getToken() {
50          if ((token == null) || token.isEmpty()) {
51              requestToken();
52          }
53          return token;
54      }
55  
56      /**
57       * Requests the token from the OAuth2 API of ORCID.org.
58       * Logs a warning, so you better check your logs and directly configure
59       * MCR.ORCID.OAuth.ReadPublicToken
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  }