|
|
|
|
|
|
|
|
|
|
#include "oauth.h" |
|
import "."; |
|
|
protected constant NONE = 0; |
|
|
constant PLAINTEXT = 1; |
|
|
constant HMAC_SHA1 = 2; |
|
|
constant RSA_SHA1 = 3; |
|
|
constant SIGTYPE = ([ |
NONE : "", |
PLAINTEXT : "PLAINTEXT", |
HMAC_SHA1 : "HMAC-SHA1", |
RSA_SHA1 : "RSA-SHA1" |
]); |
|
|
|
|
|
|
|
|
|
object get_object(int type) |
{ |
switch (type) |
{ |
case PLAINTEXT: return Plaintext(); |
case HMAC_SHA1: return HmacSha1(); |
case RSA_SHA1: return RsaSha1(); |
default: |
} |
|
error("Uknknown signature type"); |
} |
|
|
protected class Base |
{ |
|
protected int type = NONE; |
|
|
protected string method = SIGTYPE[NONE]; |
|
|
int get_type() |
{ |
return type; |
} |
|
|
string get_method() |
{ |
return method; |
} |
|
|
|
|
|
|
string build_signature(Request request, Consumer consumer, Token token); |
} |
|
|
protected class Plaintext |
{ |
inherit Base; |
protected int type = PLAINTEXT; |
protected string method = SIGTYPE[PLAINTEXT]; |
|
|
|
|
|
|
string build_signature(Request request, Consumer consumer, Token token) |
{ |
return uri_encode(sprintf("%s&%s", consumer->secret, token->secret)); |
} |
} |
|
|
protected class HmacSha1 |
{ |
inherit Base; |
protected int type = HMAC_SHA1; |
protected string method = SIGTYPE[HMAC_SHA1]; |
|
|
|
|
|
|
string build_signature(Request request, Consumer consumer, Token token) |
{ |
if (!token) token = Token("",""); |
string sigbase = request->get_signature_base(); |
string key = sprintf("%s&%s", uri_encode(consumer->secret), |
uri_encode(token->secret||"")); |
return MIME.encode_base64( |
#if constant(Crypto.HMAC) && constant(Crypto.SHA1) |
Crypto.HMAC(Crypto.SHA1)(key)(sigbase) |
#else |
MY_HMAC_SHA1(Crypto.sha)(key)(sigbase) |
#endif |
); |
} |
} |
|
|
protected class RsaSha1 |
{ |
inherit Base; |
protected int type = RSA_SHA1; |
protected string method = SIGTYPE[RSA_SHA1]; |
|
|
|
|
|
|
string build_signature(Request request, Consumer consumer, Token token) |
{ |
error("%s is not implemented.\n", CLASS_NAME(this)); |
} |
} |
|
#if !(constant(Crypto.HMAC) && constant(Crypto.SHA1)) |
|
|
class MY_HMAC_SHA1 |
{ |
function H; |
int B; |
|
void create(function h, int|void b) |
{ |
H = h; |
B = b || 64; |
} |
|
string raw_hash(string s) |
{ |
return H()->update(s)->digest(); |
} |
|
string pkcs_digest(string s) |
{ |
return Standards.PKCS.Signature.build_digestinfo(s, H()); |
} |
|
class `() |
{ |
string ikey, okey; |
|
void create(string passwd) |
{ |
if (sizeof(passwd) > B) |
passwd = raw_hash(passwd); |
if (sizeof(passwd) < B) |
passwd = passwd + "\0" * (B - sizeof(passwd)); |
|
ikey = passwd ^ ("6" * B); |
okey = passwd ^ ("\\" * B); |
} |
|
string `()(string text) |
{ |
return raw_hash(okey + raw_hash(ikey + text)); |
} |
} |
} |
#endif |
|
|