11import type { AuthStrategy , AccessTokenResponse , DecodedJWT } from './types.js' ;
2- import { getLogger } from '../logger.js' ;
2+ import { getLogger } from '../logging/ logger.js' ;
33
44const DEFAULT_ACCOUNT_MANAGER_HOST = 'account.demandware.com' ;
55
@@ -109,7 +109,8 @@ export class OAuthStrategy implements AuthStrategy {
109109 */
110110 private async clientCredentialsGrant ( ) : Promise < AccessTokenResponse > {
111111 const logger = getLogger ( ) ;
112- logger . debug ( 'Getting access token from client credentials' ) ;
112+ const url = `https://${ this . accountManagerHost } /dwsso/oauth2/access_token` ;
113+ const method = 'POST' ;
113114
114115 const params = new URLSearchParams ( {
115116 grant_type : 'client_credentials' ,
@@ -120,18 +121,44 @@ export class OAuthStrategy implements AuthStrategy {
120121 }
121122
122123 const credentials = Buffer . from ( `${ this . config . clientId } :${ this . config . clientSecret } ` ) . toString ( 'base64' ) ;
124+ const requestHeaders = {
125+ Authorization : `Basic ${ credentials } ` ,
126+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
127+ } ;
128+
129+ logger . debug (
130+ { clientId : this . config . clientId } ,
131+ `[Auth] Using OAuth client_credentials grant for client: ${ this . config . clientId } ` ,
132+ ) ;
133+ // Debug: Log request start
134+ logger . debug ( { method, url} , `[Auth REQ] ${ method } ${ url } ` ) ;
135+
136+ // Trace: Log request details
137+ logger . trace ( { headers : requestHeaders , body : params . toString ( ) } , `[Auth REQ BODY] ${ method } ${ url } ` ) ;
123138
124- const response = await fetch ( `https://${ this . accountManagerHost } /dwsso/oauth2/access_token` , {
125- method : 'POST' ,
126- headers : {
127- Authorization : `Basic ${ credentials } ` ,
128- 'Content-Type' : 'application/x-www-form-urlencoded' ,
129- } ,
139+ const startTime = Date . now ( ) ;
140+ const response = await fetch ( url , {
141+ method,
142+ headers : requestHeaders ,
130143 body : params . toString ( ) ,
131144 } ) ;
145+ const duration = Date . now ( ) - startTime ;
146+
147+ // Debug: Log response summary
148+ logger . debug (
149+ { method, url, status : response . status , duration} ,
150+ `[Auth RESP] ${ method } ${ url } ${ response . status } ${ duration } ms` ,
151+ ) ;
152+
153+ // Get response headers
154+ const responseHeaders : Record < string , string > = { } ;
155+ response . headers . forEach ( ( value , key ) => {
156+ responseHeaders [ key ] = value ;
157+ } ) ;
132158
133159 if ( ! response . ok ) {
134160 const errorText = await response . text ( ) ;
161+ logger . trace ( { headers : responseHeaders , body : errorText } , `[Auth RESP BODY] ${ method } ${ url } ` ) ;
135162 throw new Error ( `Failed to get access token: ${ response . status } ${ response . statusText } - ${ errorText } ` ) ;
136163 }
137164
@@ -141,8 +168,11 @@ export class OAuthStrategy implements AuthStrategy {
141168 scope ?: string ;
142169 } ;
143170
171+ // Trace: Log response details
172+ logger . trace ( { headers : responseHeaders , body : data } , `[Auth RESP BODY] ${ method } ${ url } ` ) ;
173+
144174 const jwt = decodeJWT ( data . access_token ) ;
145- logger . debug ( `JWT payload: ${ JSON . stringify ( jwt . payload , null , 2 ) } ` ) ;
175+ logger . trace ( { jwt : jwt . payload } , '[Auth] JWT payload' ) ;
146176
147177 const now = new Date ( ) ;
148178 const expiration = new Date ( now . getTime ( ) + data . expires_in * 1000 ) ;
0 commit comments