@@ -9,7 +9,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
99governing permissions and limitations under the License.
1010*/
1111
12- import { codes } from './AuthErrors .js'
12+ import { codes } from './errors .js'
1313
1414/**
1515 * IMS Base URLs
@@ -21,36 +21,60 @@ const IMS_BASE_URL_STAGE = 'https://ims-na1-stg1.adobelogin.com'
2121 * Gets the IMS base URL based on the environment
2222 *
2323 * @private
24- * @param {string } environment - The environment ('prod' or 'stage')
24+ * @param {string } env - The environment ('prod' or 'stage')
2525 * @returns {string } The IMS base URL
2626 */
27- function getImsUrl ( environment ) {
28- return environment === 'stage' ? IMS_BASE_URL_STAGE : IMS_BASE_URL_PROD
27+ function getImsUrl ( env ) {
28+ return env === 'stage' ? IMS_BASE_URL_STAGE : IMS_BASE_URL_PROD
2929}
3030
3131/**
3232 * Validates required parameters for client credentials flow
3333 *
3434 * @private
3535 * @param {object } params - Parameters to validate
36- * @param {string } params.clientId - The client ID
37- * @param {string } params.clientSecret - The client secret
38- * @param {string } params.orgId - The organization ID
39- * @param {string[] } params.scopes - Array of scopes
36+ * @returns {object } Validated credentials object
4037 * @throws {Error } If any required parameters are missing
4138 */
42- function validateClientCredentialsParams ( { clientId, clientSecret, orgId, scopes } ) {
39+ export function getAndValidateCredentials ( params ) {
40+ if ( ! ( typeof params === 'object' && params !== null && ! Array . isArray ( params ) ) ) {
41+ throw new codes . BAD_CREDENTIALS_FORMAT ( {
42+ sdkDetails : { paramsType : typeof params }
43+ } )
44+ }
45+
46+ if ( params . scopes && ! Array . isArray ( params . scopes ) ) {
47+ throw new codes . BAD_SCOPES_FORMAT ( {
48+ sdkDetails : { scopesType : typeof params . scopes }
49+ } )
50+ }
51+
52+ const credentials = { }
53+ credentials . clientId = params . clientId || params . client_id
54+ credentials . clientSecret = params . clientSecret || params . client_secret
55+ credentials . orgId = params . orgId || params . org_id
56+ credentials . scopes = params . scopes || [ ]
57+
58+ const { clientId, clientSecret, orgId, scopes } = credentials
4359 const missingParams = [ ]
44- if ( ! clientId ) missingParams . push ( 'clientId' )
45- if ( ! clientSecret ) missingParams . push ( 'clientSecret' )
46- if ( ! orgId ) missingParams . push ( 'orgId' )
60+ if ( ! clientId ) {
61+ missingParams . push ( 'clientId' )
62+ }
63+ if ( ! clientSecret ) {
64+ missingParams . push ( 'clientSecret' )
65+ }
66+ if ( ! orgId ) {
67+ missingParams . push ( 'orgId' )
68+ }
4769
4870 if ( missingParams . length > 0 ) {
4971 throw new codes . MISSING_PARAMETERS ( {
5072 messageValues : missingParams . join ( ', ' ) ,
5173 sdkDetails : { clientId, orgId, scopes }
5274 } )
5375 }
76+
77+ return credentials
5478}
5579
5680/**
@@ -65,10 +89,8 @@ function validateClientCredentialsParams ({ clientId, clientSecret, orgId, scope
6589 * @returns {Promise<object> } Promise that resolves with the token response
6690 * @throws {Error } If there's an error getting the access token
6791 */
68- export async function getAccessTokenByClientCredentials ( { clientId, clientSecret, orgId, scopes = [ ] , environment = 'prod' } ) {
69- validateClientCredentialsParams ( { clientId, clientSecret, orgId, scopes } )
70-
71- const imsBaseUrl = getImsUrl ( environment )
92+ export async function getAccessTokenByClientCredentials ( { clientId, clientSecret, orgId, scopes = [ ] , env } ) {
93+ const imsBaseUrl = getImsUrl ( env )
7294
7395 // Prepare form data using URLSearchParams (native Node.js)
7496 const formData = new URLSearchParams ( )
0 commit comments