-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement CreateUserPoolCommand and CreateUserPoolClientCommand
- Loading branch information
Showing
14 changed files
with
203 additions
and
33 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
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 |
---|---|---|
@@ -1,15 +1,25 @@ | ||
import type { EntityId } from 'common/types/brandedId'; | ||
import type { UserPoolClientEntity, UserPoolEntity } from 'common/types/userPool'; | ||
import { randomUUID } from 'crypto'; | ||
import { brandedId } from 'service/brandedId'; | ||
import { createShortHash } from 'service/createShortHash'; | ||
import { REGION } from 'service/envValues'; | ||
import { genPrivatekey } from 'service/privateKey'; | ||
|
||
export const userPoolMethod = { | ||
create: (val: { id: EntityId['userPool'] }): UserPoolEntity => ({ | ||
id: val.id, | ||
create: (val: { id?: EntityId['userPool']; name: string }): UserPoolEntity => ({ | ||
id: brandedId.userPool.entity.parse(`${REGION}_${createShortHash(randomUUID())}`), | ||
...val, | ||
privateKey: genPrivatekey(), | ||
createdTime: Date.now(), | ||
}), | ||
createClient: (val: { | ||
id: EntityId['userPoolClient']; | ||
id?: EntityId['userPoolClient']; | ||
name: string; | ||
userPoolId: EntityId['userPool']; | ||
}): UserPoolClientEntity => ({ id: val.id, userPoolId: val.userPoolId, createdTime: Date.now() }), | ||
}): UserPoolClientEntity => ({ | ||
id: brandedId.userPoolClient.entity.parse(randomUUID().replace(/-/g, '')), | ||
...val, | ||
createdTime: Date.now(), | ||
}), | ||
}; |
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 |
---|---|---|
@@ -1,19 +1,26 @@ | ||
import type { UserPool, UserPoolClient } from '@prisma/client'; | ||
import assert from 'assert'; | ||
import type { UserPoolClientEntity, UserPoolEntity } from 'common/types/userPool'; | ||
import { brandedId } from 'service/brandedId'; | ||
|
||
export const toUserPoolEntity = (prismaPool: UserPool): UserPoolEntity => { | ||
assert(prismaPool.name); | ||
|
||
return { | ||
id: brandedId.userPool.entity.parse(prismaPool.id), | ||
name: prismaPool.name, | ||
privateKey: prismaPool.privateKey, | ||
createdTime: prismaPool.createdAt.getTime(), | ||
}; | ||
}; | ||
|
||
export const toUserPoolClientEntity = (prismaPoolClient: UserPoolClient): UserPoolClientEntity => { | ||
assert(prismaPoolClient.name); | ||
|
||
return { | ||
id: brandedId.userPoolClient.entity.parse(prismaPoolClient.id), | ||
userPoolId: brandedId.userPool.entity.parse(prismaPoolClient.userPoolId), | ||
name: prismaPoolClient.name, | ||
createdTime: prismaPoolClient.createdAt.getTime(), | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,30 +1,69 @@ | ||
import type { ListUserPoolsTarget } from 'common/types/auth'; | ||
import assert from 'assert'; | ||
import type { | ||
CreateUserPoolClientTarget, | ||
CreateUserPoolTarget, | ||
ListUserPoolsTarget, | ||
} from 'common/types/auth'; | ||
import { DEFAULT_USER_POOL_CLIENT_ID, DEFAULT_USER_POOL_ID } from 'service/envValues'; | ||
import { prismaClient } from 'service/prismaClient'; | ||
import { prismaClient, transaction } from 'service/prismaClient'; | ||
import { userPoolMethod } from '../model/userPoolMethod'; | ||
import { userPoolCommand } from '../repository/userPoolCommand'; | ||
import { userPoolQuery } from '../repository/userPoolQuery'; | ||
|
||
export const userPoolUseCase = { | ||
initDefaults: async (): Promise<void> => { | ||
await userPoolQuery | ||
.findById(prismaClient, DEFAULT_USER_POOL_ID) | ||
.catch(() => userPoolCommand.save(userPoolMethod.create({ id: DEFAULT_USER_POOL_ID }))); | ||
initDefaults: (): Promise<void> => | ||
transaction(async (tx) => { | ||
await userPoolQuery | ||
.findById(prismaClient, DEFAULT_USER_POOL_ID) | ||
.catch(() => | ||
userPoolCommand.save( | ||
tx, | ||
userPoolMethod.create({ id: DEFAULT_USER_POOL_ID, name: 'defaultPool' }), | ||
), | ||
); | ||
|
||
await userPoolQuery.findClientById(prismaClient, DEFAULT_USER_POOL_CLIENT_ID).catch(() => | ||
userPoolCommand.saveClient( | ||
userPoolMethod.createClient({ | ||
id: DEFAULT_USER_POOL_CLIENT_ID, | ||
userPoolId: DEFAULT_USER_POOL_ID, | ||
}), | ||
), | ||
); | ||
}, | ||
await userPoolQuery.findClientById(prismaClient, DEFAULT_USER_POOL_CLIENT_ID).catch(() => | ||
userPoolCommand.saveClient( | ||
tx, | ||
userPoolMethod.createClient({ | ||
id: DEFAULT_USER_POOL_CLIENT_ID, | ||
userPoolId: DEFAULT_USER_POOL_ID, | ||
name: 'defaultPoolClient', | ||
}), | ||
), | ||
); | ||
}), | ||
listUserPools: async ( | ||
req: ListUserPoolsTarget['reqBody'], | ||
): Promise<ListUserPoolsTarget['resBody']> => { | ||
const pools = await userPoolQuery.listAll(prismaClient, req.MaxResults); | ||
|
||
return { UserPools: pools.map((p) => ({ Id: p.id })) }; | ||
return { UserPools: pools.map((p) => ({ Id: p.id, Name: p.name })) }; | ||
}, | ||
createUserPool: ( | ||
req: CreateUserPoolTarget['reqBody'], | ||
): Promise<CreateUserPoolTarget['resBody']> => | ||
transaction(async (tx) => { | ||
assert(req.PoolName); | ||
|
||
const pool = userPoolMethod.create({ name: req.PoolName }); | ||
await userPoolCommand.save(tx, pool); | ||
|
||
return { UserPool: { Id: pool.id, Name: pool.name } }; | ||
}), | ||
createUserPoolClient: ( | ||
req: CreateUserPoolClientTarget['reqBody'], | ||
): Promise<CreateUserPoolClientTarget['resBody']> => | ||
transaction(async (tx) => { | ||
assert(req.ClientName); | ||
assert(req.UserPoolId); | ||
|
||
const pool = await userPoolQuery.findById(tx, req.UserPoolId); | ||
const client = userPoolMethod.createClient({ name: req.ClientName, userPoolId: pool.id }); | ||
await userPoolCommand.saveClient(tx, client); | ||
|
||
return { | ||
UserPoolClient: { ClientId: client.id, UserPoolId: pool.id, ClientName: client.name }, | ||
}; | ||
}), | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- AlterTable | ||
ALTER TABLE "UserPool" ADD COLUMN "name" TEXT; | ||
|
||
-- AlterTable | ||
ALTER TABLE "UserPoolClient" ADD COLUMN "name" TEXT; |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com) | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the "Software"), | ||
// to deal in the Software without restriction, including without limitation | ||
// the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
// and/or sell copies of the Software, and to permit persons to whom the Software | ||
// is furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
const OFFSET_BASIS_32 = 2166136261; | ||
|
||
const fnv1a = (input: string): number => { | ||
let hash = OFFSET_BASIS_32; | ||
|
||
for (let i = 0; i < input.length; i++) { | ||
hash ^= input.charCodeAt(i); | ||
|
||
// 32-bit FNV prime: 2**24 + 2**8 + 0x93 = 16777619 | ||
// Using bitshift for accuracy and performance. Numbers in JS suck. | ||
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24); | ||
} | ||
|
||
return hash >>> 0; | ||
}; | ||
|
||
export const createShortHash = (input: string): string => fnv1a(input).toString(36); |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
CreateUserPoolClientCommand, | ||
CreateUserPoolCommand, | ||
} from '@aws-sdk/client-cognito-identity-provider'; | ||
import { cognitoClient } from 'service/cognito'; | ||
import { expect, test } from 'vitest'; | ||
|
||
test(CreateUserPoolCommand.name, async () => { | ||
const pool = await cognitoClient.send(new CreateUserPoolCommand({ PoolName: 'testPool' })); | ||
const client = await cognitoClient.send( | ||
new CreateUserPoolClientCommand({ ClientName: 'testClient', UserPoolId: pool.UserPool?.Id }), | ||
); | ||
|
||
expect(pool.UserPool?.Name === 'testPool').toBeTruthy(); | ||
expect(client.UserPoolClient?.ClientName === 'testClient').toBeTruthy(); | ||
}); |
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