-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoauth.js
103 lines (91 loc) · 3.62 KB
/
oauth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (c) 2012 Adam Jablonski
*
* Gmail Notify Extension is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* Gmail Notify Extension is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Gnome Documents; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* // Author: Adam Jabłoński <jablona123@gmail.com>
*
*/
const Extension = imports.misc.extensionUtils.getCurrentExtension();
const Sha =Extension.imports.sha;
//const Sha =imports.sha;
const Signals=imports.signals;
const GLib=imports.gi.GLib;
const _DEBUG =false;
var OAuth=function(account,method,service) {
this._init(account,method,service);
}
OAuth.prototype= {
_init : function (account,service,method) {
this.account = account ;
this.method = typeof(method) != 'undefined' ? method : "GET";
this.service = service;
this.oAuth = account.get_oauth_based();
this.oAcc = account.get_account();
this.acc_token=this.oAuth.call_get_access_token_sync(null);
this.oAuth_auth="";
this.error=null;
this._makeStrings();
},
_initStrings : function () {
this.oAuth_base=escape(this.method)+"&"+encodeURIComponent(this.service)+"&";
this.oAuth_req=this.method+" "+this.service+" ";
},
_setNonce : function() {
this.nonce=""; for (let j=1;j<=18;j++) this.nonce+=Math.floor(Math.random()*11).toString();
let dt=new Date();
this.timestamp=(dt.getTime()/1000).toFixed(0).toString();
},
_makeStrings : function () {
//https://mail.google.com/mail/b/"+_email+"/imap/
try {
this._initStrings();
this._setNonce();
let anames = ["oauth_consumer_key","oauth_nonce","oauth_signature_method","oauth_timestamp","oauth_token" ,"oauth_version"];
let avalues = [this.oAuth.consumer_key , this.nonce , "HMAC-SHA1" ,this.timestamp , encodeURIComponent(this.acc_token[1]),"1.0"]
let avalues1 = [this.oAuth.consumer_key , this.nonce , "HMAC-SHA1" ,this.timestamp , this.acc_token[1] ,"1.0"]
for (let i=0;i<anames.length;i++) {
this.oAuth_base+= encodeURIComponent(( i > 0 ? "&":"") +anames[i]+"="+avalues[i]);
this.oAuth_req += anames[i]+"=\""+avalues1[i]+"\",";
this.oAuth_auth += anames[i]+"=\""+avalues1[i]+"\",";
}
if (_DEBUG) global.log("Oauth Signature base:" + this.oAuth_base);
this.oAuth_sigKey=escape(this.oAuth.consumer_secret)+"&"+encodeURI(this.acc_token[2]);
if (_DEBUG) global.log("Oauth Signature key:" + this.oAuth_sigKey);
//now hmac sha-1 signature base with key
let shaObj = new jsSHA(this.oAuth_base, "ASCII");
let hmac = shaObj.getHMAC(this.oAuth_sigKey, "ASCII", "SHA-1", "B64");
if (_DEBUG) global.log("Oauth Signature hmac:" + hmac);
this.oAuth_req+="oauth_signature=\""+hmac+"\"";
this.oAuth_auth+="oauth_signature=\""+hmac+"\"";
if (_DEBUG) global.log("auth_req:" + this.oAuth_req);
this.oAuth_str=GLib.base64_encode(this.oAuth_req);
if (_DEBUG) global.log("auth_str:" +this.oAuth_str);
this.error=null;
}
catch (err)
{
global.log(err.message);
this.emit('error',err);
this.error=err;
}
}
};
try {
Signals.addSignalMethods(OAuth.prototype);
}
catch (err) {
global.log(err.message);
}