View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  
23  package org.opencastproject.publication.youtube.auth;
24  
25  import org.opencastproject.util.data.Collections;
26  
27  import org.apache.commons.lang3.builder.ToStringBuilder;
28  import org.json.simple.JSONObject;
29  import org.json.simple.parser.JSONParser;
30  import org.json.simple.parser.ParseException;
31  
32  import java.io.File;
33  import java.io.FileReader;
34  import java.io.IOException;
35  import java.util.List;
36  
37  /**
38   * <code>ClientCredentials</code> class represents the set of parameters required to make an authorization
39   * request.
40   */
41  public final class ClientCredentials {
42  
43    private String clientId;
44    private String credentialDatastore;
45    private String dataStoreDirectory;
46    private File clientSecrets;
47  
48    public String getClientId() {
49      return clientId;
50    }
51  
52    public String getCredentialDatastore() {
53      return credentialDatastore;
54    }
55  
56    public String getDataStoreDirectory() {
57      return dataStoreDirectory;
58    }
59  
60    public List<String> getScopes() {
61      return Collections.list("https://www.googleapis.com/auth/youtube",
62          "https://www.googleapis.com/auth/youtube.upload", "https://www.googleapis.com/auth/youtube.readonly");
63    }
64  
65    public File getClientSecrets() {
66      return clientSecrets;
67    }
68  
69    public void setClientSecrets(final File clientSecrets) throws IOException, ParseException {
70      this.clientSecrets = clientSecrets;
71      this.clientId = getValueFromArray(clientSecrets);
72    }
73  
74    public void setCredentialDatastore(final String credentialDatastore) {
75      this.credentialDatastore = credentialDatastore;
76    }
77  
78    public void setDataStoreDirectory(final String dataStoreDirectory) {
79      this.dataStoreDirectory = dataStoreDirectory;
80    }
81  
82    @Override
83    public String toString() {
84      return ToStringBuilder.reflectionToString(this);
85    }
86  
87    /**
88     * Parses a file and returns a value matching the keys provided if it exists.
89     * The file is assumed to be composed of one array, with each array element
90     * in turn being a JSONObject containing a name : value pair.
91     *
92     * @param file
93     *          file to parse
94     * @return matching value. Throws an exception if there is no matching value.
95     * @throws java.io.FileNotFoundException
96     * @throws java.io.IOException
97     * @throws java.lang.IllegalArgumentException
98     */
99    private String getValueFromArray(final File file) throws IOException, ParseException, IllegalArgumentException {
100     final JSONParser parser = new JSONParser();
101     final FileReader reader = new FileReader(file);
102     final JSONObject jsonObject = (JSONObject) parser.parse(reader);
103 
104     if (!jsonObject.containsKey("installed")) {
105       throw new IllegalArgumentException("client_secret file does not contain \"installed\" as a top level key. Did you"
106           + "set the type for your 'OAuth 2.0-Client-ID' correctly?");
107     }
108     final JSONObject array = (JSONObject) jsonObject.get("installed");
109 
110     if (!array.containsKey("client_id")) {
111       throw new IllegalArgumentException("client_secret file does not contain \"client_id\".");
112     }
113     return (String) array.get("client_id");
114   }
115 
116 }