Skip to content

Commit d44b10e

Browse files
authored
Merge pull request #1 from uwcirg/ltt-updates
Ltt updates
2 parents 5484c04 + a7a08eb commit d44b10e

File tree

6 files changed

+103
-15
lines changed

6 files changed

+103
-15
lines changed

server/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
FROM denoland/deno:1.25.2
22
EXPOSE 8000
33
WORKDIR /app
4+
USER root
5+
RUN apt-get update && \
6+
apt-get install -y sqlite3
47
USER deno
58
COPY --chown=deno deps.ts .
69
RUN deno cache deps.ts

server/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const defaultEnv = {
2-
// PUBLIC_URL: 'http://localhost:8000',
3-
PUBLIC_URL: 'https://smart-health-links-server.cirg.washington.edu',
2+
PUBLIC_URL: 'https://shl-server.inform.dev.cirg.uw.edu',
43
EMBEDDED_LENGTH_MAX: 10_000
54
};
65

server/db.ts

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,42 @@ async function updateAccessToken(endpoint: types.HealthLinkEndpoint) {
3838

3939
export const DbLinks = {
4040
create(config: types.HealthLinkConfig) {
41+
let { userId, sessionId, ...configSansUserAndSession } = config;
42+
4143
const link = {
42-
config,
44+
config: configSansUserAndSession,
4345
id: randomStringWithEntropy(32),
46+
userId: userId,
47+
sessionId: sessionId,
4448
managementToken: randomStringWithEntropy(32),
49+
created: new Date().getTime() / 1000,
4550
active: true,
4651
};
4752
db.query(
48-
`INSERT INTO shlink (id, management_token, active, config_exp, config_passcode)
49-
values (:id, :managementToken, :active, :exp, :passcode)`,
53+
`INSERT INTO shlink (id, user_id, session_id, management_token, active, created, config_exp, config_passcode)
54+
values (:id, :userId, :sessionId, :managementToken, :active, :created, :exp, :passcode)`,
5055
{
5156
id: link.id,
57+
userId: link.userId,
58+
sessionId: link.sessionId,
5259
managementToken: link.managementToken,
5360
active: link.active,
61+
created: link.created,
5462
exp: link.config.exp,
5563
passcode: link.config.passcode,
56-
},
64+
}
5765
);
5866

5967
return link;
6068
},
61-
updateConfig(shl: types.HealthLink) {
62-
db.query(`UPDATE shlink set config_passcode=:passcode, config_exp=:exp where id=:id`,
69+
updateConfig(linkId:string, config: types.HealthLinkConfig) {
70+
db.query(`UPDATE shlink set config_passcode=:passcode, config_exp=:exp, session_id=:sessionId where id=:id`,
6371
{
64-
id: shl.id,
65-
exp: shl.config.exp,
66-
passcode: shl.config.passcode
67-
})
72+
id: linkId,
73+
exp: config.exp,
74+
passcode: config.passcode,
75+
sessionId: config.sessionId
76+
});
6877
return true;
6978
},
7079
deactivate(shl: types.HealthLink) {
@@ -83,19 +92,48 @@ export const DbLinks = {
8392
id: linkRow.id as string,
8493
passcodeFailuresRemaining: linkRow.passcode_failures_remaining as number,
8594
active: Boolean(linkRow.active) as boolean,
95+
userId: linkRow.user_id as string,
96+
sessionId: linkRow.session_id as string,
97+
created: linkRow.created as string,
8698
managementToken: linkRow.management_token as string,
8799
config: {
88100
exp: linkRow.config_exp as number,
89101
passcode: linkRow.config_passcode as string,
90102
},
91103
};
92104
},
105+
getUserShl(userId: string): types.HealthLink | undefined {
106+
try {
107+
const linkRow = db
108+
.prepareQuery(`SELECT * from shlink where user_id=? and active=1 order by created desc limit 1`)
109+
.oneEntry([userId]);
110+
return {
111+
id: linkRow.id as string,
112+
passcodeFailuresRemaining: linkRow.passcode_failures_remaining as number,
113+
active: Boolean(linkRow.active) as boolean,
114+
userId: linkRow.user_id as string,
115+
sessionId: linkRow.session_id as string,
116+
created: linkRow.created as string,
117+
managementToken: linkRow.management_token as string,
118+
config: {
119+
exp: linkRow.config_exp as number,
120+
passcode: linkRow.config_passcode as string,
121+
},
122+
};
123+
} catch (e) {
124+
console.warn(e);
125+
return undefined;
126+
}
127+
},
93128
getShlInternal(linkId: string): types.HealthLink {
94129
const linkRow = db.prepareQuery(`SELECT * from shlink where id=?`).oneEntry([linkId]);
95130
return {
96131
id: linkRow.id as string,
97132
passcodeFailuresRemaining: linkRow.passcode_failures_remaining as number,
98133
active: Boolean(linkRow.active) as boolean,
134+
userId: linkRow.user_id as string,
135+
sessionId: linkRow.session_id as string,
136+
created: linkRow.created as string,
99137
managementToken: linkRow.management_token as string,
100138
config: {
101139
exp: linkRow.config_exp as number,
@@ -142,6 +180,17 @@ export const DbLinks = {
142180

143181
return true;
144182
},
183+
async deleteAllFiles(linkId: string) {
184+
185+
db.query(
186+
`delete from shlink_file where shlink = :linkId`,
187+
{
188+
linkId
189+
}
190+
);
191+
192+
return true;
193+
},
145194
async addEndpoint(linkId: string, endpoint: types.HealthLinkEndpoint): Promise<string> {
146195
const id = randomStringWithEntropy(32);
147196

server/routers/api.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,26 @@ export const shlApiRouter = new oak.Router()
8383
})
8484
.put('/shl/:shlId', async (context) => {
8585
const managementToken = await context.request.headers.get('authorization')?.split(/bearer /i)[1]!;
86+
const config = await context.request.body({ type: 'json' }).value;
8687
const shl = db.DbLinks.getManagedShl(context.params.shlId, managementToken)!;
8788
if (!shl) {
8889
throw new Error(`Can't manage SHLink ` + context.params.shlId);
8990
}
90-
const updated = db.DbLinks.updateConfig(shl);
91-
context.response.body = updated;
91+
const updated = db.DbLinks.updateConfig(context.params.shlId, config);
92+
if (!updated) {
93+
return (context.response.status = 500);
94+
}
95+
const updatedShl = db.DbLinks.getManagedShl(context.params.shlId, managementToken);
96+
delete updatedShl.managementToken
97+
context.response.body = updatedShl;
98+
})
99+
.get('/user/:userId', async (context) => {
100+
const shl = db.DbLinks.getUserShl(context.params.userId)!;
101+
if (!shl) {
102+
console.log(`Can't find SHLink for user ` + context.params.userId);
103+
return;
104+
}
105+
context.response.body = shl;
92106
})
93107
.get('/shl/:shlId/file/:fileIndex', (context) => {
94108
const ticket = manifestAccessTickets.get(context.request.url.searchParams.get('ticket')!);
@@ -152,6 +166,21 @@ export const shlApiRouter = new oak.Router()
152166
added,
153167
};
154168
})
169+
.delete('/shl/:shlId/file/all', async (context) => {
170+
const managementToken = await context.request.headers.get('authorization')?.split(/bearer /i)[1]!;
171+
const currentFileBody = await context.request.body({type: 'bytes'});
172+
173+
const shl = db.DbLinks.getManagedShl(context.params.shlId, managementToken);
174+
if (!shl) {
175+
throw new Error(`Can't manage SHLink ` + context.params.shlId);
176+
}
177+
178+
const deleted = db.DbLinks.deleteAllFiles(shl.id);
179+
context.response.body = {
180+
...shl,
181+
deleted,
182+
}
183+
})
155184
.delete('/shl/:shlId/file', async (context) => {
156185
const managementToken = await context.request.headers.get('authorization')?.split(/bearer /i)[1]!;
157186
const currentFileBody = await context.request.body({type: 'bytes'});

server/schema.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ CREATE TABLE IF NOT EXISTS cas_item(
66

77
CREATE TABLE IF NOT EXISTS shlink(
88
id VARCHAR(43) PRIMARY KEY UNIQUE,
9+
user_id VARCHAR(43) NOT NULL,
10+
session_id VARCHAR(43) NOT NULL,
911
passcode_failures_remaining INTEGER DEFAULT(5),
1012
config_passcode TEXT,
1113
config_exp DATETIME,
14+
created DATETIME,
1215
active BOOLEAN NOT NULL DEFAULT(true),
1316
management_token VARCHAR(43) NOT NULL
1417
);

server/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ export interface HealthLinkEndpoint {
2424
export interface HealthLinkConfig {
2525
passcode?: string;
2626
exp?: number;
27+
userId?: string;
28+
sessionId?: string;
2729
}
2830

2931
export interface HealthLink {
3032
config: HealthLinkConfig;
3133
active: boolean;
3234
id: string;
33-
managementToken: string;
35+
userId?: string;
36+
sessionId?: string;
37+
created: string;
38+
managementToken?: string;
3439
passcodeFailuresRemaining: number;
3540
}
3641

0 commit comments

Comments
 (0)