diff --git a/packages/bruno-requests/src/auth/oauth2-helper.ts b/packages/bruno-requests/src/auth/oauth2-helper.ts index e73ac7158..e681a5aef 100644 --- a/packages/bruno-requests/src/auth/oauth2-helper.ts +++ b/packages/bruno-requests/src/auth/oauth2-helper.ts @@ -27,7 +27,7 @@ interface RequestConfig { interface ClientCredentialsData { grant_type: string; - scope: string; + scope?: string; client_id?: string; client_secret?: string; } @@ -36,7 +36,7 @@ interface PasswordGrantData { grant_type: string; username: string; password: string; - scope: string; + scope?: string; client_id?: string; client_secret?: string; } @@ -58,10 +58,13 @@ const fetchTokenClientCredentials = async (oauth2Config: OAuth2Config) => { } const data: ClientCredentialsData = { - grant_type: 'client_credentials', - scope: scope || '' + grant_type: 'client_credentials' }; + if (scope && scope.trim() !== '') { + data.scope = scope; + } + const config: RequestConfig = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' @@ -70,7 +73,7 @@ const fetchTokenClientCredentials = async (oauth2Config: OAuth2Config) => { // Handle credentials placement if (credentialsPlacement === 'header') { - config.headers['Authorization'] = `Basic ${Buffer.from(`${clientId}:${clientSecret || ''}`).toString('base64')}`; + config.headers['Authorization'] = `Basic ${Buffer.from(`${encodeURIComponent(clientId)}:${encodeURIComponent(clientSecret || '')}`).toString('base64')}`; } else { // Credentials in body data.client_id = clientId; @@ -111,10 +114,13 @@ const fetchTokenPassword = async (oauth2Config: OAuth2Config) => { const data: PasswordGrantData = { grant_type: 'password', username, - password, - scope: scope || '' + password }; + if (scope && scope.trim() !== '') { + data.scope = scope; + } + const config: RequestConfig = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' @@ -123,7 +129,7 @@ const fetchTokenPassword = async (oauth2Config: OAuth2Config) => { // Handle credentials placement if (credentialsPlacement === 'header' && clientId) { - config.headers['Authorization'] = `Basic ${Buffer.from(`${clientId}:${clientSecret || ''}`).toString('base64')}`; + config.headers['Authorization'] = `Basic ${Buffer.from(`${encodeURIComponent(clientId)}:${encodeURIComponent(clientSecret || '')}`).toString('base64')}`; } else if (clientId) { // Credentials in body data.client_id = clientId;