Skip to content
This repository was archived by the owner on Jun 5, 2024. It is now read-only.

Commit 5bcbfbc

Browse files
author
Vincent Petry
authored
Merge pull request #100 from owncloud/feature/add-bearer-token
Add bearer token support
2 parents a9e4834 + 6d2a154 commit 5bcbfbc

6 files changed

+110
-123
lines changed

owncloud/fileManagement.js

+1-28
Original file line numberDiff line numberDiff line change
@@ -204,33 +204,6 @@ files.prototype.getFile = function(path, localPath) {
204204
});
205205
};
206206

207-
/**
208-
* Downloads a remote directory as zip
209-
* @param {string} remotePath path of the folder at OC instance
210-
* @param {string} localPath path where to download
211-
* @returns {Promise.<status>} boolean: whether the operation was successful
212-
* @returns {Promise.<error>} string: error message, if any.
213-
*/
214-
files.prototype.getDirectoryAsZip = function(path, localPath) {
215-
path = helpers._normalizePath(path);
216-
localPath = localPath || __dirname + path;
217-
localPath = helpers._checkExtensionZip(localPath);
218-
path = helpers._encodeString(path);
219-
path = encodeURIComponent(path);
220-
path = path.split('%2F').join('/');
221-
222-
var url = helpers.instance + 'index.php/apps/files/ajax/download.php?dir=' + (path);
223-
224-
return new Promise((resolve, reject) => {
225-
helpers._writeData(url, localPath)
226-
.then(status => {
227-
resolve(status);
228-
}).catch(error => {
229-
reject(error);
230-
});
231-
});
232-
};
233-
234207
/**
235208
* Upload a file
236209
* @param {string} remotePath path of the file to be created at OC instance
@@ -252,7 +225,7 @@ files.prototype.putFile = function(path, localPath, keepMTime) {
252225
}
253226

254227
headers['Content-Length'] = helpers._getFileSize(localPath);
255-
headers.authorization = "Basic " + new Buffer(helpers._username + ":" + helpers._password).toString('base64');
228+
headers.authorization = helpers._authHeader;
256229

257230
return new Promise((resolve, reject) => {
258231
helpers._readFile(path, localPath, headers).then(status => {

owncloud/helperFunctions.js

+49-34
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ function helpers() {
3939
this.OCS_SHARE_TYPE_REMOTE = 6;
4040

4141
this.instance = null;
42-
this._username = null;
43-
this._password = null;
42+
this._authHeader = null;
4443
this._version = null;
4544
this._capabilities = null;
45+
this._currentUser = null;
4646
}
4747

4848
/**
@@ -51,29 +51,15 @@ function helpers() {
5151
*/
5252
helpers.prototype.setInstance = function(instance) {
5353
this.instance = instance;
54-
};
55-
56-
/**
57-
* sets the username
58-
* @param {string} username username to be used for logging in
59-
*/
60-
helpers.prototype.setUsername = function(username) {
61-
this._username = username;
62-
63-
var instancePath = '/' + this.instance.split('/').slice(3).join('/');
64-
65-
this._davPath = instancePath + 'remote.php/dav/files/' +
66-
encodeURIComponent(this._encodeString(this._username));
67-
6854
this._webdavUrl = this.instance + 'remote.php/webdav';
6955
};
7056

