more work on auth

This commit is contained in:
Jeena Paradies 2012-10-27 00:13:35 +02:00
parent 8faf687bc5
commit 885d36c3cb
6 changed files with 85 additions and 43 deletions

View file

@ -6,8 +6,21 @@
// Licence: BSD (see attached LICENCE.txt file).
//
function getURL(url, type, callback, data) {
function getURL(url, type, callback, data, auth_header) {
$.ajax({
beforeSend: function(xhr) {
if (data) {
xhr.setRequestHeader("Content-Length", data.length);
}
if (auth_header) {
var header_data = 'MAC id=' + auth_header.mac_key_id
+ ', ts="' + auth_header.time_stamp
+ '", nonce="' + auth_header.nonce
+ '", mac="' + auth_header.mac + '"';
xhr.setRequestHeader("Authorization", header_data);
};
},
url: url,
accepts: "application/vnd.tent.v0+json",
contentType: "application/vnd.tent.v0+json",
@ -16,9 +29,10 @@ function getURL(url, type, callback, data) {
data: data,
processData: false,
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(ajaxOptions);
alert(thrownError);
alert("getURL ERROR:");
alert(xhr.statusText);
alert(ajaxOptions);
alert(thrownError);
}
});
}
@ -76,7 +90,7 @@ OauthImplementation.prototype.requestProfileURL = function (entity) {
OauthImplementation.prototype.register = function (url) {
var those = this;
getURL(url, "GET", function(resp) {
this.profile = JSON.parse(resp.responseText);
those.profile = JSON.parse(resp.responseText);
var callback = function(resp) {
var data = JSON.parse(resp.responseText);
those.authRequest(data);
@ -99,58 +113,48 @@ OauthImplementation.prototype.authRequest = function(register_data) {
+ "&state=" + this.state
+ "&tent_post_types=" + escape("https://tent.io/types/posts/status/v0.1.0");
controller.openURL_(those.apiRoot() + auth);
controller.openURL_(this.apiRoot() + auth);
}
OauthImplementation.prototype.requestAccessToken = function(responseBody) {
// /oauthtoken?code=51d0115b04d1ed94001dde751c5b360f&state=aQfH1VEohYsQr86qqyv
var urlVars = getUrlVars(responseBody);
if(this.state && this.state != "" && urlVars["state"] == this.state) {
var code = urlVars["code"];
var url = this.apiRoot() + "/apps/" + this.register_data["id"] + "/authorizations";
var nonce = makeid(4);
var time_stamp = (new Date).getTime();
var requestBody = JSON.stringify({
'code' : urlVars["code"],
'token_type' : "mac"
});
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, this.register_data["mac_key"]);
hmac.update(requestBody);
var hash = hmac.finalize();
var auth_header = {
mac_key_id: this.register_data["mac_key_id"],
time_stamp: time_stamp,
nonce: nonce,
mac: hash.toString(CryptoJS.enc.Base64)
}
var those = this;
var callback = function(resp) {
alert("requestAccessTokenTicketFinished")
alert(resp.responseText);
//those.requestAccessTokenTicketFinished(data);
};
getURL(url, "POST", callback, requestBody, auth_header);
} else {
alert("State is not the same: {" + this.state + "} vs {" + urlVars["state"] + "}")
}
this.state = null; // reset the state
/*
var urlVars = getUrlVars(responseBody);
var url = OAUTH_ACCESS_TOKEN_URL;
var _this = this;
var accessTokenKey = getUrlVars(responseBody)
var message = { method:"POST" , action:url };
OAuth.completeRequest(message,
{ consumerKey : OAUTH_CONSUMER_KEY
, consumerSecret: OAUTH_CONSUMER_SECRET
, token : urlVars["oauth_token"]
, tokenSecret : urlVars["oauth_verifier"]
});
$.ajax({
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", OAuth.getAuthorizationHeader("", message.parameters));
},
url: url,
type: 'POST',
dataType: 'text',
success: function(data) {
_this.requestAccessTokenTicketFinished(data);
},
error:function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(ajaxOptions);
alert(thrownError);
}
});*/
}

View file

@ -20,6 +20,10 @@
1F705EA6117889FA00C85707 /* sprite-icons.png in Resources */ = {isa = PBXBuildFile; fileRef = 1F705EA5117889FA00C85707 /* sprite-icons.png */; };
1F70619F1178FBB300C85707 /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1F70619E1178FBB300C85707 /* Carbon.framework */; };
1F77DB47118C5F1C007C7F1E /* Constants.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F77DB46118C5F1C007C7F1E /* Constants.m */; };
1F9816C8163915A100AFD4EE /* enc-base64-min.js in Sources */ = {isa = PBXBuildFile; fileRef = 1F9816C6163915A100AFD4EE /* enc-base64-min.js */; };
1F9816C9163915A100AFD4EE /* hmac-sha256.js in Sources */ = {isa = PBXBuildFile; fileRef = 1F9816C7163915A100AFD4EE /* hmac-sha256.js */; };
1F9816CA16391C4A00AFD4EE /* enc-base64-min.js in Resources */ = {isa = PBXBuildFile; fileRef = 1F9816C6163915A100AFD4EE /* enc-base64-min.js */; };
1F9816CB16391C4A00AFD4EE /* hmac-sha256.js in Resources */ = {isa = PBXBuildFile; fileRef = 1F9816C7163915A100AFD4EE /* hmac-sha256.js */; };
1F98DC9E124BFFD7004289ED /* pin.png in Resources */ = {isa = PBXBuildFile; fileRef = 1F98DC9D124BFFD7004289ED /* pin.png */; };
1FA09847144602530079E258 /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FA09846144602530079E258 /* libicucore.dylib */; };
1FC254941427BC050035D84B /* index_oauth.html in Resources */ = {isa = PBXBuildFile; fileRef = 1FC254931427BC050035D84B /* index_oauth.html */; };
@ -77,6 +81,8 @@
1F70619E1178FBB300C85707 /* Carbon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Carbon.framework; path = System/Library/Frameworks/Carbon.framework; sourceTree = SDKROOT; };
1F77DB45118C5F1C007C7F1E /* Constants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = Constants.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
1F77DB46118C5F1C007C7F1E /* Constants.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = Constants.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
1F9816C6163915A100AFD4EE /* enc-base64-min.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "enc-base64-min.js"; sourceTree = "<group>"; };
1F9816C7163915A100AFD4EE /* hmac-sha256.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "hmac-sha256.js"; sourceTree = "<group>"; };
1F98DC9D124BFFD7004289ED /* pin.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = pin.png; sourceTree = "<group>"; };
1FA09846144602530079E258 /* libicucore.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libicucore.dylib; path = usr/lib/libicucore.dylib; sourceTree = SDKROOT; };
1FC254911427ADF90035D84B /* OauthImplementation.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; lineEnding = 0; path = OauthImplementation.js; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.javascript; };
@ -157,6 +163,8 @@
1FFA36C71177D861006C8562 /* WebKit */ = {
isa = PBXGroup;
children = (
1F9816C6163915A100AFD4EE /* enc-base64-min.js */,
1F9816C7163915A100AFD4EE /* hmac-sha256.js */,
1F245D6E1632AEFE00E4469A /* jso.js */,
1F4673E61180F654006CC37C /* Core.js */,
1FC254911427ADF90035D84B /* OauthImplementation.js */,
@ -296,6 +304,8 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1F9816CA16391C4A00AFD4EE /* enc-base64-min.js in Resources */,
1F9816CB16391C4A00AFD4EE /* hmac-sha256.js in Resources */,
1FC2549F1427DC7F0035D84B /* Constants.js in Resources */,
1FC254951427BF150035D84B /* OauthImplementation.js in Resources */,
1F2746FC12D9057600339B4F /* dsa_pub.pem in Resources */,
@ -332,6 +342,8 @@
1F77DB47118C5F1C007C7F1E /* Constants.m in Sources */,
1F618ECA12DB5E6100E500D9 /* TweetModel.m in Sources */,
1F245D6F1632AEFE00E4469A /* jso.js in Sources */,
1F9816C8163915A100AFD4EE /* enc-base64-min.js in Sources */,
1F9816C9163915A100AFD4EE /* hmac-sha256.js in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

8
enc-base64-min.js vendored Normal file
View file

@ -0,0 +1,8 @@
/*
CryptoJS v3.0.2
code.google.com/p/crypto-js
(c) 2009-2012 by Jeff Mott. All rights reserved.
code.google.com/p/crypto-js/wiki/License
*/
(function(){var h=CryptoJS,i=h.lib.WordArray;h.enc.Base64={stringify:function(b){var e=b.words,f=b.sigBytes,c=this._map;b.clamp();for(var b=[],a=0;a<f;a+=3)for(var d=(e[a>>>2]>>>24-8*(a%4)&255)<<16|(e[a+1>>>2]>>>24-8*((a+1)%4)&255)<<8|e[a+2>>>2]>>>24-8*((a+2)%4)&255,g=0;4>g&&a+0.75*g<f;g++)b.push(c.charAt(d>>>6*(3-g)&63));if(e=c.charAt(64))for(;b.length%4;)b.push(e);return b.join("")},parse:function(b){var b=b.replace(/\s/g,""),e=b.length,f=this._map,c=f.charAt(64);c&&(c=b.indexOf(c),-1!=c&&(e=c));
for(var c=[],a=0,d=0;d<e;d++)if(d%4){var g=f.indexOf(b.charAt(d-1))<<2*(d%4),h=f.indexOf(b.charAt(d))>>>6-2*(d%4);c[a>>>2]|=(g|h)<<24-8*(a%4);a++}return i.create(c,a)},_map:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="}})();

17
hmac-sha256.js Normal file
View file

@ -0,0 +1,17 @@
/*
CryptoJS v3.0.2
code.google.com/p/crypto-js
(c) 2009-2012 by Jeff Mott. All rights reserved.
code.google.com/p/crypto-js/wiki/License
*/
var CryptoJS=CryptoJS||function(h,i){var e={},f=e.lib={},l=f.Base=function(){function a(){}return{extend:function(j){a.prototype=this;var d=new a;j&&d.mixIn(j);d.$super=this;return d},create:function(){var a=this.extend();a.init.apply(a,arguments);return a},init:function(){},mixIn:function(a){for(var d in a)a.hasOwnProperty(d)&&(this[d]=a[d]);a.hasOwnProperty("toString")&&(this.toString=a.toString)},clone:function(){return this.$super.extend(this)}}}(),k=f.WordArray=l.extend({init:function(a,j){a=
this.words=a||[];this.sigBytes=j!=i?j:4*a.length},toString:function(a){return(a||m).stringify(this)},concat:function(a){var j=this.words,d=a.words,c=this.sigBytes,a=a.sigBytes;this.clamp();if(c%4)for(var b=0;b<a;b++)j[c+b>>>2]|=(d[b>>>2]>>>24-8*(b%4)&255)<<24-8*((c+b)%4);else if(65535<d.length)for(b=0;b<a;b+=4)j[c+b>>>2]=d[b>>>2];else j.push.apply(j,d);this.sigBytes+=a;return this},clamp:function(){var a=this.words,b=this.sigBytes;a[b>>>2]&=4294967295<<32-8*(b%4);a.length=h.ceil(b/4)},clone:function(){var a=
l.clone.call(this);a.words=this.words.slice(0);return a},random:function(a){for(var b=[],d=0;d<a;d+=4)b.push(4294967296*h.random()|0);return k.create(b,a)}}),o=e.enc={},m=o.Hex={stringify:function(a){for(var b=a.words,a=a.sigBytes,d=[],c=0;c<a;c++){var e=b[c>>>2]>>>24-8*(c%4)&255;d.push((e>>>4).toString(16));d.push((e&15).toString(16))}return d.join("")},parse:function(a){for(var b=a.length,d=[],c=0;c<b;c+=2)d[c>>>3]|=parseInt(a.substr(c,2),16)<<24-4*(c%8);return k.create(d,b/2)}},q=o.Latin1={stringify:function(a){for(var b=
a.words,a=a.sigBytes,d=[],c=0;c<a;c++)d.push(String.fromCharCode(b[c>>>2]>>>24-8*(c%4)&255));return d.join("")},parse:function(a){for(var b=a.length,d=[],c=0;c<b;c++)d[c>>>2]|=(a.charCodeAt(c)&255)<<24-8*(c%4);return k.create(d,b)}},r=o.Utf8={stringify:function(a){try{return decodeURIComponent(escape(q.stringify(a)))}catch(b){throw Error("Malformed UTF-8 data");}},parse:function(a){return q.parse(unescape(encodeURIComponent(a)))}},b=f.BufferedBlockAlgorithm=l.extend({reset:function(){this._data=k.create();
this._nDataBytes=0},_append:function(a){"string"==typeof a&&(a=r.parse(a));this._data.concat(a);this._nDataBytes+=a.sigBytes},_process:function(a){var b=this._data,d=b.words,c=b.sigBytes,e=this.blockSize,g=c/(4*e),g=a?h.ceil(g):h.max((g|0)-this._minBufferSize,0),a=g*e,c=h.min(4*a,c);if(a){for(var f=0;f<a;f+=e)this._doProcessBlock(d,f);f=d.splice(0,a);b.sigBytes-=c}return k.create(f,c)},clone:function(){var a=l.clone.call(this);a._data=this._data.clone();return a},_minBufferSize:0});f.Hasher=b.extend({init:function(){this.reset()},
reset:function(){b.reset.call(this);this._doReset()},update:function(a){this._append(a);this._process();return this},finalize:function(a){a&&this._append(a);this._doFinalize();return this._hash},clone:function(){var a=b.clone.call(this);a._hash=this._hash.clone();return a},blockSize:16,_createHelper:function(a){return function(b,d){return a.create(d).finalize(b)}},_createHmacHelper:function(a){return function(b,d){return g.HMAC.create(a,d).finalize(b)}}});var g=e.algo={};return e}(Math);
(function(h){var i=CryptoJS,e=i.lib,f=e.WordArray,e=e.Hasher,l=i.algo,k=[],o=[];(function(){function e(a){for(var b=h.sqrt(a),d=2;d<=b;d++)if(!(a%d))return!1;return!0}function f(a){return 4294967296*(a-(a|0))|0}for(var b=2,g=0;64>g;)e(b)&&(8>g&&(k[g]=f(h.pow(b,0.5))),o[g]=f(h.pow(b,1/3)),g++),b++})();var m=[],l=l.SHA256=e.extend({_doReset:function(){this._hash=f.create(k.slice(0))},_doProcessBlock:function(e,f){for(var b=this._hash.words,g=b[0],a=b[1],j=b[2],d=b[3],c=b[4],h=b[5],l=b[6],k=b[7],n=0;64>
n;n++){if(16>n)m[n]=e[f+n]|0;else{var i=m[n-15],p=m[n-2];m[n]=((i<<25|i>>>7)^(i<<14|i>>>18)^i>>>3)+m[n-7]+((p<<15|p>>>17)^(p<<13|p>>>19)^p>>>10)+m[n-16]}i=k+((c<<26|c>>>6)^(c<<21|c>>>11)^(c<<7|c>>>25))+(c&h^~c&l)+o[n]+m[n];p=((g<<30|g>>>2)^(g<<19|g>>>13)^(g<<10|g>>>22))+(g&a^g&j^a&j);k=l;l=h;h=c;c=d+i|0;d=j;j=a;a=g;g=i+p|0}b[0]=b[0]+g|0;b[1]=b[1]+a|0;b[2]=b[2]+j|0;b[3]=b[3]+d|0;b[4]=b[4]+c|0;b[5]=b[5]+h|0;b[6]=b[6]+l|0;b[7]=b[7]+k|0},_doFinalize:function(){var e=this._data,f=e.words,b=8*this._nDataBytes,
g=8*e.sigBytes;f[g>>>5]|=128<<24-g%32;f[(g+64>>>9<<4)+15]=b;e.sigBytes=4*f.length;this._process()}});i.SHA256=e._createHelper(l);i.HmacSHA256=e._createHmacHelper(l)})(Math);
(function(){var h=CryptoJS,i=h.enc.Utf8;h.algo.HMAC=h.lib.Base.extend({init:function(e,f){e=this._hasher=e.create();"string"==typeof f&&(f=i.parse(f));var h=e.blockSize,k=4*h;f.sigBytes>k&&(f=e.finalize(f));for(var o=this._oKey=f.clone(),m=this._iKey=f.clone(),q=o.words,r=m.words,b=0;b<h;b++)q[b]^=1549556828,r[b]^=909522486;o.sigBytes=m.sigBytes=k;this.reset()},reset:function(){var e=this._hasher;e.reset();e.update(this._iKey)},update:function(e){this._hasher.update(e);return this},finalize:function(e){var f=
this._hasher,e=f.finalize(e);f.reset();return f.finalize(this._oKey.clone().concat(e))}})})();

View file

@ -5,7 +5,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="jQuery.js"></script>
<script type="text/javascript" src="jQuery-Plugins.js"></script>
<script type="text/javascript" src="jso.js"></script>
<script type="text/javascript" src="hmac-sha256.js"></script>
<script type="text/javascript" src="enc-base64-min.js"></script>
<script type="text/javascript" src="Constants.js"></script>
<script type="text/javascript" src="OauthImplementation.js"></script>
</head>