This repository has been archived on 2025-08-18. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Bungloo/WebKit/scripts/controller/Oauth.js
2012-11-07 03:51:27 +01:00

134 lines
No EOL
4.6 KiB
JavaScript

define([
"helper/HostApp",
"helper/Paths",
"helper/Hmac"
],
function(HostApp, Paths, Hmac) {
function Oauth() {
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;
}
Oauth.prototype.authenticate = function() {
this.entity = HostApp.stringForKey("entity");
this.requestProfileURL(this.entity);
}
Oauth.prototype.apiRoot = function() {
return this.profile["https://tent.io/types/info/core/v0.1.0"]["servers"][0];
}
Oauth.prototype.requestProfileURL = function (entity) {
var those = this;
Paths.findProfileURL(entity, function(profile_url) {
those.register(profile_url);
});
}
Oauth.prototype.register = function (url) {
var those = this;
Paths.getURL(url, "GET", function(resp) {
those.profile = JSON.parse(resp.responseText);
HostApp.setStringForKey(those.apiRoot(), "api_root");
var callback = function(resp) {
var data = JSON.parse(resp.responseText);
those.authRequest(data);
}
Paths.getURL(Paths.mkApiRootPath("/apps"), "POST", callback, JSON.stringify(those.app_info));
});
}
Oauth.prototype.authRequest = function(register_data) {
// id
// mac_key_id
// mac_key
// mac_algorithm
this.register_data = register_data;
// Needed for later App Registration Modification
HostApp.setStringForKey(register_data["mac_key"], "app_mac_key");
HostApp.setStringForKey(register_data["mac_key_id"], "app_mac_key_id");
HostApp.setStringForKey(register_data["id"], "app_id");
HostApp.setStringForKey(register_data["mac_algorithm"], "app_mac_algorithm");
this.state = Hmac.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");
HostApp.openURL(this.apiRoot() + auth);
}
Oauth.prototype.requestAccessToken = function(responseBody) {
// /oauthtoken?code=51d0115b04d1ed94001dde751c5b360f&state=aQfH1VEohYsQr86qqyv
var urlVars = Paths.getUrlVars(responseBody);
if(this.state && this.state != "" && urlVars["state"] == this.state) {
var url = Paths.mkApiRootPath("/apps/") + this.register_data["id"] + "/authorizations";
var requestBody = JSON.stringify({
'code' : urlVars["code"],
'token_type' : "mac"
});
var those = this;
var http_method = "POST";
var callback = function(resp) {
those.requestAccessTokenTicketFinished(resp.responseText);
};
var auth_header = Hmac.makeAuthHeader(
url,
http_method,
HostApp.stringForKey("app_mac_key"),
HostApp.stringForKey("app_mac_key_id")
);
Paths.getURL(url, http_method, callback, requestBody, auth_header);
} else {
alert("State is not the same: {" + this.state + "} vs {" + urlVars["state"] + "}")
}
this.state = null; // reset the state
}
Oauth.prototype.requestAccessTokenTicketFinished = function(responseBody) {
var access = JSON.parse(responseBody);
HostApp.setStringForKey(access["access_token"], "user_access_token");
HostApp.setStringForKey(access["mac_key"], "user_mac_key");
HostApp.setStringForKey(access["mac_algorithm"], "user_mac_algorithm");
HostApp.setStringForKey(access["token_type"], "user_token_type");
HostApp.loggedIn();
}
Oauth.prototype.logout = function() {
}
return Oauth;
});