/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package com.sun.socialsite.business.impl;


import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.sun.socialsite.SocialSiteException;
import com.sun.socialsite.business.Factory;
import com.sun.socialsite.pojos.OAuthAccessorRecord;
import com.sun.socialsite.pojos.OAuthConsumerRecord;
import net.oauth.OAuth;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthServiceProvider;
import net.oauth.signature.RSA_SHA1;

import org.apache.shindig.auth.SecurityToken;
import org.apache.shindig.gadgets.GadgetException;
import org.apache.shindig.gadgets.oauth.BasicOAuthStoreConsumerKeyAndSecret.KeyType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shindig.gadgets.oauth.BasicOAuthStoreConsumerIndex;
import org.apache.shindig.gadgets.oauth.BasicOAuthStoreConsumerKeyAndSecret;
import org.apache.shindig.gadgets.oauth.BasicOAuthStoreTokenIndex;
import org.apache.shindig.gadgets.oauth.OAuthStore;
import org.apache.shindig.gadgets.oauth.OAuthStore.ConsumerInfo;
import org.apache.shindig.gadgets.oauth.OAuthStore.TokenInfo;


/**
 * Stores consumer key, secrets and tokens.
 * Enables SocailSite to act as an OAuth consumer.
 */
@Singleton
public class JPAOAuthStore implements OAuthStore {

    private static Log log = LogFactory.getLog(JPAOAuthStore.class);
    private final JPAPersistenceStrategy strategy;
    private static final String CONSUMER_SECRET_KEY = "consumer_secret";
    private static final String CONSUMER_KEY_KEY = "consumer_key";
    private static final String KEY_TYPE_KEY = "key_type";

    /**
     * Key to use when no other key is found.
     */
    private BasicOAuthStoreConsumerKeyAndSecret defaultKey;
    /** Number of times we looked up a consumer key */
    private int consumerKeyLookupCount = 0;
    /** Number of times we looked up an access token */
    private int accessTokenLookupCount = 0;
    /** Number of times we added an access token */
    private int accessTokenAddCount = 0;
    /** Number of times we removed an access token */
    private int accessTokenRemoveCount = 0;
    

    @Inject
    public JPAOAuthStore(JPAPersistenceStrategy strategy) {
        this.strategy = strategy;
    }

    public void saveConsumerInfo(String gadgetUri, String serviceName,
            String consumerKey, String consumerSecret, String keyTypeStr) {

        KeyType keyType = KeyType.HMAC_SYMMETRIC;
        if (keyTypeStr.equals("RSA_PRIVATE")) {
            keyType = KeyType.RSA_PRIVATE;
            consumerSecret = convertFromOpenSsl(consumerSecret);
        }

        BasicOAuthStoreConsumerKeyAndSecret kas = new BasicOAuthStoreConsumerKeyAndSecret(
                consumerKey, consumerSecret, keyType, null);

        BasicOAuthStoreConsumerIndex index = new BasicOAuthStoreConsumerIndex();
        index.setGadgetUri(gadgetUri);
        index.setServiceName(serviceName);
        setConsumerKeyAndSecret(index, kas);
    }

}
