001/**
002 * Copyright (C) 2006-2019 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,
064                OAuth1.Configuration oauth1Config);
065
066        org.talend.sdk.component.api.service.http.Configurer newConfigurer();
067    }
068
069    @Data
070    @Builder
071    public static class Configuration {
072
073        /**
074         * The header name to set, by default it uses Authorization.
075         */
076        private String header;
077
078        /**
079         * The prefix to preppend to the header value, by default it uses "OAuth".
080         */
081        private String headerPrefix;
082
083        /**
084         * The payload hashing algorithm, default to null (ignored).
085         */
086        private String payloadHashAlgorithm;
087
088        /**
089         * The signing algorithm, default to HmacSHA1.
090         */
091        private String algorithm;
092
093        /**
094         * When using a hmac algorithm the key, if null default one is composed based on consumer key and token secret.
095         */
096        private byte[] signingHmacKey;
097
098        /**
099         * When using a signature algorithm the private key to use.
100         */
101        private PrivateKey signingSignatureKey;
102
103        /**
104         * Additional oauth parameters (not encoded).
105         */
106        private Map<String, String> oauthParameters;
107
108        /**
109         * oauth_token if set, otherwise ignored.
110         */
111        private String token;
112
113        /**
114         * OAuth token secret.
115         */
116        private String tokenSecret;
117
118        /**
119         * OAuth consumer key.
120         */
121        private String consumerKey;
122
123        /**
124         * OAuth consumer secret.
125         */
126        private String consumerSecret;
127
128        /**
129         * The nonce to use if set, otherwise it is generated.
130         */
131        private String nonce;
132
133        /**
134         * The timestamp to use if set, otherwise it is generated.
135         */
136        private Long timestamp;
137    }
138}