-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
164 lines (134 loc) · 4.45 KB
/
auth.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import Roles from 'koa-roles';
import { isString } from 'lodash';
import { ORCID as orcidUtils } from 'orcid-utils';
import config from '../config.ts';
import { getLogger } from '../log.js';
const log = getLogger('backend:middleware:auth');
/**
* Installs authorization middleware into the koa app.
*
* @param {Object} ctx - the koa context object
* @param {funtion} next - continue to next middleware
*/
const authWrapper = (users, groups, communities, personas) => {
const roles = new Roles();
roles.getUser = async ctx => {
log.debug('Retrieving current user.');
if (ctx.isAuthenticated() && ctx.state.user) {
log.debug('Returning logged in user.');
return ctx.state.user;
}
try {
const app = ctx.headers['x-api-app'];
const key = ctx.headers['x-api-key'];
if (app && key) {
return users.findOneByKey(app, key);
}
} catch (error) {
log.debug('No matching API key found:', error);
}
log.debug('No authenticated user or valid API key found.');
return;
};
roles.isMemberOf = async (group, id) => {
log.debug(`Checking if ${id} is a member of ${group}.`);
if (
config.adminUsers &&
group === 'admins' &&
isString(id) &&
orcidUtils.isValid(id)
) {
return (
config.adminUsers.includes(id) ||
(await groups.isMemberOf('admins', id))
);
} else {
return groups.isMemberOf(group, id);
}
};
roles.isMemberOfCommunity = async (community, id) => {
log.debug(`Checking if ${id} is a member of ${community}.`);
return communities.isMemberOf(community, id);
};
roles.isOwnerOfCommunity = async (community, id) => {
log.debug(`Checking if ${id} is an owner of ${community}.`);
return communities.isOwnerOf(community, id);
};
roles.isIdentityOf = async (persona, id) => {
log.debug(`Checking if ${id} is the identity of ${persona}.`);
return personas.isIdentityOf(persona, id);
};
roles.use('access private pages', async ctx => {
log.debug('Checking if user is authenticated...');
const user = await roles.getUser(ctx);
log.debug('User is authenticated:', !!user);
return !!user;
});
roles.use('access moderator pages', async ctx => {
log.debug('Checking if user can access moderator pages.');
const user = await roles.getUser(ctx);
if (!user) return false;
return (
(await roles.isMemberOf('moderators', user.orcid)) ||
(await roles.isMemberOf('admins', user.orcid))
);
});
roles.use('access admin pages', async ctx => {
log.debug('Checking if user can access admin pages.');
const user = await roles.getUser(ctx);
if (!user) return false;
return roles.isMemberOf('admins', user.orcid);
});
roles.use('access this community', async ctx => {
log.debug('Checking if user can edit this community.');
const user = await roles.getUser(ctx);
if (!user) return false;
const isAdmin = await roles.isMemberOf('admins', user.orcid);
if (isAdmin) return true;
if (ctx.state.community) {
return roles.isMemberOfCommunity(ctx.state.community, user.orcid);
} else {
return false;
}
});
roles.use('edit this community', async ctx => {
log.debug('Checking if user can edit this community.');
const user = await roles.getUser(ctx);
if (!user) return false;
const isAdmin = await roles.isMemberOf('admins', user.orcid);
if (isAdmin) return true;
if (ctx.state.community) {
return roles.isOwnerOfCommunity(ctx.state.community, user.orcid);
} else {
return false;
}
});
roles.use('edit this persona', async ctx => {
log.debug('Checking if user can edit this persona.');
const user = await roles.getUser(ctx);
if (!user) return false;
const isAdmin = await roles.isMemberOf('admins', user.orcid);
if (isAdmin) return true;
if (ctx.state.persona) {
return roles.isIdentityOf(ctx.state.persona, user.orcid);
} else {
return false;
}
});
roles.use('edit this user', async ctx => {
log.debug('Checking if user can edit this user.');
const user = await roles.getUser(ctx);
if (!user) return false;
const isAdmin = await roles.isMemberOf('admins', user.orcid);
if (isAdmin) return true;
if (ctx.state.identity) {
return (
user.uuid === ctx.state.identity || user.orcid === ctx.state.identity
);
} else {
return false;
}
});
return roles;
};
export default authWrapper;