/*
 * (C) Copyright 2017 Kyle F. Downey.
 *
 *  Licensed 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.
 */

/*
 * (C) Copyright 2017 Kyle F. Downey.
 *
 * Licensed 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 cloudwall.exchange.gdax;

import cloudwall.exchange.ExchangeLogin;
import cloudwall.infra.security.ApiKeyCallback;
import cloudwall.infra.security.ApiSecretCallback;

import javax.annotation.Nonnull;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;

/**
 * Security credentials required to log into the GDAX exchange.
 *
 * @author <a href="mailto:[email protected]">Kyle F. Downey</a>
 */
@SuppressWarnings("WeakerAccess")
public class GdaxExchangeCredentials implements ExchangeLogin {
    private final String apiKey;
    private final byte[] apiSecret;
    private final char[] passphrase;

    public GdaxExchangeCredentials(@Nonnull String apiKey,
                                   @Nonnull byte[] apiSecret,
                                   @Nonnull char[] passphrase) {
        this.apiSecret = apiSecret;
        this.apiKey = apiKey;
        this.passphrase = passphrase;
    }

    @Override
    public CallbackHandler getCallbackHandler() {
        return callbacks -> {
            for (Callback cb : callbacks) {
                if (cb instanceof ApiKeyCallback) {
                    ((ApiKeyCallback) cb).setApiKey(apiKey);
                } else if (cb instanceof ApiSecretCallback) {
                    ((ApiSecretCallback) cb).setSecretData(apiSecret);
                } else if (cb instanceof PasswordCallback) {
                    ((PasswordCallback) cb).setPassword(passphrase);
                } else {
                    throw new UnsupportedCallbackException(cb);
                }
            }
        };
    }
}