7157
/**
72-
* sets the password
73-
* @param {string} password password to be used for logging in
58+
* sets the username
59+
* @param {string} authHeader authorization header; either basic or bearer or what ever
7460
*/
75-
helpers.prototype.setPassword = function(password) {
76-
this._password = password;
61+
helpers.prototype.setAuthorization = function(authHeader) {
62+
this._authHeader = authHeader;
7763
};
7864

7965
/**
@@ -92,6 +78,14 @@ helpers.prototype.getCapabilities = function() {
9278
return this._capabilities;
9379
};
9480

81+
/**
82+
* Gets the logged in user
83+
* @returns {object} user info
84+
*/
85+
helpers.prototype.getCurrentUser= function() {
86+
return this._currentUser;
87+
};
88+
9589
/**
9690
* Updates the capabilities of user logging in.
9791
* @returns {Promise.<capabilities>} object: all capabilities
@@ -109,8 +103,29 @@ helpers.prototype._updateCapabilities = function() {
109103

110104
resolve(self._capabilities);
111105
}).catch(error => {
112-
reject(error);
113-
});
106+
reject(error);
107+
});
108+
});
109+
};
110+
111+
/**
112+
* Updates the user logging in.
113+
* @returns {Promise.<_currentUser>} object: _currentUser
114+
* @returns {Promise.<error>} string: error message, if any.
115+
*/
116+
helpers.prototype._updateCurrentUser = function() {
117+
var self = this;
118+
return new Promise((resolve, reject) => {
119+
self._makeOCSrequest('GET', self.OCS_SERVICE_CLOUD, "user")
120+
.then(data => {
121+
var body = data.data.ocs.data;
122+
123+
self._currentUser = body;
124+
125+
resolve(self._currentUser);
126+
}).catch(error => {
127+
reject(error);
128+
});
114129
});
115130
};
116131

@@ -131,13 +146,13 @@ helpers.prototype._makeOCSrequest = function(method, service, action, data) {
131146
err = "Please specify a server URL first";
132147
}
133148

134-
if (!this._username || !this._password) {
135-
err = "Please specify a username AND password first.";
149+
if (!this._authHeader) {
150+
err = "Please specify an authorization first.";
136151
}
137152

138153
// Set the headers
139154
var headers = {
140-
authorization: "Basic " + new Buffer(this._username + ":" + this._password).toString('base64'),
155+
authorization: this._authHeader,
141156
'OCS-APIREQUEST': true
142157
};
143158

@@ -228,8 +243,8 @@ helpers.prototype._makeDAVrequest = function(method, path, headerData, body) {
228243
err = "Please specify a server URL first";
229244
}
230245

231-
if (!this._username || !this._password) {
232-
err = "Please specify a username AND password first.";
246+
if (!this._authHeader) {
247+
err = "Please specify an authorization first.";
233248
}
234249

235250
path = self._normalizePath(path);
@@ -239,7 +254,7 @@ helpers.prototype._makeDAVrequest = function(method, path, headerData, body) {
239254

240255
// Set the headers
241256
var headers = {
242-
authorization: "Basic " + new Buffer(this._username + ":" + this._password).toString('base64')
257+
authorization: this._authHeader
243258
};
244259

245260
//Configure the request
@@ -337,12 +352,12 @@ helpers.prototype._get = function(url) {
337352
err = "Please specify a server URL first";
338353
}
339354

340-
if (!this._username || !this._password) {
341-
err = "Please specify a username AND password first.";
355+
if (!this._authHeader) {
356+
err = "Please specify an authorization first.";
342357
}
343358

344359
var headers = {
345-
authorization: "Basic " + new Buffer(this._username + ":" + this._password).toString('base64'),
360+
authorization: this._authHeader,
346361
'Content-Type': 'application/x-www-form-urlencoded'
347362
};
348363

@@ -388,12 +403,12 @@ helpers.prototype._writeData = function(url, fileName) {
388403
err = "Please specify a server URL first";
389404
}
390405

391-
if (!this._username || !this._password) {
392-
err = "Please specify a username AND password first.";
406+
if (!this._authHeader) {
407+
err = "Please specify an authorization first.";
393408
}
394409

395410
var headers = {
396-
authorization: "Basic " + new Buffer(this._username + ":" + this._password).toString('base64'),
411+
authorization: this._authHeader,
397412
'Content-Type': 'application/octet-stream'
398413
};
399414

owncloud/owncloud.js

+54-8
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,40 @@ function ownCloud(instance) {
6969
* @returns {Promise.<error>} string: error message, if any.
7070
*/
7171
ownCloud.prototype.login = function(username, password) {
72-
helpers.setUsername(username);
73-
helpers.setPassword(password);
72+
var basicAuth = "Basic " + new Buffer(username + ":" + password).toString('base64');
73+
helpers.setAuthorization(basicAuth);
7474

75+
var self = this;
7576
/* jshint unused: false */
7677
return new Promise((resolve, reject) => {
7778
helpers._updateCapabilities()
78-
.then(body => {
79-
resolve(true);
79+
.then(() => {
80+
resolve(self.getCurrentUser());
8081
}).catch(error => {
81-
reject(error);
82-
});
82+
reject(error);
83+
});
84+
});
85+
/* jshint unused: true */
86+
};
87+
88+
/**
89+
* Logs in to the specified ownCloud instance (Updates capabilities)
90+
* @param {string} token name of the user to login
91+
* @returns {Promise.<status>} boolean: whether login was successful or not
92+
* @returns {Promise.<error>} string: error message, if any.
93+
*/
94+
ownCloud.prototype.loginWithBearer = function(token) {
95+
helpers.setAuthorization("Bearer " + token);
96+
97+
var self = this;
98+
/* jshint unused: false */
99+
return new Promise((resolve, reject) => {
100+
helpers._updateCapabilities()
101+
.then(() => {
102+
resolve(self.getCurrentUser());
103+
}).catch(error => {
104+
reject(error);
105+
});
83106
});
84107
/* jshint unused: true */
85108
};
@@ -167,12 +190,35 @@ ownCloud.prototype.getCapabilities = function() {
167190
.then(body => {
168191
resolve(body);
169192
}).catch(error => {
170-
reject(error);
171-
});
193+
reject(error);
194+
});
172195
} else {
173196
resolve(capabilities);
174197
}
175198
});
176199
};
177200

201+
/**
202+
* Gets the currently logged in user
203+
* @returns {Promise.<capabilities>}
204+
* @returns {Promise.<reject>}
205+
*/
206+
ownCloud.prototype.getCurrentUser = function() {
207+
var self = this;
208+
var user = helpers.getCurrentUser();
209+
/* jshint unused: false */
210+
return new Promise((resolve, reject) => {
211+
if (user === null) {
212+
helpers._updateCurrentUser()
213+
.then(body => {
214+
resolve(body);
215+
}).catch(error => {
216+
reject(error);
217+
});
218+
} else {
219+
resolve(user);
220+
}
221+
});
222+
};
223+
178224
module.exports = ownCloud;

owncloud/test/test.js

+5-32
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe("Currently testing Login and initLibrary,", function() {
126126
oc = new ownCloud(owncloudURL);
127127

128128
oc.login(username, password).then(status => {
129-
expect(status).toBe(true);
129+
expect(status).toEqual({id: 'admin', 'display-name': 'admin', email: {}});
130130
done();
131131
}).catch(error => {
132132
console.log(error);
@@ -1087,7 +1087,7 @@ describe("Currently testing file sharing,", function () {
10871087
beforeEach(function (done) {
10881088
oc = new ownCloud(owncloudURL);
10891089
oc.login(username, password).then(status => {
1090-
expect(status).toBe(true);
1090+
expect(status).toEqual({id: 'admin', 'display-name': 'admin', email: {}});
10911091
return oc.users.createUser(testUser, testUserPassword);
10921092
}).then(status2 => {
10931093
expect(status2).toBe(true);
@@ -1487,7 +1487,7 @@ describe("Currently testing group management,", function() {
14871487
beforeEach(function(done) {
14881488
oc = new ownCloud(owncloudURL);
14891489
oc.login(username, password).then(status => {
1490-
expect(status).toBe(true);
1490+
expect(status).toEqual({id: 'admin', 'display-name': 'admin', email: {}});
14911491
return oc.groups.createGroup(testGroup);
14921492
}).then(status2 => {
14931493
expect(status2).toBe(true);
@@ -1567,7 +1567,7 @@ describe("Currently testing files management,", function () {
15671567
beforeEach(function(done) {
15681568
oc = new ownCloud(owncloudURL);
15691569
oc.login(username, password).then(status => {
1570-
expect(status).toBe(true);
1570+
expect(status).toEqual({id: 'admin', 'display-name': 'admin', email: {}});
15711571
done();
15721572
}).catch(error => {
15731573
expect(error).toBe(null);
@@ -1828,33 +1828,6 @@ describe("Currently testing files management,", function () {
18281828
});
18291829
});
18301830

1831-
it('checking method : getDirectoryAsZip for an existent folder', function (done) {
1832-
oc.files.getDirectoryAsZip(testFolder, downloadBasePath + timeRightNow +'.zip').then(status => {
1833-
expect(status).toBe(true);
1834-
1835-
fs.readFile(downloadBasePath + timeRightNow +'.zip', function(err, data) {
1836-
JSZip.loadAsync(data).then(function (zip) {
1837-
var count = Object.keys(zip.files).length;
1838-
expect(count).toEqual(7);
1839-
done();
1840-
});
1841-
});
1842-
}).catch(error => {
1843-
expect(error).toBe(null);
1844-
done();
1845-
});
1846-
});
1847-
1848-
it('checking method : getDirectoryAsZip for a non existent folder', function (done) {
1849-
oc.files.getDirectoryAsZip(testFolder + timeRightNow, downloadBasePath + timeRightNow +'.zip').then(status => {
1850-
expect(status).toBe(null);
1851-
done();
1852-
}).catch(error => {
1853-
expect(error).toBe('specified file/folder could not be located');
1854-
done();
1855-
});
1856-
});
1857-
18581831
it('checking method : putFile for an existent file', function (done) {
18591832
oc.files.putFile('/', localFile).then(status => {
18601833
expect(status).toBe(true);
@@ -1897,7 +1870,7 @@ describe("Currently testing files management,", function () {
18971870
expect(status).toBe(true);
18981871
return oc.files.list(testFolder + '/testDownloadDir', 'infinity');
18991872
}).then(files => {
1900-
expect(files.length).toEqual(5);
1873+
expect(files.length).toEqual(3);
19011874
done();
19021875
}).catch(error => {
19031876
expect(error).toBe(null);

owncloud/test/testUnauthenticated.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var sharedFilesWithUser = {
4646
};
4747

4848
// COMMON ERRORS
49-
var errors = ["Current user is not logged in", "Please specify a username AND password first."];
49+
var errors = ["Current user is not logged in", "Please specify an authorization first."];
5050

5151
describe("Currently testing getConfig, getVersion and getCapabilities", function() {
5252
beforeEach(function() {
@@ -587,16 +587,6 @@ describe("Currently testing files management,", function() {
587587
});
588588
});
589589

590-
it('checking method : getDirectoryAsZip', function(done) {
591-
oc.files.getDirectoryAsZip(testFolder, downloadBasePath + timeRightNow + '.zip').then(status => {
592-
expect(status).toBe(null);
593-
done();
594-
}).catch(error => {
595-
expect(errors.indexOf(error)).toBeGreaterThan(-1);
596-
done();
597-
});
598-
});
599-
600590
it('checking method : putFile', function(done) {
601591
try {
602592
oc.files.putFile('/', localFile).then(status => {

0 commit comments

Comments
 (0)