001/**
002 * Copyright (C) 2006-2018 Talend Inc. - www.talend.com
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 org.talend.sdk.component.api.service.http.configurer.oauth1;
017
018import static lombok.AccessLevel.PRIVATE;
019
020import java.security.PrivateKey;
021import java.util.Iterator;
022import java.util.Map;
023import java.util.ServiceLoader;
024
025import org.talend.sdk.component.api.meta.Internal;
026import org.talend.sdk.component.api.meta.Partial;
027
028import lombok.Builder;
029import lombok.Data;
030import lombok.NoArgsConstructor;
031
032@Partial("This doesn't fully implement oauth1 yet but is a good example of configurer entry point")
033@NoArgsConstructor(access = PRIVATE)
034public final class OAuth1 {
035
036    public static class Configurer implements org.talend.sdk.component.api.service.http.Configurer {
037
038        @Override
039        public void configure(final Connection connection, final Configurer.ConfigurerConfiguration configuration) {
040            loadProvider().newConfigurer().configure(connection, configuration);
041        }
042    }
043
044    public Map<String, String> buildParameters(final String method, final String url, final byte[] payload,
045            final OAuth1.Configuration oauth1Config) {
046        return loadProvider().buildParameters(method, url, payload, oauth1Config);
047    }
048
049    private static OAuth1Provider loadProvider() {
050        final Iterator<OAuth1Provider> iterator = ServiceLoader.load(OAuth1Provider.class).iterator();
051        if (!iterator.hasNext()) {
052            throw new IllegalStateException("No registered implementation of OAuth1Provider");
053        }
054        return iterator.next();
055    }
056
057    /**
058     * This is the SPI to load the implementation, WARNING: this is an internal API.
059     */
060    @Internal
061    public interface OAuth1Provider {
062
063        Map<String, String> buildParameters(String method, String url, byte[] payload, OAuth1.Configuration oauth1Config);
064
065        org.talend.sdk.component.api.service.http.Configurer newConfigurer();
066    }
067
068    @Data
069    @Builder
070    public static class Configuration {
071
072        /**
073         * The header name to set, by default it uses Authorization.
074         */
075        private String header;
076
077        /**
078         * The prefix to preppend to the header value, by default it uses "OAuth".
079         */
080        private String headerPrefix;
081
082        /**
083         * The payload hashing algorithm, default to null (ignored).
084         */
085        private String payloadHashAlgorithm;
086
087        /**
088         * The signing algorithm, default to HmacSHA1.
089         */
090        private String algorithm;
091
092        /**
093         * When using a hmac algorithm the key, if null default one is composed based on consumer key and token secret.
094         */
095        private byte[] signingHmacKey;
096
097        /**
098         * When using a signature algorithm the private key to use.
099         */
100        private PrivateKey signingSignatureKey;
101
102        /**
103         * Additional oauth parameters (not encoded).
104         */
105        private Map<String, String> oauthParameters;
106
107        /**
108         * oauth_token if set, otherwise ignored.
109         */
110        private String token;
111
112        /**
113         * OAuth token secret.
114         */
115        private String tokenSecret;
116
117        /**
118         * OAuth consumer key.
119         */
120        private String consumerKey;
121
122        /**
123         * OAuth consumer secret.
124         */
125        private String consumerSecret;
126
127        /**
128         * The nonce to use if set, otherwise it is generated.
129         */
130        private String nonce;
131
132        /**
133         * The timestamp to use if set, otherwise it is generated.
134         */
135        private Long timestamp;
136    }
137}