-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5564023
commit fd50ace
Showing
13 changed files
with
342 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,22 @@ import { accountIdFrom } from "../type/id.ts"; | |
import { AccountCode } from "../type/accountCode.ts"; | ||
import { AccountDisplayName } from "../type/accountDisplayName.ts"; | ||
import { getAccountByCodeResolve } from "../query/accountByCode.ts"; | ||
import {TemporaryKeyId} from "../type/id.ts"; | ||
import {TotpKeyId} from "../type/id.ts"; | ||
import { CreateAccountDuplicateCode, CreateAccountNotFoundTotpKeyId } from "../type/createAccountResult.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"; | ||
|
||
export const createAccount: g.GraphQLFieldConfig< | ||
void, | ||
Context, | ||
{ readonly code: AccountCode; readonly displayName: AccountDisplayName } | ||
{ readonly totpKeyId: TotpKeyId, readonly totpCode: TotpCode, readonly accountCode: AccountCode; readonly displayName: AccountDisplayName } | ||
> = { | ||
args: { | ||
keyId: { | ||
type: new g.GraphQLNonNull(TemporaryKeyId) | ||
totpKeyId: { | ||
type: new g.GraphQLNonNull(TotpKeyId), | ||
description: "TOTPのキーID", | ||
}, | ||
code: { | ||
type: new g.GraphQLNonNull(AccountCode), | ||
|
@@ -27,27 +33,50 @@ export const createAccount: g.GraphQLFieldConfig< | |
}, | ||
}, | ||
type: new g.GraphQLNonNull(Account), | ||
resolve: async (_, args, { denoKv }): Promise<Account> => { | ||
const existingAccountId = getAccountByCodeResolve({ | ||
code: args.code, | ||
resolve: async (_, args, { denoKv }): Promise<CreateAccountResult> => { | ||
const existingAccountId = await getAccountByCodeResolve({ | ||
accountCode: args.accountCode, | ||
denoKv, | ||
}); | ||
if(existingAccountId === null) { | ||
return { | ||
__typename: "CreateAccountDuplicateCode", | ||
accountCode: args.accountCode, | ||
}; | ||
} | ||
const totpKey = (await denoKv.get<TotpSecret>(["temporaryTotpKey", args.totpKeyId])).value; | ||
if(totpKey=== null) { | ||
return { | ||
__typename: "CreateAccountNotFoundTotpKeyId", | ||
keyId: args.totpKeyId, | ||
|
||
}; | ||
} | ||
const isValidTotpCode = await TOTP.verifyTOTP(await TOTP.importKey(totpKey), args.totpCode); | ||
if(!isValidTotpCode) { | ||
return { | ||
__typename: "CreateAccountInvalidCode", | ||
accountCode: args.accountCode, | ||
}; | ||
} | ||
const displayName = AccountDisplayName.parseValue( | ||
args.displayName || args.code, | ||
args.displayName || args.accountCode, | ||
); | ||
const accountId = accountIdFrom(createRandomId()); | ||
const createDateTime = new Date(); | ||
await denoKv.set(["account", accountId], { | ||
code: args.code, | ||
await denoKv.atomic().delete(["temporaryTotpKey", args.totpKeyId]).set(["account", accountId], { | ||
code: args.accountCode, | ||
displayName, | ||
createDateTime, | ||
}); | ||
await denoKv.set(["cache", "accountByCode", args.code], accountId); | ||
}).set(["cache", "accountByCode", args.accountCode], accountId).commit(); | ||
return { | ||
id: accountId, | ||
code: args.code, | ||
displayName: displayName, | ||
createDateTime, | ||
__typename: "CreateAccountResultOk", | ||
account: { | ||
id: accountId, | ||
code: args.accountCode, | ||
displayName: displayName, | ||
createDateTime, | ||
} | ||
}; | ||
}, | ||
description: "アカウントを作成する", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,26 +2,27 @@ import { createRandomId } from "npm:@narumincho/[email protected] | |
import * as g from "npm:graphql"; | ||
import { Context } from "../context.ts"; | ||
import { TOTP } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { TemporaryKey } from "../type/tempKey.ts"; | ||
import { temporaryKeyIdFrom } from "../type/id.ts"; | ||
import { TotpKeyAndId } from "../type/totpKeyAndId.ts"; | ||
import { totpKeyIdIdFrom } from "../type/id.ts"; | ||
import { totpSecretFrom } from "../type/totpSecret.ts"; | ||
|
||
export const createPreAccount: g.GraphQLFieldConfig< | ||
export const createTotpKey: g.GraphQLFieldConfig< | ||
void, | ||
Context, | ||
{} | ||
> = { | ||
args: {}, | ||
type: new g.GraphQLNonNull(g.GraphQLString), | ||
resolve: async (_, __, { denoKv }): Promise<TemporaryKey> => { | ||
const key = await TOTP.exportKey(await TOTP.generateKey(32)); | ||
const id = temporaryKeyIdFrom(createRandomId()); | ||
await denoKv.set(["temporaryKey", id], key, { expireIn: | ||
type: new g.GraphQLNonNull(TotpKeyAndId), | ||
resolve: async (_, __, { denoKv }): Promise<TotpKeyAndId> => { | ||
const key = totpSecretFrom(await TOTP.exportKey(await TOTP.generateKey(32))); | ||
const id = totpKeyIdIdFrom(createRandomId()); | ||
await denoKv.set(["temporaryTotpKey", id], key, { expireIn: | ||
// 30min | ||
30 * 60 * 1000, }); | ||
return { | ||
id, | ||
secret: key, | ||
}; | ||
}, | ||
description: "TOTPのキーを生成して", | ||
description: "TOTPのキーを生成してデータベースに保存する", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.