Skip to content

Commit 06e72ca

Browse files
authored
chore(auth): consolidate start url validation (#5429)
1 parent 1dcf5f7 commit 06e72ca

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/**
7+
* For compatibility, this file should not import anything that cannot be used in
8+
* web, node, or vue.
9+
*/
10+
11+
export const ssoUrlFormatRegex =
12+
/^(https?:\/\/(.+)\.awsapps\.com\/start|https?:\/\/identitycenter\.amazonaws\.com\/ssoins-[\da-zA-Z]{16})\/?$/
13+
14+
export const ssoUrlFormatMessage =
15+
'URLs must start with http:// or https://. Example: https://d-xxxxxxxxxx.awsapps.com/start'

packages/core/src/auth/sso/validation.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@ import * as vscode from 'vscode'
66
import { UnknownError } from '../../shared/errors'
77
import { AuthType } from '../auth'
88
import { SsoConnection, hasScopes, isAnySsoConnection } from '../connection'
9+
import { ssoUrlFormatMessage, ssoUrlFormatRegex } from './constants'
910

10-
export function validateSsoUrl(auth: AuthType, url: string, requiredScopes?: string[]) {
11-
const urlFormatError = validateSsoUrlFormat(url)
12-
if (urlFormatError) {
13-
return urlFormatError
14-
}
15-
16-
return validateIsNewSsoUrlAsync(auth, url, requiredScopes)
17-
}
18-
11+
/**
12+
* Returns an error message if the url is not properly formatted.
13+
* Otherwise, returns undefined.
14+
*/
1915
export function validateSsoUrlFormat(url: string) {
20-
if (!url.match(/^(http|https):\/\//i)) {
21-
return 'URLs must start with http:// or https://. Example: https://d-xxxxxxxxxx.awsapps.com/start'
16+
if (!ssoUrlFormatRegex.test(url)) {
17+
return ssoUrlFormatMessage
2218
}
2319
}
2420

packages/core/src/login/webview/vue/login.vue

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,13 @@ import { LoginOption } from './types'
278278
import { CommonAuthWebview } from './backend'
279279
import { WebviewClientFactory } from '../../../webviews/client'
280280
import { Region } from '../../../shared/regions/endpoints'
281+
import { ssoUrlFormatRegex, ssoUrlFormatMessage } from '../../../auth/sso/constants'
281282
282283
const client = WebviewClientFactory.create<CommonAuthWebview>()
283284
284285
/** Where the user is currently in the builder id setup process */
285286
type Stage = 'START' | 'SSO_FORM' | 'CONNECTED' | 'AUTHENTICATING' | 'AWS_PROFILE'
286287
287-
function validateSsoUrlFormat(url: string) {
288-
const regex =
289-
/^(https?:\/\/(.+)\.awsapps\.com\/start|https?:\/\/identitycenter\.amazonaws\.com\/ssoins-[\da-zA-Z]{16})\/?$/
290-
return regex.test(url)
291-
}
292-
293288
function getCredentialId(loginOption: LoginOption) {
294289
switch (loginOption) {
295290
case LoginOption.BUILDER_ID:
@@ -481,9 +476,8 @@ export default defineComponent({
481476
}
482477
},
483478
handleUrlInput() {
484-
if (this.startUrl && !validateSsoUrlFormat(this.startUrl)) {
485-
this.startUrlError =
486-
'URLs must start with http:// or https://. Example: https://d-xxxxxxxxxx.awsapps.com/start'
479+
if (this.startUrl && !ssoUrlFormatRegex.test(this.startUrl)) {
480+
this.startUrlError = ssoUrlFormatMessage
487481
} else if (this.startUrl && this.existingStartUrls.some((url) => url === this.startUrl)) {
488482
this.startUrlError =
489483
'A connection for this start URL already exists. Sign out before creating a new one.'

0 commit comments

Comments
 (0)