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

Commit 23c2c4f

Browse files
author
Jacob Peddicord
committed
Consistently use extractRequestUser
Some locations used req.user.user, which was a holdover from a different auth system that just happened to still work with NullAuth.
1 parent b685998 commit 23c2c4f

File tree

7 files changed

+36
-22
lines changed

7 files changed

+36
-22
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/api/packages/auth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { config } from '../../config';
2020
import { AccessError } from '../../errors';
2121

2222
export async function canValidate(req) {
23-
const groups = await auth.getGroups(req.user.user);
23+
const user = auth.extractRequestUser(req);
24+
const groups = await auth.getGroups(user);
2425

2526
if (isUserInAnyGroup(groups, config.admin.verifiers)) {
2627
return true;
@@ -30,7 +31,7 @@ export async function canValidate(req) {
3031
return true;
3132
}
3233

33-
winston.warn('User %s cannot validate package metadata', req.user.user);
34+
winston.warn('User %s cannot validate package metadata', user);
3435
return false;
3536
}
3637

server/api/packages/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import * as Immutable from 'immutable';
1616
import * as winston from 'winston';
1717

18+
import auth from '../../auth';
1819
import * as db from '../../db/packages';
1920
import { assertCanValidate } from './auth';
2021
import { WebPackage } from './interfaces';
@@ -111,7 +112,7 @@ export async function storePackage(req: any, packageId: number, info: Pick<WebPa
111112
// create a new revision if anything changed (or it didn't exist)
112113
let newId: number;
113114
if (shouldInsert) {
114-
const createdBy = req.user.user;
115+
const createdBy = auth.extractRequestUser(req);
115116
newId = await db.createPackageRevision(info.name, info.version, info.website,
116117
info.license, info.copyright, info.licenseText, createdBy);
117118
winston.info('Created a new package revision with ID %s (previous revision at %s) by %s',
@@ -126,12 +127,13 @@ export async function storePackage(req: any, packageId: number, info: Pick<WebPa
126127

127128
export async function verifyPackage(req: any, packageId: number, verified: boolean,
128129
comments: string): Promise<Partial<WebPackage>> {
130+
const user = auth.extractRequestUser(req);
129131
assertCanValidate(req);
130132
await Promise.all([
131-
db.addVerification(packageId, req.user.user, comments),
133+
db.addVerification(packageId, user, comments),
132134
db.verifyPackage(packageId, verified),
133135
]);
134-
winston.info('Package %s verified (%s) by %s', packageId, verified, req.user.user);
136+
winston.info('Package %s verified (%s) by %s', packageId, verified, user);
135137
return {packageId};
136138
}
137139

server/api/projects/auth.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('projects auth', function () {
5050
mock = {
5151
auth: {
5252
getGroups: jasmine.createSpy('getGroups').and.returnValue(Promise.resolve(['a-nobody'])),
53+
extractRequestUser: (req) => req.user.user,
5354
},
5455
config: {
5556
admin: {

server/api/projects/auth.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import { AccessLevel, AccessLevelStrength } from './interfaces';
2222
* Check if the request's user is the project's contact list.
2323
*/
2424
export function isInContacts(req: any, project: Pick<DbProject, 'contacts'>) {
25+
const user = auth.extractRequestUser(req);
2526
for (const type of Object.keys(project.contacts)) {
2627
const contactList = project.contacts[type];
27-
if (contactList.includes(req.user.user)) {
28+
if (contactList.includes(user)) {
2829
return true;
2930
}
3031
}
@@ -50,7 +51,8 @@ export async function assertProjectAccess(req: any, project: ProjectAccess, leve
5051
}
5152

5253
export async function effectivePermission(req: any, project: ProjectAccess): Promise<AccessLevel> {
53-
const reqGroups = await auth.getGroups(req.user.user);
54+
const user = auth.extractRequestUser(req);
55+
const reqGroups = await auth.getGroups(user);
5456

5557
// start by checking the global list
5658
// TODO: make global list ACL-like too

server/api/projects/index.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ describe('projects', function () {
2323
mockery.enable({useCleanCache: true, warnOnUnregistered: false});
2424
mock = {
2525
db: {},
26-
auth: {},
26+
auth: {
27+
extractRequestUser: (req) => req.user.user,
28+
},
2729
packagedb: {},
2830
assertProjectAccess: jasmine.createSpy('assertProjectAccess'),
2931
effectivePermission: jasmine.createSpy('effectivePermission'),

server/api/projects/index.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ export async function getProject(req: Request, projectId: string): Promise<WebPr
5959
}
6060

6161
export async function searchProjects(req: Request): Promise<Array<Partial<WebProject>>> {
62-
const groups = await auth.getGroups(req.user.user);
62+
const user = auth.extractRequestUser(req);
63+
const groups = await auth.getGroups(user);
6364

6465
// all projects
6566
if (req.query.all) {
@@ -85,6 +86,7 @@ function mapProjectShortInfo(dbData): Partial<WebProject> {
8586
}
8687

8788
export async function createProject(req: Request, body: WebProject): ProjectIdPromise {
89+
const user = auth.extractRequestUser(req);
8890
const projectId = await db.createProject({
8991
title: body.title,
9092
version: body.version,
@@ -93,13 +95,14 @@ export async function createProject(req: Request, body: WebProject): ProjectIdPr
9395
contacts: body.contacts,
9496
acl: body.acl,
9597
metadata: body.metadata,
96-
}, req.user.user);
98+
}, user);
9799

98-
winston.info('Project %s created by %s', projectId, req.user.user);
100+
winston.info('Project %s created by %s', projectId, user);
99101
return {projectId};
100102
}
101103

102104
export async function patchProject(req: Request, projectId, changes): ProjectIdPromise {
105+
const user = auth.extractRequestUser(req);
103106
const project = await db.getProject(projectId);
104107
await assertProjectAccess(req, project, 'editor');
105108

@@ -122,15 +125,16 @@ export async function patchProject(req: Request, projectId, changes): ProjectIdP
122125
mappedChanges[internalMap[k]] = changes[k];
123126
}
124127

125-
await db.patchProject(projectId, mappedChanges, req.user.user);
128+
await db.patchProject(projectId, mappedChanges, user);
126129

127-
winston.info('Project %s modified by %s', projectId, req.user.user);
130+
winston.info('Project %s modified by %s', projectId, user);
128131
return {projectId};
129132
}
130133

131134
export async function attachPackage(req: Request, projectId, info) {
132135
const { packageId, name, version, website, copyright, usage } = info;
133136
const { license, licenseText } = info;
137+
const user = auth.extractRequestUser(req);
134138

135139
// access check
136140
const project = await db.getProject(projectId);
@@ -151,7 +155,7 @@ export async function attachPackage(req: Request, projectId, info) {
151155
await db.updatePackagesUsed(projectId, [
152156
...project.packages_used,
153157
usageInfo,
154-
], req.user.user);
158+
], user);
155159

156160
// finally, return the updated/inserted package ID
157161
const addedPackageId = usageInfo.package_id;
@@ -160,19 +164,21 @@ export async function attachPackage(req: Request, projectId, info) {
160164
}
161165

162166
export async function detachPackage(req: Request, projectId, packageId): ProjectIdPromise {
167+
const user = auth.extractRequestUser(req);
163168
const project = await db.getProject(projectId);
164169
await assertProjectAccess(req, project, 'editor');
165170

166171
const newUsage = project.packages_used.filter((item) => {
167172
return item.package_id !== packageId;
168173
});
169174

170-
await db.updatePackagesUsed(projectId, newUsage, req.user.user);
175+
await db.updatePackagesUsed(projectId, newUsage, user);
171176
winston.info('Detached package %s from project %s', packageId, projectId);
172177
return {projectId};
173178
}
174179

175180
export async function replacePackage(req: Request, projectId: string, oldId: number, newId: number): ProjectIdPromise {
181+
const user = auth.extractRequestUser(req);
176182
const project = await db.getProject(projectId);
177183
await assertProjectAccess(req, project, 'editor');
178184

@@ -183,12 +189,13 @@ export async function replacePackage(req: Request, projectId: string, oldId: num
183189
}
184190
}
185191

186-
await db.updatePackagesUsed(projectId, usage, req.user.user);
192+
await db.updatePackagesUsed(projectId, usage, user);
187193
winston.info('Replaced package %s -> %s on project %s', oldId, newId, projectId);
188194
return {projectId};
189195
}
190196

191197
export async function generateAttributionDocument(req: Request, projectId: string, store: boolean = false) {
198+
const user = auth.extractRequestUser(req);
192199
const project = await db.getProject(projectId);
193200
await assertProjectAccess(req, project, 'viewer');
194201

@@ -216,9 +223,8 @@ export async function generateAttributionDocument(req: Request, projectId: strin
216223

217224
// save a copy if requested
218225
if (store) {
219-
const createdBy = req.user.user;
220-
documentdb.storeAttributionDocument(projectId, project.version, text, createdBy);
221-
winston.info(`Document for project ${projectId} was stored by ${createdBy}`);
226+
documentdb.storeAttributionDocument(projectId, project.version, text, user);
227+
winston.info(`Document for project ${projectId} was stored by ${user}`);
222228
return {text};
223229
}
224230

0 commit comments

Comments
 (0)