-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #802 from MoralisWeb3/feat/auth-refactor
Feat/auth refactor
- Loading branch information
Showing
22 changed files
with
600 additions
and
178 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './verifyChallengeEvmOperation'; | ||
export * from './requestChallengeEvmOperation'; |
54 changes: 54 additions & 0 deletions
54
packages/auth/src/operations/evm/requestChallengeEvmOperation.test.ts
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,54 @@ | ||
import MoralisCore from '@moralisweb3/common-core'; | ||
import { EvmAddress, EvmChain } from '@moralisweb3/common-evm-utils'; | ||
import { requestChallengeEvmOperation, RequestChallengeEvmRequest } from './requestChallengeEvmOperation'; | ||
|
||
describe('evmRequestChallengeOperation', () => { | ||
let core: MoralisCore; | ||
|
||
beforeAll(() => { | ||
core = MoralisCore.create(); | ||
}); | ||
|
||
it('serializeRequest() serializes correctly and deserializeRequest() deserializes correctly', () => { | ||
const address = '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359'; | ||
const chain = '25'; | ||
const expirationTime = '2021-01-01T00:00:00.000Z'; | ||
const notBefore = '2020-01-01T00:00:00.000Z'; | ||
|
||
const request: Required<RequestChallengeEvmRequest> = { | ||
address: EvmAddress.create(address, core), | ||
domain: 'defi.finance', | ||
chainId: EvmChain.create(chain, core), | ||
statement: 'VALID_RESPONSE', | ||
uri: 'https://defi.finance/', | ||
expirationTime: new Date(expirationTime), | ||
notBefore: new Date(notBefore), | ||
resources: ['https://docs.moralis.io/'], | ||
timeout: 50, | ||
}; | ||
|
||
const serializedRequest = requestChallengeEvmOperation.serializeRequest(request, core); | ||
|
||
expect(serializedRequest.address).toBe(address); | ||
expect(serializedRequest.chainId).toBe(chain); | ||
expect(serializedRequest.domain).toBe(request.domain); | ||
expect(serializedRequest.statement).toBe(request.statement); | ||
expect(serializedRequest.uri).toBe(request.uri); | ||
expect(serializedRequest.expirationTime).toBe(request.expirationTime); | ||
expect(serializedRequest.notBefore).toBe(request.notBefore); | ||
expect(serializedRequest.resources).toBe(request.resources); | ||
expect(serializedRequest.timeout).toBe(request.timeout); | ||
|
||
const deserializedRequest = requestChallengeEvmOperation.deserializeRequest(serializedRequest, core); | ||
|
||
expect((deserializedRequest.address as EvmAddress).checksum).toBe(address); | ||
expect((deserializedRequest.chainId as EvmChain).decimal.toString()).toBe(chain); | ||
expect(deserializedRequest.domain).toBe(request.domain); | ||
expect(deserializedRequest.statement).toBe(request.statement); | ||
expect(deserializedRequest.uri).toBe(request.uri); | ||
expect(deserializedRequest.expirationTime).toBe(request.expirationTime); | ||
expect(deserializedRequest.notBefore).toBe(request.notBefore); | ||
expect(deserializedRequest.resources).toBe(request.resources); | ||
expect(deserializedRequest.timeout).toBe(request.timeout); | ||
}); | ||
}); |
109 changes: 109 additions & 0 deletions
109
packages/auth/src/operations/evm/requestChallengeEvmOperation.ts
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,109 @@ | ||
import { Core, Camelize, Operation, DateInput } from '@moralisweb3/common-core'; | ||
import { EvmAddress, EvmAddressish, EvmChainish, EvmChainResolver } from '@moralisweb3/common-evm-utils'; | ||
import { operations } from '../../generated/types'; | ||
|
||
type OperationId = 'requestChallengeEvm'; | ||
|
||
type BodyParams = operations[OperationId]['requestBody']['content']['application/json']; | ||
type RequestParams = BodyParams; | ||
|
||
type SuccessResponse = operations[OperationId]['responses']['201']['content']['application/json']; | ||
|
||
// Exports | ||
|
||
export interface RequestChallengeEvmRequest | ||
extends Camelize<Omit<RequestParams, 'address' | 'chainId' | 'expirationTime' | 'notBefore'>> { | ||
address: EvmAddressish; | ||
chainId: EvmChainish; | ||
expirationTime?: DateInput; | ||
notBefore?: DateInput; | ||
} | ||
|
||
export type RequestChallengeEvmJSONRequest = ReturnType<typeof serializeRequest>; | ||
|
||
export type RequestChallengeEvmJSONResponse = SuccessResponse; | ||
|
||
export type RequestChallengeEvmResponse = ReturnType<typeof deserializeResponse>; | ||
|
||
export const requestChallengeEvmOperation: Operation< | ||
RequestChallengeEvmRequest, | ||
RequestChallengeEvmJSONRequest, | ||
RequestChallengeEvmResponse, | ||
RequestChallengeEvmJSONResponse | ||
> = { | ||
method: 'POST', | ||
name: 'requestChallengeEvm', | ||
id: 'requestChallengeEvm', | ||
groupName: 'evm', | ||
urlPathPattern: '/challenge/request/evm', | ||
bodyParamNames: [ | ||
'domain', | ||
'chainId', | ||
'address', | ||
'statement', | ||
'uri', | ||
'expirationTime', | ||
'notBefore', | ||
'resources', | ||
'timeout', | ||
], | ||
bodyType: 'properties', | ||
|
||
getRequestUrlParams, | ||
getRequestBody, | ||
serializeRequest, | ||
deserializeRequest, | ||
deserializeResponse, | ||
}; | ||
|
||
// Methods | ||
|
||
function getRequestUrlParams() { | ||
return {}; | ||
} | ||
|
||
function getRequestBody(request: RequestChallengeEvmRequest, core: Core) { | ||
return { | ||
domain: request.domain, | ||
chainId: EvmChainResolver.resolve(request.chainId, core).decimal.toString(), | ||
address: EvmAddress.create(request.address, core).checksum, | ||
statement: request.statement, | ||
uri: request.uri, | ||
expirationTime: request.expirationTime, | ||
notBefore: request.notBefore, | ||
resources: request.resources, | ||
timeout: request.timeout, | ||
}; | ||
} | ||
|
||
function deserializeResponse(jsonResponse: RequestChallengeEvmJSONResponse) { | ||
return jsonResponse; | ||
} | ||
|
||
function serializeRequest(request: RequestChallengeEvmRequest, core: Core) { | ||
return { | ||
domain: request.domain, | ||
chainId: EvmChainResolver.resolve(request.chainId, core).decimal.toString(), | ||
address: EvmAddress.create(request.address, core).checksum, | ||
statement: request.statement, | ||
uri: request.uri, | ||
expirationTime: request.expirationTime, | ||
notBefore: request.notBefore, | ||
resources: request.resources, | ||
timeout: request.timeout, | ||
}; | ||
} | ||
|
||
function deserializeRequest(jsonRequest: RequestChallengeEvmJSONRequest, core: Core): RequestChallengeEvmRequest { | ||
return { | ||
domain: jsonRequest.domain, | ||
chainId: EvmChainResolver.resolve(jsonRequest.chainId, core), | ||
address: EvmAddress.create(jsonRequest.address, core), | ||
statement: jsonRequest.statement, | ||
uri: jsonRequest.uri, | ||
expirationTime: jsonRequest.expirationTime, | ||
notBefore: jsonRequest.notBefore, | ||
resources: jsonRequest.resources, | ||
timeout: jsonRequest.timeout, | ||
}; | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/auth/src/operations/evm/verifyChallengeEvmOperation.test.ts
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,27 @@ | ||
import MoralisCore from '@moralisweb3/common-core'; | ||
import { verifyChallengeEvmOperation, VerifyChallengeEvmJSONRequest } from './verifyChallengeEvmOperation'; | ||
|
||
describe('evmVerifyChallengeOperation', () => { | ||
let core: MoralisCore; | ||
|
||
beforeAll(() => { | ||
core = MoralisCore.create(); | ||
}); | ||
|
||
it('serializeRequest() serializes correctly and deserializeRequest() deserializes correctly', () => { | ||
const request: Required<VerifyChallengeEvmJSONRequest> = { | ||
message: 'VALID_RESPONSE', | ||
signature: '0x12345', | ||
}; | ||
|
||
const serializedRequest = verifyChallengeEvmOperation.serializeRequest(request, core); | ||
|
||
expect(serializedRequest.message).toBe(request.message); | ||
expect(serializedRequest.signature).toBe(request.signature); | ||
|
||
const deserializedRequest = verifyChallengeEvmOperation.deserializeRequest(serializedRequest, core); | ||
|
||
expect(deserializedRequest.message).toBe(request.message); | ||
expect(deserializedRequest.signature).toBe(request.signature); | ||
}); | ||
}); |
78 changes: 78 additions & 0 deletions
78
packages/auth/src/operations/evm/verifyChallengeEvmOperation.ts
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,78 @@ | ||
import { Camelize, maybe, Operation } from '@moralisweb3/common-core'; | ||
import { EvmAddress, EvmChain } from '@moralisweb3/common-evm-utils'; | ||
import { operations } from '../../generated/types'; | ||
|
||
type OperationId = 'verifyChallengeEvm'; | ||
|
||
type BodyParams = operations[OperationId]['requestBody']['content']['application/json']; | ||
type RequestParams = BodyParams; | ||
|
||
type SuccessResponse = operations[OperationId]['responses']['201']['content']['application/json']; | ||
|
||
// Exports | ||
|
||
export interface VerifyChallengeEvmRequest extends Camelize<RequestParams> {} | ||
|
||
export type VerifyChallengeEvmJSONRequest = ReturnType<typeof serializeRequest>; | ||
|
||
export type VerifyChallengeEvmJSONResponse = SuccessResponse; | ||
|
||
export type VerifyChallengeEvmResponse = ReturnType<typeof deserializeResponse>; | ||
|
||
export const verifyChallengeEvmOperation: Operation< | ||
VerifyChallengeEvmRequest, | ||
VerifyChallengeEvmJSONRequest, | ||
VerifyChallengeEvmResponse, | ||
VerifyChallengeEvmJSONResponse | ||
> = { | ||
method: 'POST', | ||
name: 'verifyChallengeEvm', | ||
id: 'verifyChallengeEvm', | ||
groupName: 'evm', | ||
urlPathPattern: '/challenge/verify/evm', | ||
bodyParamNames: ['message', 'signature'], | ||
bodyType: 'properties', | ||
|
||
getRequestUrlParams, | ||
getRequestBody, | ||
serializeRequest, | ||
deserializeRequest, | ||
deserializeResponse, | ||
}; | ||
|
||
// Methods | ||
|
||
function getRequestUrlParams() { | ||
return {}; | ||
} | ||
|
||
function getRequestBody(request: VerifyChallengeEvmRequest) { | ||
return { | ||
message: request.message, | ||
signature: request.signature, | ||
}; | ||
} | ||
|
||
function deserializeResponse({ chainId, ...jsonResponse }: VerifyChallengeEvmJSONResponse) { | ||
return { | ||
...jsonResponse, | ||
chain: EvmChain.create(chainId), | ||
address: EvmAddress.create(jsonResponse.address), | ||
expirationTime: maybe(jsonResponse.expirationTime, (value) => new Date(value)), | ||
notBefore: maybe(jsonResponse.notBefore, (value) => new Date(value)), | ||
}; | ||
} | ||
|
||
function serializeRequest(request: VerifyChallengeEvmRequest) { | ||
return { | ||
message: request.message, | ||
signature: request.signature, | ||
}; | ||
} | ||
|
||
function deserializeRequest(jsonRequest: VerifyChallengeEvmJSONRequest): VerifyChallengeEvmRequest { | ||
return { | ||
message: jsonRequest.message, | ||
signature: jsonRequest.signature, | ||
}; | ||
} |
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,2 @@ | ||
export * from './solana'; | ||
export * from './evm'; |
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,39 @@ | ||
import { toCamel } from '@moralisweb3/common-core'; | ||
import { OpenApiInterfaceReader, OperationDefinitionReader } from '@moralisweb3/test-utils'; | ||
import { operations } from './operations'; | ||
|
||
describe('operations', () => { | ||
let reader: OpenApiInterfaceReader; | ||
|
||
beforeAll(() => { | ||
reader = new OpenApiInterfaceReader('src/generated/types.ts')!; | ||
}); | ||
|
||
for (const operation of operations) { | ||
describe(operation.name, () => { | ||
it('defines all supported parameters', () => { | ||
const openApiPathParamNames = reader.readOperationPathParamNames(operation.id)?.map(toCamel); | ||
const openApiSearchParamNames = reader.readOperationSearchParamNames(operation.id)?.map(toCamel); | ||
const openApiBodyParamNames = reader.readOperationRequestBodyParamNames(operation.id)?.map(toCamel); | ||
|
||
expect(operation.urlPathParamNames?.sort().join(',')).toBe(openApiPathParamNames?.sort().join(',')); | ||
expect(operation.urlSearchParamNames?.sort().join(',')).toBe(openApiSearchParamNames?.sort().join(',')); | ||
expect(operation.bodyParamNames?.sort().join(',')).toBe(openApiBodyParamNames?.sort().join(',')); | ||
}); | ||
|
||
it(`getRequestUrlParams() function returns all supported property names`, () => { | ||
const openApiPathParamNames = reader.readOperationPathParamNames(operation.id)?.map(toCamel); | ||
const openApiSearchParamNames = reader.readOperationSearchParamNames(operation.id); // Must be same as in API | ||
|
||
const definitionReader = new OperationDefinitionReader( | ||
`src/operations/${operation.groupName}/${operation.name}Operation.ts`, | ||
); | ||
const returnPropertyNames = definitionReader.getRequestUrlParamsFunctionReturnPropertyNames(); | ||
|
||
const expectedPropertyNames = [...(openApiPathParamNames || []), ...(openApiSearchParamNames || [])]; | ||
|
||
expect(returnPropertyNames.sort().join(',')).toBe(expectedPropertyNames.sort().join(',')); | ||
}); | ||
}); | ||
} | ||
}); |
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,9 @@ | ||
import { requestChallengeEvmOperation, verifyChallengeEvmOperation } from './evm'; | ||
import { requestChallengeSolanaOperation, verifyChallengeSolanaOperation } from './solana'; | ||
|
||
export const operations = [ | ||
requestChallengeSolanaOperation, | ||
requestChallengeEvmOperation, | ||
verifyChallengeSolanaOperation, | ||
verifyChallengeEvmOperation, | ||
]; |
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,2 @@ | ||
export * from './verifyChallengeSolanaOperation'; | ||
export * from './requestChallengeSolanaOperation'; |
Oops, something went wrong.
9ca62a0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test coverage