001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.users.google;
017
018import java.util.Properties;
019
020import org.apache.commons.codec.EncoderException;
021import org.apache.commons.codec.net.URLCodec;
022import org.apache.log4j.Logger;
023
024import core.tut.pori.properties.SystemProperty;
025
026/**
027 * Properties for Google User Service.
028 */
029public class GoogleProperties extends SystemProperty{
030  /* properties */
031  private static final String PROPERTY_SERVICE_USERS_GOOGLE = PROPERTY_SERVICE_PORI+".users.google";
032  private static final String PROPERTY_SERVICE_USERS_GOOGLE_CLIENT_ID = PROPERTY_SERVICE_USERS_GOOGLE+".google_client_id";
033  private static final String PROPERTY_SERVICE_USERS_GOOGLE_CLIENT_SECRET = PROPERTY_SERVICE_USERS_GOOGLE+".google_client_secret";
034  private static final String PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_REDIRECT_URI = PROPERTY_SERVICE_USERS_GOOGLE+".google_oauth2_redirect_uri";
035  private static final String PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_SCOPE = PROPERTY_SERVICE_USERS_GOOGLE+".google_oauth2_scope";
036  private static final String PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_URI = PROPERTY_SERVICE_USERS_GOOGLE+".google_oauth2_uri";
037  private static final String PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_USER_INFO_URI = PROPERTY_SERVICE_USERS_GOOGLE+".google_oauth2_user_info_uri";
038  private String _clientId = null;
039  private String _clientSecret = null;
040  private String _encodedOAuth2RedirectUri = null;
041  private String _oAuth2RedirectUri = null;
042  private String _oAuth2Scope = null;
043  private String _oAuth2Uri = null;
044  private String _oAuth2UserInfoUri = null;
045
046  @Override
047  public void initialize(Properties properties) throws IllegalArgumentException{
048    _clientId = properties.getProperty(PROPERTY_SERVICE_USERS_GOOGLE_CLIENT_ID);
049    _clientSecret = properties.getProperty(PROPERTY_SERVICE_USERS_GOOGLE_CLIENT_SECRET);
050    if(_clientId == null || _clientSecret == null){
051      throw new IllegalArgumentException(PROPERTY_SERVICE_USERS_GOOGLE_CLIENT_ID+" or "+PROPERTY_SERVICE_USERS_GOOGLE_CLIENT_SECRET+" is missing.");
052    }
053    _oAuth2Scope = properties.getProperty(PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_SCOPE);
054    _oAuth2Uri = properties.getProperty(PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_URI);
055    _oAuth2RedirectUri = properties.getProperty(PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_REDIRECT_URI);
056    _oAuth2UserInfoUri = properties.getProperty(PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_USER_INFO_URI);
057    if(_oAuth2Uri == null || _oAuth2Scope == null || _oAuth2RedirectUri == null || _oAuth2UserInfoUri == null){
058      throw new IllegalArgumentException(PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_USER_INFO_URI+", "+PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_URI+", "+PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_REDIRECT_URI+" or "+PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_SCOPE+" is missing.");
059    }
060    try {
061      _encodedOAuth2RedirectUri = (new URLCodec(core.tut.pori.http.Definitions.ENCODING_UTF8)).encode(_oAuth2RedirectUri);
062    } catch (EncoderException ex) {
063      Logger.getLogger(getClass()).error(ex);
064      throw new IllegalArgumentException("Bad "+PROPERTY_SERVICE_USERS_GOOGLE_OAUTH2_REDIRECT_URI);
065    }
066  }
067
068  /**
069   * @return the clientId
070   */
071  public String getClientId() {
072    return _clientId;
073  }
074
075  /**
076   * @return the clientSecret
077   */
078  public String getClientSecret() {
079    return _clientSecret;
080  }
081
082  /**
083   * @return the oAuth2Uri
084   */
085  public String getoAuth2Uri() {
086    return _oAuth2Uri;
087  }
088
089  /**
090   * @return the oAuth2Scope
091   */
092  public String getoAuth2Scope() {
093    return _oAuth2Scope;
094  }
095
096  /**
097   * @return the oAuth2RedirectUri
098   */
099  public String getoAuth2RedirectUri() {
100    return _oAuth2RedirectUri;
101  }
102
103  /**
104   * @return the encodedOAuth2RedirectUri
105   */
106  public String getEncodedOAuth2RedirectUri() {
107    return _encodedOAuth2RedirectUri;
108  }
109
110  /**
111   * @return the oAuth2UserInfoUri
112   */
113  public String getoAuth2UserInfoUri() {
114    return _oAuth2UserInfoUri;
115  }
116  
117  @Override
118  public String getPropertyFilePath() {
119    return CONFIGURATION_FILE_PATH+"google.properties";
120  }
121}