Initial commit
This commit is contained in:
commit
bfbe536886
59 files changed
+2540
No files matched your search
@@ -0,0 +1,46 @@
|
||||
package org.scribe.services;
|
||||
|
||||
import javax.crypto.*;
|
||||
import javax.crypto.spec.*;
|
||||
|
||||
import org.apache.commons.codec.binary.*;
|
||||
import org.scribe.exceptions.*;
|
||||
import org.scribe.utils.*;
|
||||
|
||||
public class HMACSha1SignatureService implements SignatureService
|
||||
{
|
||||
|
||||
private static final String EMPTY_STRING = "";
|
||||
private static final String CARRIAGE_RETURN = "\r\n";
|
||||
private static final String UTF8 = "UTF-8";
|
||||
private static final String HMAC_SHA1 = "HmacSHA1";
|
||||
private static final String METHOD = "HMAC-SHA1";
|
||||
|
||||
public String getSignature(String baseString, String apiSecret, String tokenSecret)
|
||||
{
|
||||
try
|
||||
{
|
||||
Preconditions.checkEmptyString(baseString, "Base string cant be null or empty string");
|
||||
Preconditions.checkEmptyString(apiSecret, "Api secret cant be null or empty string");
|
||||
return doSign(baseString, apiSecret + '&' + tokenSecret);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new OAuthSignatureException(baseString, e);
|
||||
}
|
||||
}
|
||||
|
||||
private String doSign(String toSign, String keyString) throws Exception
|
||||
{
|
||||
SecretKeySpec key = new SecretKeySpec((keyString).getBytes(UTF8), HMAC_SHA1);
|
||||
Mac mac = Mac.getInstance(HMAC_SHA1);
|
||||
mac.init(key);
|
||||
byte[] bytes = mac.doFinal(toSign.getBytes(UTF8));
|
||||
return new String(Base64.encodeBase64(bytes)).replace(CARRIAGE_RETURN, EMPTY_STRING);
|
||||
}
|
||||
|
||||
public String getSignatureMethod()
|
||||
{
|
||||
return METHOD;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.scribe.services;
|
||||
|
||||
public interface SignatureService
|
||||
{
|
||||
public String getSignature(String baseString, String apiSecret, String tokenSecret);
|
||||
|
||||
public String getSignatureMethod();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.scribe.services;
|
||||
|
||||
public interface TimestampService
|
||||
{
|
||||
|
||||
public String getTimestampInSeconds();
|
||||
|
||||
public String getNonce();
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.scribe.services;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class TimestampServiceImpl implements TimestampService
|
||||
{
|
||||
|
||||
private Timer timer;
|
||||
|
||||
public TimestampServiceImpl()
|
||||
{
|
||||
timer = new Timer();
|
||||
}
|
||||
|
||||
public String getNonce()
|
||||
{
|
||||
Long ts = getTs();
|
||||
return String.valueOf(ts + timer.getRandomInteger());
|
||||
}
|
||||
|
||||
public String getTimestampInSeconds()
|
||||
{
|
||||
return String.valueOf(getTs());
|
||||
}
|
||||
|
||||
private Long getTs()
|
||||
{
|
||||
return timer.getMilis() / 1000;
|
||||
}
|
||||
|
||||
void setTimer(Timer timer)
|
||||
{
|
||||
this.timer = timer;
|
||||
}
|
||||
|
||||
static class Timer
|
||||
{
|
||||
public Long getMilis()
|
||||
{
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public Integer getRandomInteger()
|
||||
{
|
||||
return new Random().nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user