Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
Copyright 2020 Twitter, Inc.
SPDX-License-Identifier: Apache-2.0
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.
NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
https://openapi-generator.tech
Do not edit the class manually.
*/
package com.twitter.clientlib.auth;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Random;
import com.google.common.io.BaseEncoding;
import com.twitter.joauth.Normalizer;
import com.twitter.joauth.Normalizer.StandardNormalizer;
import com.twitter.joauth.OAuthParams;
import com.twitter.joauth.UrlCodec;
import com.twitter.joauth.OAuthParams.OAuth1Params;
import com.twitter.joauth.Request.Pair;
import com.twitter.joauth.Signer;
import com.twitter.joauth.UrlCodec;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
class SortPair implements Comparator
{
// Used for sorting in ascending order of
// roll number
public int compare(Pair a, Pair b)
{
return a.key.compareTo(b.key);
}
}
public class TwitterApiKeyOAuth implements Authentication {
private final String headerName = "Authorization";
private static Normalizer normalizer = StandardNormalizer.getStandardNormalizer();
private static Signer signer = Signer.getStandardSigner();
private Random r = new Random(System.currentTimeMillis());
private String consumerKey;
private String consumerSecret;
private String token;
private String tokenSecret;
public TwitterApiKeyOAuth( ) {
}
public void setConsumerKey(String consumerKey) {
this.consumerKey = consumerKey;
}
public void setConsumerSecret(String consumerSecret) {
this.consumerSecret = consumerSecret;
}
public void setToken(String token) {
this.token = token;
}
public void setTokenSecret(String tokenSecret) {
this.tokenSecret = tokenSecret;
}
public String getConsumerKey() {
return consumerKey;
}
public String getConsumerSecret() {
return consumerSecret;
}
public String getToken() {
return token;
}
public String getTokenSecret() {
return tokenSecret;
}
private String generateRandomNonce() {
byte[] bytes = new byte[10];
r.nextBytes(bytes);
return UrlCodec.normalize(BaseEncoding.base64().encode(bytes)).substring(0,9);
}
private List createRequestParamsForJOAuth(List inputQueryParams) {
List queryParameters = new ArrayList();
Iterator qs = inputQueryParams.iterator();
while (qs.hasNext()) {
com.twitter.clientlib.Pair nextVale = qs.next();
queryParameters.add(new Pair(
nextVale.getName(),
UrlCodec.normalize(UrlCodec.encode(nextVale.getValue()))));
}
return queryParameters;
}
private String buildAuthHeader(OAuth1Params oAuth1Params, String signature) {
List pairs = oAuth1Params.toList(false);
pairs.add(new Pair("oauth_signature",signature));
pairs.sort(new SortPair());
List results = new ArrayList();
ListIterator pairListIterator = pairs.listIterator();
while (pairListIterator.hasNext()) {
Pair nextValue = pairListIterator.next();
results.add(nextValue.key + "=\"" + nextValue.value + "\"");
}
return "OAuth " + String.join(",",results);
}
@Override
public void applyToParams(String path, String method, List inputQueryParams, Map headerParams, Map cookieParams) {
List queryParameters = createRequestParamsForJOAuth(inputQueryParams);
long timeStampSecs = System.currentTimeMillis() / 1000;
String timeStampSecsString = String.valueOf(timeStampSecs);
String nonce = generateRandomNonce();
OAuth1Params oAuth1Params = new OAuth1Params(
getToken(),
getConsumerKey(),
nonce,
timeStampSecs,
timeStampSecsString,
null,
OAuthParams.HMAC_SHA1,
OAuthParams.ONE_DOT_OH
);
String normalizedRequest = normalizer.normalize("HTTPS", "api.twitter.com", 443, method, path, queryParameters, oAuth1Params);
try {
String signature = signer.getString(normalizedRequest, getTokenSecret(), getConsumerSecret());
String authHeader = buildAuthHeader(oAuth1Params, signature);
headerParams.put(headerName, authHeader);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}