Skip to content

Commit

Permalink
use TemporaryTotpKeyValue type
Browse files Browse the repository at this point in the history
  • Loading branch information
narumincho committed Jan 22, 2024
1 parent 6327acf commit f9b0fe1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
3 changes: 2 additions & 1 deletion server/kv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AccountCode } from "./type/accountCode.ts";
import { AccountDisplayName } from "./type/accountDisplayName.ts";
import { TotpKeyId } from "./type/id.ts";
import { TotpSecret } from "./type/totpSecret.ts";

export type Idea = {
readonly title: string;
Expand Down Expand Up @@ -34,7 +35,7 @@ export const temporaryTotpKeyKey = (
totpKeyId: TotpKeyId,
) => ["temporaryTotpKey", totpKeyId];

export type temporaryTotpKeyValue = {};
export type TemporaryTotpKeyValue = TotpSecret;

export const cacheAccountByCodeKey = (
accountCode: AccountCode,
Expand Down
9 changes: 5 additions & 4 deletions server/mutation/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { AccountDisplayName } from "../type/accountDisplayName.ts";
import { getAccountByCodeResolve } from "../query/accountByCode.ts";
import { TotpKeyId } from "../type/id.ts";
import { CreateAccountResult } from "../type/createAccountResult.ts";
import { TotpSecret } from "../type/totpSecret.ts";
import { TotpCode } from "../type/totpCode.ts";
import { TOTP } from "https://deno.land/x/[email protected]/totp.ts";
import {
Account,
cacheAccountByCodeKey,
entityKey,
temporaryTotpKeyKey,
TemporaryTotpKeyValue,
} from "../kv.ts";

export const createAccount: g.GraphQLFieldConfig<
Expand Down Expand Up @@ -56,9 +56,10 @@ export const createAccount: g.GraphQLFieldConfig<
accountCode: args.accountCode,
};
}
const totpKey =
(await denoKv.get<TotpSecret>(["temporaryTotpKey", args.totpKeyId]))
.value;
const totpKey = (await denoKv.get<TemporaryTotpKeyValue>(
temporaryTotpKeyKey(args.totpKeyId),
))
.value;
if (totpKey === null) {
return {
__typename: "CreateAccountNotFoundTotpKeyId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TOTP } from "https://deno.land/x/[email protected]/mod.ts";
import { TotpKeyAndId } from "../type/totpKeyAndId.ts";
import { totpKeyIdIdFrom } from "../type/id.ts";
import { totpSecretFrom } from "../type/totpSecret.ts";
import { temporaryTotpKeyKey, TemporaryTotpKeyValue } from "../kv.ts";

export const createTotpKey: g.GraphQLFieldConfig<
void,
Expand All @@ -17,11 +18,15 @@ export const createTotpKey: g.GraphQLFieldConfig<
await TOTP.exportKey(await TOTP.generateKey(32)),
);
const id = totpKeyIdIdFrom(crypto.randomUUID().replaceAll("-", ""));
await denoKv.set(["temporaryTotpKey", id], key, {
expireIn:
// 30min
30 * 60 * 1000,
});
await denoKv.set(
temporaryTotpKeyKey(id),
key satisfies TemporaryTotpKeyValue,
{
expireIn:
// 30min
30 * 60 * 1000,
},
);
return {
id,
secret: key,
Expand Down
2 changes: 1 addition & 1 deletion server/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as g from "npm:graphql";
import { now } from "./query/now.ts";
import { createAccount } from "./mutation/createAccount.ts";
import { accountByCode } from "./query/accountByCode.ts";
import { createTotpKey } from "./mutation/createPreAccount.ts";
import { createTotpKey } from "./mutation/createTotpKey.ts";
import { entities } from "./query/entities.ts";

const query = new g.GraphQLObjectType({
Expand Down

0 comments on commit f9b0fe1

Please sign in to comment.