got authenification working, wohoo
This commit is contained in:
parent
885d36c3cb
commit
6f3b689c3d
20 changed files with 547 additions and 347 deletions
|
@ -6,24 +6,17 @@
|
|||
// Licence: BSD (see attached LICENCE.txt file).
|
||||
//
|
||||
|
||||
MY_ENTITY = "http://lala.home.jeena.net:3002";
|
||||
|
||||
var app_info = {
|
||||
"name": "Tentia",
|
||||
"description": "A small TentStatus client.",
|
||||
"url": "http://jabs.nu/Tentia/",
|
||||
"icon": "http://jabs.nu/Tentia/icon.png",
|
||||
"redirect_uris": [
|
||||
"tentia://oauthtoken"
|
||||
],
|
||||
"scopes": {
|
||||
"read_posts": "Uses posts to show them in a list",
|
||||
"write_posts": "Posts on users behalf"
|
||||
}
|
||||
};
|
||||
|
||||
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) {
|
||||
xhr.setRequestHeader("Authorization", auth_header);
|
||||
}
|
||||
},
|
||||
url: url,
|
||||
accepts: "application/vnd.tent.v0+json",
|
||||
contentType: "application/vnd.tent.v0+json",
|
||||
|
@ -32,167 +25,15 @@ 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function makeid(len) {
|
||||
var text = "";
|
||||
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
for( var i=0; i < len; i++ )
|
||||
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
function OauthImplementation() {
|
||||
this.requestProfileURL(MY_ENTITY);
|
||||
//this.requestAToken();
|
||||
}
|
||||
|
||||
OauthImplementation.prototype.requestProfileURL = function (entity) {
|
||||
var those = this;
|
||||
getURL(entity, "HEAD", function(resp) {
|
||||
var headers = resp.getAllResponseHeaders();
|
||||
var regex = /Link: <([^>]*)>; rel="https:\/\/tent.io\/rels\/profile"/;
|
||||
var profile_url = headers.match(regex)[1]
|
||||
if (profile_url == "/profile") {
|
||||
profile_url = entity + "/profile";
|
||||
}
|
||||
alert(profile_url);
|
||||
those.register(profile_url);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
OauthImplementation.prototype.register = function (url) {
|
||||
var those = this;
|
||||
getURL(url, "GET", function(resp) {
|
||||
var profile = JSON.parse(resp.responseText);
|
||||
var server = profile["https://tent.io/types/info/core/v0.1.0"]["servers"][0];
|
||||
var callback = function(resp) {
|
||||
var data = JSON.parse(resp.responseText);
|
||||
those.authRequest(server, data);
|
||||
}
|
||||
alert(server + "/apps")
|
||||
getURL(server + "/apps", "POST", callback, JSON.stringify(app_info));
|
||||
});
|
||||
}
|
||||
|
||||
OauthImplementation.prototype.authRequest = function(server, register_data) {
|
||||
// id
|
||||
// mac_key_id
|
||||
// mac_key
|
||||
// mac_algorithm
|
||||
var state = makeid(19);
|
||||
var auth = "/oauth/authorize?client_id=" + register_data["id"]
|
||||
+ "&redirect_uri=" + escape(app_info["redirect_uris"][0])
|
||||
+ "&scope=" + Object.keys(app_info["scopes"]).join(",")
|
||||
+ "&state=" + state
|
||||
+ "&tent_post_types=" + escape("https://tent.io/types/posts/status/v0.1.0");
|
||||
|
||||
alert(server + auth)
|
||||
controller.openURL_(server + auth);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
OauthImplementation.prototype.requestAToken = function() {
|
||||
var url = OAUTH_REQUEST_TOKEN_URL;
|
||||
var _this = this;
|
||||
|
||||
var message = { method:"POST" , action:url };
|
||||
|
||||
OAuth.completeRequest(message,
|
||||
{ consumerKey : OAUTH_CONSUMER_KEY
|
||||
, consumerSecret: OAUTH_CONSUMER_SECRET
|
||||
//, token : controller.oauth.accessToken.key
|
||||
//, tokenSecret : controller.oauth.accessToken.secret
|
||||
});
|
||||
|
||||
$.ajax({
|
||||
beforeSend: function(xhr) {
|
||||
xhr.setRequestHeader("Authorization", OAuth.getAuthorizationHeader("", message.parameters));
|
||||
},
|
||||
url: url,
|
||||
type: 'POST',
|
||||
dataType: 'text',
|
||||
success: function(data) {
|
||||
_this.requestTokenTicketFinished(data);
|
||||
},
|
||||
error:function (xhr, ajaxOptions, thrownError) {
|
||||
alert(xhr.statusText);
|
||||
alert(ajaxOptions);
|
||||
alert(thrownError);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
OauthImplementation.prototype.requestTokenTicketFinished = function(data) {
|
||||
controller.openURL_(OAUTH_USER_AUTHORIZATION_URL + "?" + data);
|
||||
}
|
||||
|
||||
OauthImplementation.prototype.requestAccessToken = function(responseBody) {
|
||||
// "twittia://oauth_token?oauth_token=jCcf7ClzJMbE4coZdONi467OAQxRGOBZJsuopG8C8&oauth_verifier=BK2ZkAIz51lqI4qta8MnKc280GyDLy0OQBpdsEmjT40"
|
||||
alert("XXX");
|
||||
alert(responseBody);
|
||||
|
||||
|
||||
/*
|
||||
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);
|
||||
}
|
||||
});*/
|
||||
}
|
||||
|
||||
OauthImplementation.prototype.requestAccessTokenTicketFinished = function(responseBody) {
|
||||
var urlVars = getUrlVars(responseBody);
|
||||
controller.storeAccessToken_secret_userId_andScreenName_(
|
||||
urlVars["oauth_token"],
|
||||
urlVars["oauth_token_secret"],
|
||||
urlVars["user_id"],
|
||||
urlVars["screen_name"]
|
||||
);
|
||||
}
|
||||
|
||||
function getUrlVars(url)
|
||||
{
|
||||
function getUrlVars(url) {
|
||||
var vars = [], hash;
|
||||
if(url.indexOf("#") > -1) url = url.slice(0, url.indexOf("#"));
|
||||
var hashes = url.slice(url.indexOf('?') + 1).split('&');
|
||||
|
@ -205,4 +46,151 @@ function getUrlVars(url)
|
|||
return vars;
|
||||
}
|
||||
|
||||
function makeAuthHeader(url, http_method, mac_key, mac_key_id) {
|
||||
|
||||
url = URI(url);
|
||||
var nonce = makeid(8);
|
||||
var time_stamp = parseInt((new Date).getTime() / 1000, 10);
|
||||
|
||||
var normalizedRequestString = ""
|
||||
+ time_stamp + '\n'
|
||||
+ nonce + '\n'
|
||||
+ http_method + '\n'
|
||||
+ url.path() + '\n'
|
||||
+ url.hostname() + '\n'
|
||||
+ url.port() + '\n'
|
||||
+ '\n' ;
|
||||
|
||||
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, mac_key);
|
||||
hmac.update(normalizedRequestString);
|
||||
var hash = hmac.finalize();
|
||||
var mac = hash.toString(CryptoJS.enc.Base64);
|
||||
|
||||
return 'MAC id="' + mac_key_id +
|
||||
'", ts="' + time_stamp +
|
||||
'", nonce="' + nonce +
|
||||
'", mac="' + mac + '"';
|
||||
}
|
||||
|
||||
|
||||
function makeid(len) {
|
||||
var text = "";
|
||||
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
for( var i=0; i < len; i++ )
|
||||
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
function OauthImplementation(entity) {
|
||||
this.entity = entity || "http://lala.home.jeena.net:3002";
|
||||
this.app_info = {
|
||||
"id": null,
|
||||
"name": "Tentia",
|
||||
"description": "A small TentStatus client.",
|
||||
"url": "http://jabs.nu/Tentia/",
|
||||
"icon": "http://jabs.nu/Tentia/icon.png",
|
||||
"redirect_uris": [
|
||||
"tentia://oauthtoken"
|
||||
],
|
||||
"scopes": {
|
||||
"read_posts": "Uses posts to show them in a list",
|
||||
"write_posts": "Posts on users behalf"
|
||||
}
|
||||
};
|
||||
this.register_data = null;
|
||||
this.profile = null;
|
||||
this.state = null;
|
||||
this.requestProfileURL(this.entity);
|
||||
}
|
||||
|
||||
OauthImplementation.prototype.apiRoot = function() {
|
||||
return this.profile["https://tent.io/types/info/core/v0.1.0"]["servers"][0];
|
||||
}
|
||||
|
||||
OauthImplementation.prototype.requestProfileURL = function (entity) {
|
||||
var those = this;
|
||||
getURL(entity, "HEAD", function(resp) {
|
||||
var headers = resp.getAllResponseHeaders();
|
||||
var regex = /Link: <([^>]*)>; rel="https:\/\/tent.io\/rels\/profile"/;
|
||||
var profile_url = headers.match(regex)[1]
|
||||
if (profile_url == "/profile") {
|
||||
profile_url = entity + "/profile";
|
||||
}
|
||||
those.register(profile_url);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
OauthImplementation.prototype.register = function (url) {
|
||||
var those = this;
|
||||
getURL(url, "GET", function(resp) {
|
||||
those.profile = JSON.parse(resp.responseText);
|
||||
var callback = function(resp) {
|
||||
var data = JSON.parse(resp.responseText);
|
||||
those.authRequest(data);
|
||||
}
|
||||
getURL(those.apiRoot() + "/apps", "POST", callback, JSON.stringify(those.app_info));
|
||||
});
|
||||
}
|
||||
|
||||
OauthImplementation.prototype.authRequest = function(register_data) {
|
||||
// id
|
||||
// mac_key_id
|
||||
// mac_key
|
||||
// mac_algorithm
|
||||
this.register_data = register_data;
|
||||
|
||||
this.state = makeid(19);
|
||||
var auth = "/oauth/authorize?client_id=" + register_data["id"]
|
||||
+ "&redirect_uri=" + escape(this.app_info["redirect_uris"][0])
|
||||
+ "&scope=" + Object.keys(this.app_info["scopes"]).join(",")
|
||||
+ "&state=" + this.state
|
||||
+ "&tent_post_types=" + escape("https://tent.io/types/posts/status/v0.1.0");
|
||||
|
||||
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 url = this.apiRoot() + "/apps/" + this.register_data["id"] + "/authorizations";
|
||||
|
||||
var requestBody = JSON.stringify({
|
||||
'code' : urlVars["code"],
|
||||
'token_type' : "mac"
|
||||
});
|
||||
|
||||
var those = this;
|
||||
var callback = function(resp) {
|
||||
those.requestAccessTokenTicketFinished(resp.responseText);
|
||||
};
|
||||
|
||||
var auth_header = makeAuthHeader(url, "POST", this.register_data["mac_key"], this.register_data["mac_key_id"]);
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
OauthImplementation.prototype.requestAccessTokenTicketFinished = function(responseBody) {
|
||||
|
||||
var secret_data = {
|
||||
access: JSON.parse(responseBody),
|
||||
register_data: this.register_data
|
||||
}
|
||||
|
||||
controller.storeSecretData_(JSON.stringify(secret_data));
|
||||
}
|
||||
|
||||
var tentia_oauth;
|
Reference in a new issue