Skip to content

Commit

Permalink
chore(auth): add AtLeastOne type (#12558)
Browse files Browse the repository at this point in the history
* chore(auth): add AtLeastOne type

* fix: linter issue

* Update packages/core/src/singleton/Auth/types.ts

Co-authored-by: Francisco Rodriguez <[email protected]>

---------

Co-authored-by: Ashwin Kumar <[email protected]>
Co-authored-by: Jim Blanchard <[email protected]>
Co-authored-by: Francisco Rodriguez <[email protected]>
  • Loading branch information
4 people authored Nov 10, 2023
1 parent 414da5a commit 86ab927
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 6 additions & 2 deletions packages/core/src/singleton/Auth/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { AtLeastOne } from '../types';

// From https://github.com/awslabs/aws-jwt-verify/blob/main/src/safe-json-parse.ts
// From https://github.com/awslabs/aws-jwt-verify/blob/main/src/jwt-model.ts

interface JwtPayloadStandardFields {
exp?: number; // expires: https://tools.ietf.org/html/rfc7519#section-4.1.4
iss?: string; // issuer: https://tools.ietf.org/html/rfc7519#section-4.1.1
Expand Down Expand Up @@ -97,7 +99,9 @@ export type AuthConfigUserAttributes = Partial<
Record<AuthStandardAttributeKey, { required: boolean }>
>;

export type AuthConfig = StrictUnion<
export type AuthConfig = AtLeastOne<CognitoProviderConfig>;

export type CognitoProviderConfig = StrictUnion<
| AuthIdentityPoolConfig
| AuthUserPoolConfig
| AuthUserPoolAndIdentityPoolConfig
Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/singleton/Auth/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
import { AuthConfigurationErrorCode, assert } from './errorHelpers';
import { base64Decoder } from '../../../utils/convert';

import {
AuthConfig,
JWT,
Expand All @@ -12,6 +11,7 @@ import {
CognitoUserPoolAndIdentityPoolConfig,
CognitoIdentityPoolConfig,
StrictUnion,
OAuthConfig,
} from '../types';

export function assertTokenProviderConfig(
Expand All @@ -38,20 +38,22 @@ export function assertTokenProviderConfig(
}

export function assertOAuthConfig(
cognitoConfig?: CognitoUserPoolConfig | CognitoUserPoolAndIdentityPoolConfig
): asserts cognitoConfig is CognitoUserPoolWithOAuthConfig {
cognitoConfig?: AuthConfig['Cognito']
): asserts cognitoConfig is AuthConfig['Cognito'] & {
loginWith: {
oauth: OAuthConfig;
};
} {
const validOAuthConfig =
!!cognitoConfig?.loginWith?.oauth?.domain &&
!!cognitoConfig?.loginWith?.oauth?.redirectSignOut &&
!!cognitoConfig?.loginWith?.oauth?.redirectSignIn &&
!!cognitoConfig?.loginWith?.oauth?.responseType;

return assert(
validOAuthConfig,
AuthConfigurationErrorCode.OAuthNotConfigureException
);
}

export function assertIdentityPoolIdConfig(
cognitoConfig?: StrictUnion<
| CognitoUserPoolConfig
Expand Down

0 comments on commit 86ab927

Please sign in to comment.