Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display all authenticated apps for a user on their account page #47

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/armadietto.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@

async dispatch (req, res) {
const method = req.method.toUpperCase();
const uri = url.parse(req.url, true);

Check warning on line 142 in lib/armadietto.js

View workflow job for this annotation

GitHub Actions / node.js (14)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead

Check warning on line 142 in lib/armadietto.js

View workflow job for this annotation

GitHub Actions / node.js (16)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead

Check warning on line 142 in lib/armadietto.js

View workflow job for this annotation

GitHub Actions / node.js (18)

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead

const startBasePath = new RegExp('^/?' + this._basePath + '/?');
let match;
Expand Down Expand Up @@ -195,6 +195,12 @@
if (method === 'POST') return users.register();
}

if (uri.pathname === 'account') {
const users = new Users(this, req, res);
if (method === 'GET') return users.showLoginForm();
if (method === 'POST') return users.showAccountPage();
}

match = uri.pathname.match(/^storage\/([^/]+)(.*)$/);
if (match) {
const username = decodeURIComponent(match[1]).split('@')[0];
Expand Down
13 changes: 13 additions & 0 deletions lib/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -785,3 +785,16 @@ button[name="deny"]:focus {
#switch.light::before {
content: url('sprite.svg#icon-moon');
}
.deemphasize {
color: #a6a6a6;
font-size: 0.8em;
}
.favicon {
width: 32px;
height: 32px;
vertical-align: middle;
}

.account-info td {
font-size: 0.8em;
}
56 changes: 56 additions & 0 deletions lib/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,62 @@
});
}
}

async showLoginForm () {
if (this.redirectToSSL()) return;
this.renderHTML(200, 'login.html', { params: this.params, error: null });
}

async showAccountPage () {
if (this.blockUnsecureRequest()) return;

const expandedPermissions = {
'r': 'Read',

Check failure on line 40 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (14)

Unnecessarily quoted property 'r' found

Check failure on line 40 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (16)

Unnecessarily quoted property 'r' found

Check failure on line 40 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (18)

Unnecessarily quoted property 'r' found
'w': 'Write'

Check failure on line 41 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (14)

Unnecessarily quoted property 'w' found

Check failure on line 41 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (16)

Unnecessarily quoted property 'w' found

Check failure on line 41 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (18)

Unnecessarily quoted property 'w' found
};

try {
await this.server._store.authenticate(this.params);
const authData = await this.server._store.readAuth(this.params.username);
// this is a bit of a monster but it formats the somewhat unwieldy auth.json
// for a user into something that looks like:
// {
// "params": {"username": string},
// "host": string,
// "sessions: [
// "clientId": string, <- the url for the app as per the spec
// "permissions": [
// {
// "folder": string,
// "permissions": ["Read", "Write"] <- the permission array may contain one/both
// }
// ]
// ]
// }
//
// We're doing this transform just to make it easier on the view side to
// iterate over things.
this.renderHTML(200, 'account.html', {
params: { username: this.params.username },
host: this.getHost(),
sessions: authData.sessions ? Object.keys(authData.sessions).map(k => {

Check failure on line 68 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (14)

Expected newline between test and consequent of ternary expression

Check failure on line 68 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (14)

Expected newline between consequent and alternate of ternary expression

Check failure on line 68 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (16)

Expected newline between test and consequent of ternary expression

Check failure on line 68 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (16)

Expected newline between consequent and alternate of ternary expression

Check failure on line 68 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (18)

Expected newline between test and consequent of ternary expression

Check failure on line 68 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (18)

Expected newline between consequent and alternate of ternary expression
return {
clientId: authData.sessions[k].clientId,
permissions: Object.keys(authData.sessions[k].permissions).map(folder => {
return {
folder: folder,

Check warning on line 73 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (14)

Expected property shorthand

Check warning on line 73 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (16)

Expected property shorthand

Check warning on line 73 in lib/controllers/users.js

View workflow job for this annotation

GitHub Actions / node.js (18)

Expected property shorthand
permissions: Object.keys(authData.sessions[k].permissions[folder]).filter(perm => {
return authData.sessions[k].permissions[folder][perm];
}).map(v => expandedPermissions[v])
};
})
};
}) : []
});
} catch (error) {
this.renderHTML(409, 'login.html', { params: this.params, error });
}
}
}

module.exports = Users;
29 changes: 29 additions & 0 deletions lib/views/account.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<h2>Account Info</h2>

<p>Your storage account: <%= params.username %>@<%= host %></p>

<table class="account-info">
<thead>
<tr>
<th>Application</th>
<th>Permissions</th>
</tr>
</thead>
<tbody>
<% sessions.forEach(function(session) { %>
<tr>
<td>
<img src="<%= session.clientId %>/favicon.ico" alt="Favicon for <%= session.clientId %>" class="favicon">
<a href="<%= session.clientId %>" target="_blank"><%= session.clientId %></a>
</td>
<td>
<% session.permissions.forEach(folder => { %>
<%= folder.folder %> <span class="deemphasize">
(<%= folder.permissions.join(', ') %>)
</span>
<% }); %>
</td>
</tr>
<% }); %>
</tbody>
</table>
6 changes: 6 additions & 0 deletions lib/views/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ <h1>

<nav role="navigation">
<ul>
<li>
<a href="<%= basePath %>/">Home</a>
</li>
<li>
<a href="<%= basePath %>/account">Account</a>
</li>
<% if (signup) { %>
<li class="login">
<a class="signup" href="<%= basePath %>/signup">Sign up</a>
Expand Down
19 changes: 19 additions & 0 deletions lib/views/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h2>Log In</h2>

<form method="post" action="<%= basePath %>/account">
<% if (error) { %>
<p class="error"><%= error.message %></p>
<% } %>

<table>
<tr>
<th scope="row"><label for="username">Username</label></th>
<td><input type="text" id="username" name="username" value="<%= params.username || '' %>"></td>
</tr>
<tr>
<th scope="row"><label for="password">Password</label></th>
<td><input type="password" id="password" name="password" value=""></td>
</tr>
</table>
<p><input type="submit" value="Go"></p>
</form>
Loading