Skip to content

Commit ae3bedb

Browse files
committed
Add public mode for box auth
1 parent cdfdc02 commit ae3bedb

File tree

12 files changed

+75
-26
lines changed

12 files changed

+75
-26
lines changed

client/core/app.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ define([
33
'utils/url',
44
'core/box',
55
'core/session',
6-
'core/addons'
7-
], function (hr, url, box, session, addons) {
6+
'core/addons',
7+
'core/box'
8+
], function (hr, url, box, session, addons, box) {
89

910
// Define base application
1011
var Application = hr.Application.extend({
@@ -23,7 +24,6 @@ define([
2324
// Constructor
2425
initialize: function() {
2526
Application.__super__.initialize.apply(this, arguments);
26-
this.isAuth = false;
2727

2828
box.on("status", function(state) {
2929
this.$(".codebox-connexion-alert").toggle(!state);
@@ -37,7 +37,6 @@ define([
3737
return {
3838
'email': queries.email || hr.Storage.get("email"),
3939
'token': queries.token || hr.Storage.get("token"),
40-
'isAuth': this.isAuth
4140
};
4241
},
4342

@@ -50,7 +49,7 @@ define([
5049

5150
if (email && password) {
5251
this.doLogin(email, password, true);
53-
} else if (this.isAuth) {
52+
} else if (box.isAuth()) {
5453
addons.loadAll().then(function() {
5554
that.$(".codebox-loading-alert").remove();
5655

@@ -73,14 +72,18 @@ define([
7372
var password = this.$(".login-box #login-token").val();
7473
var tosave = this.$(".login-box #login-save").is(":checked");
7574

76-
this.doLogin(email, password, tosave).fail(function() {
77-
that.$(".login-box .form-group").addClass("has-error");
78-
});
75+
this.doLogin(email, password, tosave);
7976
},
8077

8178
// Do login
8279
doLogin: function(email, password, tosave) {
8380
var that = this;
81+
82+
// If no password: generate a random one
83+
if (!password) {
84+
password = Math.random().toString(36).substring(8);
85+
}
86+
8487
return session.start(email, password).then(function() {
8588
if (tosave) {
8689
hr.Storage.set("email", email);
@@ -89,9 +92,9 @@ define([
8992
hr.Storage.set("email", "");
9093
hr.Storage.set("token", "");
9194
}
92-
93-
that.isAuth = true;
9495
that.render();
96+
}).fail(function() {
97+
that.$(".login-box .form-group").addClass("has-error");
9598
});
9699
}
97100
});

client/core/box.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ define([
1616
box.on("box:users:remove", function(e) {
1717
collaborators.remove(collaborators.getById(e.data.userId));
1818
});
19-
box.on("set", function() {
20-
collaborators.getCollaborators();
21-
})
2219

2320
// Search for files
2421
search.handler({

client/core/session.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ define([
1919
});
2020

2121
return {
22+
// Prepare session
23+
prepare: function() {
24+
return box.status();
25+
},
26+
2227
// Start session
2328
start: function(email, token) {
2429
var that = this;
@@ -27,11 +32,12 @@ define([
2732
'email': email,
2833
'token': token
2934
}, user).then(function() {
35+
console.log("auth succeed");
3036
// Get installed addons
3137
return addons.getInstalled();
3238
}).then(function() {
33-
// Return box status
34-
return box.status();
39+
// Get collaborators
40+
return collaborators.getCollaborators();
3541
});
3642
}
3743
};

client/main.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ require([
33
"hr/hr",
44
"hr/args",
55
"core/app",
6+
"core/session",
67
"config",
78
'views/views',
89
'resources/resources',
9-
], function(_, hr, args, app, config) {
10+
], function(_, hr, args, app, session, config) {
1011
// Configure hr
1112
hr.configure(args);
1213

@@ -17,5 +18,7 @@ require([
1718
}
1819
});
1920

20-
app.run();
21+
session.prepare().then(function() {
22+
app.run();
23+
});
2124
});

client/models/box.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ define([
1515
'uptime': 0,
1616
'mtime': 0,
1717
'collaborators': 0,
18+
'auth': false
1819
},
1920

2021
/*
@@ -110,10 +111,21 @@ define([
110111

111112
return api.rpc("/auth/join", authInfo).then(function(info) {
112113
that.user.set(info);
114+
that.set("auth", true);
113115
return Q(info);
116+
}, function(err) {
117+
that.set("auth", false);
118+
return Q.reject(err);
114119
});
115120
},
116121

122+
/*
123+
* test if logged to the box
124+
*/
125+
isAuth: function() {
126+
return this.get("auth", false);
127+
},
128+
117129
/*
118130
* Get box status
119131
*/

client/resources/templates/main.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<% if (isAuth) { %>
1+
<% if (session.box.isAuth()) { %>
22
<div id="codebox">
33
<!-- Lateral bar -->
44
<%= view.component("layout.lateralbar", {}, "lateralbar") %>
@@ -37,10 +37,15 @@ <h4 class="modal-title">Connexion Error</h4>
3737
<form role="form">
3838
<div class="form-group">
3939
<input type="email" class="form-control" id="login-email" placeholder="Enter email" value="<%- email %>">
40+
<% if (session.box.get("public")) { %>
41+
<p class="help-block">This box is public, please enter an email to log in.</p>
42+
<% } %>
4043
</div>
44+
<% if (!session.box.get("public")) { %>
4145
<div class="form-group">
4246
<input type="password" class="form-control" id="login-token" placeholder="Enter token" value="<%- token %>">
4347
</div>
48+
<% } %>
4449
<div class="checkbox">
4550
<label>
4651
<input type="checkbox" id="login-save" checked> Remember me

core/cb.core/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ function setup(options, imports, register) {
1212
var title = options.title;
1313
var rootPath = options.root || path.resolve(__dirname, '..', '..');
1414

15-
var workspace = new Workspace(title, rootPath, events);
15+
var workspace = new Workspace({
16+
'root': rootPath,
17+
'title': title,
18+
'public': options.public
19+
}, events);
1620

1721
register(null, {
1822
"workspace": workspace

core/cb.core/workspace.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,29 @@ var User = require('./user').User;
88
var qfail = require('../utils').qfail;
99

1010

11-
function Workspace(name, root, events, id) {
11+
function Workspace(options, events) {
12+
options = _.defaults({}, options, {
13+
'id': uuid.v4(),
14+
'secret': uuid.v4(),
15+
'name': 'codebox',
16+
'public': true,
17+
'root': "./"
18+
});
19+
1220
// Public ID of workspace (ok to share)
13-
this.id = id || uuid.v4();
21+
this.id = options.id;
1422

1523
// Secret token used for securing cookies, sessions ...
16-
this.secret = uuid.v4();
24+
this.secret = options.secret;
1725

1826
// Name
19-
this.name = name || "project";
27+
this.name = options.name;
28+
29+
// Public
30+
this.public = options.public;
2031

2132
// Base folder
22-
this.root = root;
33+
this.root = options.root;
2334

2435
// Mapping of directories
2536
this._users = {};

core/cb.rpc.auth/service.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ function AuthRPCService(workspace, hooks) {
1212
AuthRPCService.prototype.join = function(args, meta) {
1313
var that = this;
1414

15+
meta.req.session.userId = null;
16+
1517
return this.hooks.use("auth", {
1618
'email': args.email,
1719
'token': args.token

core/cb.rpc.box/service.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ BoxRPCService.prototype.status = function(args, meta) {
1313
return Q({
1414
'status': "ok",
1515
'name': this.workspace.name,
16+
'public': this.workspace.public,
1617
'uptime': process.uptime(),
1718
'mtime': this.workspace.mtime,
18-
'collaborators': this.workspace.userCount(),
19+
'collaborators': this.workspace.userCount()
1920
});
2021
};
2122

0 commit comments

Comments
 (0)