Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sasl options to support AWS specific fields #936

Closed
wants to merge 1 commit into from

Conversation

r115
Copy link

@r115 r115 commented Oct 31, 2020

Adds optional fields to the SASLOption interface. #935

@Nevon
Copy link
Collaborator

Nevon commented Oct 31, 2020

I think we can do better than just adding optional fields, since these fields are only relevant for certain authentication mechanisms. Using conditional types and discriminating unions we can make the fields dependent on the mechanism:

type SASLMechanismOptionsMap = {
  'plain': { username: string, password: string },
  'scram-sha-256': { username: string, password: string },
  'scram-sha-512': { username: string, password: string },
  'aws': { authorizationIdentity: string, accessKeyId: string, secretAccessKey: string, sessionToken?: string },
  'oauthbearer': { oauthBearerProvider: () => Promise<OauthbearerProviderResponse> }
}

export type SASLMechanism = keyof SASLMechanismOptionsMap
type SASLMechanismOptions<T> = T extends SASLMechanism ? { mechanism: T } & SASLMechanismOptionsMap[T] : never
export type SASLOptions = SASLMechanismOptions<SASLMechanism>

It's a little verbose, but basically we map the mechanism to the desired options (SASLMechanismOptionsMap), then use that mapping to create a union type (SASLOptions) with all the possible keys of the map (SASLMechanismOptions<T>).

The result is that the fields are checked depending on the value of the mechanism, so you couldn't, for example, specify authorizationIdentity unless you are using the aws mechanism.

@r115
Copy link
Author

r115 commented Oct 31, 2020

@Nevon gotcha. Let me refactor.

Nevon added a commit that referenced this pull request Nov 18, 2020
@Nevon Nevon closed this in #966 Nov 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants